util.hpp 4.2 KB

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