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