]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Utility.h
Architecture updated.
[cpPlugins.git] / lib / cpExtensions / Utility.h
1 #ifndef __cpExtensions__Utility__h__
2 #define __cpExtensions__Utility__h__
3
4 #include <chrono>
5 #include <cstring>
6 #include <fstream>
7 #include <string>
8 #include <cpExtensions/Config.h>
9
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 );
14 #  define 
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
19
20 // -------------------------------------------------------------------------
21 #define cpExtensions_CHRONO                                             \
22   std::chrono::duration_cast< std::chrono::milliseconds >(              \
23     std::chrono::system_clock::now( ).time_since_epoch( )               \
24     ).count( )
25
26 // -------------------------------------------------------------------------
27 namespace cpExtensions
28 {
29   // -----------------------------------------------------------------------
30   inline bool IsPathSeparator( char c )
31   {
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
37   }
38
39   // -----------------------------------------------------------------------
40   inline bool IsBlank( const char& v )
41   {
42     return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
43   }
44
45   // -----------------------------------------------------------------------
46   template< class _TTokens >
47   inline void Tokenize(
48     _TTokens& tokens, const std::string& str, const std::string& delims
49     )
50   {
51     tokens.clear( );
52     if( str.size( ) > 0 )
53     {
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';
59       char* next;
60       char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
61       while( it != NULL )
62       {
63         tokens.push_back( std::string( it ) );
64         it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
65
66       } // elihw
67       delete [] buffer;
68
69     } // fi
70   }
71
72   // -----------------------------------------------------------------------
73   inline std::string Replace(
74     const std::string& str, const std::string& sub, const std::string& nsub
75     )
76   {
77     std::string res = str;
78     size_t index;
79     while( ( index = res.find( sub ) ) != std::string::npos )
80       res.replace( index, sub.size( ), nsub );
81     return( res );
82   }
83
84   // -----------------------------------------------------------------------
85   inline bool Read( std::string& buffer, const std::string& fname )
86   {
87     buffer = "";
88     std::ifstream file_stream( fname.c_str( ) );
89     if( !file_stream )
90       return( false );
91     file_stream.seekg( 0, std::ios::end );
92     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
93     file_stream.seekg( 0, std::ios::beg );
94     buffer.assign(
95       ( std::istreambuf_iterator< char >( file_stream ) ),
96       std::istreambuf_iterator< char >( )
97       );
98     file_stream.close( );
99     return( true );
100   }
101
102   // -----------------------------------------------------------------------
103   inline bool Write( const std::string& buffer, const std::string& fname )
104   {
105     std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
106     if( !file_stream )
107       return( false );
108     file_stream.write( buffer.c_str( ), buffer.size( ) );
109     return( true );
110   }
111
112   // -----------------------------------------------------------------------
113   inline std::string CanonicalPath( const std::string& path )
114   {
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 )
124     {
125       ret = canonical_path;
126       free( canonical_path );
127
128     } // fi
129 #endif // cpPlugins_Windows
130     return( ret );
131   }
132
133 } // ecapseman
134
135 #endif // __cpExtensions__Utility__h__
136
137 // eof - $RCSfile$