CCDataVisitor.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "base/CCRef.h"
  21. #include "deprecated/CCBool.h"
  22. #include "deprecated/CCInteger.h"
  23. #include "deprecated/CCFloat.h"
  24. #include "deprecated/CCDouble.h"
  25. #include "deprecated/CCString.h"
  26. #include "deprecated/CCArray.h"
  27. #include "deprecated/CCDictionary.h"
  28. #include "deprecated/CCSet.h"
  29. NS_CC_BEGIN
  30. void DataVisitor::visit(const __Bool *value)
  31. {
  32. visitObject(value);
  33. }
  34. void DataVisitor::visit(const __Integer *value)
  35. {
  36. visitObject(value);
  37. }
  38. void DataVisitor::visit(const __Float *value)
  39. {
  40. visitObject(value);
  41. }
  42. void DataVisitor::visit(const __Double *value)
  43. {
  44. visitObject(value);
  45. }
  46. void DataVisitor::visit(const __String *value)
  47. {
  48. visitObject(value);
  49. }
  50. void DataVisitor::visit(const __Array *value)
  51. {
  52. visitObject(value);
  53. }
  54. void DataVisitor::visit(const __Dictionary *value)
  55. {
  56. visitObject(value);
  57. }
  58. void DataVisitor::visit(const __Set *value)
  59. {
  60. visitObject(value);
  61. }
  62. // PrettyPrinter
  63. PrettyPrinter::PrettyPrinter(int indentLevel/* = 0 */)
  64. {
  65. setIndentLevel(indentLevel);
  66. }
  67. void PrettyPrinter::clear()
  68. {
  69. _result.clear();
  70. }
  71. std::string PrettyPrinter::getResult()
  72. {
  73. return _result;
  74. }
  75. void PrettyPrinter::visitObject(const Ref *p)
  76. {
  77. char buf[50] = {0};
  78. sprintf(buf, "%p", p);
  79. _result += buf;
  80. }
  81. void PrettyPrinter::visit(const __Bool * p)
  82. {
  83. char buf[50] = {0};
  84. sprintf(buf, "%s", p->getValue() ? "true" : "false");
  85. _result += buf;
  86. }
  87. void PrettyPrinter::visit(const __Integer *p)
  88. {
  89. char buf[50] = {0};
  90. sprintf(buf, "%d", p->getValue());
  91. _result += buf;
  92. }
  93. void PrettyPrinter::visit(const __Float *p)
  94. {
  95. char buf[50] = {0};
  96. sprintf(buf, "%f", p->getValue());
  97. _result += buf;
  98. }
  99. void PrettyPrinter::visit(const __Double *p)
  100. {
  101. char buf[50] = {0};
  102. sprintf(buf, "%lf", p->getValue());
  103. _result += buf;
  104. }
  105. void PrettyPrinter::visit(const __String *p)
  106. {
  107. _result += p->getCString();
  108. }
  109. void PrettyPrinter::visit(const __Array *p)
  110. {
  111. _result += "\n";
  112. _result += _indentStr;
  113. _result += "<array>\n";
  114. setIndentLevel(_indentLevel+1);
  115. Ref* obj;
  116. int i = 0;
  117. char buf[50] = {0};
  118. CCARRAY_FOREACH(p, obj)
  119. {
  120. if (i > 0) {
  121. _result += "\n";
  122. }
  123. sprintf(buf, "%s%02d: ", _indentStr.c_str(), i);
  124. _result += buf;
  125. PrettyPrinter v(_indentLevel);
  126. //FIXME:james obj->acceptVisitor(v);
  127. _result += v.getResult();
  128. i++;
  129. }
  130. setIndentLevel(_indentLevel-1);
  131. _result += "\n";
  132. _result += _indentStr;
  133. _result += "</array>";
  134. }
  135. void PrettyPrinter::visit(const __Dictionary *p)
  136. {
  137. _result += "\n";
  138. _result += _indentStr;
  139. _result += "<dict>\n";
  140. setIndentLevel(_indentLevel+1);
  141. DictElement* element;
  142. bool bFirstElement = true;
  143. char buf[1000] = {0};
  144. CCDICT_FOREACH(p, element)
  145. {
  146. if (!bFirstElement) {
  147. _result += "\n";
  148. }
  149. sprintf(buf, "%s%s: ", _indentStr.c_str(),element->getStrKey());
  150. _result += buf;
  151. PrettyPrinter v(_indentLevel);
  152. //FIXME:james element->getObject()->acceptVisitor(v);
  153. _result += v.getResult();
  154. bFirstElement = false;
  155. }
  156. setIndentLevel(_indentLevel-1);
  157. _result += "\n";
  158. _result += _indentStr;
  159. _result += "</dict>";
  160. }
  161. void PrettyPrinter::visit(const __Set *p)
  162. {
  163. _result += "\n";
  164. _result += _indentStr;
  165. _result += "<set>\n";
  166. setIndentLevel(_indentLevel+1);
  167. __Set* tmp = const_cast<__Set*>(p);
  168. for (int i = 0, tmp_size = tmp->count(); i < tmp_size; ++i) {
  169. if (i > 0) {
  170. _result += "\n";
  171. }
  172. _result += _indentStr.c_str();
  173. PrettyPrinter v(_indentLevel);
  174. //FIXME:james (*it)->acceptVisitor(v);
  175. _result += v.getResult();
  176. }
  177. setIndentLevel(_indentLevel-1);
  178. _result += "\n";
  179. _result += _indentStr;
  180. _result += "</set>\n";
  181. }
  182. void PrettyPrinter::setIndentLevel(int indentLevel)
  183. {
  184. _indentLevel = indentLevel;
  185. _indentStr.clear();
  186. for (int i = 0; i < _indentLevel; ++i) {
  187. _indentStr += "\t";
  188. }
  189. }
  190. NS_CC_END