ValueBaseStructParserStateMachine.cc 6.0 KB

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