1 Star 0 Fork 0

蝎子莱莱xo/cpp_projects

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
util.hpp 4.82 KB
一键复制 编辑 原始数据 按行查看 历史
lmx-xo 提交于 2024-04-13 19:30 +08:00 . 顶层makefile
#pragma once
// 工具
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <jsoncpp/json/json.h>
#include <fstream>
#include <atomic>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include <mutex>
#include <cassert>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
namespace ns_util
{
// 临时文件存放路径
const string temp_path = "./temp/";
class TimeUtil
{
public:
// 获得秒级时间戳
static string GetTimeStamp()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
return std::to_string(tv.tv_sec);
}
// 获得毫秒级时间戳
static string GetTimeMs()
{
struct timeval tv;
gettimeofday(&tv, nullptr);
return std::to_string(tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
};
// 路径工具
class PathUtil
{
public:
// 拼接路径
static string AddSuffix(const string &file_name, const string suffix)
{
string path_name = temp_path;
path_name += file_name;
path_name += suffix;
return path_name;
}
// 构建对应的.cpp文件的完整路径+后缀名
static string Src(const string &file_name)
{
return AddSuffix(file_name, ".cpp");
}
// 构建对应的.exe文件的完整路径+后缀名
static string Exe(const string &file_name)
{
return AddSuffix(file_name, ".exe");
}
// 编译时报错信息的存储文件
static string CompileError(const string &file_name)
{
return AddSuffix(file_name, ".compile_error");
}
// 标准输入
static string Stdin(const string &file_name)
{
return AddSuffix(file_name, ".stdin");
}
// 标准输出
static string Stdout(const string &file_name)
{
return AddSuffix(file_name, ".stdout");
}
// 构建对应的运行时错误文件的完整路径+后缀名
static string Stderr(const string &file_name)
{
return AddSuffix(file_name, ".stderr");
}
};
// 文件工具
class FileUtil
{
public:
// 使用stat接口判断文件是否存在
static bool IsFileExists(const string &path_name)
{
struct stat st;
if (stat(path_name.c_str(), &st) == 0)
{
// 获取属性成功,文件已经存在
return true;
}
return false;
}
// 生成唯一的文件名
// 毫秒级时间戳 + 原子性递增唯一值:来保证唯一性
static string UniqueFileName()
{
// c++11提供的原子级计数器
static std::atomic_uint id(0);
id++;
string ms = TimeUtil::GetTimeMs();
string uniq_id = std::to_string(id);
return ms + "_" + uniq_id;
}
// 将用户代码写到目标文件中
static bool WriteFile(const string &target, const string &content)
{
std::ofstream out(target);
if (!out.is_open())
{
return false;
}
out.write(content.c_str(), content.size());
out.close();
return true;
}
// 读取文件内容
static bool ReadFile(const string &target, string *content, bool keep = false)
{
(*content).clear();
std::ifstream in(target);
if (!in.is_open())
{
return false;
}
string line;
// getline内部重载了强制类型转化,可以返回bool值用于while判断
while (std::getline(in, line))
{
// getline不保存行分隔符,有些时候需要保留\n
(*content) += line;
(*content) += (keep ? "\n" : "");
}
in.close();
return true;
}
};
class StringUtil
{
public:
/******************************
* str:输入型参数,目标要切分的字符串
* target:输出型参数,保存切分完毕的结果
* sep:指定的分隔符
* ****************************/
static void SpiltString(const string &str, vector<string> *target, string sep)
{
//使用boost库中的分割字符串功能
boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
}
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/lmx-xo/cpp_projects.git
git@gitee.com:lmx-xo/cpp_projects.git
lmx-xo
cpp_projects
cpp_projects
master

搜索帮助