]> Creatis software - cpPlugins.git/blobdiff - appli/bash/cpPlugins_CreateInstances.cxx
More macos issues...
[cpPlugins.git] / appli / bash / cpPlugins_CreateInstances.cxx
index 6dccd2b5a36f6e7dbbd8cde275681fc7d38a0583..a286d9311d1737034fe37feb8efe92fecbf926be 100644 (file)
@@ -1,33 +1,19 @@
-#include <fstream>
-#include <iostream>
-
-#include <algorithm>
-#include <cstring>
+#include <cpPlugins_Config.h>
 #include <map>
 #include <vector>
 #include <sstream>
 
-#if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
-#  define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N )
-#else // defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
-#  define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B )
-#endif // defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
-
 // -------------------------------------------------------------------------
 typedef std::vector< std::string >      TLines;
 typedef std::map< char, TLines >        TParsedLines;
 typedef std::map< std::string, TLines > TVariables;
 
 // -------------------------------------------------------------------------
-bool cpPlugins_ISBLANK( const char& value );
-TLines Tokenize( const std::string& str, const std::string& delims );
-std::string Replace(
-  const std::string& str, const std::string& sub, const std::string& nsub
-  );
 bool ReadFile( TParsedLines& lines, const std::string& fname );
-void ExpandGroups( TLines& res, const TLines& lines, const TVariables& vars );
-void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars );
-void ParseIncludes( TLines& res, const TLines& lines, const std::string& ext = "" );
+void ExpandGroups( TLines& res, const TLines& lines );
+void ExpandDefinitions(
+  TLines& res, const TLines& lines, const TVariables& vars
+  );
 void PrintLines(
   const std::string& prefix, const std::string& suffix,
   const TLines& lines, std::ostream& out
@@ -62,11 +48,12 @@ int main( int argc, char* argv[] )
 
   } // fi
 
-  // Expand variable definitions
+  // Expand definitions
   TVariables vars;
   for( auto dIt = lines[ 'd' ].begin( ); dIt != lines[ 'd' ].end( ); ++dIt )
   {
-    auto tokens = Tokenize( *dIt, "=;" );
+    TLines tokens;
+    cpPlugins::TokenizeString( tokens, *dIt, "=;" );
     auto tIt = tokens.begin( );
     auto vName = *tIt;
     tIt++;
@@ -74,27 +61,26 @@ int main( int argc, char* argv[] )
       vars[ vName ].push_back( *tIt );
 
     TLines res;
-    ExpandVariables( res, vars[ vName ], vars );
+    ExpandDefinitions( res, vars[ vName ], vars );
     vars[ vName ] = res;
 
   } // rof
 
-  // First include section
-  TLines first_includes, normal_includes, template_includes, template_sources;
-  ParseIncludes( first_includes, lines[ 'f' ] );
-  ParseIncludes( normal_includes, lines[ 'i' ] );
-  ParseIncludes( template_includes, lines[ 't' ] );
-  ParseIncludes( template_sources, lines[ 't' ], "xx" );
-
   // Expand groups
-  TLines pre_classes;
-  ExpandGroups( pre_classes, lines[ 'c' ], vars );
-
-  // Expand variables
-  TLines real_classes;
-  ExpandVariables( real_classes, pre_classes, vars );
-
-  // Prepare header file
+  TLines f_includes_groups, includes_groups, templates_groups, classes_groups;
+  ExpandGroups( f_includes_groups, lines[ 'f' ] );
+  ExpandGroups( includes_groups, lines[ 'i' ] );
+  ExpandGroups( templates_groups, lines[ 't' ] );
+  ExpandGroups( classes_groups, lines[ 'c' ] );
+
+  // Expand definitions
+  TLines f_includes_list, includes_list, templates_list, classes_list;
+  ExpandDefinitions( f_includes_list, f_includes_groups, vars );
+  ExpandDefinitions( includes_list, includes_groups, vars );
+  ExpandDefinitions( templates_list, templates_groups, vars );
+  ExpandDefinitions( classes_list, classes_groups, vars );
+
+  // Write header file
   std::ofstream header_file( header_file_fname.c_str( ) );
   if( !header_file )
   {
@@ -113,7 +99,7 @@ int main( int argc, char* argv[] )
     << "#include <" << library_name << "_Export.h>" << std::endl << std::endl;
   PrintLines( "", "", lines[ 'b' ], header_file );
   header_file << std::endl;
-  PrintLines( "", "", first_includes, header_file );
+  PrintLines( "#include <", ">", f_includes_list, header_file );
   header_file
     << "#ifdef " << library_name << "_EXPORTS" << std::endl
     << "#  define " << library_name << "_PREFIX template class "
@@ -122,16 +108,16 @@ int main( int argc, char* argv[] )
     << "#  define " << library_name
     << "_PREFIX extern template class" << std::endl
     << "#endif // "
-    << library_name << "_EXPORTS" << std::endl;
-  PrintLines( "", "", normal_includes, header_file );
-  PrintLines( "", "", template_includes, header_file );
+    << library_name << "_EXPORTS" << std::endl << std::endl;
+  PrintLines( "#include <", ">", includes_list, header_file );
+  PrintLines( "#include <", ">", templates_list, header_file );
   header_file
     << std::endl << "#ifdef " << library_name << "_EXPORTS" << std::endl;
-  PrintLines( "", "", template_sources, header_file );
-  header_file << "#endif // " << library_name << "_EXPORTS" << std::endl;
-  header_file << std::endl;
+  PrintLines( "#include <", "xx>", templates_list, header_file );
+  header_file
+    << "#endif // " << library_name << "_EXPORTS" << std::endl << std::endl;
   PrintLines(
-    library_name + std::string( "_PREFIX " ), ";", real_classes, header_file
+    library_name + std::string( "_PREFIX " ), ";", classes_list, header_file
     );
   header_file
     << std::endl << "#endif // __" << library_name << "__H__" << std::endl;
@@ -149,134 +135,87 @@ int main( int argc, char* argv[] )
   source_file
     << "#include \"" << header_file_fname << "\"" << std::endl;
   source_file.close( );
-  return( 0 );
-}
 
-// -------------------------------------------------------------------------
-bool cpPlugins_ISBLANK( const char& value )
-{
-  return( value == ' ' || value == '\t' || value == '\n' || value == '\r' );
-}
-
-// -------------------------------------------------------------------------
-TLines Tokenize( const std::string& str, const std::string& delims )
-{
-  TLines tokens;
-  if( str.size( ) > 0 )
-  {
-    auto ssize = str.size( );
-    char* buffer = new char[ ssize + 1 ];
-    for( unsigned long i = 0; i < ssize; ++i )
-      buffer[ i ] = str[ i ];
-    buffer[ ssize ] = '\0';
-    char* next;
-    char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
-    while( it != NULL )
-    {
-      tokens.push_back( std::string( it ) );
-      it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
-
-    } // elihw
-    delete [] buffer;
-
-  } // fi
-  return( tokens );
-}
-
-// -------------------------------------------------------------------------
-std::string Replace(
-  const std::string& str, const std::string& sub, const std::string& nsub
-  )
-{
-  std::string res = str;
-  size_t index;
-  while( ( index = res.find( sub ) ) != std::string::npos )
-    res.replace( index, sub.size( ), nsub );
-  return( res );
+  return( 0 );
 }
 
 // -------------------------------------------------------------------------
 bool ReadFile( TParsedLines& lines, const std::string& fname )
 {
-  // Load file into a string stream
-  std::ifstream file_stream( fname.c_str( ) );
-  if( !file_stream )
-    return( false );
   std::string buffer;
-  file_stream.seekg( 0, std::ios::end );
-  buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
-  file_stream.seekg( 0, std::ios::beg );
-  buffer.assign(
-    ( std::istreambuf_iterator< char >( file_stream ) ),
-    std::istreambuf_iterator< char >( )
-    );
-  file_stream.close( );
-  std::istringstream input_stream( buffer );
-
-  // Read line by line
-  std::string line;
-  while( std::getline( input_stream, line ) )
+  if( cpPlugins::ReadFileIntoString( buffer, fname ) )
   {
-    auto cmd_pos = line.end( );
-    auto arg_pos = line.end( );
-    auto lIt = line.begin( );
-    while( lIt != line.end( ) )
+    std::istringstream input_stream( buffer );
+
+    // Read line by line
+    std::string line;
+    while( std::getline( input_stream, line ) )
     {
-      if( !cpPlugins_ISBLANK( *lIt ) )
+      auto cmd_pos = line.end( );
+      auto arg_pos = line.end( );
+      auto lIt = line.begin( );
+      while( lIt != line.end( ) )
       {
-        if( cmd_pos == line.end( ) )
+        if( !cpPlugins::IsBlank( *lIt ) )
         {
-          cmd_pos = lIt;
-          ++lIt;
+          if( cmd_pos == line.end( ) )
+          {
+            cmd_pos = lIt;
+            ++lIt;
+          }
+          else if( arg_pos == line.end( ) )
+          {
+            arg_pos = lIt;
+            lIt = line.end( );
+
+          } // fi
         }
-        else if( arg_pos == line.end( ) )
-        {
-          arg_pos = lIt;
-          lIt = line.end( );
+        else
+          ++lIt;
+
+      } // elihw
+      char cmd = *cmd_pos;
+      std::string arg;
+      arg.resize( line.end( ) - arg_pos );
+      std::copy( arg_pos, line.end( ), arg.begin( ) );
+      lines[ cmd ].push_back( arg );
 
-        } // fi
-      }
-      else
-        ++lIt;
-      
     } // elihw
-    char cmd = *cmd_pos;
-    std::string arg;
-    arg.resize( line.end( ) - arg_pos );
-    std::copy( arg_pos, line.end( ), arg.begin( ) );
-    lines[ cmd ].push_back( arg );
-
-  } // elihw
-  return( true );
+    return( true );
+  }
+  else
+    return( false );
 }
 
 // -------------------------------------------------------------------------
-void ExpandGroups( TLines& res, const TLines& lines, const TVariables& vars )
+void ExpandGroups( TLines& res, const TLines& lines )
 {
   for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
   {
     auto b_pos = lIt->find( "@{" );
     if( b_pos != std::string::npos )
     {
-      auto e_pos = lIt->find( "}" );
-      auto expansion = lIt->substr( b_pos + 2, e_pos - b_pos - 2 );
-      auto tokens = Tokenize( expansion, ";" );
-      for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
+      unsigned int braces_count = 1;
+      auto e_pos = b_pos;
+      e_pos += 2;
+      while( braces_count != 0 && e_pos < lIt->size( ) )
       {
-        auto vIt = vars.find( *tIt );
-        if( vIt != vars.end( ) )
-        {
-          auto wIt = vIt->second.begin( );
-          std::string values = *wIt;
-          for( wIt++; wIt != vIt->second.end( ); ++wIt )
-            values += ";" + *wIt;
-          *tIt = Replace( *lIt, vIt->first, values );
-        }
-        else
-          *tIt = lIt->substr( 0, b_pos ) + *tIt + lIt->substr( e_pos + 1 );
+        auto v = ( *lIt )[ e_pos ];
+        braces_count += ( v == '{' )? 1: ( ( v == '}' )? -1: 0 );
+        e_pos++;
+
+      } // elihw
+      if( braces_count == 0 )
+      {
+        auto replace = lIt->substr( b_pos, e_pos - b_pos );
+        auto expansion = replace.substr( 2, replace.size( ) - 3 );
+        TLines tokens;
+        cpPlugins::TokenizeString( tokens, expansion, ";" );
+        for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
+          *tIt = cpPlugins::ReplaceString( *lIt, replace, *tIt );
+        ExpandGroups( res, tokens );
 
-      } // rof
-      ExpandGroups( res, tokens, vars );
+      } // fi
     }
     else
       res.push_back( *lIt );
@@ -285,14 +224,19 @@ void ExpandGroups( TLines& res, const TLines& lines, const TVariables& vars )
 }
 
 // -------------------------------------------------------------------------
-void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars )
+void ExpandDefinitions(
+  TLines& res, const TLines& lines, const TVariables& vars
+  )
 {
+  std::string seps = " ,;:{}[]()\"$&<>*.";
+
   for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
   {
     auto b_pos = lIt->find( "#" );
     if( b_pos != std::string::npos )
     {
-      auto tokens = Tokenize( lIt->substr( b_pos ), " ,;:{}[]()\"$&<>*" );
+      TLines tokens;
+      cpPlugins::TokenizeString( tokens, lIt->substr( b_pos ), seps );
       std::string cmd = tokens[ 0 ];
       auto vIt = vars.find( cmd );
       if( vIt != vars.end( ) )
@@ -300,29 +244,26 @@ void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars )
         if( vIt->second.size( ) > 0 )
         {
           TLines new_res;
-          for( auto wIt = vIt->second.begin( ); wIt != vIt->second.end( ); ++wIt )
-            new_res.push_back( Replace( *lIt, cmd, *wIt ) );
-          ExpandVariables( res, new_res, vars );
+          for(
+            auto wIt = vIt->second.begin( ); wIt != vIt->second.end( ); ++wIt
+            )
+            new_res.push_back( cpPlugins::ReplaceString( *lIt, cmd, *wIt ) );
+          ExpandDefinitions( res, new_res, vars );
 
         } // fi
 
       } // fi
     }
     else
-      res.push_back( *lIt );
+      res.push_back(
+        cpPlugins::ReplaceString(
+          cpPlugins::ReplaceString( *lIt, "{", "" ), "}", ""
+          )
+        );
 
   } // rof
 }
 
-// -------------------------------------------------------------------------
-void ParseIncludes( TLines& res, const TLines& lines, const std::string& ext )
-{
-  for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
-    res.push_back(
-      std::string( "#include <" ) + *lIt + ext + std::string( ">" )
-      );
-}
-
 // -------------------------------------------------------------------------
 void PrintLines(
   const std::string& prefix, const std::string& suffix,