util.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 isUploadCompleteEvent(const char* event) {
  54. return strcmp(event, "File Transfer Complete") == 0;
  55. }
  56. bool isEvent(const char* data) {
  57. Document d;
  58. d.Parse(data);
  59. return strcmp(d["type"].GetString(), "event") == 0;
  60. }
  61. std::string createOperation(const char* op, std::vector<std::string> args) {
  62. StringBuffer s;
  63. Writer<StringBuffer, Document::EncodingType, ASCII<>> w(s);
  64. w.StartObject();
  65. w.Key("type");
  66. w.String("operation");
  67. w.Key("command");
  68. w.String(op);
  69. w.Key("args");
  70. w.StartArray();
  71. if (!args.empty()) {
  72. for (const auto& arg : args) {
  73. w.String(arg.c_str());
  74. }
  75. }
  76. w.EndArray();
  77. w.EndObject();
  78. return s.GetString();
  79. }
  80. std::string getOperation(const char* data) {
  81. Document d;
  82. d.Parse(data);
  83. if (d.HasMember("command")) {
  84. return d["command"].GetString();
  85. }
  86. return "";
  87. }
  88. QString getEvent(const char* data) {
  89. Document d;
  90. d.Parse(data);
  91. if (d.HasMember("event")) {
  92. return d["event"].GetString();
  93. }
  94. return "";
  95. }
  96. QVector<QString> getArgs(const char* data) {
  97. Document d;
  98. d.Parse(data);
  99. QVector<QString> args{};
  100. if (d.HasMember("args")) {
  101. for (const auto& m : d["args"].GetArray()) {
  102. args.push_back(m.GetString());
  103. }
  104. }
  105. return args;
  106. }
  107. CommandMap getArgMap(const char* data) {
  108. Document d;
  109. d.Parse(data);
  110. CommandMap cm{};
  111. if (d.HasMember("args")) {
  112. for (const auto& m : d["args"].GetObject()) {
  113. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  114. }
  115. }
  116. return cm;
  117. }
  118. std::string createMessage(const char* data,
  119. std::map<int, std::string> map = {}) {
  120. StringBuffer s;
  121. Writer<StringBuffer> w(s);
  122. w.StartObject();
  123. w.Key("type");
  124. w.String("custom");
  125. w.Key("message");
  126. w.String(data);
  127. w.Key("args");
  128. w.StartObject();
  129. if (!map.empty()) {
  130. for (const auto& [k, v] : map) {
  131. w.Key(std::to_string(k).c_str());
  132. w.String(v.c_str());
  133. }
  134. }
  135. w.EndObject();
  136. w.EndObject();
  137. return s.GetString();
  138. }
  139. std::string createMessage(const char* data, std::map<int, std::vector<std::string>> map = {}) {
  140. StringBuffer s;
  141. Writer<StringBuffer> w(s);
  142. w.StartObject();
  143. w.Key("type");
  144. w.String("custom");
  145. w.Key("message");
  146. w.String(data);
  147. w.Key("args");
  148. w.StartObject();
  149. if (!map.empty()) {
  150. for (const auto& [k, v] : map) {
  151. w.Key(std::to_string(k).c_str());
  152. if (!v.empty()) {
  153. w.StartArray();
  154. for (const auto& arg : v) {
  155. w.String(arg.c_str());
  156. }
  157. w.EndArray();
  158. }
  159. }
  160. }
  161. w.EndObject();
  162. w.EndObject();
  163. return s.GetString();
  164. }
  165. std::string rapidCreateMessage(const char* data,
  166. std::map<int, std::string> map = {}) {
  167. StringBuffer s;
  168. Writer<StringBuffer> w(s);
  169. w.StartObject();
  170. w.Key("type");
  171. w.String("custom");
  172. w.Key("message");
  173. w.String(data);
  174. w.Key("args");
  175. w.StartObject();
  176. if (!map.empty()) {
  177. for (const auto& [k, v] : map) {
  178. w.Key(std::to_string(k).c_str());
  179. w.String(v.c_str());
  180. }
  181. }
  182. w.EndObject();
  183. w.EndObject();
  184. return s.GetString();
  185. }
  186. bool isStartOperation(const char* data) {
  187. Document d;
  188. d.Parse(data);
  189. return strcmp(d["command"].GetString(), "start") == 0;
  190. }
  191. bool isStopOperation(const char* data) {
  192. Document d;
  193. d.Parse(data);
  194. return strcmp(d["command"].GetString(), "stop") == 0;
  195. }
  196. bool isNewSession(const char* data) {
  197. Document d;
  198. d.Parse(data);
  199. if (d.HasMember("message")) {
  200. return strcmp(d["message"].GetString(), "New Session") == 0;
  201. }
  202. return false;
  203. }
  204. bool serverWaitingForFile(const char* data) {
  205. Document d;
  206. d.Parse(data);
  207. if (d.HasMember("message")) {
  208. return strcmp(d["message"].GetString(), "File Ready") == 0;
  209. }
  210. return false;
  211. }
  212. std::string stringTupleVecToJson(
  213. std::vector<std::pair<std::string, std::string>> v) {
  214. json j{};
  215. for (const auto& row : v) {
  216. j[row.first] = row.second;
  217. }
  218. return j;
  219. }
  220. inline size_t findNullIndex(uint8_t* data) {
  221. size_t index = 0;
  222. while (data) {
  223. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  224. break;
  225. }
  226. index++;
  227. data++;
  228. }
  229. return index;
  230. }
  231. #endif // __UTIL_HPP__