descriptor.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_DESCRIPTOR_H__
  14. #define __RPC_DESCRIPTOR_H__
  15. #include <vector>
  16. #include <string>
  17. #include <set>
  18. struct rpc_param
  19. {
  20. std::string type_name;
  21. std::string var_name;
  22. std::string default_value;
  23. int16_t field_id;
  24. int8_t data_type;
  25. int8_t required_state;
  26. };
  27. struct rpc_descriptor
  28. {
  29. std::string method_name;
  30. std::string request_name;
  31. std::string response_name;
  32. std::vector<rpc_param> req_params;
  33. std::vector<rpc_param> resp_params;
  34. };
  35. struct struct_descriptor
  36. {
  37. std::vector<rpc_param> params;
  38. };
  39. struct typedef_descriptor
  40. {
  41. std::string old_type_name;
  42. std::string new_type_name;
  43. };
  44. struct Descriptor
  45. {
  46. std::string block_name;
  47. std::string block_type;
  48. //service extends
  49. std::string extends_type;
  50. // serivce
  51. std::vector<rpc_descriptor> rpcs;
  52. // struct
  53. struct_descriptor st;
  54. // enum
  55. std::vector<std::string> enum_lines;
  56. };
  57. //class ServiceDescrpitor : public Descriptor
  58. //{};
  59. struct idl_info
  60. {
  61. std::string file_name;
  62. std::string input_dir;
  63. std::string absolute_file_path;
  64. std::string file_name_prefix;
  65. std::vector<std::string> package_name;
  66. std::list<Descriptor> desc_list;
  67. std::list<idl_info> include_list;
  68. std::list<typedef_descriptor> typedef_list;
  69. //typedef name -> real cpptype
  70. std::map<std::string,std::string> typedef_mapping;
  71. };
  72. class SGenUtil
  73. {
  74. public:
  75. static const char *skip_string(const char *cursor)
  76. {
  77. while (*cursor && *cursor != '\"')
  78. {
  79. if (*cursor == '\\')
  80. {
  81. cursor++;
  82. if (!*cursor)
  83. break;
  84. }
  85. cursor++;
  86. }
  87. return cursor;
  88. }
  89. static std::vector<std::string> split_skip_string(const std::string& str,
  90. char sep)
  91. {
  92. std::vector<std::string> res;
  93. /* Can not use '\"' as the seperator. */
  94. if (sep == '\"')
  95. return res;
  96. const char *cursor = str.c_str();
  97. const char *start = cursor;
  98. while (*cursor)
  99. {
  100. if (*cursor == '\"')
  101. {
  102. cursor = SGenUtil::skip_string(++cursor);
  103. if (!*cursor)
  104. break;
  105. }
  106. else if (*cursor == sep)
  107. {
  108. if (start < cursor)
  109. res.emplace_back(start, cursor);
  110. start = cursor + 1;
  111. }
  112. cursor++;
  113. }
  114. if (start < cursor)
  115. res.emplace_back(start, cursor);
  116. return res;
  117. }
  118. /*
  119. static std::vector<std::string> split_filter_empty(const std::string& str,
  120. char sep)
  121. {
  122. std::vector<std::string> res;
  123. std::string::const_iterator start = str.begin();
  124. std::string::const_iterator end = str.end();
  125. std::string::const_iterator next = find(start, end, sep);
  126. while (next != end)
  127. {
  128. if (start < next)
  129. res.emplace_back(start, next);
  130. start = next + 1;
  131. next = find(start, end, sep);
  132. }
  133. if (start < next)
  134. res.emplace_back(start, next);
  135. return res;
  136. }
  137. */
  138. static std::vector<std::string> split_by_space(const std::string& str)
  139. {
  140. std::vector<std::string> elems;
  141. size_t offset = 0;
  142. size_t start = 0;
  143. size_t end = 0;
  144. while(offset < str.size())
  145. {
  146. start = SGenUtil::find_next_nonspace(str,offset);
  147. if (start == std::string::npos)
  148. break;
  149. end = SGenUtil::find_next_space(str,start+1);
  150. if (end == std::string::npos)
  151. {
  152. elems.push_back(str.substr(start));
  153. break;
  154. }
  155. elems.push_back(str.substr(start,end-start));
  156. offset = end+1;
  157. }
  158. return elems;
  159. }
  160. static std::string strip(const std::string& str)
  161. {
  162. std::string res;
  163. if (!str.empty())
  164. {
  165. const char *st = str.c_str();
  166. const char *ed = st + str.size() - 1;
  167. while (st <= ed)
  168. {
  169. if (!isspace(*st))
  170. break;
  171. st++;
  172. }
  173. while (ed >= st)
  174. {
  175. if (!isspace(*ed))
  176. break;
  177. ed--;
  178. }
  179. if (ed >= st)
  180. res.assign(st, ed - st + 1);
  181. }
  182. return res;
  183. }
  184. static std::string lstrip(const std::string& str)
  185. {
  186. std::string res;
  187. if (!str.empty())
  188. {
  189. const char *st = str.c_str();
  190. const char *ed = st + str.size();
  191. while (st < ed)
  192. {
  193. if (!isspace(*st))
  194. break;
  195. st++;
  196. }
  197. if (st < ed)
  198. res.assign(st, ed - st);
  199. }
  200. return res;
  201. }
  202. static bool start_with(const std::string &str, const std::string prefix)
  203. {
  204. size_t prefix_len = prefix.size();
  205. if (str.size() < prefix_len)
  206. return false;
  207. for (size_t i = 0; i < prefix_len; i++)
  208. {
  209. if (str[i] != prefix[i])
  210. return false;
  211. }
  212. return true;
  213. }
  214. static void replace(std::string& str, const std::string& before,
  215. const std::string& after)
  216. {
  217. for (size_t pos = 0; pos < str.size(); pos += after.length())
  218. {
  219. pos = str.find(before, pos);
  220. if (pos != std::string::npos)
  221. str.replace(pos, before.length(), after);
  222. else
  223. break;
  224. }
  225. }
  226. static size_t find_next_nonspace(const std::string& str, size_t pos)
  227. {
  228. for(size_t i = pos; i < str.size(); i++)
  229. {
  230. if (!std::isspace(str[i]))
  231. return i;
  232. }
  233. return std::string::npos;
  234. }
  235. static size_t find_next_space(const std::string& str, size_t pos)
  236. {
  237. for(size_t i = pos; i < str.size(); i++)
  238. {
  239. if (std::isspace(str[i]))
  240. return i;
  241. }
  242. return std::string::npos;
  243. }
  244. static std::set<std::string> *get_enum_set()
  245. {
  246. static std::set<std::string> kInstance;
  247. return &kInstance;
  248. }
  249. };
  250. #endif