]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Utility.h
Code cleaning
[cpPlugins.git] / lib / cpExtensions / Utility.h
1 #ifndef __CPEXTENSIONS__UTILITY__H__
2 #define __CPEXTENSIONS__UTILITY__H__
3
4 #include <cpExtensions/Config.h>
5 #include <chrono>
6 #include <cstring>
7 #include <fstream>
8 #include <iostream>
9 #include <string>
10
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
17
18 // -------------------------------------------------------------------------
19 #define cpExtensions_CHRONO                                             \
20   std::chrono::duration_cast< std::chrono::milliseconds >(              \
21     std::chrono::system_clock::now( ).time_since_epoch( )               \
22     ).count( )
23
24 // -------------------------------------------------------------------------
25 namespace cpExtensions
26 {
27   // -----------------------------------------------------------------------
28   inline bool IsPathSeparator( char c )
29   {
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
35   }
36
37   // -----------------------------------------------------------------------
38   inline bool IsBlank( const char& v )
39   {
40     return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
41   }
42
43   // -----------------------------------------------------------------------
44   template< class _TTokens >
45   inline void TokenizeString(
46     _TTokens& tokens, const std::string& str, const std::string& delims
47     )
48   {
49     tokens.clear( );
50     if( str.size( ) > 0 )
51     {
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';
57       char* next;
58       char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
59       while( it != NULL )
60       {
61         tokens.push_back( std::string( it ) );
62         it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
63
64       } // elihw
65       delete [] buffer;
66
67     } // fi
68   }
69
70   // -----------------------------------------------------------------------
71   inline std::string ReplaceString(
72     const std::string& str, const std::string& sub, const std::string& nsub
73     )
74   {
75     std::string res = str;
76     size_t index;
77     while( ( index = res.find( sub ) ) != std::string::npos )
78       res.replace( index, sub.size( ), nsub );
79     return( res );
80   }
81
82   // -----------------------------------------------------------------------
83   inline bool ReadFileIntoBuffer(
84     std::string& buffer, const std::string& fname
85     )
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 std::string CanonicalPath( const std::string& path )
104   {
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 )
114     {
115       ret = canonical_path;
116       free( canonical_path );
117
118     } // fi
119 #endif // cpExtensions_SYS_WINDOWS
120     return( ret );
121   }
122
123 } // ecapseman
124
125 #endif // __CPEXTENSIONS__UTILITY__H__
126
127 // eof - $RCSfile$