From 3d3199017e81ae92adeed7d887a72a4006228207 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 8 May 2004 06:35:32 +0000 Subject: [PATCH] From Ken Sewel, added osgDB::fileType(filename) method and usage of this new function in osgdem to automatcally handle directories as part of the regular -d and -t options. --- examples/osgdem/osgdem.cpp | 97 ++++++++++++++++++++++---------------- include/osgDB/FileUtils | 9 ++++ src/osgDB/FileUtils.cpp | 36 ++++++++++++++ 3 files changed, 102 insertions(+), 40 deletions(-) diff --git a/examples/osgdem/osgdem.cpp b/examples/osgdem/osgdem.cpp index 40380a288..7ec4190fc 100644 --- a/examples/osgdem/osgdem.cpp +++ b/examples/osgdem/osgdem.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -108,6 +109,56 @@ void ellipsodeTransformTest(double latitude, double longitude, double height) std::cout<<"lat = "< dataset) { + + if(filename.empty()) return; + + if(osgDB::fileType(filename) == osgDB::REGULAR_FILE) { + + osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(type, filename); + if (source) + { + if (!currentCS.empty()) + { + std::cout<<"source->setCoordySystem "<setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); + source->setCoordinateSystem(currentCS); + } + + if (geoTransformSet) + { + std::cout<<"source->setGeoTransform "<setGeoTransformPolicy(geoTransformScale ? + osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION : + osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); + source->setGeoTransform(geoTransform); + } + + dataset->addSource(source); + } + } else if (osgDB::fileType(filename) == osgDB::DIRECTORY) { + + osgDB::DirectoryContents dirContents= osgDB::getDirectoryContents(filename); + + // loop through directory contents and call processFile + std::vector::iterator i; + std::string fullfilename; + for(i = dirContents.begin(); i != dirContents.end(); ++i) { + if((*i != ".") && (*i != "..")) { + fullfilename = filename + '/' + *i; + processFile(fullfilename, type, currentCS, geoTransform, geoTransformSet, geoTransformScale, dataset); + } + } + } +} + + int main( int argc, char **argv ) { @@ -197,7 +248,6 @@ int main( int argc, char **argv ) while(possetCoordySystem "<setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); - source->setCoordinateSystem(currentCS); - } - - if (geoTransformSet) - { - std::cout<<"source->setGeoTransform "<setGeoTransformPolicy(geoTransformScale ? - osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION : - osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); - source->setGeoTransform(geoTransform); - } - - dataset->addSource(source); - } - - } // any option left unread are converted into errors to write out later. diff --git a/include/osgDB/FileUtils b/include/osgDB/FileUtils index 76ffa38ce..ef05cd73f 100644 --- a/include/osgDB/FileUtils +++ b/include/osgDB/FileUtils @@ -29,10 +29,19 @@ enum CaseSensitivity CASE_INSENSITIVE, }; +enum FileType +{ + FILE_NOT_FOUND, + REGULAR_FILE, + DIRECTORY, +}; + /** return true if a file exisits. */ extern OSGDB_EXPORT bool fileExists(const std::string& filename); +/** return type of file. */ +extern OSGDB_EXPORT FileType fileType(const std::string& filename); /** find specified file in specified file path.*/ extern OSGDB_EXPORT std::string findFileInPath(const std::string& filename, const FilePathList& filePath,CaseSensitivity caseSensitivity=CASE_SENSITIVE); diff --git a/src/osgDB/FileUtils.cpp b/src/osgDB/FileUtils.cpp index 114cddab1..edc090a1a 100644 --- a/src/osgDB/FileUtils.cpp +++ b/src/osgDB/FileUtils.cpp @@ -26,6 +26,8 @@ #define F_OK 4 #else // unix #include + #include + #include #endif #include @@ -40,6 +42,40 @@ bool osgDB::fileExists(const std::string& filename) return access( filename.c_str(), F_OK ) == 0; } +osgDB::FileType osgDB::fileType(const std::string& filename) +{ +#if defined(WIN32) && !defined(__CYGWIN__) + if (!fileExists(const std::string& filename)) + { + return FILE_NOT_FOUND; + } + else + { + return REGULAR_FILE; + } + +#else + struct stat fileStat; + + if(stat(filename.c_str(), &fileStat) == -1) + { + return FILE_NOT_FOUND; + } + + if(S_ISREG(fileStat.st_mode)) + { + return REGULAR_FILE; + } + + if(S_ISDIR(fileStat.st_mode)) + { + return DIRECTORY; + } + + return FILE_NOT_FOUND; +#endif +} + std::string osgDB::findFileInPath(const std::string& filename, const FilePathList& filepath,CaseSensitivity caseSensitivity) { if (filename.empty())