util.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef __UTIL_HPP__
  2. #define __UTIL_HPP__
  3. #include "logger.hpp"
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <memory>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <mysql/mysql.h>
  11. #include <jsoncpp/json/json.h>
  12. /*MySQL C API工具类*/
  13. class mysql_util {
  14. public:
  15. static MYSQL *mysql_create(const std::string &host, const std::string &user, const std::string &passwd, \
  16. const std::string db = "gobang", uint16_t port = 4106) {
  17. /*初始化MYSQL句柄*/
  18. MYSQL *mysql = mysql_init(nullptr);
  19. if(mysql == nullptr) {
  20. LOG(FATAL, "mysql init failed");
  21. return nullptr;
  22. }
  23. /*连接MySQL数据库*/
  24. mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0);
  25. if(mysql == nullptr) {
  26. LOG(FATAL, "mysql connect failed: %s", mysql_error(mysql));
  27. mysql_close(mysql);
  28. return nullptr;
  29. }
  30. /*设置客户端字符集*/
  31. if(mysql_set_character_set(mysql, "utf8") != 0) {
  32. LOG(ERROR, "client character set failed: %s", mysql_error(mysql));
  33. }
  34. return mysql;
  35. }
  36. static bool mysql_execute(MYSQL *mysql, const std::string &sql) {
  37. if(mysql_query(mysql, sql.c_str()) != 0) {
  38. LOG(ERROR, "sql query failed: %s", mysql_error(mysql));
  39. return false;
  40. }
  41. return true;
  42. }
  43. static void mysql_destroy(MYSQL *mysql) {
  44. if(mysql != nullptr) {
  45. mysql_close(mysql);
  46. }
  47. }
  48. };
  49. /*jsoncpp工具类*/
  50. class json_util {
  51. public:
  52. static bool serialize(Json::Value &root, std::string &str) {
  53. Json::StreamWriterBuilder swb;
  54. std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
  55. std::stringstream ss;
  56. if(sw->write(root, &ss) != 0) {
  57. LOG(ERROR, "json serialize failed");
  58. return false;
  59. }
  60. str = ss.str();
  61. return true;
  62. }
  63. static bool deserialize(const std::string &str, Json::Value &root) {
  64. Json::CharReaderBuilder crb;
  65. std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
  66. std::string err;
  67. if(cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err) == false) {
  68. LOG(ERROR, "json deserialize failed: %s", err);
  69. return false;
  70. }
  71. return true;
  72. }
  73. };
  74. /*字符串处理工具类*/
  75. class string_util {
  76. public:
  77. /*将源字符串按照特定分隔符分割为若干个子字符串*/
  78. static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res) {
  79. // ..abc..de..ef
  80. int index = 0, pos = 0;
  81. while(index < src.size()) {
  82. pos = src.find(sep, index);
  83. if(pos == std::string::npos) {
  84. res.push_back(src.substr(index));
  85. break;
  86. }
  87. if(index == pos) {
  88. index += sep.size();
  89. continue;
  90. }
  91. else {
  92. res.push_back(src.substr(index, pos - index));
  93. index = pos + sep.size();
  94. }
  95. }
  96. return res.size();
  97. }
  98. };
  99. /*读取文件数据工具类*/
  100. class file_util {
  101. public:
  102. static bool read(const std::string &filename, std::string &data) {
  103. /*以二进制形式打开文件*/
  104. std::ifstream ifs(filename, std::ios::binary);
  105. if(ifs.is_open() == false) {
  106. LOG(ERROR, "open %s file failed", filename);
  107. return false;
  108. }
  109. /*获取文件大小*/
  110. size_t size;
  111. ifs.seekg(0, std::ios::end);
  112. size = ifs.tellg();
  113. ifs.seekg(0, std::ios::beg);
  114. /*读取文件内容*/
  115. data.resize(size);
  116. ifs.read(&data[0], size);
  117. if(ifs.good() == false) {
  118. LOG(ERROR, "read %s file content failed", filename);
  119. ifs.close();
  120. return false;
  121. }
  122. /*关闭文件*/
  123. ifs.close();
  124. return true;
  125. }
  126. };
  127. #endif