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