【负载均衡oj项目】02. comm公共文件夹设计 - 包含所有需要用到的自定义工具

目录

  • 1.日志打印功能
  • 2.util工具文件设计(utilities)
    • [2.1 TimuUtil工具类 - 获取时间戳](#2.1 TimuUtil工具类 - 获取时间戳)
    • [2.2 PathUtil工具类 - 拼接文件路径](#2.2 PathUtil工具类 - 拼接文件路径)
    • [2.3 FileUtil工具类 - 文件相关工具](#2.3 FileUtil工具类 - 文件相关工具)
    • [2.4 StringUtil工具类 - 字符串分割](#2.4 StringUtil工具类 - 字符串分割)
  • [3. httplib.h文件](#3. httplib.h文件)

comm文件夹:

  1. util.hpp(常用公共功能):获取时间戳、路径拼接、文件名拼接、文件内容读取与写入、字符串分割。
  2. log.hpp(日志打印功能):打印日志功能。
  3. httplib.h(http协议):http协议的C++版本开源库。

1.日志打印功能

目的:为了便于日志信息的输出,将日志打印功能单独封装。

cpp 复制代码
#pragma once

#include <iostream>
#include <string>
#include "util.hpp"

namespace ns_log
{
    using namespace ns_util;
    enum
    {
        INFO,
        DEBUG,
        WARNING,
        ERROR,
        FATAL
    };

    // 打印日志
    inline std::ostream &Log(const std::string &level, const std::string &file_name, int line)
    {
        // 添加日志等级
        std::string message = "[";
        message += level;
        message += "]";

        // 添加报错文件名称
        message += "[";
        message += file_name;
        message += "]";

        // 添加报错行
        message += "[";
        message += std::to_string(line);
        message += "]";

        // 日志时间戳
        message += "[";
        message += TimeUtil::GetTimeStamp();
        message += "]";

        // cout 本质 内部是包含缓冲区的
        std::cout << message;

        return std::cout;
    }
    #define LOG(level) Log(#level, __FILE__, __LINE__)
}

2.util工具文件设计(utilities)

2.1 TimuUtil工具类 - 获取时间戳

  1. 秒级时间的获取,为了打印日志使用。
  2. 毫秒级时间戳的获取,为了运行和编译代码创建唯一的文件名。(我们的编译和运行模块需要先创建缓存文件,将编译和运行结果得到并输出,最后再删除缓存文件。为了文件名不冲突,我们使用毫秒级时间戳加序号的方式给缓存文件命名。)
cpp 复制代码
#include <sys/time.h>
#include <string>
// 获取时间戳
class TimeUtil
{
public:
    // 获取日志时间 -- 打印日志使用
    static std::string GetTimeStamp()
    {
        struct timeval _time;
        gettimeofday(&_time, nullptr);
        return std::to_string(_time.tv_sec);
    }
    // 获取毫秒时间戳 -- 创建代码测试文件使用
    static std::string GetTimeMs()
    {
        struct timeval _time;
        gettimeofday(&_time, nullptr);
        return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
    }
};

2.2 PathUtil工具类 - 拼接文件路径

  1. 测试文件统一存放在compile_server目录下面的temp文件夹中。
  2. Addsuffix函数将路径、文件名、后缀拼接,形成完整的文件路径。
  3. Src、Exe、CompilerError、Stdin、Stdout、Stderr函数分别是:源代码文件、可执行程序、编译报错文件、输入数据文件、标准输出文件、标准错误文件。各自返回相应后缀的文件详细路径。
cpp 复制代码
// 路径拼接 -- 测试文件统一凡在compile_server/temp/文件夹下面
const std::string temp_path = "./temp/";
class PathUtil
{
public:
    // 拼接文件后缀名 -- 此处的 file_name是没有后缀的文件名
    static std::string Addsuffix(const std::string &file_name, const std::string &suffix)
    {
        std::string path_name = temp_path;
        path_name += file_name;
        path_name += suffix;
        return path_name;
    }
    static std::string Src(const std::string file_name)
    {
        return Addsuffix(file_name, ".cpp");
    }
    static std::string Exe(const std::string file_name)
    {
        return Addsuffix(file_name, ".exe");
    }
    static std::string CompilerError(const std::string file_name)
    {
        return Addsuffix(file_name, ".compile_error");
    }

    // 运行时需要的临时文件
    static std::string Stdin(const std::string file_name)
    {
        return Addsuffix(file_name, ".stdin");
    }        
    static std::string Stdout(const std::string file_name)
    {
        return Addsuffix(file_name, ".stdout");
    }
    static std::string Stderr(const std::string file_name)
    {
        return Addsuffix(file_name, ".stderr");
    }
};

2.3 FileUtil工具类 - 文件相关工具

  1. IsFileExists函数,用来判断程序编译是否成功。
  2. UniqFileName函数通过毫秒级时间戳和自增变量得到唯一的文件名。(此处的文件名指不带路径,不带后缀的文件名)
  3. WriteFile函数将string类型的数据写入文件。
  4. ReadFile函数从文件读取数据并使用string类型输出。
cpp 复制代码
// 判断文件是否存在
class FileUtil
{
public:
    // 间接用来判断是否编译成功
    static bool IsFileExists(const std::string &path_name)
    {
        struct stat st;
        if(stat(path_name.c_str(), &st) == 0)
        {
            return true;
        }
        return false;
    }
    // 使用毫秒级时间戳+自增变量来命名 代码执行的 唯一文件名
    static std::string UniqFileName()
    {
        // TODO
        static std::atomic_uint id(0);
        id++;

        // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
        std::string ms = TimeUtil::GetTimeMs();
        std::string uniq_id = std::to_string(id);
        return ms + "_" + uniq_id;
    }

    // 打开target文件,并将content内容写入target文件
    static bool WriteFile(const std::string &target, const std::string &content)
    {   
        std::ofstream out(target);
        if(!out.is_open())
        {
            return false;
        }
        out.write(content.c_str(), content.size());
        out.close();
        return true;
    }

    // 将target文件的内容读取到content字符串中 -- keep为:是否保留\n换行符号
    static bool ReadFile(const std::string &target, std::string *content, bool keep = false)
    {
        (*content).clear();

        std::ifstream in(target);
        if(!in.is_open())
        {
            return false;
        }
        std::string line;
        // getline:不保存行分割符,有些时候需要保留\n,
        // getline内部重载了强制类型转化
        while(std::getline(in, line))
        {
            (*content) += line;
            (*content) += (keep ? "\n" : "");
        }
        in.close();
        return true;
    }
};

2.4 StringUtil工具类 - 字符串分割

使用boost工具,将字符串根据指定的分割符进行分割,并使用vector<std::string>类型输出。

cpp 复制代码
class StringUtil
{
public:
    /****************************
     * str: 输入型,目标要切分的字符串
     * target:输出型,保存切分完毕的结果
     * sep:指定分割符
 *******************************/
    static void SplitString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
    {
        // boost split
        boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
    }
};

3. httplib.h文件

直接将之前clone下来的文件夹中的httplib.h文件复制到此文件中即可。

...过云雨-CSDN博客

https://blog.csdn.net/qq_54652195/article/details/159049433

相关推荐
南山love2 小时前
Redis持久化深度解析:RDB与AOF的原理、区别及生产选型
数据库·redis·缓存
楼田莉子2 小时前
MySQL数据库的操作
数据库·mysql
2401_900151542 小时前
用Python和Twilio构建短信通知系统
jvm·数据库·python
qq_5470261792 小时前
RAG 向量数据库
数据库·langchain
昵称只能一个月修改一次。。。2 小时前
SQLite
数据库
身如柳絮随风扬2 小时前
MySQL为什么使用B+树?
数据库·b树·mysql
短剑重铸之日2 小时前
《ShardingSphere解读》04 配置驱动:ShardingSphere 中的配置体系是如何设计的?
java·数据库·后端·spring·shardingsphere
optimistic_chen2 小时前
【Vue入门】组件及组件化
前端·javascript·vue.js·html·组件
知无不研2 小时前
constexpr关键字
开发语言·c++·constexpr