1 #ifndef __cpPlugins__bash__Config__h__
2 #define __cpPlugins__bash__Config__h__
4 // -------------------------------------------------------------------------
15 // -------------------------------------------------------------------------
16 #define cpPlugins_PROCESS_DIMS "@cpPlugins_PROCESS_DIMS@"
17 #define cpPlugins_VISUAL_DIMS "@cpPlugins_VISUAL_DIMS@"
18 #define cpPlugins_ALL_CONFIGS "@cpPlugins_ALL_CONFIGS@"
19 #define cpPlugins_NUMBER_OF_FILES @cpPlugins_NUMBER_OF_FILES@
21 // -------------------------------------------------------------------------
22 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
23 #ifdef cpPlugins_bash_OS_Windows
24 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
25 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
26 #else // cpPlugins_bash_OS_Windows
27 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
28 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
29 #endif // cpPlugins_bash_OS_Windows
31 // -------------------------------------------------------------------------
32 typedef std::deque< std::string > TStrings;
33 typedef std::map< std::string, TStrings > TCommands;
37 namespace cpPlugins_bash
39 // -----------------------------------------------------------------------
40 template< class _TTokens >
42 _TTokens& tokens, const std::string& str, const std::string& delims
48 auto ssize = str.size( );
49 char* buffer = new char[ ssize + 1 ];
50 for( unsigned long i = 0; i < ssize; ++i )
51 buffer[ i ] = str[ i ];
52 buffer[ ssize ] = '\0';
54 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
57 tokens.push_back( std::string( it ) );
58 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
66 // -----------------------------------------------------------------------
67 inline std::string Replace(
68 const std::string& str, const std::string& sub, const std::string& nsub
71 std::string res = str;
73 while( ( index = res.find( sub ) ) != std::string::npos )
74 res.replace( index, sub.size( ), nsub );
78 // -----------------------------------------------------------------------
79 inline bool Read( std::string& buffer, const std::string& fname )
82 std::ifstream file_stream( fname.c_str( ) );
85 file_stream.seekg( 0, std::ios::end );
86 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
87 file_stream.seekg( 0, std::ios::beg );
89 ( std::istreambuf_iterator< char >( file_stream ) ),
90 std::istreambuf_iterator< char >( )
96 // -----------------------------------------------------------------------
97 inline bool Write( const std::string& buffer, const std::string& fname )
99 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
102 file_stream.write( buffer.c_str( ), buffer.size( ) );
106 // -----------------------------------------------------------------------
107 inline void Parse( TCommands& commands, const TStrings& lines )
109 for( auto l = lines.begin( ); l != lines.end( ); ++l )
111 auto line = l->substr( l->find_first_not_of( " " ) );
114 if( line[ 0 ] != '*' )
116 auto cmd = line.substr( 0, line.find( " " ) );
117 auto args = line.substr( line.find( " " ) + 1 );
118 commands[ cmd ].push_back( args );
127 // -----------------------------------------------------------------------
128 inline void LoadDefinitions( TCommands& commands )
131 commands[ "define" ].push_back(
132 std::string( "pdims=" ) +
133 std::string( cpPlugins_PROCESS_DIMS )
135 commands[ "define" ].push_back(
136 std::string( "vdims=" ) +
137 std::string( cpPlugins_VISUAL_DIMS )
141 commands[ "define" ].push_back(
142 std::string( "int_types=char;short;int;long" )
144 commands[ "define" ].push_back(
145 std::string( "uint_types=unsigned #int_types#" )
147 commands[ "define" ].push_back(
148 std::string( "sint_types=signed char" )
150 commands[ "define" ].push_back(
151 std::string( "real_types=float;double" )
153 commands[ "define" ].push_back(
155 "scalar_types=#int_types#;#uint_types#;#real_types#"
160 // -----------------------------------------------------------------------
161 inline void ExpandDefinitions(
162 TCommands& definitions, const TCommands& commands
165 definitions.clear( );
166 auto defs = commands.find( "define" );
167 if( defs == commands.end( ) )
170 std::map< std::string, std::string > values;
171 for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
174 cpPlugins_bash::Tokenize( toks, *dIt, "=" );
175 if( toks.size( ) == 2 )
177 auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
178 auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
179 values[ name ] = val;
184 for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
187 cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
188 for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
189 definitions[ vIt->first ].push_back( *tIt );
192 for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
194 auto name = std::string( "#" ) + dIt->first + std::string( "#" );
195 for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
199 auto vIt = eIt->second.begin( );
200 while( vIt != eIt->second.end( ) )
202 if( vIt->find( name ) != std::string::npos )
205 auto wIt = dIt->second.begin( );
206 wIt != dIt->second.end( );
209 eIt->second.push_back(
210 cpPlugins_bash::Replace( *vIt, name, *wIt )
212 vIt = eIt->second.erase( vIt );
226 // -----------------------------------------------------------------------
229 const TCommands& definitions,
230 const TCommands& commands,
231 const std::string& cmd
235 auto tIt = commands.find( cmd );
236 if( tIt == commands.end( ) )
239 for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
241 std::queue< std::string > q;
243 while( q.size( ) > 0 )
245 auto value = q.front( );
247 auto spos = value.find( "#" );
248 if( spos != std::string::npos )
250 auto name = value.substr( spos + 1 );
251 auto epos = name.find( "#" );
252 name = name.substr( 0, epos );
253 auto dIt = definitions.find( name );
254 if( dIt != definitions.end( ) )
256 name = std::string( "#" ) + name + std::string( "#" );
257 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
258 q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
263 tfiles.push_back( value );
272 #endif // __cpPlugins__bash__Config__h__