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