ValueBaseStructParserStateMachine.cc 5.9 KB

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