• 2.3.C++项目:网络版五子棋对战之实用工具类模块的设计


    一、实用工具类模块

    在这里插入图片描述

    (一)功能

    实用工具类模块主要是负责提前实现一些项目中会用到的边缘功能代码,提前实现好了就可以在项目中用到的时候直接使用了。

    二、设计和封装

    (一)日志宏封装

    #ifndef __M_LOGGER_H__
    #define __M_LOGGER_H__
    #include 
    #include 
    
    #define INF 0
    #define DBG 1
    #define ERR 2
    #define DEFAULT_LOG_LEVEL INF
    #define LOG(level, format, ...) do{\
        if (DEFAULT_LOG_LEVEL > level) break;\
        time_t t = time(NULL);\
        struct tm *lt = localtime(&t);\
        char buf[32] = {0};\
        strftime(buf, 31, "%H:%M:%S", lt);\
        fprintf(stdout, "[%s %s:%d] " format "\n", buf, __FILE__, __LINE__, ##__VA_ARGS__);\
    }while(0)
    #define ILOG(format, ...) LOG(INF, format, ##__VA_ARGS__)
    #define DLOG(format, ...) LOG(DBG, format, ##__VA_ARGS__)
    #define ELOG(format, ...) LOG(ERR, format, ##__VA_ARGS__)
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    (二)mysql_util封装

    class mysql_util {
        public:
            static MYSQL *mysql_create(const std::string &host,
                const std::string &username,
                const std::string &password,
                const std::string &dbname,
                uint16_t port = 3306) {
                MYSQL *mysql = mysql_init(NULL);
                if (mysql == NULL) {
                    ELOG("mysql init failed!");
                    return NULL;
                }
                //2. 连接服务器
                if (mysql_real_connect(mysql, 
                    host.c_str(), 
                    username.c_str(), 
                    password.c_str(), 
                    dbname.c_str(), port, NULL, 0) == NULL) {
                    ELOG("connect mysql server failed : %s", mysql_error(mysql));
                    mysql_close(mysql);
                    return NULL;
                }
                //3. 设置客户端字符集
                if (mysql_set_character_set(mysql, "utf8") != 0) {
                    ELOG("set client character failed : %s", mysql_error(mysql));
                    mysql_close(mysql);
                    return NULL;
                }
                return mysql;
            }
            static bool mysql_exec(MYSQL *mysql, const std::string &sql) {
                int ret = mysql_query(mysql, sql.c_str());
                if (ret != 0) {
                    ELOG("%s\n", sql.c_str());
                    ELOG("mysql query failed : %s\n", mysql_error(mysql));
                    return false;
                }
                return true;
            }
            static void mysql_destroy(MYSQL *mysql) {
                if (mysql != NULL) {
                    mysql_close(mysql);
                }
                return ;
            }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    (三)Jsoncpp-API封装

    class json_util{
        public:
            static bool serialize(const Json::Value &root, std::string &str) {
                Json::StreamWriterBuilder swb;
                std::unique_ptr<Json::StreamWriter>sw(swb.newStreamWriter());
                std::stringstream ss;
                int ret = sw->write(root, &ss);
                if (ret != 0) {
                    ELOG("json serialize failed!!");
                    return false;
                }
                str = ss.str();
                return true;
            }
            static bool unserialize(const std::string &str, Json::Value &root) {
                Json::CharReaderBuilder crb;
                std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
                std::string err;
                bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
                if (ret == false) {
                    ELOG("json unserialize failed: %s", err.c_str());
                    return false;
                }
                return true;
            }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    (四)file_util封装

    class file_util {
       public:
            static bool read(const std::string &filename, std::string &body) {
                //打开文件
                std::ifstream ifs(filename, std::ios::binary);
                if (ifs.is_open() == false) {
                    ELOG("%s file open failed!!", filename.c_str());
                    return false;
                }
                //获取文件大小
                size_t fsize = 0;
                ifs.seekg(0, std::ios::end);
                fsize = ifs.tellg();
                ifs.seekg(0, std::ios::beg);
                body.resize(fsize);
                //将文件所有数据读取出来
                ifs.read(&body[0], fsize);
                if (ifs.good() == false) {
                    ELOG("read %s file content failed!", filename.c_str());
                    ifs.close();
                    return false;
                }
                //关闭文件
                ifs.close();
                return true;
            }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    (五)string_util封装

    class string_util{
      public:
            static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res) {
                // 123,234,,,,345
                size_t pos, idx = 0;
                while(idx < src.size()) {
                    pos = src.find(sep, idx);
                    if (pos == std::string::npos) {
                        //没有找到,字符串中没有间隔字符了,则跳出循环
                        res.push_back(src.substr(idx));
                        break;
                    }
                    if (pos == idx) {
                        idx += sep.size();
                        continue;
                    }
                    res.push_back(src.substr(idx, pos - idx));
                    idx = pos + sep.size();
                }
                return res.size();
            }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    (三)Apache log4net™ 手册 -演示
    Smart Document Control——杜绝文件成堆和文件混乱,保证业务连续性,创建企业新阶段
    进程替换(跑路人笔记)
    Flink开发语言使用java还是Scala合适
    网络安全进阶学习第十九课——CTF之密码学
    SpringCloud总结
    自定义实现:头像上传View
    语音处理:Python实现dBFS刻度和采样值相互转换
    OPC UA 与IEC61499 深度融合(1)
    python合并多个excel
  • 原文地址:https://blog.csdn.net/weixin_54447296/article/details/133978557