From: <sv...@ww...> - 2004-10-03 02:21:42
|
Author: mkrose Date: 2004-10-02 19:20:18 -0700 (Sat, 02 Oct 2004) New Revision: 1271 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/FileUtility.h trunk/CSP/SimData/Source/FileUtility.cpp Log: Add directory reading and file extension splitting utility functions. This was motivated by osgDB::FileUtil, which provides this functionality only at a high cost of including many unnecessary header files. ==>WINDOWS USERS: warning -- directory reading has only been tested under Linux. If it builds it should work though. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1271 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-10-02 17:06:06 UTC (rev 1270) +++ trunk/CSP/SimData/CHANGES.current 2004-10-03 02:20:18 UTC (rev 1271) @@ -1,6 +1,19 @@ Version 0.4.0 (in progress) =========================== +2004-10-02: onsight + * Implement AtomicCounter using compiler intrinsics under msvc. + +==>WINDOWS USERS this is untested and may need debugging. According + to MS you'll need to pass the /Oi option to msvc. + + * Add directory reading and file extension splitting utility functions. + This was motivated by osgDB::FileUtil, which provides this functionality + only at a high cost of including many unnecessary header files. + +==>WINDOWS USERS: warning -- directory reading has only been tested under + Linux. If it builds it should work though. + 2004-10-01: onsight * Fix import SimData statements in Compile.py and Parse.py to find the local SimData package. Modified: trunk/CSP/SimData/Include/SimData/FileUtility.h =================================================================== --- trunk/CSP/SimData/Include/SimData/FileUtility.h 2004-10-02 17:06:06 UTC (rev 1270) +++ trunk/CSP/SimData/Include/SimData/FileUtility.h 2004-10-03 02:20:18 UTC (rev 1271) @@ -30,6 +30,7 @@ #include <string> +#include <vector> #include <SimData/Export.h> #include <SimData/Namespace.h> @@ -138,6 +139,27 @@ */ extern SIMDATA_EXPORT std::string const &addpath(std::string &pathlist, const std::string &path); + /** Removes the last extension from a filepath. Returns the extension + * (excluding the '.'), or an empty string if no extensions were found. + */ + extern SIMDATA_EXPORT std::string ospath::stripFileExtension(std::string &path); + + /** Get the last extension of a filename (excluding the '.'), or an empty + * string if no extensions were found. + */ + extern SIMDATA_EXPORT std::string getFileExtension(const std::string &path); + + /** Test if a file exists. + */ + extern SIMDATA_EXPORT bool exists(const std::string &path); + + typedef std::vector<std::string> DirectoryContents; + + /** Retrieve a list of entries (files and subdirectories) from the given + * path (non-recursive). + */ + extern SIMDATA_EXPORT DirectoryContents getDirectoryContents(std::string const &path); + } // namespace ospath //@} Modified: trunk/CSP/SimData/Source/FileUtility.cpp =================================================================== --- trunk/CSP/SimData/Source/FileUtility.cpp 2004-10-02 17:06:06 UTC (rev 1270) +++ trunk/CSP/SimData/Source/FileUtility.cpp 2004-10-03 02:20:18 UTC (rev 1271) @@ -30,7 +30,6 @@ #include <SimData/FileUtility.h> #include <cstdlib> -#include <cstdio> #ifdef _WIN32 @@ -43,6 +42,8 @@ # endif /* _MSC_VER */ #else /* _WIN32 */ # include <unistd.h> +# include <sys/types.h> +# include <dirent.h> #endif @@ -173,5 +174,56 @@ return denormalize(normalize(path)); } +std::string ospath::stripFileExtension(std::string &path) { + std::string ext = getFileExtension(path); + if (!ext.empty()) path = path.substr(0, path.size() - ext.size() - 1); + return ext; +} + +std::string ospath::getFileExtension(const std::string &path) { + std::string::size_type sep_idx0 = path.rfind('/'); + std::string::size_type sep_idx1 = path.rfind('\\'); + if (sep_idx0 == std::string::npos) sep_idx0 = 0; + if (sep_idx1 == std::string::npos) sep_idx1 = 0; + std::string::size_type sep_idx = std::max(sep_idx0, sep_idx1); + std::string::size_type idx = path.rfind('.'); + if (idx == std::string::npos || idx < sep_idx) return ""; + return path.substr(idx+1); +} + +bool ospath::exists(const std::string &path) { +#ifdef _WIN32 + static const int mode = 0; // existence only +#else // POSIX (hopefully) + static const int mode = F_OK; +#endif + return access(path.c_str(), mode); +} + +ospath::DirectoryContents ospath::getDirectoryContents(std::string const &path) { + DirectoryContents entries; +#ifdef _WIN32 + WIN32_FIND_DATA ffd; + std::string search_path = path + "\\*"; + HANDLE handle = FindFirstFile(search_path.c_str(), &ffd); + if (handle != INVALID_HANDLE_VALUE) { + do { + contents.push_back(ffd.cFileName); + } while (FindNextFile(handle, &ffd) != 0); + FileClose(handle); + } +#else // POSIX (hopefully) + DIR *dir = opendir(path.c_str()); + if (dir != NULL) { + struct dirent *entry; + while ((entry=readdir(dir)) != NULL) { + entries.push_back(entry->d_name); + } + closedir(dir); + } +#endif + return entries; +} + NAMESPACE_SIMDATA_END |