Last active
August 29, 2015 14:12
-
-
Save luoyetx/91a44acace513067ffb4 to your computer and use it in GitHub Desktop.
跨平台文件系统访问
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef WIN32 | |
// on Windows | |
#include <io.h> | |
#include <direct.h> | |
#else | |
// on Linux | |
#include <unistd.h> | |
#include <dirent.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#endif // WIN32 | |
#include <string.h> | |
#include "filesystem.h" | |
using namespace std; | |
namespace FileSystem { | |
int makeDir(const string& dp) { | |
if (isExists(dp)) { | |
//return Status::OK(); | |
return 0; | |
} | |
int ret_code; | |
#ifdef WIN32 | |
ret_code = mkdir(dp.c_str()); | |
#else | |
// mode: -rwxrwxr-x | |
ret_code = mkdir(dp.c_str(), 0755); | |
#endif // WIN32 | |
if (ret_code == 0) { | |
//return Status(); | |
return 0; | |
} | |
else { | |
//return Status::IOError("Can not create " + dp); | |
return -1; | |
} | |
} | |
bool isExists(const string& path) { | |
if (access(path.c_str(), 0) != -1) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
int readDir(const string& dirPath, const string& extension, vector<string>& fileNames) { | |
if (!isExists(dirPath)) { | |
//return Status::IOError("文件夹不存在"); | |
return -1; | |
} | |
#ifdef WIN32 | |
long fd; | |
_finddata_t fileInfo; | |
string matchPath = dirPath + "/*." + extension; | |
if ((fd = _findfirst(matchPath.c_str(), &fileInfo)) == -1) { | |
//return Status::IOError("无法访问文件夹"); | |
return -1; | |
} | |
do { | |
fileNames.push_back(fileInfo.name); | |
} while (_findnext(fd, &fileInfo) == 0); | |
_findclose(fd); | |
#else | |
DIR *d; | |
struct dirent *file; | |
struct stat sb; | |
// open file | |
const char* dp = dirPath.c_str(); | |
if (!(d = opendir(dp))) { | |
return Status::IOError("Can not open dir " + dirPath); | |
} | |
while ((file = readdir(d)) != NULL) { | |
// remove '.', '..', and all hidden files | |
if (strncmp(file->d_name, ".", 1) == 0) { | |
continue; | |
} | |
// remain specific extensions | |
string _(file->d_name); | |
if (getFileExt(_) == extension) { | |
fileNames.push_back(_); | |
} | |
else { | |
continue; | |
} | |
} | |
closedir(d); | |
#endif // WIN32 | |
//return Status::OK(); | |
return 0; | |
} | |
string getFileExt(const string& fp) { | |
int pos = fp.find_last_of("/"); | |
if (pos == string::npos) { | |
return fp.substr(fp.find_last_of(".") + 1); | |
} | |
else { | |
string _; | |
_ = _.substr(pos + 1); | |
return _.substr(_.find_last_of(".") + 1); | |
} | |
} | |
string getFileName(const string& fp) { | |
int pos = fp.find_last_of("/"); | |
if (pos == string::npos) { | |
return fp.substr(0, fp.find_last_of(".")); | |
} | |
else { | |
string _; | |
_ = fp.substr(pos + 1); | |
return _.substr(0, _.find_last_of(".")); | |
} | |
} | |
} // namespace FileSystem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FILESYSTEM_H | |
#define FILESYSTEM_H | |
#include <vector> | |
// 文件系统相关操作集合 | |
namespace FileSystem { | |
// 判断文件或文件夹是否存在 | |
bool isExists(const std::string& path); | |
// 读取目录下指定格式的文件 | |
int readDir(const std::string& dirPath, const std::string& extension, std::vector<std::string> &fileNames); | |
// 创建文件夹(如果文件夹存在,则什么也不做) | |
int makeDir(const std::string& dp); | |
// 获取文件名 | |
std::string getFileName(const std::string& fp); | |
// 获取文件拓展名 | |
std::string getFileExt(const std::string& fp); | |
} // namespace FileSystem | |
#endif // FILESYSTEM_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment