SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Toggle main menu visibility
Loading...
Searching...
No Matches
EntityLump.cpp
Go to the documentation of this file.
1
#include <
bsppp/EntityLump.h
>
2
3
using namespace
bsppp
;
4
using namespace
sourcepp
;
5
6
std::string_view
BSPEntityKeyValues::Element::getKey
()
const
{
7
return
this->
key
;
8
}
9
10
void
BSPEntityKeyValues::Element::setKey
(std::string_view key_) {
11
this->
key
= key_;
12
}
13
14
std::string_view
BSPEntityKeyValues::Element::getValue
()
const
{
15
return
this->
value
;
16
}
17
18
bool
BSPEntityKeyValues::Element::isInvalid
()
const
{
19
return
this
== &
getInvalid
();
20
}
21
22
const
BSPEntityKeyValues::Element
&
BSPEntityKeyValues::Element::getInvalid
() {
23
static
Element
element;
24
return
element;
25
}
26
27
bool
BSPEntityKeyValues::hasChild
(std::string_view childKey)
const
{
28
return
!this->
operator[]
(childKey).
isInvalid
();
29
}
30
31
uint64_t
BSPEntityKeyValues::getKeyValuesCount
()
const
{
32
return
this->
keyvalues
.size();
33
}
34
35
uint64_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
45
const
std::vector<BSPEntityKeyValues::Element>&
BSPEntityKeyValues::getKeyValues
()
const
{
46
return
this->
keyvalues
;
47
}
48
49
const
BSPEntityKeyValues::Element
&
BSPEntityKeyValues::operator[]
(
unsigned
int
n)
const
{
50
return
this->
keyvalues
.at(n);
51
}
52
53
BSPEntityKeyValues::Element
&
BSPEntityKeyValues::operator[]
(
unsigned
int
n) {
54
return
this->
keyvalues
.at(n);
55
}
56
57
const
BSPEntityKeyValues::Element
&
BSPEntityKeyValues::operator[]
(std::string_view childKey)
const
{
58
return
this->
operator()
(childKey);
59
}
60
61
BSPEntityKeyValues::Element
&
BSPEntityKeyValues::operator[]
(std::string_view childKey) {
62
return
this->
operator()
(childKey);
63
}
64
65
const
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
74
BSPEntityKeyValues::Element
&
BSPEntityKeyValues::operator()
(std::string_view childKey) {
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
83
const
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
96
BSPEntityKeyValues::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
109
void
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
124
std::string
BSPEntityKeyValues::bake
(
bool
useEscapes)
const
{
125
std::string out =
"{\n"
;
126
for
(
const
auto
& elem : this->
keyvalues
) {
127
out +=
"\t\""
;
128
out +=
parser::text::convertSpecialCharsToEscapes
(elem.getKey(),
parser::text::getDefaultEscapeSequencesOrNone
(useEscapes));
129
out +=
"\" \""
;
130
out +=
parser::text::convertSpecialCharsToEscapes
(elem.getValue(),
parser::text::getDefaultEscapeSequencesOrNone
(useEscapes));
131
out +=
"\"\n"
;
132
}
133
out +=
"}"
;
134
return
out;
135
}
EntityLump.h
bsppp::BSPEntityKeyValues::Element
Definition
EntityLump.h:24
bsppp::BSPEntityKeyValues::Element::getInvalid
static const Element & getInvalid()
Definition
EntityLump.cpp:22
bsppp::BSPEntityKeyValues::Element::Element
Element()=default
bsppp::BSPEntityKeyValues::Element::getKey
std::string_view getKey() const
Get the key associated with the element.
Definition
EntityLump.cpp:6
bsppp::BSPEntityKeyValues::Element::getValue
std::string_view getValue() const
Get the value associated with the element.
Definition
EntityLump.cpp:14
bsppp::BSPEntityKeyValues::Element::isInvalid
bool isInvalid() const
Check if the given element is invalid.
Definition
EntityLump.cpp:18
bsppp::BSPEntityKeyValues::Element::setKey
void setKey(std::string_view key_)
Set the key associated with the element.
Definition
EntityLump.cpp:10
bsppp::BSPEntityKeyValues::Element::value
std::string value
Definition
EntityLump.h:82
bsppp::BSPEntityKeyValues::Element::key
std::string key
Definition
EntityLump.h:81
bsppp::BSPEntityKeyValues::operator()
const Element & operator()(std::string_view childKey) const
Get the first keyvalue of the entity with the given key.
Definition
EntityLump.cpp:65
bsppp::BSPEntityKeyValues::hasChild
bool hasChild(std::string_view childKey) const
Check if this entity has one or more keyvalues with the given name.
Definition
EntityLump.cpp:27
bsppp::BSPEntityKeyValues::addKeyValue
Element & addKeyValue(std::string_view key_, V value_={})
Add a new keyvalue to the entity.
Definition
EntityLump.h:125
bsppp::BSPEntityKeyValues::getKeyValuesCount
uint64_t getKeyValuesCount() const
Get the number of keyvalues.
Definition
EntityLump.cpp:31
bsppp::BSPEntityKeyValues::bake
std::string bake(bool useEscapes) const
Definition
EntityLump.cpp:124
bsppp::BSPEntityKeyValues::getKeyValues
const std::vector< Element > & getKeyValues() const
Get the keyvalues of the entity.
Definition
EntityLump.cpp:45
bsppp::BSPEntityKeyValues::removeKeyValue
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
bsppp::BSPEntityKeyValues::keyvalues
std::vector< Element > keyvalues
Definition
EntityLump.h:139
bsppp::BSPEntityKeyValues::operator[]
const Element & operator[](unsigned int n) const
Get the keyvalue of the entity at the given index.
Definition
EntityLump.cpp:49
bsppp
Definition
BSP.h:18
sourcepp::parser::text::convertSpecialCharsToEscapes
std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert special characters like \n to escaped special characters like \\n.
Definition
Text.cpp:56
sourcepp::parser::text::getDefaultEscapeSequencesOrNone
const EscapeSequenceMap & getDefaultEscapeSequencesOrNone(bool useEscapes)
Definition
Text.cpp:27
sourcepp::string::iequals
bool iequals(std::string_view s1, std::string_view s2)
Definition
String.cpp:63
sourcepp
Definition
LZMA.h:11
src
bsppp
EntityLump.cpp
Generated on
for SourcePP by
1.17.0