]> Creatis software - cpPlugins.git/blob - appli/bash/Config.h.in
8306fe58fd74fc468e48dd1020879f7a5d59eef7
[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( "int_types=" ) +
138       std::string( cpPlugins_CONFIG_INTEGER_TYPES )
139       );
140     commands[ "define" ].push_back(
141       std::string( "real_types=" ) +
142       std::string( cpPlugins_CONFIG_REAL_TYPES )
143       );
144     commands[ "define" ].push_back(
145       std::string( "process_dims=" ) +
146       std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
147       );
148     commands[ "define" ].push_back(
149       std::string( "visual_dims=" ) +
150       std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
151       );
152     commands[ "define" ].push_back(
153       std::string( "color_pixels=" ) +
154       std::string( cpPlugins_CONFIG_COLOR_PIXELS )
155       );
156     commands[ "define" ].push_back(
157       std::string( "vectors=" ) +
158       std::string( cpPlugins_CONFIG_VECTORS )
159       );
160     commands[ "define" ].push_back(
161       std::string( "diff_tensors=" ) +
162       std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
163       );
164     commands[ "define" ].push_back(
165       std::string( "matrices=" ) +
166       std::string( cpPlugins_CONFIG_MATRICES )
167       );
168     if( std::string( cpPlugins_CONFIG_INTEGER_TYPES ) != "" )
169       commands[ "define" ].push_back(
170         std::string( "uint_types=unsigned #int_types#" )
171         );
172     commands[ "define" ].push_back(
173       std::string(
174         "scalar_pixels=#int_types#;#uint_types#;#real_types#"
175         )
176       );
177   }
178
179   // -----------------------------------------------------------------------
180   inline void ExpandDefinitions(
181     TCommands& definitions, const TCommands& commands
182     )
183   {
184     definitions.clear( );
185     auto defs = commands.find( "define" );
186     if( defs == commands.end( ) )
187       return;
188
189     std::map< std::string, std::string > values;
190     for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
191     {
192       TStrings toks;
193       cpPlugins_bash::Tokenize( toks, *dIt, "=" );
194       if( toks.size( ) == 2 )
195       {
196         auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
197         auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
198         values[ name ] = val;
199
200       } // fi
201
202     } // rof
203     for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
204     {
205       TStrings toks;
206       cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
207       for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
208         definitions[ vIt->first ].push_back( *tIt );
209
210     } // rof
211     for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
212     {
213       auto name = std::string( "#" ) + dIt->first + std::string( "#" );
214       for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
215       {
216         if( eIt != dIt )
217         {
218           auto vIt = eIt->second.begin( );
219           while( vIt != eIt->second.end( ) )
220           {
221             if( vIt->find( name ) != std::string::npos )
222             {
223               for(
224                 auto wIt = dIt->second.begin( );
225                 wIt != dIt->second.end( );
226                 ++wIt
227                 )
228                 eIt->second.push_back(
229                   cpPlugins_bash::Replace( *vIt, name, *wIt )
230                   );
231               vIt = eIt->second.erase( vIt );
232             }
233             else
234               ++vIt;
235
236           } // elihw
237
238         } // fi
239
240       } // rof
241
242     } // rof
243   }
244
245   // -----------------------------------------------------------------------
246   inline void Expand(
247     TStrings& tfiles,
248     const TCommands& definitions,
249     const TCommands& commands,
250     const std::string& cmd
251     )
252   {
253     tfiles.clear( );
254     auto tIt = commands.find( cmd );
255     if( tIt == commands.end( ) )
256       return;
257
258     for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
259     {
260       std::queue< std::string > q;
261       q.push( *fIt );
262       while( q.size( ) > 0 )
263       {
264         auto value = q.front( );
265         q.pop( );
266         auto spos = value.find( "#" );
267         if( spos != std::string::npos )
268         {
269           auto name = value.substr( spos + 1 );
270           auto epos = name.find( "#" );
271           name = name.substr( 0, epos );
272           auto dIt = definitions.find( name );
273           if( dIt != definitions.end( ) )
274           {
275             name = std::string( "#" ) + name + std::string( "#" );
276             for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
277               q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
278
279           } // fi
280         }
281         else
282           tfiles.push_back( value );
283
284       } // rof
285
286     } // rof
287   }
288
289 } // ecapseman
290
291 #endif // __cpPlugins__bash__Config__h__
292
293 // eof - $RCSfile$