1 #ifndef __cpPlugins__bash__Config__h__
2 #define __cpPlugins__bash__Config__h__
4 // -------------------------------------------------------------------------
15 // -------------------------------------------------------------------------
16 #define cpPlugins_CONFIG_BOOLEAN_TYPES "@cpPlugins_CONFIG_BOOLEAN_TYPES@"
17 #define cpPlugins_CONFIG_INTEGER_TYPES "@cpPlugins_CONFIG_INTEGER_TYPES@"
18 #define cpPlugins_CONFIG_REAL_TYPES "@cpPlugins_CONFIG_REAL_TYPES@"
19 #define cpPlugins_CONFIG_PROCESS_DIMENSIONS "@cpPlugins_CONFIG_PROCESS_DIMENSIONS@"
20 #define cpPlugins_CONFIG_VISUAL_DIMENSIONS "@cpPlugins_CONFIG_VISUAL_DIMENSIONS@"
21 #define cpPlugins_CONFIG_COLOR_PIXELS "@cpPlugins_CONFIG_COLOR_PIXELS@"
22 #define cpPlugins_CONFIG_VECTORS "@cpPlugins_CONFIG_VECTORS@"
23 #define cpPlugins_CONFIG_DIFFUSIONTENSORS "@cpPlugins_CONFIG_DIFFUSIONTENSORS@"
24 #define cpPlugins_CONFIG_MATRICES "@cpPlugins_CONFIG_MATRICES@"
26 // -------------------------------------------------------------------------
27 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
28 #ifdef cpPlugins_bash_OS_Windows
29 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
30 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
31 #else // cpPlugins_bash_OS_Windows
32 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
33 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
34 #endif // cpPlugins_bash_OS_Windows
36 // -------------------------------------------------------------------------
37 typedef std::deque< std::string > TStrings;
38 typedef std::map< std::string, TStrings > TCommands;
42 namespace cpPlugins_bash
44 // -----------------------------------------------------------------------
45 template< class _TTokens >
47 _TTokens& tokens, const std::string& str, const std::string& delims
53 auto ssize = str.size( );
54 char* buffer = new char[ ssize + 1 ];
55 for( unsigned long i = 0; i < ssize; ++i )
56 buffer[ i ] = str[ i ];
57 buffer[ ssize ] = '\0';
59 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
62 tokens.push_back( std::string( it ) );
63 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
71 // -----------------------------------------------------------------------
72 inline std::string Replace(
73 const std::string& str, const std::string& sub, const std::string& nsub
76 std::string res = str;
78 while( ( index = res.find( sub ) ) != std::string::npos )
79 res.replace( index, sub.size( ), nsub );
83 // -----------------------------------------------------------------------
84 inline bool Read( std::string& buffer, const std::string& fname )
87 std::ifstream file_stream( fname.c_str( ) );
90 file_stream.seekg( 0, std::ios::end );
91 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
92 file_stream.seekg( 0, std::ios::beg );
94 ( std::istreambuf_iterator< char >( file_stream ) ),
95 std::istreambuf_iterator< char >( )
101 // -----------------------------------------------------------------------
102 inline bool Write( const std::string& buffer, const std::string& fname )
104 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
107 file_stream.write( buffer.c_str( ), buffer.size( ) );
111 // -----------------------------------------------------------------------
112 inline void Parse( TCommands& commands, const TStrings& lines )
114 for( auto l = lines.begin( ); l != lines.end( ); ++l )
116 auto line = l->substr( l->find_first_not_of( " " ) );
119 if( line[ 0 ] != '*' )
121 auto cmd = line.substr( 0, line.find( " " ) );
122 auto args = line.substr( line.find( " " ) + 1 );
123 commands[ cmd ].push_back( args );
132 // -----------------------------------------------------------------------
133 inline void LoadDefinitions( TCommands& commands )
135 commands[ "define" ].push_back(
136 std::string( "bool_types=" ) +
137 std::string( cpPlugins_CONFIG_BOOLEAN_TYPES )
139 commands[ "define" ].push_back(
140 std::string( "int_types=" ) +
141 std::string( cpPlugins_CONFIG_INTEGER_TYPES )
143 commands[ "define" ].push_back(
144 std::string( "real_types=" ) +
145 std::string( cpPlugins_CONFIG_REAL_TYPES )
147 commands[ "define" ].push_back(
148 std::string( "process_dims=" ) +
149 std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
151 commands[ "define" ].push_back(
152 std::string( "visual_dims=" ) +
153 std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
155 commands[ "define" ].push_back(
156 std::string( "color_pixels=" ) +
157 std::string( cpPlugins_CONFIG_COLOR_PIXELS )
159 commands[ "define" ].push_back(
160 std::string( "vectors=" ) +
161 std::string( cpPlugins_CONFIG_VECTORS )
163 commands[ "define" ].push_back(
164 std::string( "diff_tensors=" ) +
165 std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
167 commands[ "define" ].push_back(
168 std::string( "matrices=" ) +
169 std::string( cpPlugins_CONFIG_MATRICES )
171 if( std::string( cpPlugins_CONFIG_INTEGER_TYPES ) != "" )
172 commands[ "define" ].push_back(
173 std::string( "uint_types=unsigned #int_types#" )
175 commands[ "define" ].push_back(
177 "scalar_pixels=#bool_types#;#int_types#;#uint_types#;#real_types#"
182 // -----------------------------------------------------------------------
183 inline void ExpandDefinitions(
184 TCommands& definitions, const TCommands& commands
187 definitions.clear( );
188 auto defs = commands.find( "define" );
189 if( defs == commands.end( ) )
192 std::map< std::string, std::string > values;
193 for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
196 cpPlugins_bash::Tokenize( toks, *dIt, "=" );
197 if( toks.size( ) == 2 )
199 auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
200 auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
201 values[ name ] = val;
206 for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
209 cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
210 for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
211 definitions[ vIt->first ].push_back( *tIt );
214 for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
216 auto name = std::string( "#" ) + dIt->first + std::string( "#" );
217 for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
221 auto vIt = eIt->second.begin( );
222 while( vIt != eIt->second.end( ) )
224 if( vIt->find( name ) != std::string::npos )
227 auto wIt = dIt->second.begin( );
228 wIt != dIt->second.end( );
231 eIt->second.push_back(
232 cpPlugins_bash::Replace( *vIt, name, *wIt )
234 vIt = eIt->second.erase( vIt );
248 // -----------------------------------------------------------------------
251 const TCommands& definitions,
252 const TCommands& commands,
253 const std::string& cmd
257 auto tIt = commands.find( cmd );
258 if( tIt == commands.end( ) )
261 for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
263 std::queue< std::string > q;
265 while( q.size( ) > 0 )
267 auto value = q.front( );
269 auto spos = value.find( "#" );
270 if( spos != std::string::npos )
272 auto name = value.substr( spos + 1 );
273 auto epos = name.find( "#" );
274 name = name.substr( 0, epos );
275 auto dIt = definitions.find( name );
276 if( dIt != definitions.end( ) )
278 name = std::string( "#" ) + name + std::string( "#" );
279 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
280 q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
285 tfiles.push_back( value );
294 #endif // __cpPlugins__bash__Config__h__