Parcourir la source

添加mysql、json、stringSplit、fileRead工具类

yaozheng0922 il y a 5 mois
Parent
commit
bf314a9be2
1 fichiers modifiés avec 135 ajouts et 0 suppressions
  1. 135 0
      source/util.hpp

+ 135 - 0
source/util.hpp

@@ -0,0 +1,135 @@
+#ifndef __UTIL_HPP__
+#define __UTIL_HPP__
+#include "logger.hpp"
+#include <iostream>
+#include <string>
+#include <vector>
+#include <memory>
+#include <fstream>
+#include <sstream>
+#include <mysql/mysql.h>
+#include <jsoncpp/json/json.h>
+
+/*MySQL C API工具类*/
+class mysql_util {
+public:
+    static MYSQL *mysql_create(const std::string &host, const std::string &user, const std::string &passwd, \
+                               const std::string db = "gobang", uint16_t port = 4106) {
+        /*初始化MYSQL句柄*/
+        MYSQL *mysql = mysql_init(nullptr);
+        if(mysql == nullptr) {
+            LOG(FATAL, "mysql init failed");
+            return nullptr;
+        }
+        /*连接MySQL数据库*/
+        mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0);
+        if(mysql == nullptr) {
+            LOG(FATAL, "mysql connect failed: %s", mysql_error(mysql));
+            mysql_close(mysql);
+            return nullptr;
+        }
+        /*设置客户端字符集*/
+        if(mysql_set_character_set(mysql, "utf8") != 0) {
+            LOG(ERROR, "client character set failed: %s", mysql_error(mysql));
+        }
+        return mysql;
+    }
+
+    static bool mysql_execute(MYSQL *mysql, const std::string &sql) {
+        if(mysql_query(mysql, sql.c_str()) != 0) {
+            LOG(ERROR, "sql query failed: %s", mysql_error(mysql));
+            return false;
+        }
+        return true;
+    }
+
+    static void mysql_destroy(MYSQL *mysql) {
+        if(mysql != nullptr) {
+            mysql_close(mysql);
+        }
+    }
+};
+
+/*jsoncpp工具类*/
+class json_util {
+public:
+    static bool serialize(Json::Value &root, std::string &str) {
+        Json::StreamWriterBuilder swb;
+        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
+        std::stringstream ss;
+        if(sw->write(root, &ss) != 0) {
+            LOG(ERROR, "json serialize failed");
+            return false;
+        }
+        str = ss.str();
+        return true;
+    }
+
+    static bool deserialize(const std::string &str, Json::Value &root) {
+        Json::CharReaderBuilder crb;
+        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
+        std::string err;
+        if(cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err) == false) {
+            LOG(ERROR, "json deserialize failed: %s", err);
+            return false;
+        }
+        return true;
+    }
+};
+
+/*字符串处理工具类*/
+class string_util {
+public:
+    /*将源字符串按照特定分隔符分割为若干个子字符串*/
+    static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res) {
+        // ..abc..de..ef
+        int index = 0, pos = 0;
+        while(index < src.size()) {
+            pos = src.find(sep, index);
+            if(pos == std::string::npos) {
+                res.push_back(src.substr(index));
+                break;
+            }
+            if(index == pos) {
+                index += sep.size();
+                continue;
+            }
+            else {
+                res.push_back(src.substr(index, pos - index));
+                index = pos + sep.size();
+            }
+        }
+        return res.size();
+    }
+};
+
+/*读取文件数据工具类*/
+class file_util {
+public:
+    static bool read(const std::string &filename, std::string &data) {
+        /*以二进制形式打开文件*/
+        std::ifstream ifs(filename, std::ios::binary);
+        if(ifs.is_open() == false) {
+            LOG(ERROR, "open %s file failed", filename);
+            return false;
+        }
+        /*获取文件大小*/
+        size_t size;
+        ifs.seekg(0, std::ios::end);
+        size = ifs.tellg();
+        ifs.seekg(0, std::ios::beg);
+        /*读取文件内容*/
+        data.resize(size);
+        ifs.read(&data[0], size);
+        if(ifs.good() == false) {
+            LOG(ERROR, "read %s file content failed", filename);
+            ifs.close();
+            return false;
+        }
+        /*关闭文件*/
+        ifs.close();
+        return true;
+    }
+};
+
+#endif