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@"
26 #define cpPlugins_CONFIG_NUMBER_OF_FILES @cpPlugins_CONFIG_NUMBER_OF_FILES@
28 // -------------------------------------------------------------------------
29 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
30 #ifdef cpPlugins_bash_OS_Windows
31 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
32 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
33 #else // cpPlugins_bash_OS_Windows
34 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
35 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
36 #endif // cpPlugins_bash_OS_Windows
38 // -------------------------------------------------------------------------
39 typedef std::deque< std::string > TStrings;
40 typedef std::map< std::string, TStrings > TCommands;
44 namespace cpPlugins_bash
46 // -----------------------------------------------------------------------
47 template< class _TTokens >
49 _TTokens& tokens, const std::string& str, const std::string& delims
55 auto ssize = str.size( );
56 char* buffer = new char[ ssize + 1 ];
57 for( unsigned long i = 0; i < ssize; ++i )
58 buffer[ i ] = str[ i ];
59 buffer[ ssize ] = '\0';
61 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
64 tokens.push_back( std::string( it ) );
65 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
73 // -----------------------------------------------------------------------
74 inline std::string Replace(
75 const std::string& str, const std::string& sub, const std::string& nsub
78 std::string res = str;
80 while( ( index = res.find( sub ) ) != std::string::npos )
81 res.replace( index, sub.size( ), nsub );
85 // -----------------------------------------------------------------------
86 inline bool Read( std::string& buffer, const std::string& fname )
89 std::ifstream file_stream( fname.c_str( ) );
92 file_stream.seekg( 0, std::ios::end );
93 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
94 file_stream.seekg( 0, std::ios::beg );
96 ( std::istreambuf_iterator< char >( file_stream ) ),
97 std::istreambuf_iterator< char >( )
103 // -----------------------------------------------------------------------
104 inline bool Write( const std::string& buffer, const std::string& fname )
106 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
109 file_stream.write( buffer.c_str( ), buffer.size( ) );
113 // -----------------------------------------------------------------------
114 inline void Parse( TCommands& commands, const TStrings& lines )
116 for( auto l = lines.begin( ); l != lines.end( ); ++l )
118 auto line = l->substr( l->find_first_not_of( " " ) );
121 if( line[ 0 ] != '*' )
123 auto cmd = line.substr( 0, line.find( " " ) );
124 auto args = line.substr( line.find( " " ) + 1 );
125 commands[ cmd ].push_back( args );
134 // -----------------------------------------------------------------------
135 inline void LoadDefinitions( TCommands& commands )
137 commands[ "define" ].push_back(
138 std::string( "int_types=" ) +
139 std::string( cpPlugins_CONFIG_INTEGER_TYPES )
141 commands[ "define" ].push_back(
142 std::string( "real_types=" ) +
143 std::string( cpPlugins_CONFIG_REAL_TYPES )
145 commands[ "define" ].push_back(
146 std::string( "process_dims=" ) +
147 std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
149 commands[ "define" ].push_back(
150 std::string( "visual_dims=" ) +
151 std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
153 commands[ "define" ].push_back(
154 std::string( "color_pixels=" ) +
155 std::string( cpPlugins_CONFIG_COLOR_PIXELS )
157 commands[ "define" ].push_back(
158 std::string( "vectors=" ) +
159 std::string( cpPlugins_CONFIG_VECTORS )
161 commands[ "define" ].push_back(
162 std::string( "diff_tensors=" ) +
163 std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
165 commands[ "define" ].push_back(
166 std::string( "matrices=" ) +
167 std::string( cpPlugins_CONFIG_MATRICES )
169 if( std::string( cpPlugins_CONFIG_INTEGER_TYPES ) != "" )
170 commands[ "define" ].push_back(
171 std::string( "uint_types=unsigned #int_types#" )
173 commands[ "define" ].push_back(
175 "scalar_pixels=#int_types#;#uint_types#;#real_types#"
180 // -----------------------------------------------------------------------
181 inline void ExpandDefinitions(
182 TCommands& definitions, const TCommands& commands
185 definitions.clear( );
186 auto defs = commands.find( "define" );
187 if( defs == commands.end( ) )
190 std::map< std::string, std::string > values;
191 for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
194 cpPlugins_bash::Tokenize( toks, *dIt, "=" );
195 if( toks.size( ) == 2 )
197 auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
198 auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
199 values[ name ] = val;
204 for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
207 cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
208 for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
209 definitions[ vIt->first ].push_back( *tIt );
212 for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
214 auto name = std::string( "#" ) + dIt->first + std::string( "#" );
215 for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
219 auto vIt = eIt->second.begin( );
220 while( vIt != eIt->second.end( ) )
222 if( vIt->find( name ) != std::string::npos )
225 auto wIt = dIt->second.begin( );
226 wIt != dIt->second.end( );
229 eIt->second.push_back(
230 cpPlugins_bash::Replace( *vIt, name, *wIt )
232 vIt = eIt->second.erase( vIt );
246 // -----------------------------------------------------------------------
249 const TCommands& definitions,
250 const TCommands& commands,
251 const std::string& cmd
255 auto tIt = commands.find( cmd );
256 if( tIt == commands.end( ) )
259 for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
261 std::queue< std::string > q;
263 while( q.size( ) > 0 )
265 auto value = q.front( );
267 auto spos = value.find( "#" );
268 if( spos != std::string::npos )
270 auto name = value.substr( spos + 1 );
271 auto epos = name.find( "#" );
272 name = name.substr( 0, epos );
273 auto dIt = definitions.find( name );
274 if( dIt != definitions.end( ) )
276 name = std::string( "#" ) + name + std::string( "#" );
277 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
278 q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
283 tfiles.push_back( value );
292 #endif // __cpPlugins__bash__Config__h__