]> Creatis software - cpPlugins.git/blob - appli/bash/Utility.h.in
Windows compilation: 3/3
[cpPlugins.git] / appli / bash / Utility.h.in
1 #ifndef __cpPlugins__bash__Utility__h__
2 #define __cpPlugins__bash__Utility__h__
3
4 #include <cstring>
5 #include <fstream>
6 #include <string>
7
8 // -------------------------------------------------------------------------
9 #define @prj_NAME_OS@
10 #ifdef @prj_NAME_OS@
11 #  define cpExtensions_STRTOK( A, B, N )  strtok_s(  A, B, N )
12 #  define cpExtensions_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
13 #else // @prj_NAME_OS@
14 #  define cpExtensions_STRTOK( A, B, N )  std::strtok( A, B )
15 #  define cpExtensions_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
16 #endif // @prj_NAME_OS@
17
18 // -------------------------------------------------------------------------
19 namespace cpPlugins_bash
20 {
21   // -----------------------------------------------------------------------
22   template< class _TTokens >
23   inline void Tokenize(
24     _TTokens& tokens, const std::string& str, const std::string& delims
25     )
26   {
27     tokens.clear( );
28     if( str.size( ) > 0 )
29     {
30       auto ssize = str.size( );
31       char* buffer = new char[ ssize + 1 ];
32       for( unsigned long i = 0; i < ssize; ++i )
33         buffer[ i ] = str[ i ];
34       buffer[ ssize ] = '\0';
35       char* next;
36       char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
37       while( it != NULL )
38       {
39         tokens.push_back( std::string( it ) );
40         it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
41
42       } // elihw
43       delete [] buffer;
44
45     } // fi
46   }
47
48   // -----------------------------------------------------------------------
49   inline std::string Replace(
50     const std::string& str, const std::string& sub, const std::string& nsub
51     )
52   {
53     std::string res = str;
54     size_t index;
55     while( ( index = res.find( sub ) ) != std::string::npos )
56       res.replace( index, sub.size( ), nsub );
57     return( res );
58   }
59
60   // -----------------------------------------------------------------------
61   inline bool Read( std::string& buffer, const std::string& fname )
62   {
63     buffer = "";
64     std::ifstream file_stream( fname.c_str( ) );
65     if( !file_stream )
66       return( false );
67     file_stream.seekg( 0, std::ios::end );
68     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
69     file_stream.seekg( 0, std::ios::beg );
70     buffer.assign(
71       ( std::istreambuf_iterator< char >( file_stream ) ),
72       std::istreambuf_iterator< char >( )
73       );
74     file_stream.close( );
75     return( true );
76   }
77
78   // -----------------------------------------------------------------------
79   inline bool Write( const std::string& buffer, const std::string& fname )
80   {
81     std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
82     if( !file_stream )
83       return( false );
84     file_stream.write( buffer.c_str( ), buffer.size( ) );
85     return( true );
86   }
87
88 } // ecapseman
89
90 #endif // __cpPlugins__bash__Utility__h__
91
92 // eof - $RCSfile$