SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
EntityLump.cpp
Go to the documentation of this file.
1#include <bsppp/EntityLump.h>
2
3using namespace bsppp;
4using namespace sourcepp;
5
6std::string_view BSPEntityKeyValues::Element::getKey() const {
7 return this->key;
8}
9
10void BSPEntityKeyValues::Element::setKey(std::string_view key_) {
11 this->key = key_;
12}
13
14std::string_view BSPEntityKeyValues::Element::getValue() const {
15 return this->value;
16}
17
19 return this == &getInvalid();
20}
21
23 static Element element;
24 return element;
25}
26
27bool BSPEntityKeyValues::hasChild(std::string_view childKey) const {
28 return !this->operator[](childKey).isInvalid();
29}
30
32 return this->keyvalues.size();
33}
34
35uint64_t BSPEntityKeyValues::getKeyValuesCount(std::string_view childKey) const {
36 uint64_t count = 0;
37 for (const auto& element : this->keyvalues) {
38 if (sourcepp::string::iequals(element.getKey(), childKey)) {
39 ++count;
40 }
41 }
42 return count;
43}
44
45const std::vector<BSPEntityKeyValues::Element>& BSPEntityKeyValues::getKeyValues() const {
46 return this->keyvalues;
47}
48
50 return this->keyvalues.at(n);
51}
52
54 return this->keyvalues.at(n);
55}
56
57const BSPEntityKeyValues::Element& BSPEntityKeyValues::operator[](std::string_view childKey) const {
58 return this->operator()(childKey);
59}
60
62 return this->operator()(childKey);
63}
64
65const BSPEntityKeyValues::Element& BSPEntityKeyValues::operator()(std::string_view childKey) const {
66 for (const auto& element : this->keyvalues) {
67 if (sourcepp::string::iequals(element.getKey(), childKey)) {
68 return element;
69 }
70 }
71 return Element::getInvalid();
72}
73
75 for (auto& element : this->keyvalues) {
76 if (sourcepp::string::iequals(element.getKey(), childKey)) {
77 return element;
78 }
79 }
80 return this->addKeyValue(childKey);
81}
82
83const BSPEntityKeyValues::Element& BSPEntityKeyValues::operator()(std::string_view childKey, unsigned int n) const {
84 unsigned int count = 0;
85 for (const auto& element : this->keyvalues) {
86 if (sourcepp::string::iequals(element.getKey(), childKey)) {
87 if (count == n) {
88 return element;
89 }
90 ++count;
91 }
92 }
93 return Element::getInvalid();
94}
95
96BSPEntityKeyValues::Element& BSPEntityKeyValues::operator()(std::string_view childKey, unsigned int n) {
97 unsigned int count = 0;
98 for (auto& element: this->keyvalues) {
99 if (sourcepp::string::iequals(element.getKey(), childKey)) {
100 if (count == n) {
101 return element;
102 }
103 ++count;
104 }
105 }
106 return this->addKeyValue(childKey);
107}
108
109void BSPEntityKeyValues::removeKeyValue(std::string_view childKey, int n) {
110 unsigned int count = 0;
111 for (auto element = this->keyvalues.begin(); element != this->keyvalues.end(); ++element) {
112 if (sourcepp::string::iequals(element->getKey(), childKey)) {
113 if (n < 0 || count == n) {
114 element = this->keyvalues.erase(element);
115 if (count == n) {
116 return;
117 }
118 }
119 ++count;
120 }
121 }
122}
123
124std::string BSPEntityKeyValues::bake(bool useEscapes) const {
125 std::string out = "{\n";
126 for (const auto& elem : this->keyvalues) {
127 out += "\t\"";
129 out += "\" \"";
131 out += "\"\n";
132 }
133 out += "}";
134 return out;
135}
static const Element & getInvalid()
Definition: EntityLump.cpp:22
std::string_view getKey() const
Get the key associated with the element.
Definition: EntityLump.cpp:6
std::string_view getValue() const
Get the value associated with the element.
Definition: EntityLump.cpp:14
bool isInvalid() const
Check if the given element is invalid.
Definition: EntityLump.cpp:18
void setKey(std::string_view key_)
Set the key associated with the element.
Definition: EntityLump.cpp:10
const Element & operator()(std::string_view childKey) const
Get the first keyvalue of the entity with the given key.
Definition: EntityLump.cpp:65
bool hasChild(std::string_view childKey) const
Check if this entity has one or more keyvalues with the given name.
Definition: EntityLump.cpp:27
Element & addKeyValue(std::string_view key_, V value_={})
Add a new keyvalue to the entity.
Definition: EntityLump.h:122
uint64_t getKeyValuesCount() const
Get the number of keyvalues.
Definition: EntityLump.cpp:31
std::string bake(bool useEscapes) const
Definition: EntityLump.cpp:124
const std::vector< Element > & getKeyValues() const
Get the keyvalues of the entity.
Definition: EntityLump.cpp:45
void removeKeyValue(std::string_view childKey, int n=-1)
Remove a keyvalue from the entity. -1 means all keyvalues with the given key.
Definition: EntityLump.cpp:109
std::vector< Element > keyvalues
Definition: EntityLump.h:136
const Element & operator[](unsigned int n) const
Get the keyvalue of the entity at the given index.
Definition: EntityLump.cpp:49
Definition: BSP.h:18
std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert special characters like \n to escaped special characters like \\n.
Definition: Text.cpp:54
const EscapeSequenceMap NO_ESCAPE_SEQUENCES
Definition: Text.cpp:26
const EscapeSequenceMap DEFAULT_ESCAPE_SEQUENCES
Definition: Text.cpp:12
bool iequals(std::string_view s1, std::string_view s2)
Definition: String.cpp:61
Definition: LZMA.h:11