#ifndef FILE_UNIT_H_20220214
#define FILE_UNIT_H_20220214
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <boost/filesystem.hpp>
#include <LogHelper.h>
#include "utility/Utility.hpp"
#include "utility/ThreadPool.hpp"
#include "3rd_party/nlohmann/json.hpp"
using nJson = nlohmann::json;
namespace fs = boost::filesystem;
namespace node
{
class FileUnit
{
public:
inline static bool
CreateNewDirectory(const std::string& strDir)
{
if (IsDirExist(strDir))
{
return true;
}
return boost::filesystem::create_directories(strDir);
}
inline static bool
CreateFile(const std::string& strAbsoluteFilePath)
{
try
{
boost::filesystem::path file(strAbsoluteFilePath);
if (boost::filesystem::is_regular_file(file))
{
std::cout << "File already exists!" << std::endl;
return false;
}
else
{
boost::filesystem::create_directories(file.parent_path());
std::ofstream ofs(file.string());
ofs.close();
std::cout << "File created successfully!" << std::endl;
return true;
}
}
catch (const std::exception& ex)
{
ECO_ERROR_STREAM("Create file failed, reason: " << ex.what());
return false;
}
}
inline static bool
IsDirExist(const std::string& strDIr)
{
if (boost::filesystem::exists(strDIr) && boost::filesystem::is_directory(strDIr))
{
return true;
}
return false;
}
inline static bool
IsFileExist(const std::string& file_name)
{
if (boost::filesystem::exists(file_name) && boost::filesystem::is_regular_file(file_name))
{
return true;
}
return false;
}
inline static bool
IsAllFileNotEmpty(const std::string& file_name, const std::vector<std::string>& suffix)
{
bool ret = true;
for (auto var : suffix)
{
std::string file_full_path = file_name + var;
if (boost::filesystem::file_size(file_full_path) <= 0)
{
ECO_WARN("file \"%d\" is empty !!!", file_full_path.c_str());
ret = false;
break;
}
}
return ret;
}
inline static uint32_t
GetFileSize(const std::string& file_name)
{
uint32_t file_size = boost::filesystem::file_size(file_name);
return file_size;
}
inline static std::string
GetFileContent(const std::string& file_name)
{
if (!IsFileExist(file_name))
{
ECO_WARN("File %s not exist", file_name.c_str());
}
else
{
std::ifstream is(file_name);
return { std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>() };
}
return std::string();
}
inline static std::vector<uint8_t>
GetFileContentAsVector(const std::string& file_name)
{
if (!IsFileExist(file_name))
{
ECO_WARN("File %s not exist", file_name.c_str());
}
else
{
uint32_t file_size = GetFileSize(file_name);
std::vector<uint8_t> buffer;
buffer.resize(file_size);
std::ifstream inFile(file_name, std::ios::binary);
inFile.read((char*)buffer.data(), buffer.size());
inFile.close();
ECO_DEBUG("read div binary_size = %d, file_size = %d", buffer.size(), file_size);
return std::move(buffer);
}
return std::vector<uint8_t>();
}
inline static bool
WriteContentToFile(const std::string& file_name, const std::string& content)
{
std::ofstream file(file_name, std::ios::binary | std::ios::out | std::ios::trunc);
file << content;
file.close();
sync();
return true;
}
template<typename T>
inline static bool
WriteToFileByBoost(const std::string& filename, const T& data)
{
ECO_INFO("[file]Write file: %s", filename.c_str());
try
{
std::ofstream ofs(filename, std::ios::out | std::ios::binary);
if (ofs.is_open())
{
ofs.write(reinterpret_cast<const char*>(data.data()), data.size());
return true;
}
else
{
ECO_ERROR("The file cannot be opened.");
return false;
}
}
catch (const std::exception& ex)
{
ECO_ERROR_STREAM("An exception occurred when writing to the file: " << ex.what());
return false;
}
}
template<typename T>
inline static bool
WriteToFileBySystem(const std::string& filename, const T& data)
{
auto startTime = Utility::GetCurrentTime();
if (!FileUnit::IsFileExist(filename))
{
if (!FileUnit::CreateFile(filename))
{
return false;
}
}
ECO_INFO("[file]Try to write file: %s", filename.c_str());
int fd;
try
{
fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if (fd == -1)
{
ECO_ERROR("The file cannot be opened.");
return false;
}
ssize_t bytesWritten = write(fd, data.data(), data.size());
if (bytesWritten == -1)
{
close(fd);
ECO_ERROR("An exception occurred when writing to the file.");
return false;
}
auto func = [&](const int& fd) {
int ret = fsync(fd);
if (ret == -1)
{
ECO_ERROR("An exception occurred when executing fsync.");
perror("An exception occurred when executing fsync.");
}
close(fd);
};
ThreadPool::instance().enqueue(func, fd);
}
catch (const std::exception& ex)
{
ECO_ERROR_STREAM("An exception occurred when writing to the file: " << ex.what());
close(fd);
return false;
}
auto endTime = Utility::GetCurrentTime();
ECO_WARN_NEW("Exec WriteToFileBySystem Func time: {}ms", endTime - startTime);
return true;
}
inline static bool
WriteVectorContentToFile(const std::string& file_name, const std::vector<uint8_t>& content)
{
std::ofstream file(file_name, std::ios::binary | std::ios::out | std::ios::trunc);
file.write((char*)content.data(), content.size());
file.close();
sync();
int file_size = GetFileSize(file_name);
ECO_DEBUG("write div binary_size = %d, file_size = %d", content.size(), file_size);
return true;
}
inline static void
RemoveAllFilesInDir(const std::string& strDIr, bool isRemoveDir = false)
{
boost::filesystem::path dir(strDIr);
if (boost::filesystem::is_directory(dir))
{
for (boost::filesystem::directory_iterator iter(dir), end_iter; iter != end_iter; ++iter)
{
const boost::filesystem::path& path = iter->path();
if (boost::filesystem::is_directory(path))
{
if (isRemoveDir)
RemoveAllFilesInDir(path.string());
}
else
{
boost::filesystem::remove(path);
}
}
if (isRemoveDir && boost::filesystem::is_empty(dir))
{
boost::filesystem::remove(dir);
}
}
}
inline static nJson
LoadFile2Json(const std::string& strFullPath)
{
ECO_INFO_NEW("Try to load file: '{}'", strFullPath);
if (strFullPath.empty())
{
ECO_ERROR("The path of file is empty.");
return {};
}
if (!IsFileExist(strFullPath))
{
ECO_WARN("The file does not exist, '%s'", strFullPath.c_str());
return {};
}
std::ifstream file(strFullPath);
nJson rootJson;
try
{
file >> rootJson;
file.close();
}
catch (const std::exception& e)
{
file.close();
ECO_ERROR("Parse config file 2 json failed, reason: \n%s", e.what());
return {};
}
return rootJson;
}
inline static bool
CopyDirFromTo(const std::string& srcAbsDir, const std::string& targetAbsDir)
{
fs::path sourceDir{ srcAbsDir };
fs::path targetDir{ targetAbsDir };
if (!fs::exists(targetDir))
{
ECO_WARN_NEW("[CopyDir] No such target directory: {}, create it...\n", targetDir.filename().c_str());
fs::create_directory(targetDir);
}
for (fs::directory_entry& srcEntry : fs::recursive_directory_iterator(sourceDir))
{
fs::path relativePath = fs::relative(srcEntry.path(), sourceDir);
fs::path destPath = targetDir / relativePath;
try
{
if (fs::is_directory(srcEntry.path()))
{
ECO_INFO("[CopyDir] try 2 create target sub directory: %s\n", targetDir.filename().c_str());
fs::create_directory(destPath);
}
else
{
fs::copy_file(srcEntry.path(), destPath, fs::copy_option::overwrite_if_exists);
}
}
catch (fs::filesystem_error& ex)
{
ECO_ERROR_NEW("[CopyDir] error, reason: {}\n", ex.what());
return false;
}
}
return true;
}
};
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!