]> Creatis software - cpPlugins.git/blobdiff - appli/bash/Config.h.in
...
[cpPlugins.git] / appli / bash / Config.h.in
diff --git a/appli/bash/Config.h.in b/appli/bash/Config.h.in
new file mode 100644 (file)
index 0000000..09eb417
--- /dev/null
@@ -0,0 +1,294 @@
+#ifndef __cpPlugins__bash__Config__h__
+#define __cpPlugins__bash__Config__h__
+
+// -------------------------------------------------------------------------
+#include <cmath>
+#include <cstring>
+#include <deque>
+#include <fstream>
+#include <iostream>
+#include <map>
+#include <queue>
+#include <sstream>
+#include <string>
+
+// -------------------------------------------------------------------------
+#define cpPlugins_CONFIG_BOOLEAN_TYPES      "@cpPlugins_CONFIG_BOOLEAN_TYPES@"
+#define cpPlugins_CONFIG_INTEGER_TYPES      "@cpPlugins_CONFIG_INTEGER_TYPES@"
+#define cpPlugins_CONFIG_REAL_TYPES         "@cpPlugins_CONFIG_REAL_TYPES@"
+#define cpPlugins_CONFIG_PROCESS_DIMENSIONS "@cpPlugins_CONFIG_PROCESS_DIMENSIONS@"
+#define cpPlugins_CONFIG_VISUAL_DIMENSIONS  "@cpPlugins_CONFIG_VISUAL_DIMENSIONS@"
+#define cpPlugins_CONFIG_COLOR_PIXELS       "@cpPlugins_CONFIG_COLOR_PIXELS@"
+#define cpPlugins_CONFIG_VECTORS            "@cpPlugins_CONFIG_VECTORS@"
+#define cpPlugins_CONFIG_DIFFUSIONTENSORS   "@cpPlugins_CONFIG_DIFFUSIONTENSORS@"
+#define cpPlugins_CONFIG_MATRICES           "@cpPlugins_CONFIG_MATRICES@"
+#define cpPlugins_ALL_CONFIGS               "@cpPlugins_ALL_CONFIGS@"
+#define cpPlugins_CONFIG_NUMBER_OF_FILES    @cpPlugins_CONFIG_NUMBER_OF_FILES@
+
+// -------------------------------------------------------------------------
+#define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
+#ifdef cpPlugins_bash_OS_Windows
+#  define cpPlugins_bash_STRTOK( A, B, N )  strtok_s(  A, B, N )
+#  define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
+#else // cpPlugins_bash_OS_Windows
+#  define cpPlugins_bash_STRTOK( A, B, N )  std::strtok( A, B )
+#  define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
+#endif // cpPlugins_bash_OS_Windows
+
+// -------------------------------------------------------------------------
+typedef std::deque< std::string > TStrings;
+typedef std::map< std::string, TStrings > TCommands;
+
+/**
+ */
+namespace cpPlugins_bash
+{
+  // -----------------------------------------------------------------------
+  template< class _TTokens >
+  inline void Tokenize(
+    _TTokens& tokens, const std::string& str, const std::string& delims
+    )
+  {
+    tokens.clear( );
+    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_bash_STRTOK( buffer, delims.c_str( ), &next );
+      while( it != NULL )
+      {
+        tokens.push_back( std::string( it ) );
+        it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
+
+      } // elihw
+      delete [] buffer;
+
+    } // fi
+  }
+
+  // -----------------------------------------------------------------------
+  inline 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 );
+  }
+
+  // -----------------------------------------------------------------------
+  inline bool Read( std::string& buffer, const std::string& fname )
+  {
+    buffer = "";
+    std::ifstream file_stream( fname.c_str( ) );
+    if( !file_stream )
+      return( false );
+    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( );
+    return( true );
+  }
+
+  // -----------------------------------------------------------------------
+  inline bool Write( const std::string& buffer, const std::string& fname )
+  {
+    std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
+    if( !file_stream )
+      return( false );
+    file_stream.write( buffer.c_str( ), buffer.size( ) );
+    return( true );
+  }
+
+  // -----------------------------------------------------------------------
+  inline void Parse( TCommands& commands, const TStrings& lines )
+  {
+    for( auto l = lines.begin( ); l != lines.end( ); ++l )
+    {
+      auto line = l->substr( l->find_first_not_of( " " ) );
+      if( line != "" )
+      {
+        if( line[ 0 ] != '*' )
+        {
+          auto cmd = line.substr( 0, line.find( " " ) );
+          auto args = line.substr( line.find( " " ) + 1 );
+          commands[ cmd ].push_back( args );
+
+        } // fi
+
+      } // fi
+
+    } // rof
+  }
+
+  // -----------------------------------------------------------------------
+  inline void LoadDefinitions( TCommands& commands )
+  {
+    commands[ "define" ].push_back(
+      std::string( "int_types=" ) +
+      std::string( cpPlugins_CONFIG_INTEGER_TYPES )
+      );
+    commands[ "define" ].push_back(
+      std::string( "real_types=" ) +
+      std::string( cpPlugins_CONFIG_REAL_TYPES )
+      );
+    commands[ "define" ].push_back(
+      std::string( "process_dims=" ) +
+      std::string( cpPlugins_CONFIG_PROCESS_DIMENSIONS )
+      );
+    commands[ "define" ].push_back(
+      std::string( "visual_dims=" ) +
+      std::string( cpPlugins_CONFIG_VISUAL_DIMENSIONS )
+      );
+    commands[ "define" ].push_back(
+      std::string( "color_pixels=" ) +
+      std::string( cpPlugins_CONFIG_COLOR_PIXELS )
+      );
+    commands[ "define" ].push_back(
+      std::string( "vectors=" ) +
+      std::string( cpPlugins_CONFIG_VECTORS )
+      );
+    commands[ "define" ].push_back(
+      std::string( "diff_tensors=" ) +
+      std::string( cpPlugins_CONFIG_DIFFUSIONTENSORS )
+      );
+    commands[ "define" ].push_back(
+      std::string( "matrices=" ) +
+      std::string( cpPlugins_CONFIG_MATRICES )
+      );
+    if( std::string( cpPlugins_CONFIG_INTEGER_TYPES ) != "" )
+      commands[ "define" ].push_back(
+        std::string( "uint_types=unsigned #int_types#" )
+        );
+    commands[ "define" ].push_back(
+      std::string(
+        "scalar_pixels=#int_types#;#uint_types#;#real_types#"
+        )
+      );
+  }
+
+  // -----------------------------------------------------------------------
+  inline void ExpandDefinitions(
+    TCommands& definitions, const TCommands& commands
+    )
+  {
+    definitions.clear( );
+    auto defs = commands.find( "define" );
+    if( defs == commands.end( ) )
+      return;
+
+    std::map< std::string, std::string > values;
+    for( auto dIt = defs->second.begin( ); dIt != defs->second.end( ); ++dIt )
+    {
+      TStrings toks;
+      cpPlugins_bash::Tokenize( toks, *dIt, "=" );
+      if( toks.size( ) == 2 )
+      {
+        auto name = toks[ 0 ].substr( toks[ 0 ].find_first_not_of( " " ) );
+        auto val = toks[ 1 ].substr( toks[ 1 ].find_first_not_of( " " ) );
+        values[ name ] = val;
+
+      } // fi
+
+    } // rof
+    for( auto vIt = values.begin( ); vIt != values.end( ); ++vIt )
+    {
+      TStrings toks;
+      cpPlugins_bash::Tokenize( toks, vIt->second, ";" );
+      for( auto tIt = toks.begin( ); tIt != toks.end( ); ++tIt )
+        definitions[ vIt->first ].push_back( *tIt );
+
+    } // rof
+    for( auto dIt = definitions.begin( ); dIt != definitions.end( ); ++dIt )
+    {
+      auto name = std::string( "#" ) + dIt->first + std::string( "#" );
+      for( auto eIt = definitions.begin( ); eIt != definitions.end( ); ++eIt )
+      {
+        if( eIt != dIt )
+        {
+          auto vIt = eIt->second.begin( );
+          while( vIt != eIt->second.end( ) )
+          {
+            if( vIt->find( name ) != std::string::npos )
+            {
+              for(
+                auto wIt = dIt->second.begin( );
+                wIt != dIt->second.end( );
+                ++wIt
+                )
+                eIt->second.push_back(
+                  cpPlugins_bash::Replace( *vIt, name, *wIt )
+                  );
+              vIt = eIt->second.erase( vIt );
+            }
+            else
+              ++vIt;
+
+          } // elihw
+
+        } // fi
+
+      } // rof
+
+    } // rof
+  }
+
+  // -----------------------------------------------------------------------
+  inline void Expand(
+    TStrings& tfiles,
+    const TCommands& definitions,
+    const TCommands& commands,
+    const std::string& cmd
+    )
+  {
+    tfiles.clear( );
+    auto tIt = commands.find( cmd );
+    if( tIt == commands.end( ) )
+      return;
+
+    for( auto fIt = tIt->second.begin( ); fIt != tIt->second.end( ); ++fIt )
+    {
+      std::queue< std::string > q;
+      q.push( *fIt );
+      while( q.size( ) > 0 )
+      {
+        auto value = q.front( );
+        q.pop( );
+        auto spos = value.find( "#" );
+        if( spos != std::string::npos )
+        {
+          auto name = value.substr( spos + 1 );
+          auto epos = name.find( "#" );
+          name = name.substr( 0, epos );
+          auto dIt = definitions.find( name );
+          if( dIt != definitions.end( ) )
+          {
+            name = std::string( "#" ) + name + std::string( "#" );
+            for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
+              q.push( cpPlugins_bash::Replace( value, name, *vIt ) );
+
+          } // fi
+        }
+        else
+          tfiles.push_back( value );
+
+      } // rof
+
+    } // rof
+  }
+
+} // ecapseman
+
+#endif // __cpPlugins__bash__Config__h__
+
+// eof - $RCSfile$