diff --git a/include/osgDB/Registry b/include/osgDB/Registry index 76c7d1e21..b8333040a 100644 --- a/include/osgDB/Registry +++ b/include/osgDB/Registry @@ -81,6 +81,11 @@ class OSGDB_EXPORT Registry : public osg::Referenced * by the libdb_tiff readerwriter plugin.*/ void addFileExtensionAlias(const std::string mapExt, const std::string toExt); + /** Reads a file that configures extension mappings. File is ASCII text + * and each line contains the parameyters to the addFileExtensionAlias + * method. Lines can be commented out with an initial '#' character.*/ + bool readPluginAliasConfigurationFile( const std::string& file ); + void addDotOsgWrapper(DotOsgWrapper* wrapper); void removeDotOsgWrapper(DotOsgWrapper* wrapper); @@ -469,6 +474,9 @@ class OSGDB_EXPORT Registry : public osg::Referenced // map to alias to extensions to plugins. ExtensionAliasMap _extAliasMap; + + // Utility: Removes whitespace from both ends of a string. + static std::string trim( const std::string& str ); // options to pass to reader writers. osg::ref_ptr _options; diff --git a/src/osgDB/Registry.cpp b/src/osgDB/Registry.cpp index f58d2802b..37c950779 100644 --- a/src/osgDB/Registry.cpp +++ b/src/osgDB/Registry.cpp @@ -474,6 +474,58 @@ void Registry::addFileExtensionAlias(const std::string mapExt, const std::string _extAliasMap[mapExt] = toExt; } +bool Registry::readPluginAliasConfigurationFile( const std::string& file ) +{ + std::string fileName = osgDB::findDataFile( file ); + if (fileName.empty()) + { + osg::notify( osg::WARN) << "Can't find plugin alias config file \"" << file << "\"." << std::endl; + return false; + } + + std::ifstream ifs; + ifs.open( fileName.c_str() ); + if (!ifs.good()) + { + osg::notify( osg::WARN) << "Can't open plugin alias config file \"" << fileName << "\"." << std::endl; + return false; + } + + int lineNum( 0 ); + while (ifs.good()) + { + std::string raw; + ++lineNum; + std::getline( ifs, raw ); + std::string ln = trim( raw ); + if (ln.empty()) continue; + if (ln[0] == '#') continue; + + std::string::size_type spIdx = ln.find_first_of( " \t" ); + if (spIdx == ln.npos) + { + // mapExt and toExt must be on the same line, separated by a space. + osg::notify( osg::WARN) << file << ", line " << lineNum << ": Syntax error: missing space in \"" << raw << "\"." << std::endl; + continue; + } + + const std::string mapExt = trim( ln.substr( 0, spIdx ) ); + const std::string toExt = trim( ln.substr( spIdx+1 ) ); + addFileExtensionAlias( mapExt, toExt ); + } + return true; +} + +std::string Registry::trim( const std::string& str ) +{ + if (!str.size()) return str; + std::string::size_type first = str.find_first_not_of( " \t" ); + std::string::size_type last = str.find_last_not_of( " \t\r\n" ); + if ((first==str.npos) || (last==str.npos)) return std::string( "" ); + return str.substr( first, last-first+1 ); +} + + std::string Registry::createLibraryNameForFile(const std::string& fileName) { std::string ext = getLowerCaseFileExtension(fileName);