]> Creatis software - cpPlugins.git/blobdiff - appli/bash/cpPlugins_CreateInstances.cxx
...
[cpPlugins.git] / appli / bash / cpPlugins_CreateInstances.cxx
index 99eb60ef6e9a249ac11aac6d4351e73a62bb2f70..e9a4590ca9f05d81fd56366cd511759cd9c42cbe 100644 (file)
@@ -1,25 +1,30 @@
+#include <fstream>
+#include <iostream>
+
 #include <algorithm>
+#include <cctype>
 #include <cstring>
-#include <iostream>
-#include <iterator>
-#include <fstream>
 #include <map>
-#include <set>
-#include <sstream>
-#include <streambuf>
-#include <string>
 #include <vector>
+#include <sstream>
 
 // -------------------------------------------------------------------------
-typedef std::set< std::string >       TSet;
-typedef std::map< std::string, TSet > TInstances;
-typedef std::vector< std::string >    TVector;
+typedef std::vector< std::string >      TLines;
+typedef std::map< char, TLines >        TParsedLines;
+typedef std::map< std::string, TLines > TVariables;
 
 // -------------------------------------------------------------------------
-TVector Tokenize( const std::string& str, const std::string& delims );
-TSet Combine( const TSet& X, const TSet& Y );
-void Replace(
-  std::string& str, const std::string& sub, const std::string& nsub
+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 );
+void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars );
+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
   );
 
 // -------------------------------------------------------------------------
@@ -29,256 +34,140 @@ int main( int argc, char* argv[] )
   {
     std::cerr
       << "Usage: " << argv[ 0 ]
-      << " input_definitions dir library_name header_filename"
+      << " input_definitions library_name header_file source_file"
       << std::endl;
     return( 1 );
 
   } // fi
-  std::string input_definitions_file_name = argv[ 1 ];
-  std::string dir = argv[ 2 ];
-  std::string library_name = argv[ 3 ];
-  std::string head_fname = argv[ 4 ];
+  std::string lname = argv[ 2 ];
 
-  // Load file into a buffer
-  std::ifstream file_stream( input_definitions_file_name.c_str( ) );
-  if( !file_stream )
+  // Read file and simple parse it
+  TParsedLines lines;
+  if( !ReadFile( lines, argv[ 1 ] ) )
   {
     std::cerr
       << "Error opening file: \""
-      << input_definitions_file_name << "\""
+      << argv[ 1 ] << "\""
       << std::endl;
     return( 1 );
 
   } // fi
-  std::string buf;
-  file_stream.seekg( 0, std::ios::end );
-  buf.reserve( file_stream.tellg( ) );
-  file_stream.seekg( 0, std::ios::beg );
-  buf.assign(
-    ( std::istreambuf_iterator< char >( file_stream ) ),
-    std::istreambuf_iterator< char >( )
-    );
-  file_stream.close( );
-  std::istringstream input_str( buf );
-
-  // Process file
-  std::string line;
-  TVector incl, first_incl;
-  TInstances instances;
-  TVector classes;
-  while( std::getline( input_str, line ) )
-  {
-    if( line[ 0 ] == 'f' )
-    {
-      first_incl.push_back( line.substr( line.find_first_not_of( ' ', 1 ) ) );
-    }
-    else if( line[ 0 ] == 'i' )
-    {
-      incl.push_back( line.substr( line.find_first_not_of( ' ', 1 ) ) );
-    }
-    else if( line[ 0 ] == 'c' )
-    {
-      classes.push_back( line.substr( line.find_first_not_of( ' ', 1 ) ) );
-    }
-    else if( line[ 0 ] == 'a' )
-    {
-      TVector tokens = Tokenize( line, "=" );
-
-      // Get argument name
-      TVector arg_tokens = Tokenize( tokens[ 0 ], " " );
-      std::string arg = arg_tokens[ 0 ];
-      unsigned int i = 0;
-      while( arg[ 0 ] != '#' && i < arg_tokens.size( ) )
-        arg = arg_tokens[ ++i ];
-
-      // Get values
-      TVector values_tokens = Tokenize( tokens[ 1 ], ";" );
-      for( auto t = values_tokens.begin( ); t != values_tokens.end( ); ++t )
-      {
-        std::string value = t->substr( t->find_first_not_of( ' ' ) );
-        unsigned int value_len = value.size( );
-        while( value[ value_len - 1 ] == ' ' && value_len > 0 )
-          value_len--;
-        value = value.substr( 0, value_len );
-
-        if( value == "#integers" )
-        {
-          instances[ arg ].insert( "char" );
-          instances[ arg ].insert( "short" );
-          instances[ arg ].insert( "int" );
-          instances[ arg ].insert( "long" );
-          instances[ arg ].insert( "unsigned char" );
-          instances[ arg ].insert( "unsigned short" );
-          instances[ arg ].insert( "unsigned int" );
-          instances[ arg ].insert( "unsigned long" );
-        }
-        else if( value == "#integers_ptr" )
-        {
-          instances[ arg ].insert( "char*" );
-          instances[ arg ].insert( "short*" );
-          instances[ arg ].insert( "int*" );
-          instances[ arg ].insert( "long*" );
-          instances[ arg ].insert( "unsigned char*" );
-          instances[ arg ].insert( "unsigned short*" );
-          instances[ arg ].insert( "unsigned int*" );
-          instances[ arg ].insert( "unsigned long*" );
-        }
-        else if( value == "#floats" )
-        {
-          instances[ arg ].insert( "double" );
-          instances[ arg ].insert( "float" );
-        }
-        else if( value == "#floats_ptr" )
-        {
-          instances[ arg ].insert( "double*" );
-          instances[ arg ].insert( "float*" );
-        }
-        else if( value == "#all_dims" )
-        {
-          instances[ arg ].insert( "1" );
-          instances[ arg ].insert( "2" );
-          instances[ arg ].insert( "3" );
-          instances[ arg ].insert( "4" );
-        }
-        else if( value == "#all_visual_dims" )
-        {
-          instances[ arg ].insert( "2" );
-          instances[ arg ].insert( "3" );
-        }
-        else
-          instances[ arg ].insert( value );
-
-      } // rof
 
-    } // fi
-
-  } // elihw
-
-  // Span all possible types
-  TVector all_real_classes;
-  for( auto clsIt = classes.begin( ); clsIt != classes.end( ); ++clsIt )
+  // Build definitions
+  TVariables vars;
+  for( auto dIt = lines[ 'd' ].begin( ); dIt != lines[ 'd' ].end( ); ++dIt )
   {
-    // Extract types
-    std::string name = clsIt->substr( clsIt->find_first_not_of( " " ) );
-    std::string tok = name;
-    auto sharpPos = tok.find_first_of( "#" );
-    TInstances li;
-    while( sharpPos != std::string::npos )
-    {
-      tok = tok.substr( sharpPos );
-      auto spacePos = tok.find_first_of( " " );
-      auto arg = tok.substr( 0, spacePos );
-      auto aIt = instances.find( arg );
-      if( aIt != instances.end( ) )
-        li[ arg ] = aIt->second;
-      tok = tok.substr( spacePos );
-      sharpPos = tok.find_first_of( "#" );
-
-    } // eliwh
-    if( li.size( ) > 0 )
-    {
-      // Combine types
-      TSet combs;
-      if( li.size( ) > 1 )
-      {
-        auto iIt = li.begin( );
-        auto jIt = iIt;
-        jIt++;
-        for( ; jIt != li.end( ); ++iIt, ++jIt )
-        {
-          if( iIt == li.begin( ) )
-            combs = Combine( iIt->second, jIt->second );
-          else
-            combs = Combine( combs, jIt->second );
-
-        } // rof
-      }
-      else
-        combs = li.begin( )->second;
-
-      // Write instantiations
-      for( auto combIt = combs.begin( ); combIt != combs.end( ); ++combIt )
-      {
-        char* buffer = new char[ combIt->size( ) + 1 ];
-        std::strcpy( buffer, combIt->c_str( ) );
-        buffer[ combIt->size( ) ] = '\0';
-        char* tok = std::strtok( buffer, "#" );
-        std::map< std::string, std::string > real_instance;
-        for( auto lIt = li.begin( ); lIt != li.end( ); ++lIt )
-        {
-          real_instance[ lIt->first ] = std::string( tok );
-          tok = std::strtok( NULL, "#" );
-
-        } // rof
-        delete buffer;
-
-        std::string real_name = name;
-        auto riIt = real_instance.begin( );
-        for( ; riIt != real_instance.end( ); ++riIt )
-          Replace( real_name, riIt->first, riIt->second );
-        all_real_classes.push_back( real_name );
-
-      } // rof
-    }
-    else
-      all_real_classes.push_back( name );
+    auto tokens = Tokenize( *dIt, "=;" );
+    auto tIt = tokens.begin( );
+    auto vName = *tIt;
+    tIt++;
+    for( ; tIt != tokens.end( ); ++tIt )
+      vars[ vName ].push_back( *tIt );
 
   } // rof
 
-  // Write files
-  std::ofstream out_str( head_fname.c_str( ) );
-  if( !out_str )
+  // 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;
+  ParseIncludes( template_sources, lines[ 't' ], "xx" );
+
+  // Expand groups
+  TLines classes;
+  ExpandGroups( classes, lines[ 'c' ] );
+
+  // 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 ] );
+  if( !header_file )
   {
-    std::cerr << "Error opening file \"" << head_fname << "\"" << std::endl;
+    std::cerr
+      << "Error opening \"" << argv[ 3 ] << "\" for writing." << std::endl;
     return( 1 );
 
   } // fi
+  PrintLines( "", "", global_header, header_file );
+  header_file
+    << "#include <" << lname << "_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 );
+  PrintLines( "", "", normal_includes, header_file );
+  PrintLines( "", "", template_includes, header_file );
+  header_file << std::endl << "#ifdef " << lname << "_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 << std::endl;
+  PrintLines( "", "", end_global_header, header_file );
+  header_file.close( );
+
+  // Write source file
+  std::ofstream source_file( argv[ 4 ] );
+  if( !source_file )
+  {
+    std::cerr
+      << "Error opening \"" << argv[ 3 ] << "\" for writing." << std::endl;
+    return( 1 );
 
-  out_str << "#ifndef __" << dir << "__" << library_name << "__H__" << std::endl;
-  out_str << "#define __" << dir << "__" << library_name << "__H__" << std::endl << std::endl;
-  out_str << "#include <" << dir << "/" << library_name << "_Export.h>" << std::endl;
-
-  // First incl
-  for( auto inclIt = first_incl.begin( ); inclIt != first_incl.end( ); ++inclIt )
-    out_str
-      << "#include <" << *inclIt << ">" << std::endl;
-  out_str << std::endl;
-
-  std::string base_name = dir + std::string( "_" ) + library_name;
-  out_str
-    << std::endl
-    << "#ifdef " << base_name << "_EXPORTS" << std::endl
-    << "#  undef  ITK_MANUAL_INSTANTIATION" << std::endl
-    << "#  define " << base_name << "_PREFIX template class " << base_name << "_EXPORT" << std::endl
-    << "#else" << std::endl
-    << "#  define ITK_MANUAL_INSTANTIATION" << std::endl
-    << "#  define " << base_name << "_PREFIX extern template class" << std::endl
-    << "#endif" << std::endl << std::endl;
-
-  // Incl
-  for( auto inclIt = incl.begin( ); inclIt != incl.end( ); ++inclIt )
-    out_str
-      << "#include <" << *inclIt << ">" << std::endl;
-  out_str << std::endl;
-
-  // All classes
-  for( auto clsIt = all_real_classes.begin( ); clsIt != all_real_classes.end( ); ++clsIt )
-    out_str
-      << base_name << "_PREFIX " << *clsIt << ";" << std::endl;
-  out_str << std::endl;
-
-  out_str << "#endif // __" << dir << "__" << library_name << "__H__" << std::endl;
-  out_str << std::endl << "// eof" << std::endl;
-
-  out_str.close( );
-
+  } // 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
+     );
+  */
+  source_file.close( );
   return( 0 );
 }
 
 // -------------------------------------------------------------------------
-TVector Tokenize( const std::string& str, const std::string& delims )
+TLines Tokenize( const std::string& str, const std::string& delims )
 {
-  TVector tokens;
+  TLines tokens;
   if( str.size( ) > 0 )
   {
     char* buffer = new char[ str.size( ) + 1 ];
@@ -298,37 +187,163 @@ TVector Tokenize( const std::string& str, const std::string& delims )
 }
 
 // -------------------------------------------------------------------------
-TSet Combine( const TSet& X, const TSet& Y )
+std::string Replace(
+  const std::string& str, const std::string& sub, const std::string& nsub
+  )
+{
+  std::string res = str;
+  size_t index = 0;
+  while( true )
+  {
+    index = res.find( sub,  index );
+    if( index == std::string::npos ) break;
+    res.replace( index, sub.size( ), nsub );
+    index += sub.size( );
+
+  } // elihw
+  return( res );
+}
+
+// -------------------------------------------------------------------------
+bool ReadFile( TParsedLines& lines, const std::string& fname )
 {
-  TSet Z;
-  for( auto xIt = X.begin( ); xIt != X.end( ); ++xIt )
+  // 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( 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 ) )
   {
-    for( auto yIt = Y.begin( ); yIt != Y.end( ); ++yIt )
+    auto cmd_pos = line.end( );
+    auto arg_pos = line.end( );
+    auto lIt = line.begin( );
+    while( lIt != line.end( ) )
     {
-      std::stringstream val;
-      val << *xIt << "#" << *yIt;
-      Z.insert( val.str( ) );
+      if( !std::isblank( *lIt ) )
+      {
+        if( cmd_pos == line.end( ) )
+        {
+          cmd_pos = lIt;
+          ++lIt;
+        }
+        else if( arg_pos == line.end( ) )
+        {
+          arg_pos = lIt;
+          lIt = line.end( );
+
+        } // 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 );
+}
 
-    } // rof
+// -------------------------------------------------------------------------
+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 )
+        *tIt = lIt->substr( 0, b_pos ) + *tIt + lIt->substr( e_pos + 1 );
+      ExpandGroups( res, tokens );
+    }
+    else
+      res.push_back( *lIt );
 
   } // rof
-  return( Z );
 }
 
 // -------------------------------------------------------------------------
-void Replace(
-  std::string& str, const std::string& sub, const std::string& nsub
-  )
+void ExpandVariables( TLines& res, const TLines& lines, const TVariables& vars )
 {
-  size_t index = 0;
-  while( true )
+  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 )
   {
-    index = str.find( sub,  index );
-    if( index == std::string::npos ) break;
-    str.replace( index, sub.size( ), nsub );
-    index += sub.size( );
+    auto b_pos = lIt->find( "#" );
+    if( b_pos != std::string::npos )
+    {
+      auto tokens = Tokenize( lIt->substr( b_pos ), " ,;:{}[]()\"$&<>" );
+      std::string cmd = tokens[ 0 ];
+      if(
+        cmd == "#int_types" || cmd == "#uint_types" || cmd == "#float_types"
+        )
+      {
+        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( ) )
+        {
+          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 );
 
-  } // elihw
+        } // fi
+
+      } // fi
+    }
+    else
+      res.push_back( *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,
+  const TLines& lines, std::ostream& out
+  )
+{
+  for( auto i = lines.begin( ); i != lines.end( ); ++i )
+    out << prefix << *i << suffix << std::endl;
 }
 
 // eof - $RCSfile$