]> Creatis software - cpPlugins.git/blobdiff - appli/bash/cpPlugins_CreateInstances.cxx
...
[cpPlugins.git] / appli / bash / cpPlugins_CreateInstances.cxx
index e9a4590ca9f05d81fd56366cd511759cd9c42cbe..2340678bc095db7b199481a607c36f1415767737 100644 (file)
@@ -19,9 +19,9 @@ 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 );
+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 ParseIncludes( TLines& res, const TLines& lines, const std::string& ext = "" );
 void PrintLines(
   const std::string& prefix, const std::string& suffix,
   const TLines& lines, std::ostream& out
@@ -39,21 +39,24 @@ int main( int argc, char* argv[] )
     return( 1 );
 
   } // fi
-  std::string lname = argv[ 2 ];
+  std::string input_definitions_fname = argv[ 1 ];
+  std::string library_name = argv[ 2 ];
+  std::string header_file_fname = argv[ 3 ];
+  std::string source_file_fname = argv[ 4 ];
 
   // Read file and simple parse it
   TParsedLines lines;
-  if( !ReadFile( lines, argv[ 1 ] ) )
+  if( !ReadFile( lines, input_definitions_fname ) )
   {
     std::cerr
       << "Error opening file: \""
-      << argv[ 1 ] << "\""
+      << input_definitions_fname << "\""
       << std::endl;
     return( 1 );
 
   } // fi
 
-  // Build definitions
+  // Expand variable definitions
   TVariables vars;
   for( auto dIt = lines[ 'd' ].begin( ); dIt != lines[ 'd' ].end( ); ++dIt )
   {
@@ -64,102 +67,81 @@ int main( int argc, char* argv[] )
     for( ; tIt != tokens.end( ); ++tIt )
       vars[ vName ].push_back( *tIt );
 
+    TLines res;
+    ExpandVariables( res, vars[ vName ], vars );
+    vars[ vName ] = res;
+
   } // rof
 
   // First include section
-  TLines first_includes;
-  ParseIncludes( first_includes, lines[ 'f' ], "" );
-
-  TLines normal_includes;
-  ParseIncludes( normal_includes, lines[ 'i' ], "" );
-
-  TLines template_includes;
-  ParseIncludes( template_includes, lines[ 't' ], "" );
-
-  TLines template_sources;
+  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 classes;
-  ExpandGroups( classes, lines[ 'c' ] );
+  TLines pre_classes;
+  ExpandGroups( pre_classes, lines[ 'c' ], vars );
 
   // Expand variables
   TLines real_classes;
-  ExpandVariables( real_classes, classes, vars );
-
-  // Prepare precompiler options
-  TLines global_header;
-  std::stringstream global_header_stream;
-  global_header_stream
-    << "#ifndef __" << lname << "__H__" << std::endl
-    << "#define __" << lname << "__H__" << std::endl<< std::endl
-    << "#include <cpPlugins_Config.h>" << std::endl << std::endl;
-  global_header.push_back( global_header_stream.str( ) );
-
-  TLines macro_header;
-  std::stringstream macro_header_stream;
-  macro_header_stream
-    << "#ifdef " << lname << "_EXPORTS" << std::endl
-    << "#  define " << lname << "_PREFIX template class "
-    << lname << "_EXPORT" << std::endl
-    << "#else // " << lname << "_EXPORTS" << std::endl
-    << "#  define " << lname << "_PREFIX extern template class" << std::endl
-    << "#endif // " << lname << "_EXPORTS" << std::endl;
-  macro_header.push_back( macro_header_stream.str( ) );
-
-  TLines end_global_header;
-  std::stringstream end_global_header_stream;
-  end_global_header_stream
-    << "#endif // __" << lname << "__H__" << std::endl;
-  end_global_header.push_back( end_global_header_stream.str( ) );
-
-  // Write header file
-  std::ofstream header_file( argv[ 3 ] );
+  ExpandVariables( real_classes, pre_classes, vars );
+
+  // Prepare header file
+  std::ofstream header_file( header_file_fname.c_str( ) );
   if( !header_file )
   {
     std::cerr
-      << "Error opening \"" << argv[ 3 ] << "\" for writing." << std::endl;
+      << "Error opening \"" << header_file_fname
+      << "\" for writing." << std::endl;
     return( 1 );
 
   } // fi
-  PrintLines( "", "", global_header, header_file );
+
+  // Print header
   header_file
-    << "#include <" << lname << "_Export.h>" << std::endl << std::endl;
+    << "#ifndef __" << library_name << "__H__" << std::endl
+    << "#define __" << library_name << "__H__" << std::endl<< std::endl
+    << "#include <cpPlugins_Config.h>" << std::endl
+    << "#include <" << library_name << "_Export.h>" << std::endl << std::endl;
   PrintLines( "", "", lines[ 'b' ], header_file );
   header_file << std::endl;
   PrintLines( "", "", first_includes, header_file );
-  PrintLines( "", "", macro_header, header_file );
+  header_file
+    << "#ifdef " << library_name << "_EXPORTS" << std::endl
+    << "#  define " << library_name << "_PREFIX template class "
+    << library_name << "_EXPORT" << std::endl
+    << "#else // " << library_name << "_EXPORTS" << std::endl
+    << "#  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 );
-  header_file << std::endl << "#ifdef " << lname << "_EXPORTS" << std::endl;
+  header_file
+    << std::endl << "#ifdef " << library_name << "_EXPORTS" << std::endl;
   PrintLines( "", "", template_sources, header_file );
-  header_file << "#endif // " << lname << "_EXPORTS" << std::endl;
-  header_file << std::endl;
-  PrintLines( lname + std::string( "_PREFIX " ), ";", real_classes, header_file );
+  header_file << "#endif // " << library_name << "_EXPORTS" << std::endl;
   header_file << std::endl;
-  PrintLines( "", "", end_global_header, header_file );
+  PrintLines(
+    library_name + std::string( "_PREFIX " ), ";", real_classes, header_file
+    );
+  header_file
+    << std::endl << "#endif // __" << library_name << "__H__" << std::endl;
   header_file.close( );
 
   // Write source file
-  std::ofstream source_file( argv[ 4 ] );
+  std::ofstream source_file( source_file_fname );
   if( !source_file )
   {
     std::cerr
-      << "Error opening \"" << argv[ 3 ] << "\" for writing." << std::endl;
+      << "Error opening \"" << header_file_fname << "\" for writing." << std::endl;
     return( 1 );
 
   } // fi
   source_file
-    << "#include \"" << argv[ 3 ] << "\"" << std::endl;
-  /* TODO
-     PrintLines( "", "", first_includes, source_file );
-     PrintLines( "", "", template_includes, source_file );
-     source_file << std::endl;
-     PrintLines(
-     std::string( "template class " ) + lname + std::string( "_EXPORT " ),
-     ";", real_classes, source_file
-     );
-  */
+    << "#include \"" << header_file_fname << "\"" << std::endl;
   source_file.close( );
   return( 0 );
 }
@@ -192,15 +174,9 @@ std::string Replace(
   )
 {
   std::string res = str;
-  size_t index = 0;
-  while( true )
-  {
-    index = res.find( sub,  index );
-    if( index == std::string::npos ) break;
+  size_t index;
+  while( ( index = res.find( sub ) ) != std::string::npos )
     res.replace( index, sub.size( ), nsub );
-    index += sub.size( );
-
-  } // elihw
   return( res );
 }
 
@@ -260,19 +236,32 @@ bool ReadFile( TParsedLines& lines, const std::string& fname )
 }
 
 // -------------------------------------------------------------------------
-void ExpandGroups( TLines& res, const TLines& lines )
+void ExpandGroups( TLines& res, const TLines& lines, const TVariables& vars )
 {
   for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
   {
-    auto b_pos = lIt->find( "#{" );
+    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 )
-        *tIt = lIt->substr( 0, b_pos ) + *tIt + lIt->substr( e_pos + 1 );
-      ExpandGroups( res, tokens );
+      {
+        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 );
+
+      } // rof
+      ExpandGroups( res, tokens, vars );
     }
     else
       res.push_back( *lIt );
@@ -283,9 +272,6 @@ void ExpandGroups( TLines& res, const TLines& lines )
 // -------------------------------------------------------------------------
 void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars )
 {
-  const char* int_types[] = { "char", "short", "int", "long" };
-  const char* float_types[] = { "float", "double" };
-  unsigned int n_int_types = 4, n_float_types = 2;
   for( auto lIt = lines.begin( ); lIt != lines.end( ); ++lIt )
   {
     auto b_pos = lIt->find( "#" );
@@ -293,27 +279,13 @@ void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars )
     {
       auto tokens = Tokenize( lIt->substr( b_pos ), " ,;:{}[]()\"$&<>" );
       std::string cmd = tokens[ 0 ];
-      if(
-        cmd == "#int_types" || cmd == "#uint_types" || cmd == "#float_types"
-        )
+      auto vIt = vars.find( cmd );
+      if( vIt != vars.end( ) )
       {
-        const char** types = ( cmd == "#float_types" )? float_types: int_types;
-        unsigned int size = ( cmd == "#float_types" )? n_float_types: n_int_types;
-        std::string ustr = ( ( cmd == "#uint_types" )? "unsigned ": "" );
-        TLines new_res;
-        for( unsigned int i = 0; i < size; ++i )
-          new_res.push_back( Replace( *lIt, cmd, ustr + types[ i ] ) );
-        ExpandVariables( res, new_res, vars );
-      }
-      else
-      {
-        auto vIt = vars.find( cmd );
-        if( vIt != vars.end( ) )
+        if( vIt->second.size( ) > 0 )
         {
           TLines new_res;
-          for(
-            auto wIt = vIt->second.begin( ); wIt != vIt->second.end( ); ++wIt
-            )
+          for( auto wIt = vIt->second.begin( ); wIt != vIt->second.end( ); ++wIt )
             new_res.push_back( Replace( *lIt, cmd, *wIt ) );
           ExpandVariables( res, new_res, vars );