From e5916bd329611f11ff2f31ff42e853bac21b825a Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Tue, 28 Jun 2016 08:23:43 -0500 Subject: [PATCH] ... --- lib/cpBaseQtApplication/MainWindow.cxx | 12 +++++- lib/cpBaseQtApplication/MainWindow.h | 1 + lib/cpPlugins/Interface.cxx | 53 ++++++++++++++++++++---- lib/cpPlugins/Interface.h | 15 ++----- lib/cpPlugins/Utilities.h | 56 +++++++++++++++++--------- 5 files changed, 96 insertions(+), 41 deletions(-) diff --git a/lib/cpBaseQtApplication/MainWindow.cxx b/lib/cpBaseQtApplication/MainWindow.cxx index 1a42557..d70d4c0 100644 --- a/lib/cpBaseQtApplication/MainWindow.cxx +++ b/lib/cpBaseQtApplication/MainWindow.cxx @@ -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 ) ); } diff --git a/lib/cpBaseQtApplication/MainWindow.h b/lib/cpBaseQtApplication/MainWindow.h index ca06a21..7c7bcf9 100644 --- a/lib/cpBaseQtApplication/MainWindow.h +++ b/lib/cpBaseQtApplication/MainWindow.h @@ -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; diff --git a/lib/cpPlugins/Interface.cxx b/lib/cpPlugins/Interface.cxx index 8ca6832..f898e7b 100644 --- a/lib/cpPlugins/Interface.cxx +++ b/lib/cpPlugins/Interface.cxx @@ -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 ) diff --git a/lib/cpPlugins/Interface.h b/lib/cpPlugins/Interface.h index 0f46554..86c46b9 100644 --- a/lib/cpPlugins/Interface.h +++ b/lib/cpPlugins/Interface.h @@ -8,10 +8,6 @@ #include #include -/* TODO - #include -*/ - 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 ); diff --git a/lib/cpPlugins/Utilities.h b/lib/cpPlugins/Utilities.h index b919c20..ff033b4 100644 --- a/lib/cpPlugins/Utilities.h +++ b/lib/cpPlugins/Utilities.h @@ -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__ -- 2.45.1