Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
BBDocument.cpp
Go to the documentation of this file.
1/*
2 This code is adopted from https://github.com/zethon/bbcpp
3 at commit 852a02dda37a17f458dd68ecf5461141f93edce2.
4 See reprint of original license (MIT) in README.txt
5*/
6
7#include <cstring>
8#include <cctype>
9#include "BBDocument.h"
10
11namespace bbcpp
12{
13
14BBNode::BBNode(NodeType nodeType, const std::string& name)
15 : _name(name), _nodeType(nodeType)
16{
17 // nothing to do
18}
19
20BBText &BBDocument::newText(const std::string &text)
21{
22 // first try to append this text to the item on top of the stack
23 // if that is a BBText object, if not, then see if the last element
24 // pushed to BBDocument is a text item, and if so append this to that
25 // text
26 if (_stack.size() > 0 && _stack.top()->getChildren().size() > 0)
27 {
28 auto totalChildCnt = _stack.top()->getChildren().size();
29 auto textnode = _stack.top()->getChildren().at(totalChildCnt - 1)->downCast<BBTextPtr>(false);
30 if (textnode)
31 {
32 textnode->append(text);
33 return *textnode;
34 }
35 }
36 else if (_children.size() > 0)
37 {
38 auto textnode = _children.back()->downCast<BBTextPtr>(false);
39 if (textnode)
40 {
41 textnode->append(text);
42 return *textnode;
43 }
44 }
45
46 // ok, there was no previous text element so we wil either add this text
47 // element as a child of the top item OR we'll add it to the BBDocucment
48 // object
49 auto textNode = std::make_shared<BBText>(text);
50 if (_stack.size() > 0)
51 {
52 _stack.top()->appendChild(textNode);
53 }
54 else
55 {
56 // add this node to the document-node if needed
57 appendChild(textNode);
58 }
59
60 return *textNode;
61}
62
63BBElement& BBDocument::newElement(const std::string &name)
64{
65 auto newNode = std::make_shared<BBElement>(name);
66 if (_stack.size() > 0)
67 {
68 _stack.top()->appendChild(newNode);
69 }
70 else
71 {
72 // add this node to the document-node if needed
73 appendChild(newNode);
74 }
75
76 _stack.push(newNode);
77 return *newNode;
78}
79
81{
82 auto newNode = std::make_shared<BBElement>(name, BBElement::CLOSING);
83 if (_stack.size() > 0)
84 {
85 _stack.top()->appendChild(newNode);
86 _stack.pop();
87 }
88 else
89 {
90 appendChild(newNode);
91 }
92
93 return *newNode;
94}
95
96BBElement& BBDocument::newKeyValueElement(const std::string& name, const ParameterMap& pairs)
97{
98 auto newNode = std::make_shared<BBElement>(name, BBElement::PARAMETER);
99 if (_stack.size() > 0)
100 {
101 _stack.top()->appendChild(newNode);
102 }
103 else
104 {
105 // add this node to the document-node if needed
106 appendChild(newNode);
107 }
108
109 for (const auto& kv : pairs)
110 {
111 newNode->setOrAddParameter(kv.first, kv.second);
112 }
113
114 _stack.push(newNode);
115 return *newNode;
116}
117
118
119
120} // namespace
BBElement & newClosingElement(const std::string &name)
BBElement & newKeyValueElement(const std::string &name, const ParameterMap &pairs)
BBElement & newElement(const std::string &name)
BBNodeStack _stack
Definition BBDocument.h:634
BBText & newText(const std::string &text=std::string())
virtual void appendChild(BBNodePtr node)
Definition BBDocument.h:133
BBNodeList _children
Definition BBDocument.h:155
const BBNodeList & getChildren() const
Definition BBDocument.h:131
BBNode(NodeType nodeType, const std::string &name)
std::map< std::string, std::string > ParameterMap
Definition BBDocument.h:67
std::shared_ptr< BBText > BBTextPtr
Definition BBDocument.h:59