ValueBaseStructParserStateMachine.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2012 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "ValueBaseStructParserStateMachine.h"
  36. #include <cstring>
  37. #include "XmlRpcRequestParserController.h"
  38. #include "ValueBaseStructParserStateImpl.h"
  39. #include "ValueBase.h"
  40. namespace aria2 {
  41. namespace {
  42. auto valueState =
  43. new ValueValueBaseStructParserState();
  44. auto dictState =
  45. new DictValueBaseStructParserState();
  46. auto dictKeyState =
  47. new DictKeyValueBaseStructParserState();
  48. auto dictDataState =
  49. new DictDataValueBaseStructParserState();
  50. auto arrayState =
  51. new ArrayValueBaseStructParserState();
  52. auto arrayDataState =
  53. new ArrayDataValueBaseStructParserState();
  54. auto stringState =
  55. new StringValueBaseStructParserState();
  56. auto numberState =
  57. new NumberValueBaseStructParserState();
  58. auto boolState =
  59. new BoolValueBaseStructParserState();
  60. auto nullState =
  61. new NullValueBaseStructParserState();
  62. } // namespace
  63. std::unique_ptr<ValueBase>
  64. ValueBaseStructParserStateMachine::noResult()
  65. {
  66. return nullptr;
  67. }
  68. ValueBaseStructParserStateMachine::ValueBaseStructParserStateMachine()
  69. : ctrl_{make_unique<rpc::XmlRpcRequestParserController>()}
  70. {
  71. stateStack_.push(valueState);
  72. }
  73. ValueBaseStructParserStateMachine::~ValueBaseStructParserStateMachine()
  74. {}
  75. void ValueBaseStructParserStateMachine::reset()
  76. {
  77. while(!stateStack_.empty()) {
  78. stateStack_.pop();
  79. }
  80. stateStack_.push(valueState);
  81. ctrl_->reset();
  82. }
  83. void ValueBaseStructParserStateMachine::beginElement(int elementType)
  84. {
  85. stateStack_.top()->beginElement(this, elementType);
  86. }
  87. void ValueBaseStructParserStateMachine::endElement(int elementType)
  88. {
  89. stateStack_.top()->endElement(this, elementType);
  90. stateStack_.pop();
  91. }
  92. std::unique_ptr<ValueBase> ValueBaseStructParserStateMachine::getResult()
  93. {
  94. return popCurrentFrameValue();
  95. }
  96. void ValueBaseStructParserStateMachine::charactersCallback
  97. (const char* data, size_t len)
  98. {
  99. sessionData_.str.append(data, len);
  100. }
  101. void ValueBaseStructParserStateMachine::numberCallback
  102. (int64_t number, int frac, int exp)
  103. {
  104. sessionData_.number.number = number;
  105. sessionData_.number.frac = frac;
  106. sessionData_.number.exp = exp;
  107. }
  108. void ValueBaseStructParserStateMachine::boolCallback(bool bval)
  109. {
  110. sessionData_.bval = bval;
  111. }
  112. const std::string& ValueBaseStructParserStateMachine::getCharacters() const
  113. {
  114. return sessionData_.str;
  115. }
  116. const ValueBaseStructParserStateMachine::NumberData&
  117. ValueBaseStructParserStateMachine::getNumber() const
  118. {
  119. return sessionData_.number;
  120. }
  121. bool ValueBaseStructParserStateMachine::getBool() const
  122. {
  123. return sessionData_.bval;
  124. }
  125. void ValueBaseStructParserStateMachine::popArrayFrame()
  126. {
  127. ctrl_->popArrayFrame();
  128. }
  129. void ValueBaseStructParserStateMachine::popDictFrame()
  130. {
  131. ctrl_->popStructFrame();
  132. }
  133. void ValueBaseStructParserStateMachine::pushFrame()
  134. {
  135. ctrl_->pushFrame();
  136. }
  137. void ValueBaseStructParserStateMachine::setCurrentFrameValue
  138. (std::unique_ptr<ValueBase> value)
  139. {
  140. ctrl_->setCurrentFrameValue(std::move(value));
  141. }
  142. const std::unique_ptr<ValueBase>&
  143. ValueBaseStructParserStateMachine::getCurrentFrameValue() const
  144. {
  145. return ctrl_->getCurrentFrameValue();
  146. }
  147. std::unique_ptr<ValueBase>
  148. ValueBaseStructParserStateMachine::popCurrentFrameValue()
  149. {
  150. return ctrl_->popCurrentFrameValue();
  151. }
  152. void ValueBaseStructParserStateMachine::setCurrentFrameName(std::string name)
  153. {
  154. ctrl_->setCurrentFrameName(std::move(name));
  155. }
  156. void ValueBaseStructParserStateMachine::pushDictState()
  157. {
  158. stateStack_.push(dictState);
  159. }
  160. void ValueBaseStructParserStateMachine::pushDictKeyState()
  161. {
  162. sessionData_.str.clear();
  163. stateStack_.push(dictKeyState);
  164. }
  165. void ValueBaseStructParserStateMachine::pushDictDataState()
  166. {
  167. stateStack_.push(dictDataState);
  168. }
  169. void ValueBaseStructParserStateMachine::pushArrayState()
  170. {
  171. stateStack_.push(arrayState);
  172. }
  173. void ValueBaseStructParserStateMachine::pushArrayDataState()
  174. {
  175. stateStack_.push(arrayDataState);
  176. }
  177. void ValueBaseStructParserStateMachine::pushStringState()
  178. {
  179. sessionData_.str.clear();
  180. stateStack_.push(stringState);
  181. }
  182. void ValueBaseStructParserStateMachine::pushNumberState()
  183. {
  184. memset(&sessionData_.number, 0, sizeof(sessionData_.number));
  185. stateStack_.push(numberState);
  186. }
  187. void ValueBaseStructParserStateMachine::pushBoolState()
  188. {
  189. sessionData_.bval = false;
  190. stateStack_.push(boolState);
  191. }
  192. void ValueBaseStructParserStateMachine::pushNullState()
  193. {
  194. stateStack_.push(nullState);
  195. }
  196. } // namespace aria2