1 #ifndef __cpPlugins__bash__Config__h__
2 #define __cpPlugins__bash__Config__h__
4 // -------------------------------------------------------------------------
15 // -------------------------------------------------------------------------
16 #define cpPlugins_CONFIG_INTEGER_TYPES "@cpPlugins_CONFIG_INTEGER_TYPES@"
17 #define cpPlugins_CONFIG_REAL_TYPES "@cpPlugins_CONFIG_REAL_TYPES@"
18 #define cpPlugins_CONFIG_PROCESS_DIMENSIONS "@cpPlugins_CONFIG_PROCESS_DIMENSIONS@"
19 #define cpPlugins_CONFIG_VISUAL_DIMENSIONS "@cpPlugins_CONFIG_VISUAL_DIMENSIONS@"
20 #define cpPlugins_CONFIG_COLOR_PIXELS "@cpPlugins_CONFIG_COLOR_PIXELS@"
21 #define cpPlugins_CONFIG_VECTORS "@cpPlugins_CONFIG_VECTORS@"
22 #define cpPlugins_CONFIG_DIFFUSIONTENSORS "@cpPlugins_CONFIG_DIFFUSIONTENSORS@"
23 #define cpPlugins_CONFIG_MATRICES "@cpPlugins_CONFIG_MATRICES@"
25 // -------------------------------------------------------------------------
26 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
27 #ifdef cpPlugins_bash_OS_Windows
28 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
29 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
30 #else // cpPlugins_bash_OS_Windows
31 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
32 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
33 #endif // cpPlugins_bash_OS_Windows
35 // -------------------------------------------------------------------------
36 typedef std::deque< std::string > TStrings;
37 typedef std::map< std::string, TStrings > TCommands;
41 namespace cpPlugins_bash
43 // -----------------------------------------------------------------------
44 template< class _TTokens >
46 _TTokens& tokens, const std::string& str, const std::string& delims
52 auto ssize = str.size( );
53 char* buffer = new char[ ssize + 1 ];
54 for( unsigned long i = 0; i < ssize; ++i )
55 buffer[ i ] = str[ i ];
56 buffer[ ssize ] = '\0';
58 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
61 tokens.push_back( std::string( it ) );
62 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
70 // -----------------------------------------------------------------------
71 inline std::string Replace(
72 const std::string& str, const std::string& sub, const std::string& nsub
75 std::string res = str;
77 while( ( index = res.find( sub ) ) != std::string::npos )
78 res.replace( index, sub.size( ), nsub );
82 // -----------------------------------------------------------------------
83 inline bool Read( std::string& buffer, const std::string& fname )
86 std::ifstream file_stream( fname.c_str( ) );
89 file_stream.seekg( 0, std::ios::end );
90 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
91 file_stream.seekg( 0, std::ios::beg );
93 ( std::istreambuf_iterator< char >( file_stream ) ),
94 std::istreambuf_iterator< char >( )
100 // -----------------------------------------------------------------------
101 inline bool Write( const std::string& buffer, const std::string& fname )
103 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
106 file_stream.write( buffer.c_str( ), buffer.size( ) );
110 // -----------------------------------------------------------------------
111 inline void Parse( TCommands& commands, const TStrings& lines )
113 for( auto l = lines.begin( ); l != lines.end( ); ++l )
115 auto line = l->substr( l->find_first_not_of( " " ) );
118 if( line[ 0 ] != '*' )
120 auto cmd = line.substr( 0, line.find( " " ) );
121 auto args = line.substr( line.find( " " ) + 1 );
122 commands[ cmd ].push_back( args );
131 // -----------------------------------------------------------------------
132 inline void LoadDefinitions( TCommands& commands )
134 commands[ "define" ].push_back(
135 std::string( "int_types=" ) +
136 std::string( cpPlugins_CONFIG_INTEGER_TYPES )
138 commands[ "define" ].push_back(
139 std::string( "real_types=" ) +
140 std::string( cpPlugins_CONFIG_REAL_TYPES )
142 commands[ "define" ].push_back(
143 std::string( "process_dims=" ) +
144 std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
146 commands[ "define" ].push_back(
147 std::string( "visual_dims=" ) +
148 std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
150 commands[ "define" ].push_back(
151 std::string( "color_pixels=" ) +
152 std::string( cpPlugins_CONFIG_COLOR_PIXELS )
154 commands[ "define" ].push_back(
155 std::string( "vectors=" ) +
156 std::string( cpPlugins_CONFIG_VECTORS )
158 commands[ "define" ].push_back(
159 std::string( "diff_tensors=" ) +
160 std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
162 commands[ "define" ].push_back(
163 std::string( "matrices=" ) +
164 std::string( cpPlugins_CONFIG_MATRICES )
166 commands[ "define" ].push_back(
167 std::string( "uint_types=unsigned #int_types#" )
169 commands[ "define" ].push_back(
170 std::string( "scalar_pixels=#int_types#;#uint_types#;#real_types#" )
174 // -----------------------------------------------------------------------
175 inline void ExpandDefinitions(
176 TCommands& definitions, const TCommands& commands
179 definitions.clear( );
180 auto defs = commands.find( "define" );
181 if( defs == commands.end( ) )
184 std::map< std::string, std::string > values;
185 for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
188 cpPlugins_bash::Tokenize( toks, *dIt, "=" );
189 if( toks.size( ) == 2 )
191 auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
192 auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
193 values[ name ] = val;
198 for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
201 cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
202 for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
203 definitions[ vIt->first ].push_back( *tIt );
206 for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
208 auto name = std::string( "#" ) + dIt->first + std::string( "#" );
209 for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
213 auto vIt = eIt->second.begin( );
214 while( vIt != eIt->second.end( ) )
216 if( vIt->find( name ) != std::string::npos )
219 auto wIt = dIt->second.begin( );
220 wIt != dIt->second.end( );
223 eIt->second.push_back(
224 cpPlugins_bash::Replace( *vIt, name, *wIt )
226 vIt = eIt->second.erase( vIt );
240 // -----------------------------------------------------------------------
243 const TCommands& definitions,
244 const TCommands& commands,
245 const std::string& cmd
249 auto tIt = commands.find( cmd );
250 if( tIt == commands.end( ) )
253 for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
255 std::queue< std::string > q;
257 while( q.size( ) > 0 )
259 auto value = q.front( );
261 auto spos = value.find( "#" );
262 if( spos != std::string::npos )
264 auto name = value.substr( spos + 1 );
265 auto epos = name.find( "#" );
266 name = name.substr( 0, epos );
267 auto dIt = definitions.find( name );
268 if( dIt != definitions.end( ) )
270 name = std::string( "#" ) + name + std::string( "#" );
271 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
272 q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
277 tfiles.push_back( value );
286 #endif // __cpPlugins__bash__Config__h__