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