]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface.cxx
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 )