]> Creatis software - cpPlugins.git/blob - appli/bash/Config.h.in
...
[cpPlugins.git] / appli / bash / Config.h.in
1 #ifndef __cpPlugins__bash__Config__h__
2 #define __cpPlugins__bash__Config__h__
3
4 // -------------------------------------------------------------------------
5 #include <cmath>
6 #include <cstring>
7 #include <deque>
8 #include <fstream>
9 #include <iostream>
10 #include <map>
11 #include <queue>
12 #include <sstream>
13 #include <string>
14
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
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
36
37 // -------------------------------------------------------------------------
38 typedef std::deque< std::string > TStrings;
39 typedef std::map< std::string, TStrings > TCommands;
40
41 /**
42  */
43 namespace cpPlugins_bash
44 {
45   // -----------------------------------------------------------------------
46   template< class _TTokens >
47   inline void Tokenize(
48     _TTokens& tokens, const std::string& str, const std::string& delims
49     )
50   {
51     tokens.clear( );
52     if( str.size( ) > 0 )
53     {
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';
59       char* next;
60       char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
61       while( it != NULL )
62       {
63         tokens.push_back( std::string( it ) );
64         it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
65
66       } // elihw
67       delete [] buffer;
68
69     } // fi
70   }
71
72   // -----------------------------------------------------------------------
73   inline std::string Replace(
74     const std::string& str, const std::string& sub, const std::string& nsub
75     )
76   {
77     std::string res = str;
78     size_t index;
79     while( ( index = res.find( sub ) ) != std::string::npos )
80       res.replace( index, sub.size( ), nsub );
81     return( res );
82   }
83
84   // -----------------------------------------------------------------------
85   inline bool Read( std::string& buffer, const std::string& fname )
86   {
87     buffer = "";
88     std::ifstream file_stream( fname.c_str( ) );
89     if( !file_stream )
90       return( false );
91     file_stream.seekg( 0, std::ios::end );
92     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
93     file_stream.seekg( 0, std::ios::beg );
94     buffer.assign(
95       ( std::istreambuf_iterator< char >( file_stream ) ),
96       std::istreambuf_iterator< char >( )
97       );
98     file_stream.close( );
99     return( true );
100   }
101
102   // -----------------------------------------------------------------------
103   inline bool Write( const std::string& buffer, const std::string& fname )
104   {
105     std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
106     if( !file_stream )
107       return( false );
108     file_stream.write( buffer.c_str( ), buffer.size( ) );
109     return( true );
110   }
111
112   // -----------------------------------------------------------------------
113   inline void Parse( TCommands& commands, const TStrings& lines )
114   {
115     for( auto l = lines.begin( ); l != lines.end( ); ++l )
116     {
117       auto line = l->substr( l->find_first_not_of( " " ) );
118       if( line != "" )
119       {
120         if( line[ 0 ] != '*' )
121         {
122           auto cmd = line.substr( 0, line.find( " " ) );
123           auto args = line.substr( line.find( " " ) + 1 );
124           commands[ cmd ].push_back( args );
125
126         } // fi
127
128       } // fi
129
130     } // rof
131   }
132
133   // -----------------------------------------------------------------------
134   inline void LoadDefinitions( TCommands& commands )
135   {
136     commands[ "define" ].push_back(
137       std::string( "bool_types=" ) +
138       std::string( cpPlugins_CONFIG_BOOLEAN_TYPES )
139       );
140     commands[ "define" ].push_back(
141       std::string( "int_types=" ) +
142       std::string( cpPlugins_CONFIG_INTEGER_TYPES )
143       );
144     commands[ "define" ].push_back(
145       std::string( "real_types=" ) +
146       std::string( cpPlugins_CONFIG_REAL_TYPES )
147       );
148     commands[ "define" ].push_back(
149       std::string( "process_dims=" ) +
150       std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
151       );
152     commands[ "define" ].push_back(
153       std::string( "visual_dims=" ) +
154       std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
155       );
156     commands[ "define" ].push_back(
157       std::string( "color_pixels=" ) +
158       std::string( cpPlugins_CONFIG_COLOR_PIXELS )
159       );
160     commands[ "define" ].push_back(
161       std::string( "vectors=" ) +
162       std::string( cpPlugins_CONFIG_VECTORS )
163       );
164     commands[ "define" ].push_back(
165       std::string( "diff_tensors=" ) +
166       std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
167       );
168     commands[ "define" ].push_back(
169       std::string( "matrices=" ) +
170       std::string( cpPlugins_CONFIG_MATRICES )
171       );
172     if( std::string( cpPlugins_CONFIG_INTEGER_TYPES ) != "" )
173       commands[ "define" ].push_back(
174         std::string( "uint_types=unsigned #int_types#" )
175         );
176     commands[ "define" ].push_back(
177       std::string(
178         "scalar_pixels=#bool_types#;#int_types#;#uint_types#;#real_types#"
179         )
180       );
181   }
182
183   // -----------------------------------------------------------------------
184   inline void ExpandDefinitions(
185     TCommands& definitions, const TCommands& commands
186     )
187   {
188     definitions.clear( );
189     auto defs = commands.find( "define" );
190     if( defs == commands.end( ) )
191       return;
192
193     std::map< std::string, std::string > values;
194     for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
195     {
196       TStrings toks;
197       cpPlugins_bash::Tokenize( toks, *dIt, "=" );
198       if( toks.size( ) == 2 )
199       {
200         auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
201         auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
202         values[ name ] = val;
203
204       } // fi
205
206     } // rof
207     for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
208     {
209       TStrings toks;
210       cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
211       for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
212         definitions[ vIt->first ].push_back( *tIt );
213
214     } // rof
215     for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
216     {
217       auto name = std::string( "#" ) + dIt->first + std::string( "#" );
218       for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
219       {
220         if( eIt != dIt )
221         {
222           auto vIt = eIt->second.begin( );
223           while( vIt != eIt->second.end( ) )
224           {
225             if( vIt->find( name ) != std::string::npos )
226             {
227               for(
228                 auto wIt = dIt->second.begin( );
229                 wIt != dIt->second.end( );
230                 ++wIt
231                 )
232                 eIt->second.push_back(
233                   cpPlugins_bash::Replace( *vIt, name, *wIt )
234                   );
235               vIt = eIt->second.erase( vIt );
236             }
237             else
238               ++vIt;
239
240           } // elihw
241
242         } // fi
243
244       } // rof
245
246     } // rof
247   }
248
249   // -----------------------------------------------------------------------
250   inline void Expand(
251     TStrings& tfiles,
252     const TCommands& definitions,
253     const TCommands& commands,
254     const std::string& cmd
255     )
256   {
257     tfiles.clear( );
258     auto tIt = commands.find( cmd );
259     if( tIt == commands.end( ) )
260       return;
261
262     for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
263     {
264       std::queue< std::string > q;
265       q.push( *fIt );
266       while( q.size( ) > 0 )
267       {
268         auto value = q.front( );
269         q.pop( );
270         auto spos = value.find( "#" );
271         if( spos != std::string::npos )
272         {
273           auto name = value.substr( spos + 1 );
274           auto epos = name.find( "#" );
275           name = name.substr( 0, epos );
276           auto dIt = definitions.find( name );
277           if( dIt != definitions.end( ) )
278           {
279             name = std::string( "#" ) + name + std::string( "#" );
280             for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
281               q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
282
283           } // fi
284         }
285         else
286           tfiles.push_back( value );
287
288       } // rof
289
290     } // rof
291   }
292
293 } // ecapseman
294
295 #endif // __cpPlugins__bash__Config__h__
296
297 // eof - $RCSfile$