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