ValueBaseStructParserStateMachine.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 = new ValueValueBaseStructParserState();
  43. auto dictState = new DictValueBaseStructParserState();
  44. auto dictKeyState = new DictKeyValueBaseStructParserState();
  45. auto dictDataState = new DictDataValueBaseStructParserState();
  46. auto arrayState = new ArrayValueBaseStructParserState();
  47. auto arrayDataState = new ArrayDataValueBaseStructParserState();
  48. auto stringState = new StringValueBaseStructParserState();
  49. auto numberState = new NumberValueBaseStructParserState();
  50. auto boolState = new BoolValueBaseStructParserState();
  51. auto nullState = new NullValueBaseStructParserState();
  52. } // namespace
  53. std::unique_ptr<ValueBase> ValueBaseStructParserStateMachine::noResult()
  54. {
  55. return nullptr;
  56. }
  57. ValueBaseStructParserStateMachine::ValueBaseStructParserStateMachine()
  58. : ctrl_{make_unique<rpc::XmlRpcRequestParserController>()}
  59. {
  60. stateStack_.push(valueState);
  61. }
  62. ValueBaseStructParserStateMachine::~ValueBaseStructParserStateMachine() =
  63. default;
  64. void ValueBaseStructParserStateMachine::reset()
  65. {
  66. while (!stateStack_.empty()) {
  67. stateStack_.pop();
  68. }
  69. stateStack_.push(valueState);
  70. ctrl_->reset();
  71. }
  72. void ValueBaseStructParserStateMachine::beginElement(int elementType)
  73. {
  74. stateStack_.top()->beginElement(this, elementType);
  75. }
  76. void ValueBaseStructParserStateMachine::endElement(int elementType)
  77. {
  78. stateStack_.top()->endElement(this, elementType);
  79. stateStack_.pop();
  80. }
  81. std::unique_ptr<ValueBase> ValueBaseStructParserStateMachine::getResult()
  82. {
  83. return popCurrentFrameValue();
  84. }
  85. void ValueBaseStructParserStateMachine::charactersCallback(const char* data,
  86. size_t len)
  87. {
  88. sessionData_.str.append(data, len);
  89. }
  90. void ValueBaseStructParserStateMachine::numberCallback(int64_t number, int frac,
  91. int exp)
  92. {
  93. sessionData_.number.number = number;
  94. sessionData_.number.frac = frac;
  95. sessionData_.number.exp = exp;
  96. }
  97. void ValueBaseStructParserStateMachine::boolCallback(bool bval)
  98. {
  99. sessionData_.bval = bval;
  100. }
  101. const std::string& ValueBaseStructParserStateMachine::getCharacters() const
  102. {
  103. return sessionData_.str;
  104. }
  105. const ValueBaseStructParserStateMachine::NumberData&
  106. ValueBaseStructParserStateMachine::getNumber() const
  107. {
  108. return sessionData_.number;
  109. }
  110. bool ValueBaseStructParserStateMachine::getBool() const
  111. {
  112. return sessionData_.bval;
  113. }
  114. void ValueBaseStructParserStateMachine::popArrayFrame()
  115. {
  116. ctrl_->popArrayFrame();
  117. }
  118. void ValueBaseStructParserStateMachine::popDictFrame()
  119. {
  120. ctrl_->popStructFrame();
  121. }
  122. void ValueBaseStructParserStateMachine::pushFrame() { ctrl_->pushFrame(); }
  123. void ValueBaseStructParserStateMachine::setCurrentFrameValue(
  124. std::unique_ptr<ValueBase> value)
  125. {
  126. ctrl_->setCurrentFrameValue(std::move(value));
  127. }
  128. const std::unique_ptr<ValueBase>&
  129. ValueBaseStructParserStateMachine::getCurrentFrameValue() const
  130. {
  131. return ctrl_->getCurrentFrameValue();
  132. }
  133. std::unique_ptr<ValueBase>
  134. ValueBaseStructParserStateMachine::popCurrentFrameValue()
  135. {
  136. return ctrl_->popCurrentFrameValue();
  137. }
  138. void ValueBaseStructParserStateMachine::setCurrentFrameName(std::string name)
  139. {
  140. ctrl_->setCurrentFrameName(std::move(name));
  141. }
  142. void ValueBaseStructParserStateMachine::pushDictState()
  143. {
  144. stateStack_.push(dictState);
  145. }
  146. void ValueBaseStructParserStateMachine::pushDictKeyState()
  147. {
  148. sessionData_.str.clear();
  149. stateStack_.push(dictKeyState);
  150. }
  151. void ValueBaseStructParserStateMachine::pushDictDataState()
  152. {
  153. stateStack_.push(dictDataState);
  154. }
  155. void ValueBaseStructParserStateMachine::pushArrayState()
  156. {
  157. stateStack_.push(arrayState);
  158. }
  159. void ValueBaseStructParserStateMachine::pushArrayDataState()
  160. {
  161. stateStack_.push(arrayDataState);
  162. }
  163. void ValueBaseStructParserStateMachine::pushStringState()
  164. {
  165. sessionData_.str.clear();
  166. stateStack_.push(stringState);
  167. }
  168. void ValueBaseStructParserStateMachine::pushNumberState()
  169. {
  170. memset(&sessionData_.number, 0, sizeof(sessionData_.number));
  171. stateStack_.push(numberState);
  172. }
  173. void ValueBaseStructParserStateMachine::pushBoolState()
  174. {
  175. sessionData_.bval = false;
  176. stateStack_.push(boolState);
  177. }
  178. void ValueBaseStructParserStateMachine::pushNullState()
  179. {
  180. stateStack_.push(nullState);
  181. }
  182. void ValueBaseStructParserStateMachine::setAllowEmptyMemberName(bool b)
  183. {
  184. ctrl_->setAllowEmptyMemberName(b);
  185. }
  186. } // namespace aria2