]> Creatis software - cpPlugins.git/commitdiff
...
authorLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Tue, 28 Jun 2016 13:23:43 +0000 (08:23 -0500)
committerLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Tue, 28 Jun 2016 13:23:43 +0000 (08:23 -0500)
lib/cpBaseQtApplication/MainWindow.cxx
lib/cpBaseQtApplication/MainWindow.h
lib/cpPlugins/Interface.cxx
lib/cpPlugins/Interface.h
lib/cpPlugins/Utilities.h

index 1a425579b44d19b51f0bb2dbdabcc580e0d6a665..d70d4c014bca5b19988f95a417bfb165ab5b38b1 100644 (file)
@@ -49,7 +49,17 @@ MainWindow(
 {
   QFileInfo i( argv[ 0 ] );
   if( i.exists( ) )
-    this->m_Interface.UpdateEnvironments( i.canonicalPath( ).toStdString( ) );
+  {
+    this->m_ExecutionPath = i.canonicalPath( ).toStdString( );
+    try
+    {
+      this->m_Interface.OpenEnvironments( this->m_ExecutionPath );
+      this->m_Interface.UpdateEnvironments( this->m_ExecutionPath );
+    }
+    catch( ... ) { }
+  }
+  else
+    this->m_ExecutionPath = "";
   this->m_Workspace.SetInterface( &( this->m_Interface ) );
 }
 
index ca06a2167bd10cc91f8c0b6579b7a6704c388cc4..7c7bcf9551d676f95c340c5195d302ec0ec46025 100644 (file)
@@ -90,6 +90,7 @@ namespace cpBaseQtApplication
     _TBlocker     m_Blocker;
     std::string   m_PluginsPath;
 
+    std::string m_ExecutionPath;
     cpPlugins::Interface m_Interface;
     cpPlugins::Workspace m_Workspace;
 
index 8ca68322ada90e5a9bb4703b0a024ff344f4603f..f898e7b1e92d7c2237cc34d05159d1c4664c2b28 100644 (file)
@@ -102,15 +102,9 @@ LoadEnvironment( )
     dir << *i;
     if( !cpPlugins::IsPathSeparator( i->back( ) ) )
       dir << cpPlugins_PATH_SEPARATOR;
+    std::string fname = dir.str( ) + std::string( cpPlugins_CONFIG );
     std::string config_file;
-    if(
-      cpPlugins::ReadFileIntoBuffer(
-        config_file,
-        cpPlugins::CanonicalPath(
-          dir.str( ) + std::string( cpPlugins_CONFIG )
-          )
-        )
-      )
+    if( cpPlugins::ReadFileIntoBuffer( config_file, fname ) )
     {
       std::istringstream input( config_file );
       for( std::string line; std::getline( input, line ); )
@@ -155,6 +149,49 @@ LoadEnvironment( )
       );
 }
 
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::
+SaveEnvironments( const std::string& dir ) const
+{
+  if( this->m_Paths.size( ) > 0 )
+  {
+    std::stringstream buffer;
+    auto i = this->m_Paths.begin( );
+    for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
+      buffer << *i << std::endl;
+
+    std::stringstream fname;
+    fname << dir;
+    if( cpPlugins::IsPathSeparator( dir.back( ) ) )
+      fname << cpPlugins_PATH_SEPARATOR;
+    fname << cpPlugins_CONFIG;
+    if( !( cpPlugins::WriteBufferToFile( buffer.str( ), fname.str( ) ) ) )
+      throw std::runtime_error( "Error writing environment file." );
+  }
+  else
+    throw std::runtime_error( "No paths to save." );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::
+OpenEnvironments( const std::string& dir )
+{
+  std::stringstream fname;
+  fname << dir;
+  if( cpPlugins::IsPathSeparator( dir.back( ) ) )
+    fname << cpPlugins_PATH_SEPARATOR;
+  fname << cpPlugins_CONFIG;
+  std::string buffer;
+  if( cpPlugins::ReadFileIntoBuffer( buffer, fname.str( ) ) )
+  {
+    std::istringstream input( buffer );
+    for( std::string line; std::getline( input, line ); )
+      this->m_Paths.insert( cpPlugins::CanonicalPath( line ) );
+  }
+  else
+    throw std::runtime_error( "Error opening environment file." );
+}
+
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::
 LoadFile( const std::string& fname )
index 0f4655453df15b46e33d344e766d9ef8c3b1f996..86c46b9ce18b60535a5cff5f2583c16faed42ee8 100644 (file)
@@ -8,10 +8,6 @@
 #include <utility>
 #include <vector>
 
-/* TODO
-   #include <string>
-*/
-
 namespace cpPlugins
 {
   /**
@@ -31,14 +27,6 @@ namespace cpPlugins
     };
     typedef std::map< std::string, std::map< std::string, TCreatorData > > TFilters;
 
-    /* TODO
-       typedef std::pair< std::string, TCreator >               TDynFunc;
-       typedef std::map< std::string, TDynFunc >                TDynFilter;
-       typedef std::map< std::string, TDynFilter >              TDynFilters;
-       typedef std::pair< std::string, void* >                  TDynFileInfo;
-       typedef std::map< std::string, TDynFileInfo >            TDynLibraries;
-    */
-
   public:
     Interface( );
     virtual ~Interface( );
@@ -51,6 +39,9 @@ namespace cpPlugins
 
     void UpdateEnvironments( const std::string& new_environment );
     void LoadEnvironment( );
+    void SaveEnvironments( const std::string& dir ) const;
+    void OpenEnvironments( const std::string& dir );
+
     void LoadFile( const std::string& fname );
     void LoadPlugin( const std::string& pname );
     void LoadDirectory( const std::string& dirname );
index b919c202adbc4f15fc38696a0161a800fdd2d82e..ff033b4b62277e674a8c85cded12860f8f6e19af 100644 (file)
@@ -87,26 +87,6 @@ namespace cpPlugins
     return( res );
   }
 
-  // -----------------------------------------------------------------------
-  inline bool ReadFileIntoBuffer(
-    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 std::string CanonicalPath( const std::string& path )
   {
@@ -128,6 +108,42 @@ namespace cpPlugins
     return( ret );
   }
 
+  // -----------------------------------------------------------------------
+  inline bool WriteBufferToFile(
+    const std::string& buffer, const std::string& fname
+    )
+  {
+    std::ofstream file_stream(
+      CanonicalPath( fname ).c_str( ), std::ofstream::binary
+      );
+    if( !file_stream )
+      return( false );
+    file_stream.write( buffer.c_str( ), buffer.size( ) );
+    return( true );
+  }
+
+  // -----------------------------------------------------------------------
+  inline bool ReadFileIntoBuffer(
+    std::string& buffer, const std::string& fname
+    )
+  {
+    buffer = "";
+    std::ifstream file_stream(
+      CanonicalPath( fname ).c_str( ), std::ifstream::binary
+      );
+    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 );
+  }
+
 } // ecapseman
 
 #endif // __CPPLUGINS__UTILITY__H__