configparser.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "configparser.h"
  2. #include "loghelper.h"
  3. ConfigParser::ConfigParser()
  4. {
  5. this->current_app_version = 0;
  6. this->current_seg_version = 0;
  7. }
  8. bool ConfigParser::readConfig(QString content)
  9. {
  10. QString errorStr;
  11. int errorLine;
  12. int errorColumn;
  13. QDomDocument doc;
  14. if(!doc.setContent(content,false,&errorStr,&errorLine,&errorColumn))
  15. {
  16. qDebug()<<errorStr<<errorLine<<errorColumn;
  17. LogHelper::writeLog(errorStr + " " +\
  18. QString::number(errorLine) + " " +QString::number(errorColumn));
  19. return false;
  20. }
  21. QDomElement root = doc.documentElement();
  22. if (root.tagName()!="SpeedNumCube")
  23. {
  24. qDebug()<<root.tagName();
  25. return false;
  26. }
  27. parseConfigElement(root);
  28. return true;
  29. }
  30. void ConfigParser::parseConfigElement(const QDomElement &element)
  31. {
  32. QDomNode child = element.firstChild();
  33. while (!child.isNull())
  34. {
  35. if(child.toElement().tagName()=="config_urls")
  36. {
  37. parseConfigUrlsElement(child.toElement());
  38. }
  39. if(child.toElement().tagName()=="app_ver_map")
  40. {
  41. parseAppMapElement(child.toElement());
  42. }
  43. if(child.toElement().tagName()=="seg_ver_map")
  44. {
  45. parseSegMapElement(child.toElement());
  46. }
  47. if(child.toElement().tagName()=="banner_ad")
  48. {
  49. banner_ad= child.toElement().text();
  50. }
  51. if(child.toElement().tagName()=="text_ads")
  52. {
  53. parseAdElement(child.toElement());
  54. }
  55. if(child.toElement().tagName()=="corp_prefix")
  56. {
  57. parseCorpPrefixElement(child.toElement());
  58. }
  59. if(child.toElement().tagName()=="client_config")
  60. {
  61. parseClientConfigElement(child.toElement());
  62. }
  63. if(child.toElement().tagName()=="app")
  64. {
  65. parseAppElement(child.toElement());
  66. }
  67. if(child.toElement().tagName()=="seg")
  68. {
  69. parseSegElement(child.toElement());
  70. }
  71. child = child.nextSibling();
  72. }
  73. }
  74. void ConfigParser::parseAppMapElement(const QDomElement &element)
  75. {
  76. QDomNode child = element.firstChild();
  77. while (!child.isNull())
  78. {
  79. int sn = child.toElement().attribute("sn").toInt();
  80. QString ver = child.toElement().text();
  81. app_map.insert(ver,sn);
  82. child = child.nextSibling();
  83. }
  84. }
  85. void ConfigParser::parseSegMapElement(const QDomElement &element)
  86. {
  87. QDomNode child = element.firstChild();
  88. while (!child.isNull())
  89. {
  90. int sn = child.toElement().attribute("sn").toInt();
  91. QString ver = child.toElement().text();
  92. seg_map.insert(ver,sn);
  93. child = child.nextSibling();
  94. }
  95. }
  96. void ConfigParser::parseConfigUrlsElement(const QDomElement &element)
  97. {
  98. QDomNode child = element.firstChild();
  99. while (!child.isNull())
  100. {
  101. config_urls << child.toElement().text();
  102. child = child.nextSibling();
  103. }
  104. }
  105. void ConfigParser::parseAppElement(const QDomElement &element)
  106. {
  107. QDomNode child = element.firstChild();
  108. while (!child.isNull())
  109. {
  110. if(child.toElement().tagName()=="current_app_version")
  111. {
  112. current_app_version= child.toElement().text().toInt();
  113. this->update = child.toElement().attribute("update");
  114. }
  115. if(child.toElement().tagName()=="patch_urls")
  116. {
  117. parseAppPatchUrlElement(child.toElement());
  118. }
  119. if(child.toElement().tagName()=="update_logs")
  120. {
  121. parseAppUpdateLogElement(child.toElement());
  122. }
  123. child = child.nextSibling();
  124. }
  125. }
  126. void ConfigParser::parseSegElement(const QDomElement &element)
  127. {
  128. QDomNode child = element.firstChild();
  129. while (!child.isNull())
  130. {
  131. if(child.toElement().tagName()=="current_seg_version")
  132. {
  133. current_seg_version= child.toElement().text().toInt();
  134. }
  135. if(child.toElement().tagName()=="patch_urls")
  136. {
  137. parseSegPatchUrlElement(child.toElement());
  138. }
  139. if(child.toElement().tagName()=="update_logs")
  140. {
  141. parseSegUpdateLogElement(child.toElement());
  142. }
  143. child = child.nextSibling();
  144. }
  145. }
  146. void ConfigParser::parseAppPatchUrlElement(const QDomElement &element)
  147. {
  148. QDomNode child = element.firstChild();
  149. while (!child.isNull())
  150. {
  151. int sn = child.toElement().attribute("sn").toInt();
  152. QString url = child.toElement().text();
  153. app_patch_urls.insert(sn,url);
  154. child = child.nextSibling();
  155. }
  156. }
  157. void ConfigParser::parseAppUpdateLogElement(const QDomElement &element)
  158. {
  159. QDomNode child = element.firstChild();
  160. while (!child.isNull())
  161. {
  162. int sn = child.toElement().attribute("sn").toInt();
  163. QString url = child.toElement().text();
  164. app_update_logs.insert(sn,url);
  165. child = child.nextSibling();
  166. }
  167. }
  168. void ConfigParser::parseSegPatchUrlElement(const QDomElement &element)
  169. {
  170. QDomNode child = element.firstChild();
  171. while (!child.isNull())
  172. {
  173. int sn = child.toElement().attribute("sn").toInt();
  174. QString url = child.toElement().text();
  175. seg_patch_urls.insert(sn,url);
  176. child = child.nextSibling();
  177. }
  178. }
  179. void ConfigParser::parseSegUpdateLogElement(const QDomElement &element)
  180. {
  181. QDomNode child = element.firstChild();
  182. while (!child.isNull())
  183. {
  184. int sn = child.toElement().attribute("sn").toInt();
  185. QString url = child.toElement().text();
  186. seg_update_logs.insert(sn,url);
  187. child = child.nextSibling();
  188. }
  189. }
  190. void ConfigParser::parseAdElement(const QDomElement &element)
  191. {
  192. QDomNode child = element.firstChild();
  193. while (!child.isNull())
  194. {
  195. QString url = child.toElement().attribute("link");
  196. QString content = child.toElement().text();
  197. text_ads.insert(url,content);
  198. child = child.nextSibling();
  199. }
  200. }
  201. void ConfigParser::parseCorpPrefixElement(const QDomElement &element)
  202. {
  203. QDomNode child = element.firstChild();
  204. while (!child.isNull())
  205. {
  206. QString corp = child.toElement().attribute("corp");
  207. QString content = child.toElement().text();
  208. corp_prefix.insert(corp,content);
  209. child = child.nextSibling();
  210. }
  211. }
  212. void ConfigParser::parseClientConfigElement(const QDomElement &element)
  213. {
  214. QDomNode child = element.firstChild();
  215. while (!child.isNull())
  216. {
  217. QStringList list;
  218. QString name = child.toElement().attribute("name");
  219. QString ws_url = child.toElement().attribute("ws_url");
  220. QString notice_url = child.toElement().attribute("notice_url");
  221. list<<name<<ws_url<<notice_url;
  222. QString content = child.toElement().text();
  223. client_config.insert(content,list);
  224. child = child.nextSibling();
  225. }
  226. }