]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Utilities.h
d4eff536b392d3ce7a3d0c08c4fc817bc3bdccec
[cpPlugins.git] / lib / cpPlugins / Utilities.h
1 #ifndef __CPPLUGINS__UTILITY__H__
2 #define __CPPLUGINS__UTILITY__H__
3
4 #include <cpPlugins/Config.h>
5 #include <chrono>
6 #include <cstring>
7 #include <fstream>
8 #include <iostream>
9
10 // -------------------------------------------------------------------------
11 #ifdef cpPlugins_SYS_WINDOWS
12 #  define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N )
13 #else // cpPlugins_SYS_WINDOWS
14 #  define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B )
15 #endif // cpPlugins_SYS_WINDOWS
16
17 // -------------------------------------------------------------------------
18 #define cpPlugins_CHRONO                                                \
19   std::chrono::duration_cast< std::chrono::milliseconds >(              \
20     std::chrono::system_clock::now( ).time_since_epoch( )               \
21     ).count( )
22
23 // -------------------------------------------------------------------------
24 #define cpPlugins_Id_Macro( N, C )                                      \
25   public:                                                               \
26   virtual const char* GetClassName( ) const ITK_OVERRIDE                \
27   { return( #N ); }                                                     \
28   virtual const char* GetClassCategory( ) const ITK_OVERRIDE            \
29   { return( #C ); }
30
31
32 // -------------------------------------------------------------------------
33 namespace cpPlugins
34 {
35   // -----------------------------------------------------------------------
36   inline bool IsPathSeparator( char c )
37   {
38 #ifdef cpPlugins_SYS_WINDOWS
39     return( c == '\\' || c == cpPlugins_PATH_SEPARATOR );
40 #else // cpPlugins_SYS_WINDOWS
41     return( c == cpPlugins_PATH_SEPARATOR );
42 #endif // cpPlugins_SYS_WINDOWS
43   }
44
45   // -----------------------------------------------------------------------
46   inline bool IsBlank( const char& v )
47   {
48     return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
49   }
50
51   // -----------------------------------------------------------------------
52   template< class _TTokens >
53   inline void TokenizeString(
54     _TTokens& tokens, const std::string& str, const std::string& delims
55     )
56   {
57     tokens.clear( );
58     if( str.size( ) > 0 )
59     {
60       auto ssize = str.size( );
61       char* buffer = new char[ ssize + 1 ];
62       for( unsigned long i = 0; i < ssize; ++i )
63         buffer[ i ] = str[ i ];
64       buffer[ ssize ] = '\0';
65       char* next;
66       char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
67       while( it != NULL )
68       {
69         tokens.push_back( std::string( it ) );
70         it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
71
72       } // elihw
73       delete [] buffer;
74
75     } // fi
76   }
77
78   // -----------------------------------------------------------------------
79   inline std::string ReplaceString(
80     const std::string& str, const std::string& sub, const std::string& nsub
81     )
82   {
83     std::string res = str;
84     size_t index;
85     while( ( index = res.find( sub ) ) != std::string::npos )
86       res.replace( index, sub.size( ), nsub );
87     return( res );
88   }
89
90   // -----------------------------------------------------------------------
91   inline std::string CanonicalPath( const std::string& path )
92   {
93     std::string ret = "";
94 #ifdef cpPlugins_SYS_WINDOWS
95     TCHAR  buffer[ 4096 ] = TEXT( "" );
96     TCHAR** lppPart = { NULL };
97     GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
98     ret = std::string( buffer );
99 #else // cpPlugins_SYS_WINDOWS
100     char* canonical_path = realpath( path.c_str( ), NULL );
101     if( canonical_path != NULL )
102     {
103       ret = canonical_path;
104       free( canonical_path );
105
106     } // fi
107 #endif // cpPlugins_SYS_WINDOWS
108     return( ret );
109   }
110
111   // -----------------------------------------------------------------------
112   inline bool WriteBufferToFile(
113     const std::string& buffer, const std::string& fname
114     )
115   {
116     std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
117     if( !file_stream )
118       return( false );
119     file_stream.write( buffer.c_str( ), buffer.size( ) );
120     return( true );
121   }
122
123   // -----------------------------------------------------------------------
124   inline bool ReadFileIntoBuffer(
125     std::string& buffer, const std::string& fname
126     )
127   {
128     buffer = "";
129     std::ifstream file_stream(
130       CanonicalPath( fname ).c_str( ), std::ifstream::binary
131       );
132     if( !file_stream )
133       return( false );
134     file_stream.seekg( 0, std::ios::end );
135     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
136     file_stream.seekg( 0, std::ios::beg );
137     buffer.assign(
138       ( std::istreambuf_iterator< char >( file_stream ) ),
139       std::istreambuf_iterator< char >( )
140       );
141     file_stream.close( );
142     return( true );
143   }
144
145 } // ecapseman
146
147 #endif // __CPPLUGINS__UTILITY__H__
148
149 // eof - $RCSfile$