diff --git a/include/osgDB/FileUtils b/include/osgDB/FileUtils index 4c34f98ed..849d49f7a 100644 --- a/include/osgDB/FileUtils +++ b/include/osgDB/FileUtils @@ -5,36 +5,22 @@ #ifndef OSGDB_FILEUTILS #define OSGDB_FILEUTILS 1 -#include +#include #include +#include #include namespace osgDB { -/** initialize the data file path, - * uses OSGFILEPATH environmental - * variable to defined.*/ -OSGDB_EXPORT extern void initFilePath( void ); -/** set the data file path.*/ -OSGDB_EXPORT extern void setFilePath( const char *_path ); +/** return true if a file exisits. */ +OSGDB_EXPORT extern bool fileExists(const std::string& filename); -/** set the data file path.*/ -OSGDB_EXPORT extern const char* getFilePath(); -/** find specified file if specified file path.*/ -OSGDB_EXPORT extern char *findFileInPath( const char *_file, const char * filePath ); - -/** find specified file on the set data file path.*/ -OSGDB_EXPORT extern char *findFile( const char *file ); - -/** find specified dso/dll.*/ -OSGDB_EXPORT extern char *findDSO( const char *name ); - -/** simple list of names to represent a directory's contents. */ -typedef std::vector DirectoryContents; +/** find specified file in specified file path.*/ +OSGDB_EXPORT extern std::string findFileInPath(const std::string& filename, const FilePathList& filePath); /** return the directory/filename of a file if its is contained within specified directory. * return "" if directory does not contain file. If caseInsensitive is set to true then @@ -43,11 +29,50 @@ typedef std::vector DirectoryContents; */ OSGDB_EXPORT extern std::string findFileInDirectory(const std::string& fileName,const std::string& dirName,bool caseInsensitive=false); +/** simple list of names to represent a directory's contents. */ +typedef std::vector DirectoryContents; + /** return the contents of a directory. * returns an empty array on any error.*/ OSGDB_EXPORT extern DirectoryContents getDirectoryContents(const std::string& dirName); + +inline void setDataFilePathList(const FilePathList& filepath) { osgDB::Registry::instance()->setDataFilePathList(filepath); } + +inline void setDataFilePathList(const std::string& paths) { osgDB::Registry::instance()->setDataFilePathList(paths); } + +inline FilePathList& getDataFilePathList() { return osgDB::Registry::instance()->getDataFilePathList(); } + +OSGDB_EXPORT extern std::string findDataFile(const std::string& filename); + +/** Convinience class for pushing a path on construction, and popping the path + * and destruction. This helps keep the addition of a path local to a block + * of code, even in the presence of exceptions.*/ +class PushAndPopDataPath +{ + public: + PushAndPopDataPath(const std::string& path) + { + getDataFilePathList().push_front(path); + } + + ~PushAndPopDataPath() + { + getDataFilePathList().pop_front(); + } +}; + + +inline void setLibraryFilePathList(const FilePathList& filepaths) { osgDB::Registry::instance()->setLibraryFilePathList(filepaths); } + +inline void setLibraryFilePathList(const std::string& paths) { osgDB::Registry::instance()->setLibraryFilePathList(paths); } + +inline FilePathList& getLibraryFilePathList() { return osgDB::Registry::instance()->getLibraryFilePathList(); } + +OSGDB_EXPORT extern std::string findLibraryFile(const std::string& filename); + + } #endif diff --git a/include/osgDB/Registry b/include/osgDB/Registry index 736abf45b..7324c3d64 100644 --- a/include/osgDB/Registry +++ b/include/osgDB/Registry @@ -5,19 +5,24 @@ #ifndef OSGDB_REGISTRY #define OSGDB_REGISTRY 1 -#include -#include -#include - #include #include #include #include +#include +#include +#include +#include + + namespace osgDB { +/** list of directories to search through which searching for files. */ +typedef std::deque FilePathList; + /** Registry is a singleton factory which stores the reader/writers which are linked in @@ -97,6 +102,46 @@ class OSGDB_EXPORT Registry const ReaderWriter::Options* getOptions() const { return _options.get(); } + /** initilize both the Data and Library FilePaths, by default called by the + * constructor, so it should only be required if you want to force + * the re-reading of environmental variables.*/ + void initFilePathLists() { initDataFilePathList(); initLibraryFilePathList(); } + + /** initilize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/ + void initDataFilePathList(); + + /** Set the data file path using a list of paths stored in a FilePath, which is used when search for data files.*/ + void setDataFilePathList(const FilePathList& filepath) { _dataFilePath = filepath; } + + /** Set the data file path using a single string deliminated either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/ + void setDataFilePathList(const std::string& paths) { _dataFilePath.clear(); convertStringPathIntoFilePathList(paths,_dataFilePath); } + + /** get the data file path which is used when search for data files.*/ + FilePathList& getDataFilePathList() { return _dataFilePath; } + + /** get the const data file path which is used when search for data files.*/ + const FilePathList& getDataFilePathList() const { return _dataFilePath; } + + /** initilize the Library FilePath by reading the OSG_LIBRARY_PATH + * and the appropriate system environmental variables*/ + void initLibraryFilePathList(); + + /** Set the library file path using a list of paths stored in a FilePath, which is used when search for data files.*/ + void setLibraryFilePathList(const FilePathList& filepath) { _libraryFilePath = filepath; } + + /** Set the library file path using a single string deliminated either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/ + void setLibraryFilePathList(const std::string& paths) { _libraryFilePath.clear(); convertStringPathIntoFilePathList(paths,_libraryFilePath); } + + /** get the library file path which is used when search for library (dso/dll's) files.*/ + FilePathList& getLibraryFilePathList() { return _libraryFilePath; } + + /** get the const library file path which is used when search for library (dso/dll's) files.*/ + const FilePathList& getLibraryFilePathList() const { return _libraryFilePath; } + + /** convert a string containing a list of paths deliminated either with ';' (Windows) or ':' (All other platforms) into FilePath represetation.*/ + static void convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath); + + private: typedef std::map > DotOsgWrapperMap; @@ -138,6 +183,9 @@ class OSGDB_EXPORT Registry // options to pass to reader writers. osg::ref_ptr _options; + + FilePathList _dataFilePath; + FilePathList _libraryFilePath; }; diff --git a/src/osgDB/DynamicLibrary.cpp b/src/osgDB/DynamicLibrary.cpp index 3826d46f0..72c660242 100644 --- a/src/osgDB/DynamicLibrary.cpp +++ b/src/osgDB/DynamicLibrary.cpp @@ -36,15 +36,15 @@ DynamicLibrary::~DynamicLibrary() DynamicLibrary* DynamicLibrary::loadLibrary(const std::string& libraryName) { - char* fullLibraryName = osgDB::findDSO( libraryName.c_str() ); - if (fullLibraryName==NULL) return NULL; + std::string fullLibraryName = osgDB::findLibraryFile(libraryName); + if (fullLibraryName.empty()) return NULL; #if defined(WIN32) && !defined(__CYGWIN__) - HANDLE handle = LoadLibrary( fullLibraryName ); + HANDLE handle = LoadLibrary( fullLibraryName.c_str() ); if (handle) return osgNew DynamicLibrary(libraryName,handle); notify(WARN) << "DynamicLibrary::failed loading "< + +#include +#include +#include + +#if defined(WIN32) && !defined(__CYGWIN__) + #include + #include + #include + // set up for windows so acts just like unix access(). + #define F_OK 4 +#else + // unix. + #include + #include #endif + +bool osgDB::fileExists(const std::string& filename) +{ + return access( filename.c_str(), F_OK ) == 0; +} + +std::string osgDB::findFileInPath(const std::string& filename, const FilePathList& filepath) +{ + if (filename.empty()) return filename; + + if(fileExists(filename)) return filename; + + for(FilePathList::const_iterator itr=filepath.begin(); + itr!=filepath.end(); + ++itr) + { + std::string path = *itr + '/'+ filename; + osg::notify(osg::DEBUG_INFO) << "FindFileInPath() : trying " << path << " ...\n"; + if(fileExists(path)) return path; + } + + return std::string(); +} + +std::string osgDB::findDataFile(const std::string& filename) +{ + if (filename.empty()) return filename; + + const FilePathList& filepath = Registry::instance()->getDataFilePathList(); + + std::string fileFound = findFileInPath(filename, filepath); + if (!fileFound.empty()) return fileFound; + + // if a directory is included in the filename, get just the (simple) filename itself and try that + std::string simpleFileName = getSimpleFileName(filename); + if (simpleFileName!=filename) + { + std::string fileFound = findFileInPath(simpleFileName, filepath); + if (!fileFound.empty()) return fileFound; + } + + // return empty string. + return std::string(); +} + +std::string osgDB::findLibraryFile(const std::string& filename) +{ + if (filename.empty()) return filename; + + const FilePathList& filepath = Registry::instance()->getLibraryFilePathList(); + + std::string fileFound = findFileInPath(filename, filepath); + if (!fileFound.empty()) return fileFound; + + // if a directory is included in the filename, get just the (simple) filename itself and try that + std::string simpleFileName = getSimpleFileName(filename); + if (simpleFileName!=filename) + { + std::string fileFound = findFileInPath(simpleFileName, filepath); + if (!fileFound.empty()) return fileFound; + } + + // failed with direct paths, + // now try prepending the filename with "osgPlugins/" + return findFileInPath("osgPlugins/"+simpleFileName,filepath); +} + +std::string osgDB::findFileInDirectory(const std::string& fileName,const std::string& dirName,bool caseInsensitive) +{ + bool needFollowingBackslash = false; + bool needDirectoryName = true; + osgDB::DirectoryContents dc; + + if (dirName.empty()) + { + dc = osgDB::getDirectoryContents("."); + needFollowingBackslash = false; + needDirectoryName = false; + } + else if (dirName=="." || dirName=="./" || dirName==".\\") + { + dc = osgDB::getDirectoryContents("."); + needFollowingBackslash = false; + needDirectoryName = false; + } + else + { + dc = osgDB::getDirectoryContents(dirName); + char lastChar = dirName[dirName.size()-1]; + if (lastChar=='/') needFollowingBackslash = false; + else if (lastChar=='\\') needFollowingBackslash = false; + else needFollowingBackslash = true; + needDirectoryName = true; + } + + for(osgDB::DirectoryContents::iterator itr=dc.begin(); + itr!=dc.end(); + ++itr) + { + if ((caseInsensitive && osgDB::equalCaseInsensitive(fileName,*itr)) || + (fileName==*itr)) + { + if (!needDirectoryName) return *itr; + else if (needFollowingBackslash) return dirName+'/'+*itr; + else return dirName+*itr; + } + } + return ""; +} + +#if defined(WIN32) && !defined(__CYGWIN) + + #include + #include + + osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) + { + osgDB::DirectoryContents contents; + + WIN32_FIND_DATA data; + HANDLE handle = FindFirstFile((dirName + "\\*").c_str(), &data); + if (handle != INVALID_HANDLE_VALUE) + { + do + { + contents.push_back(data.cFileName); + } + while (FindNextFile(handle, &data) != 0); + + FindClose(handle); + } + return contents; + } + +#else + + #include + osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) + { + osgDB::DirectoryContents contents; + + DIR *handle = opendir(dirName.c_str()); + if (handle) + { + dirent *rc; + while((rc = readdir(handle))!=NULL) + { + contents.push_back(rc->d_name); + } + closedir(handle); + } + + return contents; + } + +#endif // unix getDirectoryContexts + +#endif // ! target mac carbon diff --git a/src/osgDB/FileUtils_Mac.cpp b/src/osgDB/FileUtils_Mac.cpp index 8e79a474e..7a4807df5 100644 --- a/src/osgDB/FileUtils_Mac.cpp +++ b/src/osgDB/FileUtils_Mac.cpp @@ -498,49 +498,7 @@ char *osgDB::findDSO( const char *name ) } -std::string osgDB::findFileInDirectory(const std::string& fileName,const std::string& dirName,bool caseInsensitive) -{ - bool needFollowingBackslash = false; - bool needDirectoryName = true; - osgDB::DirectoryContents dc; - - if (dirName.empty()) - { - dc = osgDB::getDirectoryContents("."); - needFollowingBackslash = false; - needDirectoryName = false; - } - else if (dirName=="." || dirName=="./" || dirName==".\\") - { - dc = osgDB::getDirectoryContents("."); - needFollowingBackslash = false; - needDirectoryName = false; - } - else - { - dc = osgDB::getDirectoryContents(dirName); - char lastChar = dirName[dirName.size()-1]; - if (lastChar=='/') needFollowingBackslash = false; - else if (lastChar=='\\') needFollowingBackslash = false; - else needFollowingBackslash = true; - needDirectoryName = true; - } - - for(osgDB::DirectoryContents::iterator itr=dc.begin(); - itr!=dc.end(); - ++itr) - { - if ((caseInsensitive && osgDB::equalCaseInsensitive(fileName,*itr)) || - (fileName==*itr)) - { - if (!needDirectoryName) return *itr; - else if (needFollowingBackslash) return dirName+'/'+*itr; - else return dirName+*itr; - } - } - return ""; -} - +/***************************************************************************/ // TODO osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) @@ -549,3 +507,28 @@ osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) notify(WARN)<<"Warning : MAC implementation doesn't implement getDirectoryContents yet"< -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -using namespace osg; -using namespace osgDB; - - -#define FILEUTILS_MAX_PATH_LENGTH 2048 - -#if __sgi -char *PathDelimitor = ":"; -static const char *s_default_file_path = ".:"; -static const char *s_default_dso_path = "/usr/lib32/:/usr/local/lib32/"; -static char *s_filePath = ".:"; -#elif defined(__CYGWIN__) -char *PathDelimitor = ":"; -static const char *s_default_file_path = ".:"; -static const char *s_default_dso_path = "/usr/bin/:/usr/local/bin/:"; -static char *s_filePath = ".:"; -#else -char *PathDelimitor = ":"; -static const char *s_default_file_path = ".:"; -static const char *s_default_dso_path = "/usr/lib/:/usr/local/lib/:"; -static char *s_filePath = ".:"; -#endif - -static bool s_filePathInitialized = false; - -void osgDB::initFilePath( void ) -{ - char *ptr; - if( (ptr = getenv( "OSG_FILE_PATH" )) ) - { - notify(DEBUG_INFO) << "osgDB::Init("< -osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) -{ - osgDB::DirectoryContents contents; - - DIR *handle = opendir(dirName.c_str()); - if (handle) - { - dirent *rc; - while((rc = readdir(handle))!=NULL) - { - contents.push_back(rc->d_name); - } - closedir(handle); - } - - return contents; -} diff --git a/src/osgDB/FileUtils_Windows.cpp b/src/osgDB/FileUtils_Windows.cpp deleted file mode 100644 index 9c28037fa..000000000 --- a/src/osgDB/FileUtils_Windows.cpp +++ /dev/null @@ -1,262 +0,0 @@ -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -using namespace osg; -using namespace osgDB; - - -#define FILEUTILS_MAX_PATH_LENGTH 2048 - -char *PathDelimitor = ";"; -static const char *s_default_file_path = ".;"; -static const char *s_default_dso_path = "C:/Windows/System/;"; -static char *s_filePath = ".;"; - -#define F_OK 4 - -static bool s_filePathInitialized = false; - -void osgDB::initFilePath( void ) -{ - char *ptr; - if( (ptr = getenv( "OSG_FILE_PATH" )) ) - { - notify(DEBUG_INFO) << "osgDB::Init("< -#include - -osgDB::DirectoryContents osgDB::getDirectoryContents(const std::string& dirName) -{ - osgDB::DirectoryContents contents; - - WIN32_FIND_DATA data; - HANDLE handle = FindFirstFile((dirName + "\\*").c_str(), &data); - if (handle != INVALID_HANDLE_VALUE) - { - do - { - contents.push_back(data.cFileName); - } - while (FindNextFile(handle, &data) != 0); - - FindClose(handle); - } - return contents; -} diff --git a/src/osgDB/Registry.cpp b/src/osgDB/Registry.cpp index 495ad6bde..306df3b76 100644 --- a/src/osgDB/Registry.cpp +++ b/src/osgDB/Registry.cpp @@ -18,6 +18,15 @@ using namespace osg; using namespace osgDB; +void PrintFilePathList(ostream& stream,const FilePathList& filepath) +{ + for(FilePathList::const_iterator itr=filepath.begin(); + itr!=filepath.end(); + ++itr) + { + stream << " "<< *itr<& commandLine) { @@ -749,8 +867,10 @@ bool Registry::writeObject(const osg::Object& obj,Output& fw) // ReaderWriter::ReadResult Registry::readObject(const std::string& fileName) { - char *file = findFile( fileName.c_str() ); - if (file==NULL) return ReaderWriter::ReadResult("Warning: file \""+fileName+"\" not found."); + std::string file = findDataFile( fileName ); + if (file.empty()) return ReaderWriter::ReadResult("Warning: file \""+fileName+"\" not found."); + + PushAndPopDataPath tmpfile(getFilePath(fileName)); // record the existing reader writer. std::set rwOriginal; @@ -845,8 +965,10 @@ ReaderWriter::WriteResult Registry::writeObject(const Object& obj,const std::str ReaderWriter::ReadResult Registry::readImage(const std::string& fileName) { - char *file = findFile( fileName.c_str() ); - if (file==NULL) return ReaderWriter::ReadResult("Warning: file \""+fileName+"\" not found."); + std::string file = findDataFile( fileName ); + if (file.empty()) return ReaderWriter::ReadResult("Warning: file \""+fileName+"\" not found."); + + PushAndPopDataPath tmpfile(getFilePath(fileName)); // record the existing reader writer. std::set rwOriginal; @@ -941,8 +1063,10 @@ ReaderWriter::WriteResult Registry::writeImage(const Image& image,const std::str ReaderWriter::ReadResult Registry::readNode(const std::string& fileName) { - char *file = findFile( fileName.c_str() ); - if (file==NULL) return ReaderWriter::ReadResult("Warning: file \""+fileName+"\" not found."); + std::string file = findDataFile( fileName ); + if (file.empty()) return ReaderWriter::ReadResult("Warning: file \""+fileName+"\" not found."); + + PushAndPopDataPath tmpfile(getFilePath(fileName)); // record the existing reader writer. std::set rwOriginal; @@ -1041,3 +1165,26 @@ ReaderWriter::WriteResult Registry::writeNode(const Node& node,const std::string return results.front(); } + +void Registry::convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath) +{ +#ifdef WIN32 + char delimitor = ';'; +#else + char delimitor = ':'; +#endif + + if (!paths.empty()) + { + std::string::size_type start = 0; + std::string::size_type end; + while ((end = paths.find_first_of(delimitor,start))!=std::string::npos) + { + filepath.push_back(std::string(paths,start,end-start)); + start = end+1; + } + + filepath.push_back(std::string(paths,start)); + } + +} diff --git a/src/osgPlugins/flt/FltFile.cpp b/src/osgPlugins/flt/FltFile.cpp index b64a6a0de..5f7e9cc14 100644 --- a/src/osgPlugins/flt/FltFile.cpp +++ b/src/osgPlugins/flt/FltFile.cpp @@ -114,10 +114,10 @@ bool FltFile::readFile(const std::string& fileName) if (!fin.open(fileName)) { // havn't found file, look in OSGFILEPATH - char* newFileName = osgDB::findFile(fileName.c_str()); + std::string foundFileName = osgDB::findDataFile(fileName); - if (!newFileName) return false; - if (!fin.open(newFileName)) return false; + if (foundFileName.empty()) return false; + if (!fin.open(foundFileName)) return false; } osg::notify(osg::INFO) << "Loading " << fileName << " ... " << std::endl; diff --git a/src/osgPlugins/flt/Input.cpp b/src/osgPlugins/flt/Input.cpp index 86c4c6aa2..56ea9f975 100644 --- a/src/osgPlugins/flt/Input.cpp +++ b/src/osgPlugins/flt/Input.cpp @@ -56,10 +56,10 @@ bool FileInput::open(const std::string& fileName) if (_file == NULL) { // ok havn't found file, resort to using findFile... - char* newFileName = osgDB::findFile(fileName.c_str()); + std::string newFileName = osgDB::findDataFile(fileName); - if (!newFileName) return false; - _file=::fopen( newFileName, "rb"); + if (newFileName.empty()) return false; + _file=::fopen( newFileName.c_str(), "rb"); if (_file == NULL) return false; } _eof = false; diff --git a/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp b/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp index 30cb95331..6ad4dee17 100644 --- a/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp +++ b/src/osgPlugins/osgtgz/ReaderWriterOSGTGZ.cpp @@ -71,7 +71,8 @@ class sgReaderWriterOSGTGZ : public osgDB::ReaderWriter system( command ); osg::Group *grp = new osg::Group; - osgDB::setFilePath( dirname ); + + osgDB::PushAndPopDataPath tmppath(dirname ); osgDB::DirectoryContents contents = osgDB::getDirectoryContents(dirname); for(osgDB::DirectoryContents::iterator itr = contents.begin(); diff --git a/src/osgPlugins/tgz/ReaderWriterTGZ.cpp b/src/osgPlugins/tgz/ReaderWriterTGZ.cpp index 064eb263c..fbdf2d69a 100644 --- a/src/osgPlugins/tgz/ReaderWriterTGZ.cpp +++ b/src/osgPlugins/tgz/ReaderWriterTGZ.cpp @@ -70,7 +70,8 @@ class ReaderWriterTGZ : public osgDB::ReaderWriter system( command ); osg::Group *grp = new osg::Group; - osgDB::setFilePath( dirname ); + + osgDB::PushAndPopDataPath tmppath(dirname ); // deactivate the automatic generation of images to geode's. bool prevCreateNodeFromImage = osgDB::Registry::instance()->getCreateNodeFromImage(); diff --git a/src/osgPlugins/txp/ReaderWriterTXP.cpp b/src/osgPlugins/txp/ReaderWriterTXP.cpp index 6dea20f01..8fa77791b 100644 --- a/src/osgPlugins/txp/ReaderWriterTXP.cpp +++ b/src/osgPlugins/txp/ReaderWriterTXP.cpp @@ -32,7 +32,7 @@ public: { Group* ret = 0; // search the SGL data path - std::string foundname = osgDB::findFile(filename.c_str()); + std::string foundname = osgDB::findDataFile(filename); if( !foundname.empty()) { if (archive.OpenFile(foundname.c_str())) diff --git a/src/osgPlugins/txp/TrPageArchive.cpp b/src/osgPlugins/txp/TrPageArchive.cpp index e0b08bd89..c102d1b07 100644 --- a/src/osgPlugins/txp/TrPageArchive.cpp +++ b/src/osgPlugins/txp/TrPageArchive.cpp @@ -57,7 +57,9 @@ bool TrPageArchive::OpenFile(const char* file) SetDirectory("."); else { - osgDB::setFilePath(m_alternate_path.c_str()); + // push the path to the front of the list so that all subsequenct + // files get loaded relative to this if possible. + osgDB::getDataFilePathList().push_front(m_alternate_path); SetDirectory(m_alternate_path.c_str()); } diff --git a/src/osgPlugins/zip/ReaderWriterZIP.cpp b/src/osgPlugins/zip/ReaderWriterZIP.cpp index 4cebecd95..b7f3d0797 100644 --- a/src/osgPlugins/zip/ReaderWriterZIP.cpp +++ b/src/osgPlugins/zip/ReaderWriterZIP.cpp @@ -61,7 +61,8 @@ class ReaderWriterZIP : public osgDB::ReaderWriter #endif osg::Group *grp = new osg::Group; - osgDB::setFilePath( dirname ); + + osgDB::PushAndPopDataPath tmppath(dirname ); bool prevCreateNodeFromImage = osgDB::Registry::instance()->getCreateNodeFromImage(); osgDB::Registry::instance()->setCreateNodeFromImage(false); diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index abc6b5121..e46aa8612 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -29,38 +29,38 @@ using namespace osg; using namespace osgText; -// define the default paths to look for fonts. -// note delimator is : for unix, ; for windows. -#if defined(__linux) || defined(__FreeBSD__) || defined (__sgi) || defined (__DARWIN_OSX__) - static char* s_FontFilePath = ".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives"; -#elif defined(WIN32) - static char* s_FontFilePath = ".;C:/winnt/fonts;C:/windows/fonts"; -#else - static char* s_FontFilePath = ".:"; -#endif - std::string findFontFile(const std::string& str) { // try looking in OSGFILEPATH etc first for fonts. - char* filename = osgDB::findFile(str.c_str()); - if (filename) return std::string(filename); + std::string filename = osgDB::findDataFile(str); + if (!filename.empty()) return std::string(filename); -#if defined(WIN32) - // try windir environment - char *ptr; - if ((ptr = getenv( "windir" ))) - { - static std::string osPath(ptr); - s_FontFilePath = const_cast(osPath.c_str()); - } -#endif - // else fallback into the standard font file paths. - if (s_FontFilePath) + static osgDB::FilePathList s_FontFilePath; + static bool initialized = false; + if (!initialized) { - filename = osgDB::findFileInPath(str.c_str(),s_FontFilePath); - if (filename) return std::string(filename); + initialized = true; + #if defined(WIN32) + osgDB::Registry::convertStringPathIntoFilePathList( + ".;C:/winnt/fonts;C:/windows/fonts", + s_FontFilePath); + + char *ptr; + if ((ptr = getenv( "windir" ))) + { + s_FontFilePath.push_back(ptr); + } + #else + osgDB::Registry::convertStringPathIntoFilePathList( + ".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives", + s_FontFilePath); + #endif } + + filename = osgDB::findFileInPath(str,s_FontFilePath); + if (!filename.empty()) return filename; + osg::notify(osg::WARN)<<"Warning: font file \""<