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