//
// Created by ouyang on 19-7-9.
//
#ifndef FACE_CLUSTER_OS_PATH_HPP
#define FACE_CLUSTER_OS_PATH_HPP
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
//#include<io.h>
#include <unistd.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
#include <fstream>
using namespace std;
namespace OU_OS {
#define MK_DIR_MODE S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
#define MKDIR(DirPath) mkdir(DirPath, MK_DIR_MODE)
#define LEFTSTRIP 0
#define RIGHTSTRIP 1
#define BOTHSTRIP 2
template<class Type>
Type stringToNum(const string &str) {
istringstream iss(str);
Type num;
iss >> num;
return num;
}
void splitString(const string &src, vector<string> &vec, const string &c) {
string::size_type pos1, pos2;
pos2 = src.find(c);
pos1 = 0;
while (string::npos != pos2) {
vec.push_back(src.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = src.find(c, pos1);
}
if (pos1 != src.length()) {
vec.push_back(src.substr(pos1));
}
}
string do_strip(const std::string &str, int striptype, const std::string & chars)
{
std::string::size_type strlen = str.size();
std::string::size_type charslen = chars.size();
std::string::size_type i, j;
//默认情况下,去除空白符
if (0 == charslen)
{
i = 0;
//去掉左边空白字符
if (striptype != RIGHTSTRIP)
{
while (i < strlen&&::isspace(str[i]))
{
i++;
}
}
j = strlen;
//去掉右边空白字符
if (striptype != LEFTSTRIP)
{
j--;
while (j >= i&&::isspace(str[j]))
{
j--;
}
j++;
}
}
else
{
//把删除序列转为c字符串
const char*sep = chars.c_str();
i = 0;
if (striptype != RIGHTSTRIP)
{
//memchr函数:从sep指向的内存区域的前charslen个字节查找str[i]
while (i < strlen && memchr(sep, str[i], charslen))
{
i++;
}
}
j = strlen;
if (striptype != LEFTSTRIP)
{
j--;
while (j >= i && memchr(sep, str[j], charslen))
{
j--;
}
j++;
}
//如果无需要删除的字符
if (0 == i&& j == strlen)
{
return str;
}
else
{
return str.substr(i, j - i);
}
}
}
string strip( const std::string & str, const std::string & chars=" " )
{
return do_strip( str, BOTHSTRIP, chars );
}
std::string lstrip( const std::string & str, const std::string & chars=" " )
{
return do_strip( str, LEFTSTRIP, chars );
}
std::string rstrip( const std::string & str, const std::string & chars=" " )
{
return do_strip( str, RIGHTSTRIP, chars );
}
void GetFiles(string path, vector<string> &filesPath) {
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(path.c_str())) == NULL) {
perror("Open dir error...");
exit(1);
}
while ((ptr = readdir(dir)) != NULL) {
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
continue;
else if (ptr->d_type == 8) ///file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
filesPath.push_back(path + "/" + ptr->d_name);
else if (ptr->d_type == 10) ///link file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
continue;
else if (ptr->d_type == 4) ///dir
{
string tempPath = path + "/" + string(ptr->d_name);
GetFiles(tempPath, filesPath);
//files.push_back(ptr->d_name);
/*
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_nSame);
readFileList(base);
*/
}
}
closedir(dir);
}
void GetSubDirs(string path, vector<string> &subDirs) {
DIR *dir;
struct dirent *ptr;
subDirs.clear();
if ((dir = opendir(path.c_str())) == NULL) {
perror("Open dir error...");
exit(1);
}
while ((ptr = readdir(dir)) != NULL) {
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
continue;
else if (ptr->d_type == 8) ///file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
//subDirs.push_back(path + "/" + ptr->d_name);
continue;
else if (ptr->d_type == 10) ///link file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
continue;
else if (ptr->d_type == 4) ///dir
{
subDirs.push_back(path + "/" + ptr->d_name);
}
}
closedir(dir);
}
bool isFile(string path)
{
ifstream _file;
_file.open(path);
if (_file)
{
_file.close();
return true;
}
else
{
_file.close();
return false;
}
}
bool isDir(string path)
{
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(path.c_str())) == NULL) {
return false;
}
ptr = readdir(dir);
if(ptr->d_type == 4)
{
closedir(dir);
return true;
}
closedir(dir);
return false;
}
bool exist(string path)
{
ifstream _file;
_file.open(path);
if (_file)
{
_file.close();
return true;
}
else
{
_file.close();
return false;
}
}
string baseName(string path)
{
vector<string> vec;
splitString(path, vec, "/");
return vec.back();
}
string baseNameWithoutSuffix(string path)
{
string fileName = baseName(path);
vector<string> vec;
splitString(fileName, vec, ".");
int lenVec = vec.size();
int baseIndex = lenVec >= 2 ? lenVec - 2 : 0;
return vec.at(baseIndex);
}
string dirName(string path)
{
string dirName = "";
vector<string> vec;
splitString(path, vec, "/");
vector<string> ::iterator itt = vec.begin();
for (;itt != vec.end() - 1 ; ++itt) {
if (itt != vec.end() - 2)
{
dirName += *itt + "/";
}
else
{
dirName += *itt;
}
}
return dirName;
}
string absPath(string path)
{
char abs_path_buff[512];
realpath(path.c_str(), abs_path_buff);
return string(abs_path_buff);
}
void sys_cmd(string strCmd)
{
system(strCmd.c_str());
}
}
#endif //FACE_CLUSTER_OS_PATH_HPP
int main(){
//直接按照命名空间方法使用
OU_OS::splitString(lineContents, vecCons, "\t");
return 0;
}
网友评论