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