The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
simple_wml.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by David White <dave@whitevine.net>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#pragma once
17
18#include <cassert>
19#include <cstddef>
20#include <cstring>
21#include <iosfwd>
22#include <limits>
23#include <memory>
24#include <string>
25#include <vector>
26
27#include "exceptions.hpp"
28
29namespace simple_wml {
30
31struct error : public game::error {
32 error(const char* msg);
33};
34
36{
37public:
38 string_span() : str_(nullptr), size_(0)
39 {}
40 string_span(const char* str, size_t size) : str_(str), size_(size)
41 {}
42 string_span(const char* str) : str_(str), size_(strlen(str))
43 {}
44 string_span(const char* begin, const char* end) : str_(begin), size_(end - begin)
45 {}
46 string_span(const std::string& str) : str_(str.c_str()), size_(str.size())
47 {}
48 // Disallow temporaries to avoid dangling references.
49 string_span(std::string&&) = delete;
50
51 typedef const char* const_iterator;
52 typedef const char* iterator;
53 typedef const char value_type;
54
55 operator std::string_view() const { return std::string_view(str_, size_); }
56
57 bool operator==(const char* o) const {
58 const char* i1 = str_;
59 const char* i2 = str_ + size_;
60 while(i1 != i2 && *o && *i1 == *o) {
61 ++i1;
62 ++o;
63 }
64
65 return i1 == i2 && *o == 0;
66 }
67 bool operator!=(const char* o) const {
68 return !operator==(o);
69 }
70 bool operator==(const std::string& o) const {
71 return size_ == o.size() && memcmp(str_, o.data(), size_) == 0;
72 }
73 bool operator!=(const std::string& o) const {
74 return !operator==(o);
75 }
76 bool operator==(const string_span& o) const {
77 return size_ == o.size_ && memcmp(str_, o.str_, size_) == 0;
78 }
79 bool operator!=(const string_span& o) const {
80 return !operator==(o);
81 }
82 bool operator<(const string_span& o) const {
83 const size_t len = size_ < o.size_ ? size_ : o.size_;
84 for(size_t n = 0; n < len; ++n) {
85 if(str_[n] != o.str_[n]) {
86 if(str_[n] < o.str_[n]) {
87 return true;
88 } else {
89 return false;
90 }
91 }
92 }
93
94 return size_ < o.size_;
95 }
96
97 const char* begin() const { return str_; }
98 const char* end() const { return str_ + size_; }
99
100 size_t size() const { return size_; }
101 bool empty() const { return size_ == 0; }
102 bool is_null() const { return str_ == nullptr; }
103
104 bool to_bool(bool default_value=false) const;
105 int to_int() const;
106 std::string to_string() const;
107
108 //returns a duplicate of the string span in a new[] allocated buffer
109 char* duplicate() const;
110
111private:
112 const char* str_;
113 size_t size_;
114};
115
116std::ostream& operator<<(std::ostream& o, const string_span& s);
117
118class document;
119
120class node
121{
122public:
123 node(document& doc, node* parent);
124 node(document& doc, node* parent, const char** str, int depth=0);
125 ~node();
127 {
128 attribute(const string_span& k, const string_span& v) : key(k), value(v) {}
131 };
132 typedef std::vector<node*> child_list;
133
134 const string_span& operator[](const char* key) const;
135 const string_span& attr(const char* key) const {
136 return (*this)[key];
137 }
138
139 bool has_attr(const char* key) const;
140
141 //sets an attribute in the WML node. The node will keep a direct reference
142 //to key and value which it will maintain for its lifetime. The caller
143 //MUST guarantee that the key and value buffers remain valid for the
144 //lifetime of the node.
145 node& set_attr(const char* key, const char* value);
146
147 //functions which are identical to set_attr() except that the buffer
148 //referred to by 'value' will be duplicated and the new buffer managed by
149 //the node. The caller may destroy the value buffer as soon as the function
150 //returns. The key buffer must remain valid for the lifetime of the node.
151 node& set_attr_dup(const char* key, const char* value);
152 node& set_attr_dup(const char* key, const string_span& value);
153 /** As above but convert @a value to a WML value */
154 node& set_attr_esc(const char* key, string_span value);
155
156 node& set_attr_int(const char* key, int value);
157
158 node& add_child(const char* name);
159 node& add_child_at(const char* name, std::size_t index);
160 void remove_child(const char* name, std::size_t index);
161 void remove_child(const string_span& name, std::size_t index);
162
163 node* child(const char* name);
164 const node* child(const char* name) const;
165
166 node& child_or_add(const char* name);
167
168 const child_list& children(const char* name) const;
169
170 const string_span& first_child() const;
171
172 bool is_dirty() const { return output_cache_.is_null(); }
173
175
176 int output_size() const;
177 void output(char*& buf, CACHE_STATUS status=DO_NOT_MODIFY_CACHE);
178
179 void copy_into(node& n) const;
180
181 bool no_children() const { return children_.empty(); }
182 bool one_child() const { return children_.size() == 1 && children_.begin()->second.size() == 1; }
183
184 void apply_diff(const node& diff);
185
186 void set_doc(document* doc);
187
188 int nchildren() const;
189 int nattributes_recursive() const;
190
191private:
192 node(const node&) = delete;
193 node& operator=(const node&) = delete;
194
195 int get_children(const string_span& name);
196 int get_children(const char* name);
197
198 void set_dirty();
199 void shift_buffers(ptrdiff_t offset);
200
202
203 typedef std::vector<attribute> attribute_list;
205
207
208 typedef std::pair<string_span, child_list> child_pair;
209 typedef std::vector<child_pair> child_map;
210
211 static child_map::const_iterator find_in_map(const child_map& m, const string_span& attr);
212 static child_map::iterator find_in_map(child_map& m, const string_span& attr);
214
215 //a node position indicates the index into the child map where the node
216 //is, and then the index into the child list within where the node is.
217 struct node_pos {
219 : child_map_index(static_cast<unsigned short>(child_map_index)),
220 child_list_index(static_cast<unsigned short>(child_list_index))
221 {
222 assert(child_map_index >= 0 && child_map_index <= std::numeric_limits<unsigned short>::max());
223 assert(child_list_index >= 0 && child_list_index <= std::numeric_limits<unsigned short>::max());
224 }
225 unsigned short child_map_index;
226 unsigned short child_list_index;
227 };
228
229 //a list of all the children in order.
230 std::vector<node_pos> ordered_children_;
231
232 void insert_ordered_child(int child_map_index, int child_list_index);
233 void remove_ordered_child(int child_map_index, int child_list_index);
234 void remove_ordered_child_list(int child_map_index);
235
236 void check_ordered_children() const;
237
239};
240
241std::string node_to_string(const node& n);
242
244
246
248{
249public:
250 document();
251 explicit document(char* buf, INIT_BUFFER_CONTROL control=INIT_TAKE_OWNERSHIP);
252 document(const char* buf, INIT_STATE state);
253 explicit document(string_span compressed_buf);
254 ~document();
255 const char* dup_string(const char* str);
256 const char* dup_string(string_span str);
257 const char* esc_string(string_span str);
258 node& root() { if(!root_) { generate_root(); } return *root_; }
259 const node& root() const { if(!root_) { const_cast<document*>(this)->generate_root(); } return *root_; }
260
261 const char* output();
262 string_span output_compressed(bool bzip2 = false);
263
264 void compress();
265
266 std::unique_ptr<document> clone();
267
268 const string_span& operator[](const char* key) const {
269 return root()[key];
270 }
271
272 const string_span& attr(const char* key) const {
273 return root()[key];
274 }
275
276 node* child(const char* name) {
277 return root().child(name);
278 }
279
280 const node* child(const char* name) const {
281 return root().child(name);
282 }
283
284 const node::child_list& children(const char* name) const {
285 return root().children(name);
286 }
287
288 node& set_attr(const char* key, const char* value) {
289 return root().set_attr(key, value);
290 }
291
292 node& set_attr_dup(const char* key, const char* value) {
293 return root().set_attr_dup(key, value);
294 }
295
296 void take_ownership_of_buffer(char* buffer) {
297 buffers_.push_back(buffer);
298 }
299
300 void swap(document& o) noexcept;
301 void clear();
302
303 static std::string stats();
304
305 static std::size_t document_size_limit;
306private:
307 void generate_root();
308 document(const document&) = delete;
309 document& operator=(const document&) = delete;
310
312 const char* output_;
313 std::vector<char*> buffers_;
315
316 //linked list of documents for accounting purposes
317 void attach_list();
318 void detach_list();
321};
322
323/** Implement non-member swap function for std::swap (calls @ref document::swap). */
324void swap(document& lhs, document& rhs) noexcept;
325
326}
void take_ownership_of_buffer(char *buffer)
const char * output()
std::unique_ptr< document > clone()
static std::string stats()
const string_span & operator[](const char *key) const
const node::child_list & children(const char *name) const
const node & root() const
document(const document &)=delete
static std::size_t document_size_limit
void swap(document &o) noexcept
node & set_attr(const char *key, const char *value)
const char * esc_string(string_span str)
std::vector< char * > buffers_
string_span output_compressed(bool bzip2=false)
const char * dup_string(const char *str)
string_span compressed_buf_
node & set_attr_dup(const char *key, const char *value)
document & operator=(const document &)=delete
const node * child(const char *name) const
const string_span & attr(const char *key) const
node * child(const char *name)
const string_span & attr(const char *key) const
int output_size() const
void set_doc(document *doc)
void insert_ordered_child(int child_map_index, int child_list_index)
void check_ordered_children() const
void remove_child(const char *name, std::size_t index)
int nchildren() const
node(const node &)=delete
bool no_children() const
bool is_dirty() const
int get_children(const string_span &name)
const string_span & operator[](const char *key) const
node & operator=(const node &)=delete
const child_list & children(const char *name) const
void apply_diff(const node &diff)
attribute_list attr_
bool has_attr(const char *key) const
node & set_attr_int(const char *key, int value)
std::vector< child_pair > child_map
node * child(const char *name)
std::vector< node * > child_list
void remove_ordered_child_list(int child_map_index)
int nattributes_recursive() const
std::vector< node_pos > ordered_children_
std::vector< attribute > attribute_list
const string_span & first_child() const
child_map children_
node & add_child(const char *name)
node & child_or_add(const char *name)
node & set_attr_esc(const char *key, string_span value)
As above but convert value to a WML value
node & set_attr(const char *key, const char *value)
string_span output_cache_
node & add_child_at(const char *name, std::size_t index)
void copy_into(node &n) const
node & set_attr_dup(const char *key, const char *value)
void remove_ordered_child(int child_map_index, int child_list_index)
void shift_buffers(ptrdiff_t offset)
std::pair< string_span, child_list > child_pair
bool one_child() const
static child_map::const_iterator find_in_map(const child_map &m, const string_span &attr)
bool to_bool(bool default_value=false) const
std::string to_string() const
string_span(const char *begin, const char *end)
bool operator<(const string_span &o) const
bool operator==(const std::string &o) const
char * duplicate() const
const char * end() const
const char * begin() const
bool operator!=(const string_span &o) const
string_span(const std::string &str)
string_span(const char *str)
bool operator==(const char *o) const
const char * const_iterator
string_span(const char *str, size_t size)
bool operator==(const string_span &o) const
bool operator!=(const char *o) const
string_span(std::string &&)=delete
bool operator!=(const std::string &o) const
static std::ostream & output()
Definition log.cpp:104
void swap(document &lhs, document &rhs) noexcept
Implement non-member swap function for std::swap (calls document::swap).
std::ostream & operator<<(std::ostream &o, const string_span &s)
std::string node_to_string(const node &n)
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
Base class for all the errors encountered by the engine.
attribute(const string_span &k, const string_span &v)
node_pos(int child_map_index, int child_list_index)
unsigned short child_map_index
unsigned short child_list_index
static map_location::direction n
static map_location::direction s