1 #ifndef __cpExtensions__Utility__h__
2 #define __cpExtensions__Utility__h__
8 #include <cpExtensions/Config.h>
10 // -------------------------------------------------------------------------
11 #ifdef cpExtensions_OS_Windows
12 # define cpExtensions_STRTOK( A, B, N ) strtok_s( A, B, N )
13 # define cpExtensions_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
14 #else // cpExtensions_OS_Windows
15 # define cpExtensions_STRTOK( A, B, N ) std::strtok( A, B )
16 # define cpExtensions_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
17 #endif // cpExtensions_OS_Windows
19 // -------------------------------------------------------------------------
20 #define cpExtensions_CHRONO \
21 std::chrono::duration_cast< std::chrono::milliseconds >( \
22 std::chrono::system_clock::now( ).time_since_epoch( ) \
25 // -------------------------------------------------------------------------
26 namespace cpExtensions
28 // -----------------------------------------------------------------------
29 inline bool IsPathSeparator( char c )
31 #ifdef cpExtensions_OS_Windows
32 return( c == '/' || c == cpExtensions_PATH_SEPARATOR );
33 #else // cpExtensions_OS_Windows
34 return( c == cpExtensions_PATH_SEPARATOR );
35 #endif // cpExtensions_OS_Windows
38 // -----------------------------------------------------------------------
39 inline bool IsBlank( const char& v )
41 return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
44 // -----------------------------------------------------------------------
45 template< class _TTokens >
47 _TTokens& tokens, const std::string& str, const std::string& delims
53 auto ssize = str.size( );
54 char* buffer = new char[ ssize + 1 ];
55 for( unsigned long i = 0; i < ssize; ++i )
56 buffer[ i ] = str[ i ];
57 buffer[ ssize ] = '\0';
59 char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
62 tokens.push_back( std::string( it ) );
63 it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
71 // -----------------------------------------------------------------------
72 inline std::string Replace(
73 const std::string& str, const std::string& sub, const std::string& nsub
76 std::string res = str;
78 while( ( index = res.find( sub ) ) != std::string::npos )
79 res.replace( index, sub.size( ), nsub );
83 // -----------------------------------------------------------------------
84 inline bool Read( std::string& buffer, const std::string& fname )
87 std::ifstream file_stream( fname.c_str( ) );
90 file_stream.seekg( 0, std::ios::end );
91 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
92 file_stream.seekg( 0, std::ios::beg );
94 ( std::istreambuf_iterator< char >( file_stream ) ),
95 std::istreambuf_iterator< char >( )
101 // -----------------------------------------------------------------------
102 inline bool Write( const std::string& buffer, const std::string& fname )
104 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
107 file_stream.write( buffer.c_str( ), buffer.size( ) );
111 // -----------------------------------------------------------------------
112 inline std::string CanonicalPath( const std::string& path )
114 std::string ret = "";
115 #ifdef cpExtensions_OS_Windows
116 TCHAR buffer[ 4096 ] = TEXT( "" );
117 TCHAR** lppPart = { NULL };
118 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
119 ret = std::string( buffer );
120 #else // cpExtensions_OS_Windows
121 char* canonical_path = realpath( path.c_str( ), NULL );
122 if( canonical_path != NULL )
124 ret = canonical_path;
125 free( canonical_path );
128 #endif // cpExtensions_OS_Windows
134 #endif // __cpExtensions__Utility__h__