1 #ifndef __cpExtensions__Utility__h__
2 #define __cpExtensions__Utility__h__
8 #include <cpExtensions/Config.h>
10 // -------------------------------------------------------------------------
11 #ifdef cpPlugins_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 );
15 #else // cpPlugins_Windows
16 # define cpExtensions_STRTOK( A, B, N ) std::strtok( A, B )
17 # define cpExtensions_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
18 #endif // cpPlugins_Windows
20 // -------------------------------------------------------------------------
21 #define cpExtensions_CHRONO \
22 std::chrono::duration_cast< std::chrono::milliseconds >( \
23 std::chrono::system_clock::now( ).time_since_epoch( ) \
26 // -------------------------------------------------------------------------
27 namespace cpExtensions
29 // -----------------------------------------------------------------------
30 inline bool IsPathSeparator( char c )
32 #ifdef cpPlugins_Windows
33 return( c == '\\' || c == cpExtensions_PATH_SEPARATOR );
34 #else // cpPlugins_Windows
35 return( c == cpExtensions_PATH_SEPARATOR );
36 #endif // cpPlugins_Windows
39 // -----------------------------------------------------------------------
40 inline bool IsBlank( const char& v )
42 return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
45 // -----------------------------------------------------------------------
46 template< class _TTokens >
48 _TTokens& tokens, const std::string& str, const std::string& delims
54 auto ssize = str.size( );
55 char* buffer = new char[ ssize + 1 ];
56 for( unsigned long i = 0; i < ssize; ++i )
57 buffer[ i ] = str[ i ];
58 buffer[ ssize ] = '\0';
60 char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
63 tokens.push_back( std::string( it ) );
64 it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
72 // -----------------------------------------------------------------------
73 inline std::string Replace(
74 const std::string& str, const std::string& sub, const std::string& nsub
77 std::string res = str;
79 while( ( index = res.find( sub ) ) != std::string::npos )
80 res.replace( index, sub.size( ), nsub );
84 // -----------------------------------------------------------------------
85 inline bool Read( 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 bool Write( const std::string& buffer, const std::string& fname )
105 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
108 file_stream.write( buffer.c_str( ), buffer.size( ) );
112 // -----------------------------------------------------------------------
113 inline std::string CanonicalPath( const std::string& path )
115 std::string ret = "";
116 #ifdef cpPlugins_Windows
117 TCHAR buffer[ 4096 ] = TEXT( "" );
118 TCHAR** lppPart = { NULL };
119 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
120 ret = std::string( buffer );
121 #else // cpPlugins_Windows
122 char* canonical_path = realpath( path.c_str( ), NULL );
123 if( canonical_path != NULL )
125 ret = canonical_path;
126 free( canonical_path );
129 #endif // cpPlugins_Windows
135 #endif // __cpExtensions__Utility__h__