You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

138 lines
3.5 KiB

#include "fileutils.hpp"
using namespace iflytop;
using namespace core;
bool FileUtils::exist(const string &path) {
struct stat statInfo;
if (stat(path.c_str(), &statInfo) == 0) {
return true;
}
return false;
}
bool FileUtils::isDirectory(const string &path) {
struct stat statInfo;
if (stat(path.c_str(), &statInfo) == 0) {
if (S_ISDIR(statInfo.st_mode)) {
return true;
} else {
return false;
}
}
return false;
}
// mkfile
bool FileUtils::makeDirIfNoExist(const string &path) {
string::size_type sepPos = path.find_last_of("/");
if (sepPos == string::npos) {
return false;
}
string dirPath = path.substr(0, sepPos);
if (exist(dirPath)) {
return true;
}
int ret = system(("mkdir -p " + dirPath).c_str());
// mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
return 0 == ret ? true : false;
}
// readfile
string FileUtils::readFileAsString(const string &filePath) {
if (!exist(filePath)) {
return "";
}
fstream file(filePath.c_str(), ios::in | ios::binary);
stringstream sstream;
sstream << file.rdbuf();
file.close();
string str(sstream.str());
sstream.clear();
return str;
}
std::vector<char> FileUtils::readFileAsBuffer(const string &filePath) {
std::vector<char> ret;
if (!exist(filePath)) {
return std::move(ret);
}
fstream fs;
fs.open(filePath.c_str(), ios::in | ios::binary);
if (!fs.is_open()) {
return std::move(ret);
}
fs.seekg(0, ios::end);
int fileSize = fs.tellg();
fs.seekg(0, ios::beg);
ret.resize(fileSize);
char *dataAddr = (char *)ret.data();
fs.read(dataAddr, fileSize);
fs.close();
return std::move(ret);
}
// write file
bool FileUtils::writeToFile(const string &fileName, const string &buf) {
return writeToFile(fileName, buf.c_str(), buf.size());
}
shared_ptr<ofstream> FileUtils::openTrunc(const string &fileName) {
makeDirIfNoExist(fileName);
shared_ptr<ofstream> file(new ofstream(fileName, std::ios::trunc | std::ios::binary));
return file;
}
bool FileUtils::writeToFile(const string &fileName, const char *buf, size_t size) {
std::ofstream outfile1(fileName, std::ios::trunc | std::ios::binary);
if (outfile1.is_open()) {
outfile1.write((char *)buf, size);
if (outfile1.good()) return true;
return false;
} else {
return false;
}
}
#if 0
bool FileUtils::getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1) {}
bool FileUtils::getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1) {}
// filename operat
string FileUtils::getRepetitionFileName(string fileName, string suffix, int maxNum = -1) {
string curName;
if (maxNum >= 0) {
for (int i = 0; i < maxNum; i++) {
curName = fmt::format("{}{}.{}", fileName, i, suffix);
if (!FileUtils::exist(curName)) return curName;
}
} else {
for (int i = 0; i <= 65536; i++) {
curName = fmt::format("{}{}.{}", fileName, i, suffix);
if (!FileUtils::exist(curName)) return curName;
}
}
return fmt::format("{}{}.{}", fileName, "reachMax", suffix);
}
string FileUtils::getRepetitionDirName(string fileName, int maxNum = -1) {
string curName;
if (maxNum >= 0) {
for (int i = 0; i < maxNum; i++) {
curName = fmt::format("{}{}", fileName, i);
if (!FileUtils::exist(curName)) return curName + "/";
}
} else {
for (int i = 0; i <= 65536; i++) {
curName = fmt::format("{}{}", fileName, i);
if (!FileUtils::exist(curName)) return curName + "/";
}
}
return "";
}
#endif