From 5d5cf261386aafd4276e443b3a2ccdf3a43cb024 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 12 Sep 2012 16:02:02 +0000 Subject: [PATCH] Added osgDB::getSortedDirectoryContents and osgDB::FileNameComparator to help with sorting directory contents into alphabetic and numerical order. --- include/osgDB/FileNameUtils | 50 +++++++++++++++++++++++++++++++++++++ include/osgDB/FileUtils | 5 ++++ src/osgDB/FileUtils.cpp | 7 ++++++ 3 files changed, 62 insertions(+) diff --git a/include/osgDB/FileNameUtils b/include/osgDB/FileNameUtils index 0094cb6aa..ed1e552b4 100644 --- a/include/osgDB/FileNameUtils +++ b/include/osgDB/FileNameUtils @@ -81,6 +81,56 @@ extern OSGDB_EXPORT std::string getRealPath(const std::string& path); /** Splits a path into elements between separators (including Windows' root, if any). */ extern OSGDB_EXPORT void getPathElements(const std::string& path, std::vector & out_elements); +/** Functor for helping sort filename in alphabetical and numerical order when using in conjunction with std::sort.*/ +struct FileNameComparator +{ + inline bool operator() (const std::string& lhs, const std::string& rhs) const + { + std::string::size_type size_lhs = lhs.size(); + std::string::size_type size_rhs = rhs.size(); + std::string::size_type pos_lhs = 0; + std::string::size_type pos_rhs = 0; + while(pos_lhs='0' && lhs[pos_lhs]<='9'; + bool numeric_rhs = rhs[pos_rhs]>='0' && rhs[pos_rhs]<='9'; + if (numeric_lhs && numeric_rhs) + { + std::string::size_type start_lhs = pos_lhs; + ++pos_lhs; + while(pos_lhs='0' && lhs[pos_lhs]<='9')) ++pos_lhs; + + std::string::size_type start_rhs = pos_rhs; + ++pos_rhs; + while(pos_rhs='0' && rhs[pos_rhs]<='9')) ++pos_rhs; + + if (pos_lhsrhs[start_rhs]) return false; + ++start_lhs; + ++start_rhs; + } + } + else + { + if (c_lhs DirectoryContents; * Returns an empty array on any error.*/ extern OSGDB_EXPORT DirectoryContents getDirectoryContents(const std::string& dirName); +/** Return the contents of a directory, sorting the names into alphabetic and numberical order. + * Return value will contain filenames only, not absolute paths. + * Returns an empty array on any error.*/ +extern OSGDB_EXPORT DirectoryContents getSortedDirectoryContents(const std::string& dirName); + /** Return the list of filenames that match the given filename with wildcards. * Will only expand '*', and will not expand wildcards in directory, only in * filename part of the given filename. diff --git a/src/osgDB/FileUtils.cpp b/src/osgDB/FileUtils.cpp index bb919f381..a018675d6 100644 --- a/src/osgDB/FileUtils.cpp +++ b/src/osgDB/FileUtils.cpp @@ -549,6 +549,13 @@ static void appendInstallationLibraryFilePaths(osgDB::FilePathList& filepath) #endif // unix getDirectoryContexts +osgDB::DirectoryContents osgDB::getSortedDirectoryContents(const std::string& dirName) +{ + osgDB::DirectoryContents filenames = osgDB::getDirectoryContents(dirName); + std::sort(filenames.begin(), filenames.end(), osgDB::FileNameComparator()); + return filenames; +} + osgDB::DirectoryContents osgDB::expandWildcardsInFilename(const std::string& filename) {