1 #ifndef __cpPlugins__bash__Config__h__
2 #define __cpPlugins__bash__Config__h__
4 // -------------------------------------------------------------------------
15 // -------------------------------------------------------------------------
16 #define cpPlugins_CONFIG_PROCESS_DIMENSIONS "@cpPlugins_CONFIG_PROCESS_DIMENSIONS@"
17 #define cpPlugins_CONFIG_VISUAL_DIMENSIONS "@cpPlugins_CONFIG_VISUAL_DIMENSIONS@"
18 #define cpPlugins_CONFIG_COLOR_PIXELS "@cpPlugins_CONFIG_COLOR_PIXELS@"
19 #define cpPlugins_CONFIG_VECTOR_PIXELS "@cpPlugins_CONFIG_VECTOR_PIXELS@"
20 #define cpPlugins_ALL_CONFIGS "@cpPlugins_ALL_CONFIGS@"
21 #define cpPlugins_CONFIG_NUMBER_OF_FILES @cpPlugins_CONFIG_NUMBER_OF_FILES@
23 // -------------------------------------------------------------------------
24 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
25 #ifdef cpPlugins_bash_OS_Windows
26 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
27 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
28 #else // cpPlugins_bash_OS_Windows
29 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
30 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
31 #endif // cpPlugins_bash_OS_Windows
33 // -------------------------------------------------------------------------
34 typedef std::deque< std::string > TStrings;
35 typedef std::map< std::string, TStrings > TCommands;
39 namespace cpPlugins_bash
41 // -----------------------------------------------------------------------
42 template< class _TTokens >
44 _TTokens& tokens, const std::string& str, const std::string& delims
50 auto ssize = str.size( );
51 char* buffer = new char[ ssize + 1 ];
52 for( unsigned long i = 0; i < ssize; ++i )
53 buffer[ i ] = str[ i ];
54 buffer[ ssize ] = '\0';
56 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
59 tokens.push_back( std::string( it ) );
60 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
68 // -----------------------------------------------------------------------
69 inline std::string Replace(
70 const std::string& str, const std::string& sub, const std::string& nsub
73 std::string res = str;
75 while( ( index = res.find( sub ) ) != std::string::npos )
76 res.replace( index, sub.size( ), nsub );
80 // -----------------------------------------------------------------------
81 inline bool Read( std::string& buffer, const std::string& fname )
84 std::ifstream file_stream( fname.c_str( ) );
87 file_stream.seekg( 0, std::ios::end );
88 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
89 file_stream.seekg( 0, std::ios::beg );
91 ( std::istreambuf_iterator< char >( file_stream ) ),
92 std::istreambuf_iterator< char >( )
98 // -----------------------------------------------------------------------
99 inline bool Write( const std::string& buffer, const std::string& fname )
101 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
104 file_stream.write( buffer.c_str( ), buffer.size( ) );
108 // -----------------------------------------------------------------------
109 inline void Parse( TCommands& commands, const TStrings& lines )
111 for( auto l = lines.begin( ); l != lines.end( ); ++l )
113 auto line = l->substr( l->find_first_not_of( " " ) );
116 if( line[ 0 ] != '*' )
118 auto cmd = line.substr( 0, line.find( " " ) );
119 auto args = line.substr( line.find( " " ) + 1 );
120 commands[ cmd ].push_back( args );
129 // -----------------------------------------------------------------------
130 inline void LoadDefinitions( TCommands& commands )
133 commands[ "define" ].push_back(
134 std::string( "pdims=" ) +
135 std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
137 commands[ "define" ].push_back(
138 std::string( "vdims=" ) +
139 std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
143 commands[ "define" ].push_back(
144 std::string( "int_types=char;short;int;long" )
146 commands[ "define" ].push_back(
147 std::string( "uint_types=unsigned #int_types#" )
149 commands[ "define" ].push_back(
150 std::string( "sint_types=signed char" )
152 commands[ "define" ].push_back(
153 std::string( "real_types=float;double" )
155 commands[ "define" ].push_back(
157 "scalar_types=#int_types#;#uint_types#;#real_types#"
162 // -----------------------------------------------------------------------
163 inline void ExpandDefinitions(
164 TCommands& definitions, const TCommands& commands
167 definitions.clear( );
168 auto defs = commands.find( "define" );
169 if( defs == commands.end( ) )
172 std::map< std::string, std::string > values;
173 for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
176 cpPlugins_bash::Tokenize( toks, *dIt, "=" );
177 if( toks.size( ) == 2 )
179 auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
180 auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
181 values[ name ] = val;
186 for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
189 cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
190 for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
191 definitions[ vIt->first ].push_back( *tIt );
194 for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
196 auto name = std::string( "#" ) + dIt->first + std::string( "#" );
197 for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
201 auto vIt = eIt->second.begin( );
202 while( vIt != eIt->second.end( ) )
204 if( vIt->find( name ) != std::string::npos )
207 auto wIt = dIt->second.begin( );
208 wIt != dIt->second.end( );
211 eIt->second.push_back(
212 cpPlugins_bash::Replace( *vIt, name, *wIt )
214 vIt = eIt->second.erase( vIt );
228 // -----------------------------------------------------------------------
231 const TCommands& definitions,
232 const TCommands& commands,
233 const std::string& cmd
237 auto tIt = commands.find( cmd );
238 if( tIt == commands.end( ) )
241 for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
243 std::queue< std::string > q;
245 while( q.size( ) > 0 )
247 auto value = q.front( );
249 auto spos = value.find( "#" );
250 if( spos != std::string::npos )
252 auto name = value.substr( spos + 1 );
253 auto epos = name.find( "#" );
254 name = name.substr( 0, epos );
255 auto dIt = definitions.find( name );
256 if( dIt != definitions.end( ) )
258 name = std::string( "#" ) + name + std::string( "#" );
259 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
260 q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
265 tfiles.push_back( value );
274 #endif // __cpPlugins__bash__Config__h__