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@"
25 #define cpPlugins_ALL_CONFIGS "@cpPlugins_ALL_CONFIGS@"
27 // -------------------------------------------------------------------------
28 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
29 #ifdef cpPlugins_bash_OS_Windows
30 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
31 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
32 #else // cpPlugins_bash_OS_Windows
33 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
34 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
35 #endif // cpPlugins_bash_OS_Windows
37 // -------------------------------------------------------------------------
38 typedef std::deque< std::string > TStrings;
39 typedef std::map< std::string, TStrings > TCommands;
43 namespace cpPlugins_bash
45 // -----------------------------------------------------------------------
46 template< class _TTokens >
48 _TTokens& tokens, const std::string& str, const std::string& delims
54 auto ssize = str.size( );
55 char* buffer = new char[ ssize + 1 ];
56 for( unsigned long i = 0; i < ssize; ++i )
57 buffer[ i ] = str[ i ];
58 buffer[ ssize ] = '\0';
60 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
63 tokens.push_back( std::string( it ) );
64 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
72 // -----------------------------------------------------------------------
73 inline std::string Replace(
74 const std::string& str, const std::string& sub, const std::string& nsub
77 std::string res = str;
79 while( ( index = res.find( sub ) ) != std::string::npos )
80 res.replace( index, sub.size( ), nsub );
84 // -----------------------------------------------------------------------
85 inline bool Read( std::string& buffer, const std::string& fname )
88 std::ifstream file_stream( fname.c_str( ) );
91 file_stream.seekg( 0, std::ios::end );
92 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
93 file_stream.seekg( 0, std::ios::beg );
95 ( std::istreambuf_iterator< char >( file_stream ) ),
96 std::istreambuf_iterator< char >( )
102 // -----------------------------------------------------------------------
103 inline bool Write( const std::string& buffer, const std::string& fname )
105 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
108 file_stream.write( buffer.c_str( ), buffer.size( ) );
112 // -----------------------------------------------------------------------
113 inline void Parse( TCommands& commands, const TStrings& lines )
115 for( auto l = lines.begin( ); l != lines.end( ); ++l )
117 auto line = l->substr( l->find_first_not_of( " " ) );
120 if( line[ 0 ] != '*' )
122 auto cmd = line.substr( 0, line.find( " " ) );
123 auto args = line.substr( line.find( " " ) + 1 );
124 commands[ cmd ].push_back( args );
133 // -----------------------------------------------------------------------
134 inline void LoadDefinitions( TCommands& commands )
136 commands[ "define" ].push_back(
137 std::string( "int_types=" ) +
138 std::string( cpPlugins_CONFIG_INTEGER_TYPES )
140 commands[ "define" ].push_back(
141 std::string( "real_types=" ) +
142 std::string( cpPlugins_CONFIG_REAL_TYPES )
144 commands[ "define" ].push_back(
145 std::string( "process_dims=" ) +
146 std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
148 commands[ "define" ].push_back(
149 std::string( "visual_dims=" ) +
150 std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
152 commands[ "define" ].push_back(
153 std::string( "color_pixels=" ) +
154 std::string( cpPlugins_CONFIG_COLOR_PIXELS )
156 commands[ "define" ].push_back(
157 std::string( "vectors=" ) +
158 std::string( cpPlugins_CONFIG_VECTORS )
160 commands[ "define" ].push_back(
161 std::string( "diff_tensors=" ) +
162 std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
164 commands[ "define" ].push_back(
165 std::string( "matrices=" ) +
166 std::string( cpPlugins_CONFIG_MATRICES )
168 if( std::string( cpPlugins_CONFIG_INTEGER_TYPES ) != "" )
169 commands[ "define" ].push_back(
170 std::string( "uint_types=unsigned #int_types#" )
172 commands[ "define" ].push_back(
174 "scalar_pixels=#int_types#;#uint_types#;#real_types#"
179 // -----------------------------------------------------------------------
180 inline void ExpandDefinitions(
181 TCommands& definitions, const TCommands& commands
184 definitions.clear( );
185 auto defs = commands.find( "define" );
186 if( defs == commands.end( ) )
189 std::map< std::string, std::string > values;
190 for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
193 cpPlugins_bash::Tokenize( toks, *dIt, "=" );
194 if( toks.size( ) == 2 )
196 auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
197 auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
198 values[ name ] = val;
203 for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
206 cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
207 for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
208 definitions[ vIt->first ].push_back( *tIt );
211 for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
213 auto name = std::string( "#" ) + dIt->first + std::string( "#" );
214 for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
218 auto vIt = eIt->second.begin( );
219 while( vIt != eIt->second.end( ) )
221 if( vIt->find( name ) != std::string::npos )
224 auto wIt = dIt->second.begin( );
225 wIt != dIt->second.end( );
228 eIt->second.push_back(
229 cpPlugins_bash::Replace( *vIt, name, *wIt )
231 vIt = eIt->second.erase( vIt );
245 // -----------------------------------------------------------------------
248 const TCommands& definitions,
249 const TCommands& commands,
250 const std::string& cmd
254 auto tIt = commands.find( cmd );
255 if( tIt == commands.end( ) )
258 for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
260 std::queue< std::string > q;
262 while( q.size( ) > 0 )
264 auto value = q.front( );
266 auto spos = value.find( "#" );
267 if( spos != std::string::npos )
269 auto name = value.substr( spos + 1 );
270 auto epos = name.find( "#" );
271 name = name.substr( 0, epos );
272 auto dIt = definitions.find( name );
273 if( dIt != definitions.end( ) )
275 name = std::string( "#" ) + name + std::string( "#" );
276 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
277 q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
282 tfiles.push_back( value );
291 #endif // __cpPlugins__bash__Config__h__