1 #ifndef __CPEXTENSIONS__UTILITY__H__
2 #define __CPEXTENSIONS__UTILITY__H__
4 #include <cpExtensions/Config.h>
11 // -------------------------------------------------------------------------
12 #ifdef cpExtensions_SYS_WINDOWS
13 # define cpExtensions_STRTOK( A, B, N ) strtok_s( A, B, N )
14 #else // cpExtensions_SYS_WINDOWS
15 # define cpExtensions_STRTOK( A, B, N ) std::strtok( A, B )
16 #endif // cpExtensions_SYS_WINDOWS
18 // -------------------------------------------------------------------------
19 #define cpExtensions_CHRONO \
20 std::chrono::duration_cast< std::chrono::milliseconds >( \
21 std::chrono::system_clock::now( ).time_since_epoch( ) \
24 // -------------------------------------------------------------------------
25 namespace cpExtensions
27 // -----------------------------------------------------------------------
28 inline bool IsPathSeparator( char c )
30 #ifdef cpExtensions_SYS_WINDOWS
31 return( c == '\\' || c == cpExtensions_PATH_SEPARATOR );
32 #else // cpExtensions_SYS_WINDOWS
33 return( c == cpExtensions_PATH_SEPARATOR );
34 #endif // cpExtensions_SYS_WINDOWS
37 // -----------------------------------------------------------------------
38 inline bool IsBlank( const char& v )
40 return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
43 // -----------------------------------------------------------------------
44 template< class _TTokens >
45 inline void TokenizeString(
46 _TTokens& tokens, const std::string& str, const std::string& delims
52 auto ssize = str.size( );
53 char* buffer = new char[ ssize + 1 ];
54 for( unsigned long i = 0; i < ssize; ++i )
55 buffer[ i ] = str[ i ];
56 buffer[ ssize ] = '\0';
58 char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
61 tokens.push_back( std::string( it ) );
62 it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
70 // -----------------------------------------------------------------------
71 inline std::string ReplaceString(
72 const std::string& str, const std::string& sub, const std::string& nsub
75 std::string res = str;
77 while( ( index = res.find( sub ) ) != std::string::npos )
78 res.replace( index, sub.size( ), nsub );
82 // -----------------------------------------------------------------------
83 inline bool ReadFileIntoBuffer(
84 std::string& buffer, const std::string& fname
88 std::ifstream file_stream( fname.c_str( ) );
91 file_stream.seekg( 0, std::ios::end );
92 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
93 file_stream.seekg( 0, std::ios::beg );
95 ( std::istreambuf_iterator< char >( file_stream ) ),
96 std::istreambuf_iterator< char >( )
102 // -----------------------------------------------------------------------
103 inline std::string CanonicalPath( const std::string& path )
105 std::string ret = "";
106 #ifdef cpExtensions_SYS_WINDOWS
107 TCHAR buffer[ 4096 ] = TEXT( "" );
108 TCHAR** lppPart = { NULL };
109 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
110 ret = std::string( buffer );
111 #else // cpExtensions_SYS_WINDOWS
112 char* canonical_path = realpath( path.c_str( ), NULL );
113 if( canonical_path != NULL )
115 ret = canonical_path;
116 free( canonical_path );
119 #endif // cpExtensions_SYS_WINDOWS
125 #endif // __CPEXTENSIONS__UTILITY__H__