utilFile.h #ifndef RGM_FILEUTILITY_HPP_ #define RGM_FILEUTILITY_HPP_ #include <boost/filesystem.hpp> namespace RGM { namespace fs = boost::filesystem; /// Utility functions for file management class FileUtil { public: /// File separator #if (defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)) static const char FILESEP = '\\'; #else static const char FILESEP = '/'; #endif /// check if a file or dir exists static bool exists(const std::string & input); /// Create dir from input ("D:\a\b\c.ext" or "D:\a\b\", i.e. "D:\a\b") static void CreateDir( const std::string& input ); /// Get (dir, filebasename, extname) from a full file name static std::vector<std::string> FileParts(const std::string& fileName); static std::string GetParentDir(const std::string& filePath); static std::string GetFileExtension(const std::string& filePath); static std::string GetFileBaseName(const std::string& filePath); /// Check if a file already existed static bool CheckFileExists(const std::string &fileName); /// Check if a dir existed, if not, create it static bool VerifyDirectoryExists(const std::string& directoryName, bool create=true); /// Delete all files under a dir static void ClearDirectory(const std::string& directoryName); /// Get all files under a dir static void GetFileList(std::vector<std::string>& files, const std::string& path, const std::string& ext, bool useRootDir=false); /// Get current work directory static std::string GetCurrentWorkDirectory(); /// Add the file separator to filePath if necessary static void VerifyTheLastFileSep(std::string &filePath); }; // class FileUtil } // namespace RGM #endif // RGM_FILEUTILITY_HPP_ utilFile.cpp #include <boost/foreach.hpp> #include "UtilFile.hpp" namespace RGM { bool FileUtil::exists(const std::string & input) { return fs::exists(fs::path(input)); } void FileUtil::CreateDir( const std::string& input ) { fs::path p(input.c_str()); if (!fs::is_directory(p)) { fs::path pp = p.parent_path(); fs::create_directories(pp); } } std::vector<std::string> FileUtil::FileParts(const std::string& fileName)//分离文件名 { fs::path p( fileName.c_str() ); std::vector<std::string> vstr(3); vstr[0] = p.parent_path().string(); vstr[1] = p.stem().string(); vstr[2] = p.extension().string(); return vstr; } std::string FileUtil::GetParentDir(const std::string& filePath) { return fs::path(filePath.c_str()).parent_path().string(); } std::string FileUtil::GetFileExtension(const std::string& filePath) { return fs::path(filePath.c_str()).extension().string(); } std::string FileUtil::GetFileBaseName(const std::string& filePath) { return fs::path(filePath.c_str()).stem().string(); } bool FileUtil::CheckFileExists(const std::string& fileName) { return fs::exists(fs::path(fileName.c_str())); } bool FileUtil::VerifyDirectoryExists(const std::string& directoryName, bool create) { fs::path p(directoryName.c_str()); bool exist = fs::exists(p); if (!exist && create) { fs::create_directories(p); } return exist; } void FileUtil::ClearDirectory(const std::string& directoryName) { fs::path p(directoryName.c_str()); fs::remove_all( p ); fs::create_directory( p ); } //获取指定目录下所有文件 void FileUtil::GetFileList(std::vector<std::string>& files, const std::string& path, const std::string& ext, bool useRootDir) { fs::path targetDir(path.c_str()); fs::directory_iterator it(targetDir), eod; bool getAllFiles = (ext.compare("*")==0); BOOST_FOREACH(fs::path const &p, std::make_pair(it, eod)) { if (fs::is_regular_file(p)) { if ( getAllFiles ) { useRootDir ? files.push_back( p.string() ) : files.push_back(p.filename().string()); } else { if (ext.compare(p.extension().string())==0) { useRootDir ? files.push_back( p.string() ) : files.push_back(p.filename().string()); } } } } } //获取工作目录 std::string FileUtil::GetCurrentWorkDirectory() { fs::path p = fs::initial_path(); return p.string(); } //在文件的结尾加上\\ void FileUtil::VerifyTheLastFileSep(std::string &filePath) { if ( filePath.find_last_of("/\\") != (filePath.length() - 1) ) { filePath = filePath + FILESEP; } } } // namespace RGM |
|
来自: 昵称12110172 > 《boost库学习》