-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
config.cpp
1489 lines (1234 loc) · 37.4 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003 by David White <dave@whitevine.net>
Copyright (C) 2005 - 2016 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Routines related to configuration-files / WML.
*/
#include "config.hpp"
#include "global.hpp"
#include "log.hpp"
#include "util.hpp"
#include "utils/const_clone.hpp"
#include <cstdlib>
#include <cstring>
#include <deque>
#include <istream>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define DBG_CF LOG_STREAM(debug, log_config)
struct tconfig_implementation
{
/**
* Implementation for the wrappers for
* [const] config& child(const std::string& key, const std::string& parent);
*
* @tparam T A pointer to the config.
*/
template<class T>
static typename utils::tconst_clone<config, T>::reference
child(
T config
, const std::string& key
, const std::string& parent)
{
config->check_valid();
assert(!parent.empty());
assert(parent[0] == '[');
assert(parent[parent.size() - 1] == ']');
if(config->has_child(key)) {
return *(config->children.find(key)->second.front());
}
/**
* @todo Implement a proper wml_exception here.
*
* at the moment there seem to be dependency issues, which i don't want
* to fix right now.
*/
// FAIL(missing_mandatory_wml_section(parent, key));
std::stringstream sstr;
sstr << "Mandatory WML child »[" << key << "]« missing in »"
<< parent << "«. Please report this bug.";
throw config::error(sstr.str());
}
};
/* ** Attribute value implementation ** */
// Special string values.
const std::string config::attribute_value::s_yes("yes");
const std::string config::attribute_value::s_no("no");
const std::string config::attribute_value::s_true("true");
const std::string config::attribute_value::s_false("false");
/** Default implementation, but defined out-of-line for efficiency reasons. */
config::attribute_value::attribute_value()
: value_()
{
}
/** Default implementation, but defined out-of-line for efficiency reasons. */
config::attribute_value::~attribute_value()
{
}
/** Default implementation, but defined out-of-line for efficiency reasons. */
config::attribute_value::attribute_value(const config::attribute_value &that)
: value_(that.value_)
{
}
/** Default implementation, but defined out-of-line for efficiency reasons. */
config::attribute_value &config::attribute_value::operator=(const config::attribute_value &that)
{
value_ = that.value_;
return *this;
}
config::attribute_value &config::attribute_value::operator=(bool v)
{
value_ = yes_no(v);
return *this;
}
config::attribute_value &config::attribute_value::operator=(int v)
{
value_ = v;
return *this;
}
config::attribute_value &config::attribute_value::operator=(long long v)
{
if ( v > 0 )
// We can store this unsigned.
return *this = static_cast<unsigned long long>(v);
if ( v >= INT_MIN )
// We can store this as an int.
return *this = static_cast<int>(v);
// Getting to this point should be rare. (Currently, getting here means
// something like there was so much draining in a campaign that the
// total damage taken is not only negative, but so negative that an
// int cannot hold the value.) So rare that it is not worth precise
// treatment; just use a double.
value_ = static_cast<double>(v);
return *this;
}
config::attribute_value &config::attribute_value::operator=(unsigned long long v)
{
// Use int for smaller numbers.
if ( v <= INT_MAX )
return *this = static_cast<int>(v);
value_ = v;
return *this;
}
config::attribute_value &config::attribute_value::operator=(double v)
{
// Try to store integers in other types.
if ( v > 0.0 ) {
// Convert to unsigned and pass this off to that assignment operator.
unsigned long long ull = static_cast<unsigned long long>(v);
if ( static_cast<double>(ull) == v )
return *this = ull;
}
else {
// Convert to integer and pass this off to that assignment operator.
int i = static_cast<int>(v);
if ( static_cast<double>(i) == v )
return *this = i;
}
// If we get here, this does in fact get stored as a double.
value_ = v;
return *this;
}
namespace {
/**
* Attempts to convert @a source to the template type.
* This is to avoid "overzealous reinterpretations of certain WML strings as
* numeric types" (c.f. bug #19201).
* @returns true if the conversion was successful and the source string
* can be reobtained by streaming the result.
*/
template<typename To>
bool from_string_verify(const std::string & source, To & res)
{
// Check 1: convertable to the target type.
std::istringstream in_str(source);
if ( !(in_str >> res) )
return false;
// Check 2: convertable back to the same string.
std::ostringstream out_str;
out_str << res;
return out_str.str() == source;
}
}
config::attribute_value &config::attribute_value::operator=(const std::string &v)
{
// Handle some special strings.
if (v.empty()) { value_ = v; return *this; }
if ( v == s_yes ) { value_ = yes_no(true); return *this; }
if ( v == s_no ) { value_ = yes_no(false); return *this; }
if ( v == s_true ) { value_ = true_false(true); return *this; }
if ( v == s_false ) { value_ = true_false(false); return *this; }
// Attempt to convert to a number.
char *eptr;
double d = strtod(v.c_str(), &eptr);
if ( *eptr == '\0' ) {
// Possibly a number. See what type it should be stored in.
// (All conversions will be from the string since the largest integer
// type could have more precision than a double.)
if ( d > 0.0 ) {
// The largest type for positive integers is unsigned long long.
unsigned long long ull = 0;
if ( from_string_verify<unsigned long long>(v, ull) )
return *this = ull;
}
else {
// The largest (variant) type for negative integers is int.
int i = 0;
if ( from_string_verify<int>(v, i) )
return *this = i;
}
// This does not look like an integer, so it should be a double.
// However, make sure it can convert back to the same string (in
// case this is a string that just looks like a numeric value).
std::ostringstream tester;
tester << d;
if ( tester.str() == v ) {
value_ = d;
return *this;
}
}
// No conversion possible. Store the string.
value_ = v;
return *this;
}
config::attribute_value &config::attribute_value::operator=(const t_string &v)
{
if (!v.translatable()) return *this = v.str();
value_ = v;
return *this;
}
bool config::attribute_value::to_bool(bool def) const
{
if ( const yes_no *p = boost::get<const yes_no>(&value_) )
return *p;
if ( const true_false *p = boost::get<const true_false>(&value_) )
return *p;
// No other types are ever recognized as boolean.
return def;
}
namespace {
/// Visitor for converting a variant to a numeric type (T).
template <typename T>
class attribute_numeric_visitor : public boost::static_visitor<T>
{
public:
// Constructor stores the default value.
attribute_numeric_visitor(T def) : def_(def) {}
T operator()(boost::blank const &) const { return def_; }
T operator()(bool) const { return def_; }
T operator()(int i) const { return static_cast<T>(i); }
T operator()(unsigned long long u) const { return static_cast<T>(u); }
T operator()(double d) const { return static_cast<T>(d); }
T operator()(std::string const &s) const { return lexical_cast_default<T>(s, def_); }
T operator()(t_string const &) const { return def_; }
private:
const T def_;
};
}
int config::attribute_value::to_int(int def) const
{
return apply_visitor(attribute_numeric_visitor<int>(def));
}
long long config::attribute_value::to_long_long(long long def) const
{
return apply_visitor(attribute_numeric_visitor<long long>(def));
}
unsigned config::attribute_value::to_unsigned(unsigned def) const
{
return apply_visitor(attribute_numeric_visitor<unsigned>(def));
}
size_t config::attribute_value::to_size_t(size_t def) const
{
return apply_visitor(attribute_numeric_visitor<size_t>(def));
}
time_t config::attribute_value::to_time_t(time_t def) const
{
return apply_visitor(attribute_numeric_visitor<time_t>(def));
}
double config::attribute_value::to_double(double def) const
{
return apply_visitor(attribute_numeric_visitor<double>(def));
}
/// Visitor for converting a variant to a string.
class config::attribute_value::string_visitor
: public boost::static_visitor<std::string>
{
public:
std::string operator()(const boost::blank &) const { return std::string(); }
std::string operator()(const yes_no & b) const { return b.str(); }
std::string operator()(const true_false & b) const { return b.str(); }
std::string operator()(int i) const { return str_cast(i); }
std::string operator()(unsigned long long u) const { return str_cast(u); }
std::string operator()(double d) const { return str_cast(d); }
std::string operator()(std::string const &s) const { return s; }
std::string operator()(t_string const &s) const { return s.str(); }
};
std::string config::attribute_value::str() const
{
return apply_visitor(string_visitor());
}
t_string config::attribute_value::t_str() const
{
if (const t_string *p = boost::get<const t_string>(&value_)) return *p;
return str();
}
/**
* Tests for an attribute that was never set.
*/
bool config::attribute_value::blank() const
{
return boost::get<const boost::blank>(&value_) != NULL;
}
/**
* Tests for an attribute that either was never set or was set to "".
*/
bool config::attribute_value::empty() const
{
if (boost::get<const boost::blank>(&value_)) return true;
if (const std::string *p = boost::get<const std::string>(&value_)) return p->empty();
return false;
}
/// Visitor handling equality checks.
class config::attribute_value::equality_visitor
: public boost::static_visitor<bool>
{
public:
// Most generic: not equal.
template <typename T, typename U>
bool operator()(const T &, const U &) const { return false; }
// Same types are comparable and might be equal.
template <typename T>
bool operator()(const T & lhs, const T & rhs) const { return lhs == rhs; }
// Boolean values can be compared.
bool operator()(const true_false & lhs, const yes_no & rhs) const { return bool(lhs) == bool(rhs); }
bool operator()(const yes_no & lhs, const true_false & rhs) const { return bool(lhs) == bool(rhs); }
};
/**
* Checks for equality of the attribute values when viewed as strings.
* Exception: Boolean synonyms can be equal ("yes" == "true").
* Note: Blanks have no string representation, so do not equal "" (an empty string).
*/
bool config::attribute_value::operator==(const config::attribute_value &other) const
{
return boost::apply_visitor(equality_visitor(), value_, other.value_);
}
/**
* Checks for equality of the attribute values when viewed as strings.
* Exception: Boolean synonyms can be equal ("yes" == "true").
* Note: Blanks have no string representation, so do not equal "" (an empty string).
* Also note that translatable string are never equal to non translatable strings.
*/
bool config::attribute_value::equals(const std::string &str) const
{
attribute_value v;
v = str;
return *this == v;
// if c["a"] = "1" then this solution would have resulted in c["a"] == "1" beeing false
// because a["a"] is '1' and not '"1"'.
// return boost::apply_visitor(boost::bind( equality_visitor(), _1, boost::cref(str) ), value_);
// that's why we don't use it.
}
std::ostream &operator<<(std::ostream &os, const config::attribute_value &v)
{
// Simple implementation, but defined out-of-line because of the templating
// involved.
return os << v.value_;
}
/* ** config implementation ** */
config config::invalid;
const char* config::diff_track_attribute = "__diff_track";
void config::check_valid() const
{
if (!*this)
throw error("Mandatory WML child missing yet untested for. Please report.");
}
void config::check_valid(const config &cfg) const
{
if (!*this || !cfg)
throw error("Mandatory WML child missing yet untested for. Please report.");
}
config::config() : values(), children(), ordered_children()
{
}
config::config(const config& cfg) : values(cfg.values), children(), ordered_children()
{
append_children(cfg);
}
config::config(const std::string& child) : values(), children(), ordered_children()
{
add_child(child);
}
config::~config()
{
clear();
}
config& config::operator=(const config& cfg)
{
if(this == &cfg) {
return *this;
}
config tmp(cfg);
swap(tmp);
return *this;
}
#ifdef HAVE_CXX11
config::config(config &&cfg):
values(std::move(cfg.values)),
children(std::move(cfg.children)),
ordered_children(std::move(cfg.ordered_children))
{
}
config &config::operator=(config &&cfg)
{
clear();
swap(cfg);
return *this;
}
#endif
bool config::valid_id(const std::string& id)
{
if (id.empty()) {
return false;
}
BOOST_FOREACH(char c, id) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
//valid character.
}
else {
return false;
}
}
return true;
}
bool config::has_attribute(const std::string &key) const
{
check_valid();
return values.find(key) != values.end();
}
bool config::has_old_attribute(const std::string &key, const std::string &old_key, const std::string& msg) const
{
check_valid();
if (values.find(key) != values.end()) {
return true;
} else if (values.find(old_key) != values.end()) {
if (!msg.empty())
lg::wml_error() << msg;
return true;
}
return false;
}
void config::remove_attribute(const std::string &key)
{
check_valid();
values.erase(key);
}
void config::append_children(const config &cfg)
{
check_valid(cfg);
BOOST_FOREACH(const any_child &value, cfg.all_children_range()) {
add_child(value.key, value.cfg);
}
}
void config::append_attributes(const config &cfg)
{
check_valid(cfg);
BOOST_FOREACH(const attribute &v, cfg.values) {
values[v.first] = v.second;
}
}
void config::append_children(const config &cfg, const std::string& key)
{
check_valid(cfg);
BOOST_FOREACH(const config &value, cfg.child_range(key)) {
add_child(key, value);
}
}
void config::append(const config &cfg)
{
append_children(cfg);
BOOST_FOREACH(const attribute &v, cfg.values) {
values[v.first] = v.second;
}
}
void config::merge_children(const std::string& key)
{
check_valid();
if (child_count(key) < 2) return;
config merged_children;
BOOST_FOREACH(const config &cfg, child_range(key)) {
merged_children.append(cfg);
}
clear_children(key);
add_child(key,merged_children);
}
void config::merge_children_by_attribute(const std::string& key, const std::string& attribute)
{
check_valid();
if (child_count(key) < 2) return;
typedef std::map<std::string, config> config_map;
config_map merged_children_map;
BOOST_FOREACH(const config &cfg, child_range(key)) {
const std::string &value = cfg[attribute];
config_map::iterator m = merged_children_map.find(value);
if ( m!=merged_children_map.end() ) {
m->second.append(cfg);
} else {
merged_children_map.insert(make_pair(value, cfg));
}
}
clear_children(key);
BOOST_FOREACH(const config_map::value_type &i, merged_children_map) {
add_child(key,i.second);
}
}
config::child_itors config::child_range(const std::string& key)
{
check_valid();
child_map::iterator i = children.find(key);
static child_list dummy;
child_list *p = &dummy;
if (i != children.end()) p = &i->second;
return child_itors(child_iterator(p->begin()), child_iterator(p->end()));
}
config::const_child_itors config::child_range(const std::string& key) const
{
check_valid();
child_map::const_iterator i = children.find(key);
static child_list dummy;
const child_list *p = &dummy;
if (i != children.end()) p = &i->second;
return const_child_itors(const_child_iterator(p->begin()), const_child_iterator(p->end()));
}
unsigned config::child_count(const std::string &key) const
{
check_valid();
child_map::const_iterator i = children.find(key);
if(i != children.end()) {
return i->second.size();
}
return 0;
}
unsigned config::all_children_count() const
{
return ordered_children.size();
}
bool config::has_child(const std::string &key) const
{
check_valid();
return children.find(key) != children.end();
}
config &config::child(const std::string& key, int n)
{
check_valid();
const child_map::const_iterator i = children.find(key);
if (i == children.end()) {
DBG_CF << "The config object has no child named »"
<< key << "«.\n";
return invalid;
}
if (n < 0) n = i->second.size() + n;
if(size_t(n) < i->second.size()) {
return *i->second[n];
} else {
DBG_CF << "The config object has only »" << i->second.size()
<< "« children named »" << key
<< "«; request for the index »" << n << "« cannot be honored.\n";
return invalid;
}
}
config& config::child(const std::string& key, const std::string& parent)
{
return tconfig_implementation::child(this, key, parent);
}
const config& config::child(
const std::string& key
, const std::string& parent) const
{
return tconfig_implementation::child(this, key, parent);
}
const config & config::child_or_empty(const std::string& key) const
{
static const config empty_cfg;
check_valid();
child_map::const_iterator i = children.find(key);
if (i != children.end() && !i->second.empty())
return *i->second.front();
return empty_cfg;
}
config &config::child_or_add(const std::string &key)
{
child_map::const_iterator i = children.find(key);
if (i != children.end() && !i->second.empty())
return *i->second.front();
return add_child(key);
}
config& config::add_child(const std::string& key)
{
check_valid();
child_list& v = children[key];
v.push_back(new config());
ordered_children.push_back(child_pos(children.find(key),v.size()-1));
return *v.back();
}
config& config::add_child(const std::string& key, const config& val)
{
check_valid(val);
child_list& v = children[key];
v.push_back(new config(val));
ordered_children.push_back(child_pos(children.find(key),v.size()-1));
return *v.back();
}
#ifdef HAVE_CXX11
config &config::add_child(const std::string &key, config &&val)
{
check_valid(val);
child_list &v = children[key];
v.push_back(new config(std::move(val)));
ordered_children.push_back(child_pos(children.find(key), v.size() - 1));
return *v.back();
}
#endif
config &config::add_child_at(const std::string &key, const config &val, unsigned index)
{
check_valid(val);
child_list& v = children[key];
if(index > v.size()) {
throw error("illegal index to add child at");
}
v.insert(v.begin()+index,new config(val));
bool inserted = false;
const child_pos value(children.find(key),index);
std::vector<child_pos>::iterator ord = ordered_children.begin();
for(; ord != ordered_children.end(); ++ord) {
if (ord->pos != value.pos) continue;
if (!inserted && ord->index == index) {
ord = ordered_children.insert(ord,value);
inserted = true;
} else if (ord->index >= index) {
ord->index++;
}
}
if(!inserted) {
ordered_children.push_back(value);
}
return *v[index];
}
namespace {
struct remove_ordered
{
remove_ordered(const config::child_map::iterator &iter) : iter_(iter) {}
bool operator()(const config::child_pos &pos) const
{ return pos.pos == iter_; }
private:
config::child_map::iterator iter_;
};
}
void config::clear_children(const std::string& key)
{
check_valid();
child_map::iterator i = children.find(key);
if (i == children.end()) return;
ordered_children.erase(std::remove_if(ordered_children.begin(),
ordered_children.end(), remove_ordered(i)), ordered_children.end());
BOOST_FOREACH(config *c, i->second) {
delete c;
}
children.erase(i);
}
void config::splice_children(config &src, const std::string &key)
{
check_valid(src);
child_map::iterator i_src = src.children.find(key);
if (i_src == src.children.end()) return;
src.ordered_children.erase(std::remove_if(src.ordered_children.begin(),
src.ordered_children.end(), remove_ordered(i_src)),
src.ordered_children.end());
child_list &dst = children[key];
child_map::iterator i_dst = children.find(key);
unsigned before = dst.size();
dst.insert(dst.end(), i_src->second.begin(), i_src->second.end());
src.children.erase(i_src);
// key might be a reference to i_src->first, so it is no longer usable.
for (unsigned j = before; j < dst.size(); ++j) {
ordered_children.push_back(child_pos(i_dst, j));
}
}
void config::recursive_clear_value(const std::string& key)
{
check_valid();
values.erase(key);
BOOST_FOREACH(const any_child &value, all_children_range()) {
const_cast<config *>(&value.cfg)->recursive_clear_value(key);
}
}
std::vector<config::child_pos>::iterator config::remove_child(
const child_map::iterator &pos, unsigned index)
{
/* Find the position with the correct index and decrement all the
indices in the ordering that are above this index. */
unsigned found = 0;
BOOST_FOREACH(child_pos &p, ordered_children)
{
if (p.pos != pos) continue;
if (p.index == index)
found = &p - &ordered_children.front();
else if (p.index > index)
--p.index;
}
// Remove from the child map.
delete pos->second[index];
pos->second.erase(pos->second.begin() + index);
// Erase from the ordering and return the next position.
return ordered_children.erase(ordered_children.begin() + found);
}
config::all_children_iterator config::erase(const config::all_children_iterator& i)
{
return all_children_iterator(remove_child(i.i_->pos, i.i_->index));
}
void config::remove_child(const std::string &key, unsigned index)
{
check_valid();
child_map::iterator i = children.find(key);
if (i == children.end() || index >= i->second.size()) {
ERR_CF << "Error: attempting to delete non-existing child: "
<< key << "[" << index << "]\n";
return;
}
remove_child(i, index);
}
const config::attribute_value &config::operator[](const std::string &key) const
{
check_valid();
const attribute_map::const_iterator i = values.find(key);
if (i != values.end()) return i->second;
static const attribute_value empty_attribute;
return empty_attribute;
}
const config::attribute_value *config::get(const std::string &key) const
{
check_valid();
attribute_map::const_iterator i = values.find(key);
return i != values.end() ? &i->second : NULL;
}
config::attribute_value &config::operator[](const std::string &key)
{
check_valid();
return values[key];
}
const config::attribute_value &config::get_old_attribute(const std::string &key, const std::string &old_key, const std::string &msg) const
{
check_valid();
attribute_map::const_iterator i = values.find(key);
if (i != values.end())
return i->second;
i = values.find(old_key);
if (i != values.end()) {
if (!msg.empty())
lg::wml_error() << msg;
return i->second;
}
static const attribute_value empty_attribute;
return empty_attribute;
}
void config::merge_attributes(const config &cfg)
{
check_valid(cfg);
assert(this != &cfg);
BOOST_FOREACH(const attribute &v, cfg.values) {
std::string key = v.first;
if (key.substr(0,7) == "add_to_") {
std::string add_to = key.substr(7);
values[add_to] = values[add_to].to_int() + v.second.to_int();
} else
values[v.first] = v.second;
}
}
config::const_attr_itors config::attribute_range() const
{
check_valid();
return const_attr_itors(const_attribute_iterator(values.begin()),
const_attribute_iterator(values.end()));
}
namespace {
struct config_has_value {
config_has_value(const std::string& name, const std::string& value)
: name_(name), value_()
{
value_ = value;
}
bool operator()(const config* cfg) const { return (*cfg)[name_] == value_; }
private:
std::string name_;
config::attribute_value value_;
};
} // end namespace
config &config::find_child(const std::string &key, const std::string &name,
const std::string &value)
{
check_valid();
const child_map::iterator i = children.find(key);
if(i == children.end()) {
DBG_CF << "Key »" << name << "« value »" << value
<< "« pair not found as child of key »" << key << "«.\n";
return invalid;
}
const child_list::iterator j = std::find_if(i->second.begin(),
i->second.end(),
config_has_value(name,value));
if(j != i->second.end()) {
return **j;
} else {
DBG_CF << "Key »" << name << "« value »" << value
<< "« pair not found as child of key »" << key << "«.\n";
return invalid;
}
}
namespace {
/**
* Helper struct for iterative config clearing.
*/
struct config_clear_state
{
config_clear_state()
: c(NULL)
, mi()
, vi(0)
{
}
config* c; //the config being inspected
config::child_map::iterator mi; //current child map entry
size_t vi; //index into the child map item vector
};
}