#ifndef _UTILS_HPP_
#define _UTILS_HPP_
#include <unistd.h> // access F_OK
#include <stdlib.h> // system
#include <sstream> // ostringstream
#include <assert.h>
#include <vector>
#include <dirent.h>
#include <cstring>
namespace utils {
// 路径是否以正斜杠结束
std::string pathWithSlash(const std::string path, bool withSlash=true) {
if(withSlash == true && path[path.length() - 1] != '/')
return path + '/';
if(withSlash == false && path[path.length() - 1] == '/')
return path.substr(0, path.length() - 1);
return path;
}
// 获取路径中包含的文件名,默认不带后缀
std::string getFileName(std::string path, bool withExtension=false) {
size_t a = path.find_last_of('/');
size_t b = path.find_last_of('.');
assert(b < path.length());
if(withExtension)
return path.substr(a+1);
return path.substr(a+1, b-a-1);
}
// 获取文件或目录的绝对路径
std::string getAbsolutePath(const char* pathOrFile) {
char abs_path_buff[PATH_MAX];
if(realpath(pathOrFile, abs_path_buff))
return abs_path_buff;
return NULL;
}
// 递归创建文件夹
void mkdirp(const std::string Path) {
if(access(Path.c_str(), F_OK) == -1) {
std::string dir = "mkdir -p " + Path;
system(dir.c_str());
}
}
// 转为字符串类型
template<typename T>
std::string toString(const T& t){
std::ostringstream oss;
oss << t;
return oss.str();
}
// 类型转换
// 用法:utils::typeCovert<string, int>("111")
// 用法:utils::typeCovert<int, string>(123)
template<class in, class out>
out typeCovert(const in &value) {
std::stringstream ss;
ss << value;
out res;
ss >> res;
return res;
}
// 搜索文件夹
std::vector<std::string> listdir(const char *path, const std::string extension="") {
std::vector<std::string> res;
int lex = extension.length();
DIR *dir;
struct dirent *dirp;
std::stringstream ss;
assert((dir=opendir(path)) != NULL);
while((dirp=readdir(dir)) != NULL) {
if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0) // current dir OR parrent dir
continue;
else if(dirp->d_type == 8 || dirp->d_type == 10) { // file OR link file
ss.str("");
ss << path << "/" << dirp->d_name;
if(extension == "")
res.push_back(ss.str());
else if(ss.str().substr(ss.str().size()-lex, lex) == extension)
res.push_back(ss.str());
}
else if(extension == "") { // folder
ss.str("");
ss << path << "/" << dirp->d_name;
res.push_back(ss.str());
}
}
closedir(dir);
return res;
}
void _recurseListdir(const char *path, const std::string extension, std::vector<std::string>& res) {
static int lex = extension.length();
DIR *dir;
struct dirent *dirp;
std::stringstream ss;
assert((dir=opendir(path)) != NULL);
while ((dirp=readdir(dir)) != NULL) {
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..") == 0)
continue;
else if(dirp->d_type == 8 || dirp->d_type == 10) {
ss.str("");
ss << path << "/" << dirp->d_name;
if(extension == "")
res.push_back(ss.str());
else if(ss.str().substr(ss.str().size()-lex, lex) == extension)
res.push_back(ss.str());
}
}
dir=opendir(path);
while ((dirp=readdir(dir)) != NULL) {
if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..")==0) // current dir OR parrent dir
continue;
else if(dirp->d_type == 4){ // dir
ss.str("");
ss<<path<<"/"<<dirp->d_name;
if(extension == "")
res.push_back(ss.str());
_recurseListdir(ss.str().c_str(), extension, res);
}
}
closedir(dir);
}
// 递归搜索文件夹
std::vector<std::string> walkdir(const char *path, const std::string extension="") {
static std::vector<std::string> res;
_recurseListdir(path, extension, res);
return res;
}
} // namespace utils
#endif
C++:常用函数
最新推荐文章于 2021-01-27 15:06:25 发布