logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://hacktivis.me/git/libbulletml.git

bulletmlparser.cpp (3990B)


  1. #include "bulletmlparser.h"
  2. #include "bulletmlerror.h"
  3. #include <map>
  4. namespace {
  5. /// 内部のみで使用するクラス。
  6. class IDPool {
  7. public:
  8. static int getID(BulletMLNode::Name domain, const std::string& key) {
  9. KeyToID& kti = map_[domain];
  10. KeyToID::iterator ite = kti.find(key);
  11. if (ite == kti.end()) {
  12. int id = maxMap_[domain]++;
  13. map_[domain][key] = id;
  14. return id;
  15. }
  16. else {
  17. return ite->second;
  18. }
  19. }
  20. static void init() {
  21. map_.insert(std::make_pair(BulletMLNode::bullet, KeyToID()));
  22. map_.insert(std::make_pair(BulletMLNode::action, KeyToID()));
  23. map_.insert(std::make_pair(BulletMLNode::fire, KeyToID()));
  24. maxMap_.insert(std::make_pair(BulletMLNode::bullet, 0));
  25. maxMap_.insert(std::make_pair(BulletMLNode::action, 0));
  26. maxMap_.insert(std::make_pair(BulletMLNode::fire, 0));
  27. }
  28. static void quit() {
  29. map_.clear();
  30. maxMap_.clear();
  31. }
  32. private:
  33. typedef std::map<std::string, unsigned int> KeyToID;
  34. typedef std::map<BulletMLNode::Name, KeyToID> DomainToIDMap;
  35. typedef std::map<BulletMLNode::Name, int> DomainToMaxID;
  36. static DomainToIDMap map_;
  37. static DomainToMaxID maxMap_;
  38. };
  39. }
  40. IDPool::DomainToIDMap IDPool::map_;
  41. IDPool::DomainToMaxID IDPool::maxMap_;
  42. BulletMLParser::BulletMLParser()
  43. : bulletml_(0), isHorizontal_(false)
  44. {}
  45. BulletMLParser::~BulletMLParser() {
  46. // BulletMLNode はルートノードだけ破壊すれば良い
  47. delete bulletml_;
  48. }
  49. void BulletMLParser::build() {
  50. IDPool::init();
  51. parse();
  52. IDPool::quit();
  53. }
  54. BulletMLNode* BulletMLParser::getBulletRef(int id) {
  55. BulletMLError::doAssert((int)bulletMap_.size() > id && bulletMap_[id] != 0,
  56. "bulletRef key doesn't exist.");
  57. return bulletMap_[id];
  58. }
  59. BulletMLNode* BulletMLParser::getActionRef(int id) {
  60. BulletMLError::doAssert((int)actionMap_.size() > id && actionMap_[id] != 0,
  61. "actionRef key doesn't exist.");
  62. return actionMap_[id];
  63. }
  64. BulletMLNode* BulletMLParser::getFireRef(int id) {
  65. BulletMLError::doAssert((int)fireMap_.size() > id && fireMap_[id] != 0,
  66. "fireRef key doesn't exist.");
  67. return fireMap_[id];
  68. }
  69. BulletMLNode* BulletMLParser::addContent(const std::string& name) {
  70. // ルートノードは別処理
  71. if (name == "bulletml") {
  72. bulletml_ = new BulletMLNode(name);
  73. return bulletml_;
  74. }
  75. BulletMLError::doAssert(bulletml_ != 0, "<bulletml> doesn't come.");
  76. return new BulletMLNode(name);
  77. }
  78. void BulletMLParser::addAttribute(const MyAttributes& attr,
  79. BulletMLNode* elem)
  80. {
  81. if (!attr.empty()) {
  82. MyAttributeIte ite = attr.begin();
  83. while (ite != attr.end()) {
  84. const std::string key(*ite);
  85. ite++;
  86. const std::string val(*ite);
  87. ite++;
  88. if (key == "type") elem->setType(val);
  89. else if (key == "label") {
  90. BulletMLNode::Name name = elem->getName();
  91. BulletMLNode::Name domain;
  92. if (name == BulletMLNode::bulletRef) {
  93. domain = BulletMLNode::bullet;
  94. }
  95. else if (name == BulletMLNode::actionRef) {
  96. domain = BulletMLNode::action;
  97. }
  98. else if (name == BulletMLNode::fireRef) {
  99. domain = BulletMLNode::fire;
  100. }
  101. else {
  102. domain = name;
  103. }
  104. int id = IDPool::getID(domain, val);
  105. if (name == BulletMLNode::bullet) {
  106. if ((int)bulletMap_.size() <= id){
  107. bulletMap_.resize(id+1, 0);}
  108. bulletMap_[id] = elem;
  109. }
  110. else if (name == BulletMLNode::action) {
  111. if ((int)actionMap_.size() <= id){
  112. actionMap_.resize(id+1, 0);
  113. }
  114. actionMap_[id] = elem;
  115. }
  116. else if (name == BulletMLNode::fire) {
  117. if ((int)fireMap_.size() <= id)
  118. fireMap_.resize(id+1, 0);
  119. fireMap_[id] = elem;
  120. }
  121. else if (
  122. name == BulletMLNode::bulletRef ||
  123. name == BulletMLNode::actionRef ||
  124. name == BulletMLNode::fireRef)
  125. {
  126. elem->setRefID(id);
  127. }
  128. else {
  129. BulletMLError::doAssert(
  130. "he can't have attribute \"label\".");
  131. }
  132. if (elem->getName() == BulletMLNode::action &&
  133. val.length() >= 3 && val.substr(0, 3) == "top") {
  134. topActions_.push_back(elem);
  135. }
  136. }
  137. }
  138. }
  139. }