util.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. namespace Kontainer {
  16. /** Reverse Iterator */
  17. template <class T>
  18. class ReverseIterator {
  19. T& _obj;
  20. public:
  21. ReverseIterator(T &obj) : _obj(obj) {}
  22. auto begin() {return _obj.rbegin();}
  23. auto end() {return _obj.rend();}
  24. };
  25. }
  26. namespace {
  27. using namespace rapidjson;
  28. using json = nlohmann::json;
  29. typedef std::string KOperation;
  30. typedef std::vector<std::pair<std::string, std::string>> TupVec;
  31. typedef std::vector<std::map<int, std::string>> MapVec;
  32. typedef std::vector<std::string> StdStringVec;
  33. typedef std::map<int, std::string> CommandMap;
  34. struct KSession {
  35. int id;
  36. int fd;
  37. int status;
  38. };
  39. std::string getJsonString(std::string s) {
  40. Document d;
  41. d.Parse(s.c_str());
  42. StringBuffer buffer;
  43. PrettyWriter<StringBuffer> writer(buffer);
  44. d.Accept(writer);
  45. return buffer.GetString();
  46. }
  47. std::string createMessage(const char* data, std::string args = "") {
  48. StringBuffer s;
  49. Writer<StringBuffer> w(s);
  50. w.StartObject();
  51. w.Key("type");
  52. w.String("custom");
  53. w.Key("message");
  54. w.String(data);
  55. w.Key("args");
  56. w.String(args.c_str());
  57. w.EndObject();
  58. return s.GetString();
  59. }
  60. bool isOperation(const char* data) {
  61. Document d;
  62. d.Parse(data);
  63. return strcmp(d["type"].GetString(), "operation") == 0;
  64. }
  65. bool isUploadCompleteEvent(const char* event) {
  66. return strcmp(event, "File Transfer Complete") == 0;
  67. }
  68. bool isEvent(const char* data) {
  69. Document d;
  70. d.Parse(data);
  71. return strcmp(d["type"].GetString(), "event") == 0;
  72. }
  73. // TODO: This should be "message", no?
  74. bool isMessage(const char* data) {
  75. Document d;
  76. d.Parse(data);
  77. if (d.HasMember("message")) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. std::string createOperation(const char* op, std::vector<std::string> args) {
  84. StringBuffer s;
  85. Writer<StringBuffer, Document::EncodingType, ASCII<>> w(s);
  86. w.StartObject();
  87. w.Key("type");
  88. w.String("operation");
  89. w.Key("command");
  90. w.String(op);
  91. w.Key("args");
  92. w.StartArray();
  93. if (!args.empty()) {
  94. for (const auto& arg : args) {
  95. w.String(arg.c_str());
  96. }
  97. }
  98. w.EndArray();
  99. w.EndObject();
  100. return s.GetString();
  101. }
  102. std::string getOperation(const char* data) {
  103. Document d;
  104. d.Parse(data);
  105. if (d.HasMember("command")) {
  106. return d["command"].GetString();
  107. }
  108. return "";
  109. }
  110. QString getEvent(const char* data) {
  111. Document d;
  112. d.Parse(data);
  113. if (d.HasMember("event")) {
  114. return d["event"].GetString();
  115. }
  116. return "";
  117. }
  118. QString getMessage(const char* data) {
  119. Document d;
  120. d.Parse(data);
  121. if (d.HasMember("message")) {
  122. return d["message"].GetString();
  123. }
  124. return "";
  125. }
  126. QVector<QString> getShortArgs(const char* data) {
  127. Document d;
  128. d.Parse(data);
  129. QVector<QString> args{};
  130. if (d.HasMember("args")) {
  131. if (d["args"].IsArray()) {
  132. for (const auto& m : d["args"].GetArray()) {
  133. if (m.GetStringLength() < 100) {
  134. args.push_back(m.GetString());
  135. }
  136. }
  137. } else {
  138. for (const auto& m : d["args"].GetObject()) {
  139. QString arg = m.name.GetString();
  140. arg += ": ";
  141. arg += m.value.GetString();
  142. args.push_back(arg);
  143. }
  144. }
  145. }
  146. return args;
  147. }
  148. QVector<QString> getArgs(const char* data) {
  149. Document d;
  150. d.Parse(data);
  151. QVector<QString> args{};
  152. if (d.HasMember("args")) {
  153. for (const auto& m : d["args"].GetArray()) {
  154. args.push_back(m.GetString());
  155. }
  156. }
  157. return args;
  158. }
  159. CommandMap getArgMap(const char* data) {
  160. Document d;
  161. d.Parse(data);
  162. CommandMap cm{};
  163. if (d.HasMember("args")) {
  164. for (const auto& m : d["args"].GetObject()) {
  165. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  166. }
  167. }
  168. return cm;
  169. }
  170. std::string createMessage(const char* data,
  171. std::map<int, std::string> map = {}) {
  172. StringBuffer s;
  173. Writer<StringBuffer> w(s);
  174. w.StartObject();
  175. w.Key("type");
  176. w.String("custom");
  177. w.Key("message");
  178. w.String(data);
  179. w.Key("args");
  180. w.StartObject();
  181. if (!map.empty()) {
  182. for (const auto& [k, v] : map) {
  183. w.Key(std::to_string(k).c_str());
  184. w.String(v.c_str());
  185. }
  186. }
  187. w.EndObject();
  188. w.EndObject();
  189. return s.GetString();
  190. }
  191. std::string createMessage(const char* data, std::map<int, std::vector<std::string>> map = {}) {
  192. StringBuffer s;
  193. Writer<StringBuffer> w(s);
  194. w.StartObject();
  195. w.Key("type");
  196. w.String("custom");
  197. w.Key("message");
  198. w.String(data);
  199. w.Key("args");
  200. w.StartObject();
  201. if (!map.empty()) {
  202. for (const auto& [k, v] : map) {
  203. w.Key(std::to_string(k).c_str());
  204. if (!v.empty()) {
  205. w.StartArray();
  206. for (const auto& arg : v) {
  207. w.String(arg.c_str());
  208. }
  209. w.EndArray();
  210. }
  211. }
  212. }
  213. w.EndObject();
  214. w.EndObject();
  215. return s.GetString();
  216. }
  217. std::string rapidCreateMessage(const char* data,
  218. std::map<int, std::string> map = {}) {
  219. StringBuffer s;
  220. Writer<StringBuffer> w(s);
  221. w.StartObject();
  222. w.Key("type");
  223. w.String("custom");
  224. w.Key("message");
  225. w.String(data);
  226. w.Key("args");
  227. w.StartObject();
  228. if (!map.empty()) {
  229. for (const auto& [k, v] : map) {
  230. w.Key(std::to_string(k).c_str());
  231. w.String(v.c_str());
  232. }
  233. }
  234. w.EndObject();
  235. w.EndObject();
  236. return s.GetString();
  237. }
  238. bool isStartOperation(const char* data) {
  239. Document d;
  240. d.Parse(data);
  241. return strcmp(d["command"].GetString(), "start") == 0;
  242. }
  243. bool isStopOperation(const char* data) {
  244. Document d;
  245. d.Parse(data);
  246. return strcmp(d["command"].GetString(), "stop") == 0;
  247. }
  248. bool isNewSession(const char* data) {
  249. Document d;
  250. d.Parse(data);
  251. if (d.HasMember("message")) {
  252. return strcmp(d["message"].GetString(), "New Session") == 0;
  253. }
  254. return false;
  255. }
  256. bool serverWaitingForFile(const char* data) {
  257. Document d;
  258. d.Parse(data);
  259. if (d.HasMember("message")) {
  260. return strcmp(d["message"].GetString(), "File Ready") == 0;
  261. }
  262. return false;
  263. }
  264. std::string stringTupleVecToJson(
  265. std::vector<std::pair<std::string, std::string>> v) {
  266. json j{};
  267. for (const auto& row : v) {
  268. j[row.first] = row.second;
  269. }
  270. return j;
  271. }
  272. inline size_t findNullIndex(uint8_t* data) {
  273. size_t index = 0;
  274. while (data) {
  275. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  276. break;
  277. }
  278. index++;
  279. data++;
  280. }
  281. return index;
  282. }
  283. }
  284. #endif // __UTIL_HPP__