util.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef __UTIL_HPP__
  2. #define __UTIL_HPP__
  3. #include <string>
  4. #include <charconv>
  5. #include <utility>
  6. #include <vector>
  7. #include <QDebug>
  8. #include "rapidjson/writer.h"
  9. #include "rapidjson/pointer.h"
  10. #include "rapidjson/stringbuffer.h"
  11. #include "rapidjson/document.h"
  12. #include "json.hpp"
  13. using namespace rapidjson;
  14. using json = nlohmann::json;
  15. typedef std::string KOperation;
  16. typedef std::vector<std::pair<std::string, std::string>> TupVec;
  17. typedef std::vector<std::map<int, std::string>> MapVec;
  18. typedef std::vector<std::string> StdStringVec;
  19. typedef std::map<int, std::string> CommandMap;
  20. struct KSession {
  21. int id;
  22. int fd;
  23. int status;
  24. };
  25. //std::string createMessage(const char* data, std::string args = "") {
  26. // json data_json{};
  27. // data_json["type"] = "custom";
  28. // data_json["args"] = args;
  29. // data_json["message"] = data;
  30. // return data_json.dump();
  31. //}
  32. std::string rapidCreateMessage(const char* data, std::string args = "") {
  33. StringBuffer s;
  34. Writer<StringBuffer> w(s);
  35. w.StartObject();
  36. w.Key("type");
  37. w.String("custom");
  38. w.Key("message");
  39. w.String(data);
  40. w.Key("args");
  41. w.String(args.c_str());
  42. w.EndObject();
  43. return s.GetString();
  44. }
  45. bool rapidIsOperation(const char* data) {
  46. Document d;
  47. d.Parse(data);
  48. return strcmp(d["type"].GetString(), "operation");
  49. }
  50. CommandMap rapidGetArgMap(const char* data) {
  51. Document d;
  52. d.Parse(data);
  53. CommandMap cm{};
  54. if (d.HasMember("args")) {
  55. for (const auto& m : d["args"].GetObject()) {
  56. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  57. }
  58. }
  59. return cm;
  60. }
  61. std::string rapidCreateMessage(const char* data,
  62. std::map<int, std::string> map = {}) {
  63. StringBuffer s;
  64. Writer<StringBuffer> w(s);
  65. w.StartObject();
  66. w.Key("type");
  67. w.String("custom");
  68. w.Key("message");
  69. w.String(data);
  70. w.Key("args");
  71. w.StartObject();
  72. if (!map.empty()) {
  73. for (const auto& [k, v] : map) {
  74. w.Key(std::to_string(k).c_str());
  75. w.String(v.c_str());
  76. }
  77. }
  78. w.EndObject();
  79. w.EndObject();
  80. return s.GetString();
  81. }
  82. bool rapidIsStartOperation(const char* data) {
  83. Document d;
  84. d.Parse(data);
  85. return strcmp(d["command"].GetString(), "start");
  86. }
  87. bool rapidIsStopOperation(const char* data) {
  88. Document d;
  89. d.Parse(data);
  90. return strcmp(d["command"].GetString(), "stop");
  91. }
  92. bool rapidIsNewSession(const char* data) {
  93. Document d;
  94. d.Parse(data);
  95. if (d.HasMember("message")) {
  96. return strcmp(d["message"].GetString(), "New Session") == 0;
  97. }
  98. return false;
  99. }
  100. //std::string createMessageWithDataMap(const char* data,
  101. // std::map<int, std::string> map = {}) {
  102. // json data_json{};
  103. // data_json["type"] = "custom";
  104. // data_json["message"] = data;
  105. // data_json["args"] = nullptr;
  106. // if (!map.empty()) {
  107. // for (const auto& [k, v] : map) {
  108. // data_json["args"][k] = v;
  109. // }
  110. // }
  111. // return data_json;
  112. //}
  113. //std::string createMessage(const char* data, const char* extra = "") {
  114. // json data_json{};
  115. // data_json["type"] = "custom";
  116. // data_json["message"] = data;
  117. // data_json["args"] = extra;
  118. // return data_json.dump();
  119. //}
  120. //std::string createMessage(const char* data, TupVec v = {}) {
  121. // json data_json{};
  122. // data_json["type"] = "custom";
  123. // data_json["message"] = data;
  124. // data_json["args"] = nullptr;
  125. // if (!v.empty()) {
  126. // for (const auto& r : v) {
  127. // data_json["args"][r.first] = r.second;
  128. // }
  129. // }
  130. // return data_json.dump();
  131. //}
  132. std::string createOperation(const char* op, std::vector<std::string> args) {
  133. json operation_json{};
  134. operation_json["type"] = "operation";
  135. operation_json["command"] = op;
  136. if (!args.empty()) {
  137. operation_json["args"] = args;
  138. }
  139. return operation_json.dump();
  140. }
  141. //std::string stringTupleVecToJson(
  142. // std::vector<std::pair<std::string, std::string>> v) {
  143. // json j{};
  144. // for (const auto& row : v) {
  145. // j[row.first] = row.second;
  146. // }
  147. // return j;
  148. //}
  149. //bool isOperation(json data) { return *(data.find("type")) == "operation"; }
  150. //bool isNewSession(json data) {
  151. // return *(data.find("message")) == "New Session";
  152. //}
  153. //bool isStartOperation(std::string operation) { return operation == "start"; }
  154. //bool isStopOperation(std::string operation) { return operation == "stop"; }
  155. inline size_t findNullIndex(uint8_t* data) {
  156. size_t index = 0;
  157. while (data) {
  158. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  159. break;
  160. }
  161. index++;
  162. data++;
  163. }
  164. return index;
  165. }
  166. #endif // __UTIL_HPP__