91 lines
2.7 KiB
C++
91 lines
2.7 KiB
C++
#include <filesystem>
|
|
#include <fstream>
|
|
#include <vector>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <VUtils/FileHandler.h>
|
|
#include <VUtils/Logging.h>
|
|
#include <pwd.h>
|
|
|
|
namespace VUtils {
|
|
|
|
bool FileHandler::fileExists(const std::string &fileName) {
|
|
return std::filesystem::exists(fileName);
|
|
}
|
|
|
|
std::string FileHandler::readFile(const std::string &fileName) {
|
|
std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
|
|
std::ifstream::pos_type fileSize = ifs.tellg();
|
|
ifs.seekg(0, std::ios::beg);
|
|
|
|
std::vector<char> bytes(fileSize);
|
|
ifs.read(bytes.data(), fileSize);
|
|
|
|
return std::string(bytes.data(), fileSize);
|
|
}
|
|
|
|
bool FileHandler::writeFile(const std::string &fileName, const std::string &content) {
|
|
try {
|
|
std::ofstream ofs(fileName);
|
|
ofs << content;
|
|
ofs.close();
|
|
} catch (std::exception &e) {
|
|
ERR("Save Failed: %s", e.what());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool FileHandler::isDirectory(const std::string &fileName) {
|
|
return std::filesystem::is_directory(fileName);
|
|
}
|
|
|
|
std::string FileHandler::getExtension(const std::string &fileName) {
|
|
auto ext = std::filesystem::path(fileName).extension().string();
|
|
if (ext.empty()) {
|
|
return ext;
|
|
}
|
|
// remove first dot!
|
|
return ext.erase(0, 1);
|
|
}
|
|
|
|
int FileHandler::getFileID(const std::string &fileName) {
|
|
return open(fileName.c_str(), O_RDONLY);
|
|
}
|
|
|
|
long FileHandler::getFileSize(int fd) {
|
|
struct stat stat_buf;
|
|
int rc = fstat(fd, &stat_buf);
|
|
return rc == 0 ? stat_buf.st_size : -1;
|
|
}
|
|
|
|
void FileHandler::closeFD(int fd) {
|
|
close(fd);
|
|
}
|
|
|
|
std::string FileHandler::getFileName(const std::basic_string<char> &name) {
|
|
auto p = std::filesystem::path(name);
|
|
return p.filename().replace_extension("");
|
|
}
|
|
|
|
bool FileHandler::createDirectoryIfNotExist(const std::basic_string<char> &fileName) {
|
|
auto p = std::filesystem::path(fileName);
|
|
if (!p.has_parent_path()) {
|
|
return false;
|
|
}
|
|
if (FileHandler::isDirectory(p.parent_path())) {
|
|
return true;
|
|
}
|
|
return std::filesystem::create_directories(p.parent_path());
|
|
}
|
|
char *FileHandler::getHomeDirectory() {
|
|
char *homedir;
|
|
if ((homedir = getenv("HOME")) == nullptr) {
|
|
homedir = getpwuid(getuid())->pw_dir;
|
|
}
|
|
return homedir;
|
|
}
|
|
std::string FileHandler::getFromHomeDir(const std::basic_string<char> &path) {
|
|
return std::string(getHomeDirectory() + path);
|
|
}
|
|
} |