util.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <QVector>
  9. #include "rapidjson/writer.h"
  10. #include "rapidjson/prettywriter.h"
  11. #include "rapidjson/pointer.h"
  12. #include "rapidjson/stringbuffer.h"
  13. #include "rapidjson/document.h"
  14. #include "json.hpp"
  15. using namespace rapidjson;
  16. using json = nlohmann::json;
  17. typedef std::string KOperation;
  18. typedef std::vector<std::pair<std::string, std::string>> TupVec;
  19. typedef std::vector<std::map<int, std::string>> MapVec;
  20. typedef std::vector<std::string> StdStringVec;
  21. typedef std::map<int, std::string> CommandMap;
  22. struct KSession {
  23. int id;
  24. int fd;
  25. int status;
  26. };
  27. std::string getJsonString(std::string s) {
  28. Document d;
  29. d.Parse(s.c_str());
  30. StringBuffer buffer;
  31. PrettyWriter<StringBuffer> writer(buffer);
  32. d.Accept(writer);
  33. return buffer.GetString();
  34. }
  35. std::string createMessage(const char* data, std::string args = "") {
  36. StringBuffer s;
  37. Writer<StringBuffer> w(s);
  38. w.StartObject();
  39. w.Key("type");
  40. w.String("custom");
  41. w.Key("message");
  42. w.String(data);
  43. w.Key("args");
  44. w.String(args.c_str());
  45. w.EndObject();
  46. return s.GetString();
  47. }
  48. bool isOperation(const char* data) {
  49. Document d;
  50. d.Parse(data);
  51. return strcmp(d["type"].GetString(), "operation") == 0;
  52. }
  53. bool isEvent(const char* data) {
  54. Document d;
  55. d.Parse(data);
  56. return strcmp(d["type"].GetString(), "event") == 0;
  57. }
  58. std::string createOperation(const char* op, std::vector<std::string> args) {
  59. StringBuffer s;
  60. Writer<StringBuffer, Document::EncodingType, ASCII<>> w(s);
  61. w.StartObject();
  62. w.Key("type");
  63. w.String("operation");
  64. w.Key("command");
  65. w.String(op);
  66. w.Key("args");
  67. w.StartArray();
  68. if (!args.empty()) {
  69. for (const auto& arg : args) {
  70. w.String(arg.c_str());
  71. }
  72. }
  73. w.EndArray();
  74. w.EndObject();
  75. return s.GetString();
  76. }
  77. std::string getOperation(const char* data) {
  78. Document d;
  79. d.Parse(data);
  80. if (d.HasMember("command")) {
  81. return d["command"].GetString();
  82. }
  83. return "";
  84. }
  85. QString getEvent(const char* data) {
  86. Document d;
  87. d.Parse(data);
  88. if (d.HasMember("event")) {
  89. return d["event"].GetString();
  90. }
  91. return "";
  92. }
  93. QVector<QString> getArgs(const char* data) {
  94. Document d;
  95. d.Parse(data);
  96. QVector<QString> args{};
  97. if (d.HasMember("args")) {
  98. for (const auto& m : d["args"].GetArray()) {
  99. args.push_back(m.GetString());
  100. }
  101. }
  102. return args;
  103. }
  104. CommandMap getArgMap(const char* data) {
  105. Document d;
  106. d.Parse(data);
  107. CommandMap cm{};
  108. if (d.HasMember("args")) {
  109. for (const auto& m : d["args"].GetObject()) {
  110. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  111. }
  112. }
  113. return cm;
  114. }
  115. std::string createMessage(const char* data,
  116. std::map<int, std::string> map = {}) {
  117. StringBuffer s;
  118. Writer<StringBuffer> w(s);
  119. w.StartObject();
  120. w.Key("type");
  121. w.String("custom");
  122. w.Key("message");
  123. w.String(data);
  124. w.Key("args");
  125. w.StartObject();
  126. if (!map.empty()) {
  127. for (const auto& [k, v] : map) {
  128. w.Key(std::to_string(k).c_str());
  129. w.String(v.c_str());
  130. }
  131. }
  132. w.EndObject();
  133. w.EndObject();
  134. return s.GetString();
  135. }
  136. std::string createMessage(const char* data, std::map<int, std::vector<std::string>> map = {}) {
  137. StringBuffer s;
  138. Writer<StringBuffer> w(s);
  139. w.StartObject();
  140. w.Key("type");
  141. w.String("custom");
  142. w.Key("message");
  143. w.String(data);
  144. w.Key("args");
  145. w.StartObject();
  146. if (!map.empty()) {
  147. for (const auto& [k, v] : map) {
  148. w.Key(std::to_string(k).c_str());
  149. if (!v.empty()) {
  150. w.StartArray();
  151. for (const auto& arg : v) {
  152. w.String(arg.c_str());
  153. }
  154. w.EndArray();
  155. }
  156. }
  157. }
  158. w.EndObject();
  159. w.EndObject();
  160. return s.GetString();
  161. }
  162. std::string rapidCreateMessage(const char* data,
  163. std::map<int, std::string> map = {}) {
  164. StringBuffer s;
  165. Writer<StringBuffer> w(s);
  166. w.StartObject();
  167. w.Key("type");
  168. w.String("custom");
  169. w.Key("message");
  170. w.String(data);
  171. w.Key("args");
  172. w.StartObject();
  173. if (!map.empty()) {
  174. for (const auto& [k, v] : map) {
  175. w.Key(std::to_string(k).c_str());
  176. w.String(v.c_str());
  177. }
  178. }
  179. w.EndObject();
  180. w.EndObject();
  181. return s.GetString();
  182. }
  183. bool isStartOperation(const char* data) {
  184. Document d;
  185. d.Parse(data);
  186. return strcmp(d["command"].GetString(), "start") == 0;
  187. }
  188. bool isStopOperation(const char* data) {
  189. Document d;
  190. d.Parse(data);
  191. return strcmp(d["command"].GetString(), "stop") == 0;
  192. }
  193. bool isNewSession(const char* data) {
  194. Document d;
  195. d.Parse(data);
  196. if (d.HasMember("message")) {
  197. return strcmp(d["message"].GetString(), "New Session") == 0;
  198. }
  199. return false;
  200. }
  201. bool serverWaitingForFile(const char* data) {
  202. Document d;
  203. d.Parse(data);
  204. if (d.HasMember("message")) {
  205. return strcmp(d["message"].GetString(), "File Ready") == 0;
  206. }
  207. return false;
  208. }
  209. std::string stringTupleVecToJson(
  210. std::vector<std::pair<std::string, std::string>> v) {
  211. json j{};
  212. for (const auto& row : v) {
  213. j[row.first] = row.second;
  214. }
  215. return j;
  216. }
  217. inline size_t findNullIndex(uint8_t* data) {
  218. size_t index = 0;
  219. while (data) {
  220. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  221. break;
  222. }
  223. index++;
  224. data++;
  225. }
  226. return index;
  227. }
  228. #endif // __UTIL_HPP__