#include <cpPlugins/OS/DLLManager.h>
#include <cpPlugins/Interface/Dirent.h>
#include <cpExtensions/Utility.h>
+#include <regex>
// -------------------------------------------------------------------------
cpPlugins::Interface::Plugins::
// -------------------------------------------------------------------------
void cpPlugins::Interface::Plugins::
-AddEnvironments( const std::string& new_environment )
+AddEnvironments( const std::string& env )
{
- std::vector< std::string > tokens;
- cpExtensions::Tokenize( tokens, new_environment, cpPlugins_ENV_SEPARATOR );
- for( auto i = tokens.begin( ); i != tokens.end( ); ++i )
- {
- std::stringstream dir;
- dir << cpExtensions::CanonicalPath( *i );
- if( dir.str( ) != "" )
- this->m_Paths.insert( dir.str( ) );
-
- } // rof
+ std::vector< std::string > directories;
+ cpExtensions::Tokenize( directories, env, cpPlugins_ENV_SEPARATOR );
+ for( auto dir = directories.begin( ); dir != directories.end( ); ++dir )
+ this->m_Paths.insert( cpExtensions::CanonicalPath( *dir ) );
}
// -------------------------------------------------------------------------
void cpPlugins::Interface::Plugins::
LoadEnvironments( )
{
- std::stringstream all_errors;
+ std::set< std::string > libs;
for( auto d = this->m_Paths.begin( ); d != this->m_Paths.end( ); ++d )
{
- std::stringstream name;
- name << *d << cpPlugins_CONFIG;
+ std::stringstream fname;
+ fname << *d << cpPlugins_PATH_SEPARATOR << cpPlugins_CONFIG;
std::string buffer;
- if( cpExtensions::Read( buffer, name.str( ) ) )
+ if( cpExtensions::Read( buffer, fname.str( ) ) )
{
std::istringstream input( buffer );
for( std::string line; std::getline( input, line ); )
{
std::vector< std::string > tokens;
- cpExtensions::Tokenize( tokens, line, "@" );
- std::string library_file = "";
- if( tokens[ 0 ] == "local" )
- library_file =
- cpExtensions::CanonicalPath(
- *d + std::string( cpPlugins_LIB_PREFIX ) +
- tokens[ 1 ] + std::string( cpPlugins_LIB_EXT )
- );
- else if( tokens[ 0 ] == "global" )
- library_file = tokens[ 1 ];
-
- if( library_file != "" )
+ cpExtensions::Tokenize( tokens, line, "|" );
+ if( tokens.size( ) == 2 )
{
- std::string error = "";
- void* hnd = cpPlugins::OS::DLLManager::Load( library_file, error );
- if( hnd != NULL )
- this->m_Libraries[ library_file ] = hnd;
+ std::string pth = tokens[ 0 ];
+ std::string lib = tokens[ 1 ];
+ if( lib.find( "*" ) != std::string::npos )
+ {
+ std::string ext( cpPlugins_LIB_EXT );
+ DIR* dir;
+ struct dirent* ent;
+ if( ( dir = opendir( pth.c_str( ) ) ) != NULL )
+ {
+ while( ( ent = readdir( dir ) ) != NULL )
+ {
+ std::string fname( ent->d_name );
+ long pos = long( fname.size( ) ) - long( ext.size( ) );
+ if( pos > 0 )
+ {
+ if( fname.substr( pos ) == ext )
+ {
+ std::regex re( lib );
+std::smatch match;
+if( std::regex_search( fname, match, re ) && match.size( ) >= 1 )
+ {
+ std::stringstream str;
+ str
+ << pth << cpPlugins_PATH_SEPARATOR << fname;
+ libs.insert( str.str( ) );
+ } // fi
+
+ } // fi
+
+ } // fi
+
+ } // elihw
+ closedir( dir );
+
+ } // fi
+ }
else
- all_errors << " ; " << error;
+ {
+ std::stringstream str;
+ str
+ << pth << cpPlugins_PATH_SEPARATOR << cpPlugins_LIB_PREFIX
+ << lib << cpPlugins_LIB_EXT;
+ libs.insert( str.str( ) );
+
+ } // fi
} // fi
} // fi
} // rof
+ for( auto l = libs.begin( ); l != libs.end( ); ++l )
+ {
+ std::string lib = cpExtensions::CanonicalPath( *l );
+ if( lib != "" )
+ {
+ if( this->m_Libraries.find( lib ) == this->m_Libraries.end( ) )
+{
+ std::string error = "";
+ void* hnd = cpPlugins::OS::DLLManager::Load( lib, error );
+ if( hnd != NULL )
+ this->m_Libraries[ lib ] = hnd;
- // Throw errors
- if( all_errors.str( ) != "" )
- throw std::runtime_error(
- std::string( "Loading environment libraries errors: " ) +
- all_errors.str( )
- );
+} // fi
+
+ } // fi
+
+ } // rof
}
// -------------------------------------------------------------------------
void cpPlugins::Interface::Plugins::
-SaveEnvironments( const std::string& dir ) const
+LoadPaths( const std::string& dir )
{
- std::stringstream buffer;
- for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
- buffer << *i << std::endl;
-
- std::stringstream fname;
+ std::stringstream fname, envs;
fname << dir;
if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
fname << cpExtensions_PATH_SEPARATOR;
fname << cpPlugins_PATHS;
- if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) )
- throw std::runtime_error( "Error writing environment file." );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-OpenEnvironments( const std::string& dir )
-{
- std::vector< std::string > tokens;
- cpExtensions::Tokenize( tokens, dir, cpPlugins_ENV_SEPARATOR );
- for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
- {
- std::stringstream fname;
- fname << *tIt;
- if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
- fname << cpExtensions_PATH_SEPARATOR;
- fname << cpPlugins_PATHS;
std::string buffer;
if( cpExtensions::Read( buffer, fname.str( ) ) )
{
std::istringstream input( buffer );
- std::stringstream paths;
for( std::string line; std::getline( input, line ); )
- paths << line << cpPlugins_ENV_SEPARATOR;
- this->AddEnvironments( paths.str( ) );
- }
- else
- {
- bool success = true;
- try
- {
- this->LoadDirectory( dir );
- }
- catch( ... )
- {
- success = false;
-
- } // yrt
- if( success )
- this->AddEnvironments( dir );
+ envs << line << cpPlugins_ENV_SEPARATOR;
} // fi
+ if( envs.str( ).size( ) > 0 )
+ this->AddEnvironments( envs.str( ) );
+}
- } // rof
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::Plugins::
+SavePaths( const std::string& dir ) const
+{
+ std::stringstream buffer;
+ for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
+ buffer << *i << std::endl;
+
+ std::stringstream fname;
+ fname << dir;
+ if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
+ fname << cpExtensions_PATH_SEPARATOR;
+ fname << cpPlugins_PATHS;
+ if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) )
+ throw std::runtime_error( "Error writing environment file." );
}
// -------------------------------------------------------------------------
if( p != NULL )
str << p << cpPlugins_ENV_SEPARATOR;
str << ".";
- this->OpenEnvironments( str.str( ) );
-
- // Try to read locally defined paths
- /* TODO
- std::vector< std::string > tokens;
- cpExtensions::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
- for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
- {
- try
- {
- this->OpenEnvironments( *t );
- }
- catch( ... ) { }
-
- } // rof
- */
+ this->AddEnvironments( str.str( ) );
}
// -------------------------------------------------------------------------