Kea: data.cc Source File

| Kea 3.2.0-git

data.cc
Go to the documentation of this file.
1// Copyright (C) 2010-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/data.h>
10#include <util/bigints.h>
11
12#include <cstring>
13#include <cassert>
14#include <climits>
15#include <list>
16#include <map>
17#include <cstdio>
18#include <iostream>
19#include <iomanip>
20#include <set>
21#include <string>
22#include <sstream>
23#include <fstream>
24#include <cerrno>
25
26#include <boost/lexical_cast.hpp>
27
28#include <cmath>
29
30using namespace std;
31
32using isc::util::int128_t;
33
34namespace {
35const char* const WHITESPACE = " \b\f\n\r\t";
36} // end anonymous namespace
37
38namespace isc {
39namespace data {
40
41constexpr unsigned Element::MAX_NESTING_LEVEL;
42
43std::string
44Element::Position::str() const {
45 std::ostringstream ss;
46 ss << file_ << ":" << line_ << ":" << pos_;
47 return (ss.str());
48}
49
50std::ostream&
51 operator<<(std::ostream& out, const Element::Position& pos) {
52 out << pos.str();
53 return (out);
54}
55
56void
57Element::removeEmptyContainersRecursively(unsigned level) {
58 if (level <= 0) {
59 // Cycles are by definition not empty so no need to throw.
60 return;
61 }
62 if (type_ == list || type_ == map) {
63 size_t s(size());
64 for (size_t i = 0; i < s; ++i) {
65 // Get child.
66 ElementPtr child;
67 if (type_ == list) {
68 child = getNonConst(i);
69 } else if (type_ == map) {
70 std::string const key(get(i)->stringValue());
71 // The ElementPtr - ConstElementPtr disparity between
72 // ListElement and MapElement is forcing a const cast here.
73 // It's undefined behavior to modify it after const casting.
74 // The options are limited. I've tried templating, moving
75 // this function from a member function to free-standing and
76 // taking the Element template as argument. I've tried
77 // making it a virtual function with overridden
78 // implementations in ListElement and MapElement. Nothing
79 // works.
80 child = boost::const_pointer_cast<Element>(get(key));
81 }
82
83 // Makes no sense to continue for non-container children.
84 if (child->getType() != list && child->getType() != map) {
85 continue;
86 }
87
88 // Recurse if not empty.
89 if (!child->empty()){
90 child->removeEmptyContainersRecursively(level - 1);
91 }
92
93 // When returning from recursion, remove if empty.
94 if (child->empty()) {
95 remove(i);
96 --i;
97 --s;
98 }
99 }
100 }
101}
102
103std::string
104Element::str() const {
105 std::stringstream ss;
106 toJSON(ss);
107 return (ss.str());
108}
109
110std::string
111Element::toWire() const {
112 std::stringstream ss;
113 toJSON(ss);
114 return (ss.str());
115}
116
117void
118 Element::toWire(std::ostream& ss) const {
119 toJSON(ss);
120}
121
122bool
123Element::getValue(int64_t&) const {
124 return (false);
125}
126
127bool
128 Element::getValue(double&) const {
129 return (false);
130}
131
132bool
133 Element::getValue(bool&) const {
134 return (false);
135}
136
137bool
138 Element::getValue(std::string&) const {
139 return (false);
140}
141
142bool
143 Element::getValue(std::vector<ElementPtr>&) const {
144 return (false);
145}
146
147bool
148 Element::getValue(std::map<std::string, ConstElementPtr>&) const {
149 return (false);
150}
151
152bool
153Element::setValue(const long long int) {
154 return (false);
155}
156
157bool
158 Element::setValue(isc::util::int128_t const&) {
159 return (false);
160}
161
162bool
163 Element::setValue(const double) {
164 return (false);
165}
166
167bool
168 Element::setValue(const bool) {
169 return (false);
170}
171
172bool
173 Element::setValue(const std::string&) {
174 return (false);
175}
176
177bool
178 Element::setValue(const std::vector<ElementPtr>&) {
179 return (false);
180}
181
182bool
183 Element::setValue(const std::map<std::string, ConstElementPtr>&) {
184 return (false);
185}
186
187 ConstElementPtr
188Element::get(const int) const {
189 throwTypeError("get(int) called on a non-container Element");
190}
191
192 ElementPtr
193Element::getNonConst(const int) const {
194 throwTypeError("get(int) called on a non-container Element");
195}
196
197void
198Element::set(const size_t, ElementPtr) {
199 throwTypeError("set(int, element) called on a non-list Element");
200}
201
202void
203Element::add(ElementPtr) {
204 throwTypeError("add() called on a non-list Element");
205}
206
207void
208Element::remove(const int) {
209 throwTypeError("remove(int) called on a non-container Element");
210}
211
212size_t
213Element::size() const {
214 throwTypeError("size() called on a non-list Element");
215}
216
217bool
218Element::empty() const {
219 throwTypeError("empty() called on a non-container Element");
220}
221
222 ConstElementPtr
223 Element::get(const std::string&) const {
224 throwTypeError("get(string) called on a non-map Element");
225}
226
227void
228 Element::set(const std::string&, ConstElementPtr) {
229 throwTypeError("set(name, element) called on a non-map Element");
230}
231
232void
233 Element::remove(const std::string&) {
234 throwTypeError("remove(string) called on a non-map Element");
235}
236
237bool
238Element::contains(const std::string&) const {
239 throwTypeError("contains(string) called on a non-map Element");
240}
241
242 ConstElementPtr
243Element::find(const std::string&) const {
244 throwTypeError("find(string) called on a non-map Element");
245}
246
247bool
248 Element::find(const std::string&, ConstElementPtr&) const {
249 return (false);
250}
251
252namespace {
253inline void
254throwJSONError(const std::string& error, const std::string& file, int line,
255 int pos) {
256 std::stringstream ss;
257 ss << error << " in " + file + ":" << line << ":" << pos;
258 isc_throw(JSONError, ss.str());
259}
260} // end anonymous namespace
261
262std::ostream&
263 operator<<(std::ostream& out, const Element& e) {
264 return (out << e.str());
265}
266
267bool
268operator==(const Element& a, const Element& b) {
269 return (a.equals(b));
270}
271
272 bool operator!=(const Element& a, const Element& b) {
273 return (!a.equals(b));
274}
275
276 bool
277 operator<(Element const& a, Element const& b) {
278 if (a.getType() != b.getType()) {
279 isc_throw(BadValue, "cannot compare Elements of different types");
280 }
281 switch (a.getType()) {
282 case Element::integer:
283 return a.intValue() < b.intValue();
284 case Element::real:
285 return a.doubleValue() < b.doubleValue();
286 case Element::boolean:
287 return b.boolValue() || !a.boolValue();
288 case Element::string:
289 return std::strcmp(a.stringValue().c_str(), b.stringValue().c_str()) < 0;
290 default:
291 isc_throw(BadValue, "cannot compare Elements of type " << to_string(a.getType()));
292 }
293}
294
295//
296// factory functions
297//
298 ElementPtr
299Element::create(const Position& pos) {
300 return (ElementPtr(new NullElement(pos)));
301}
302
303 ElementPtr
304 Element::create(const long long int i, const Position& pos) {
305 return (ElementPtr(new IntElement(static_cast<int64_t>(i), pos)));
306}
307
308 ElementPtr
309 Element::create(const isc::util::int128_t& i, const Position& pos) {
310 return (ElementPtr(new BigIntElement(i, pos)));
311}
312
313 ElementPtr
314 Element::create(const int i, const Position& pos) {
315 return (create(static_cast<long long int>(i), pos));
316}
317
318 ElementPtr
319 Element::create(const long int i, const Position& pos) {
320 return (create(static_cast<long long int>(i), pos));
321}
322
323 ElementPtr
324 Element::create(const uint32_t i, const Position& pos) {
325 return (create(static_cast<long long int>(i), pos));
326}
327
328 ElementPtr
329 Element::create(const double d, const Position& pos) {
330 return (ElementPtr(new DoubleElement(d, pos)));
331}
332
333 ElementPtr
334 Element::create(const bool b, const Position& pos) {
335 return (ElementPtr(new BoolElement(b, pos)));
336}
337
338 ElementPtr
339 Element::create(const std::string& s, const Position& pos) {
340 return (ElementPtr(new StringElement(s, pos)));
341}
342
343 ElementPtr
344 Element::create(const char *s, const Position& pos) {
345 return (create(std::string(s), pos));
346}
347
348 ElementPtr
349Element::createList(const Position& pos) {
350 return (ElementPtr(new ListElement(pos)));
351}
352
353 ElementPtr
354Element::createMap(const Position& pos) {
355 return (ElementPtr(new MapElement(pos)));
356}
357
358
359//
360// helper functions for fromJSON factory
361//
362namespace {
363bool
364charIn(const int c, const char* chars) {
365 const size_t chars_len = std::strlen(chars);
366 for (size_t i = 0; i < chars_len; ++i) {
367 if (chars[i] == c) {
368 return (true);
369 }
370 }
371 return (false);
372}
373
374void
375skipChars(std::istream& in, const char* chars, int& line, int& pos) {
376 int c = in.peek();
377 while (charIn(c, chars) && c != EOF) {
378 if (c == '\n') {
379 ++line;
380 pos = 1;
381 } else {
382 ++pos;
383 }
384 in.ignore();
385 c = in.peek();
386 }
387}
388
389// skip on the input stream to one of the characters in chars
390// if another character is found this function throws JSONError
391// unless that character is specified in the optional may_skip
392//
393// It returns the found character (as an int value).
394int
395skipTo(std::istream& in, const std::string& file, int& line, int& pos,
396 const char* chars, const char* may_skip="") {
397 int c = in.get();
398 ++pos;
399 while (c != EOF) {
400 if (c == '\n') {
401 pos = 1;
402 ++line;
403 }
404 if (charIn(c, may_skip)) {
405 c = in.get();
406 ++pos;
407 } else if (charIn(c, chars)) {
408 while (charIn(in.peek(), may_skip)) {
409 if (in.peek() == '\n') {
410 pos = 1;
411 ++line;
412 } else {
413 ++pos;
414 }
415 in.ignore();
416 }
417 return (c);
418 } else {
419 throwJSONError(std::string("'") + std::string(1, c) + "' read, one of \"" + chars + "\" expected", file, line, pos);
420 }
421 }
422 throwJSONError(std::string("EOF read, one of \"") + chars + "\" expected", file, line, pos);
423 return (c); // shouldn't reach here, but some compilers require it
424}
425
426// TODO: Should we check for all other official escapes here (and
427// error on the rest)?
428std::string
429strFromStringstream(std::istream& in, const std::string& file,
430 const int line, int& pos) {
431 std::stringstream ss;
432 int c = in.get();
433 ++pos;
434 if (c == '"') {
435 c = in.get();
436 ++pos;
437 } else {
438 throwJSONError("String expected", file, line, pos);
439 }
440
441 while (c != EOF && c != '"') {
442 if (c == '\\') {
443 // see the spec for allowed escape characters
444 int d;
445 switch (in.peek()) {
446 case '"':
447 c = '"';
448 break;
449 case '/':
450 c = '/';
451 break;
452 case '\\':
453 c = '\\';
454 break;
455 case 'b':
456 c = '\b';
457 break;
458 case 'f':
459 c = '\f';
460 break;
461 case 'n':
462 c = '\n';
463 break;
464 case 'r':
465 c = '\r';
466 break;
467 case 't':
468 c = '\t';
469 break;
470 case 'u':
471 // skip first 0
472 in.ignore();
473 ++pos;
474 c = in.peek();
475 if (c != '0') {
476 throwJSONError("Unsupported unicode escape", file, line, pos);
477 }
478 // skip second 0
479 in.ignore();
480 ++pos;
481 c = in.peek();
482 if (c != '0') {
483 throwJSONError("Unsupported unicode escape", file, line, pos - 2);
484 }
485 // get first digit
486 in.ignore();
487 ++pos;
488 d = in.peek();
489 if ((d >= '0') && (d <= '9')) {
490 c = (d - '0') << 4;
491 } else if ((d >= 'A') && (d <= 'F')) {
492 c = (d - 'A' + 10) << 4;
493 } else if ((d >= 'a') && (d <= 'f')) {
494 c = (d - 'a' + 10) << 4;
495 } else {
496 throwJSONError("Not hexadecimal in unicode escape", file, line, pos - 3);
497 }
498 // get second digit
499 in.ignore();
500 ++pos;
501 d = in.peek();
502 if ((d >= '0') && (d <= '9')) {
503 c |= d - '0';
504 } else if ((d >= 'A') && (d <= 'F')) {
505 c |= d - 'A' + 10;
506 } else if ((d >= 'a') && (d <= 'f')) {
507 c |= d - 'a' + 10;
508 } else {
509 throwJSONError("Not hexadecimal in unicode escape", file, line, pos - 4);
510 }
511 break;
512 default:
513 throwJSONError("Bad escape", file, line, pos);
514 }
515 // drop the escaped char
516 in.ignore();
517 ++pos;
518 }
519 ss.put(c);
520 c = in.get();
521 ++pos;
522 }
523 if (c == EOF) {
524 throwJSONError("Unterminated string", file, line, pos);
525 }
526 return (ss.str());
527}
528
529std::string
530wordFromStringstream(std::istream& in, int& pos) {
531 std::stringstream ss;
532 while (isalpha(in.peek())) {
533 ss << (char) in.get();
534 }
535 pos += ss.str().size();
536 return (ss.str());
537}
538
539std::string
540numberFromStringstream(std::istream& in, int& pos) {
541 std::stringstream ss;
542 while (isdigit(in.peek()) || in.peek() == '+' || in.peek() == '-' ||
543 in.peek() == '.' || in.peek() == 'e' || in.peek() == 'E') {
544 ss << (char) in.get();
545 }
546 pos += ss.str().size();
547 return (ss.str());
548}
549
550// Should we change from IntElement and DoubleElement to NumberElement
551// that can also hold an e value? (and have specific getters if the
552// value is larger than an int can handle)
553//
554// At the moment of writing, the only way that the code flow can reach the
555// int128_t cast, and not throw, is by retrieving one of the few bigint
556// statistics using this method so the cast to int128_t can be removed
557// as there is no deserialization of bigints required, although the only
558// benefit would be better performance for error cases, so it's arguable.
559 ElementPtr
560fromStringstreamNumber(std::istream& in, const std::string& file,
561 const int line, int& pos) {
562 // Remember position where the value starts. It will be set in the
563 // Position structure of the Element to be created.
564 const uint32_t start_pos = pos;
565 // This will move the pos to the end of the value.
566 const std::string number = numberFromStringstream(in, pos);
567
568 // Catch leading zeros: raise an error as logging is not available.
569 if (((number.size() > 1) && (number[0] == '0') && isdigit(number[1])) ||
570 ((number.size() > 2) && (number[0] == '-') &&
571 (number[1] == '0') && isdigit(number[2]))) {
572 throwJSONError("Illegal leading zeros in '" + number + "'",
573 file, line, start_pos);
574 }
575 // Is it a double?
576 if (number.find_first_of(".eE") < number.size()) {
577 try {
578 return (Element::create(boost::lexical_cast<double>(number),
579 Element::Position(file, line, start_pos)));
580 } catch (const boost::bad_lexical_cast& exception) {
581 throwJSONError("Number overflow while trying to cast '" + number +
582 "' to double: " + exception.what(),
583 file, line, start_pos);
584 }
585 }
586
587 // Is it an integer?
588 try {
589 return (Element::create(boost::lexical_cast<int64_t>(number),
590 Element::Position(file, line, start_pos)));
591 } catch (const boost::bad_lexical_cast& exception64) {
592 // Is it a big integer?
593 try {
594 return (Element::create(int128_t(number),
595 Element::Position(file, line, start_pos)));
596 } catch (overflow_error const& exception128) {
597 throwJSONError("Number overflow while trying to cast '" + number +
598 "' to int64 and subsequently to int128: " +
599 exception64.what() + ", " + exception128.what(),
600 file, line, start_pos);
601 }
602 }
603 return (ElementPtr());
604}
605
606 ElementPtr
607fromStringstreamBool(std::istream& in, const std::string& file,
608 const int line, int& pos) {
609 // Remember position where the value starts. It will be set in the
610 // Position structure of the Element to be created.
611 const uint32_t start_pos = pos;
612 // This will move the pos to the end of the value.
613 const std::string word = wordFromStringstream(in, pos);
614
615 if (word == "true") {
616 return (Element::create(true, Element::Position(file, line,
617 start_pos)));
618 } else if (word == "false") {
619 return (Element::create(false, Element::Position(file, line,
620 start_pos)));
621 } else {
622 throwJSONError(std::string("Bad boolean value: ") + word, file,
623 line, start_pos);
624 }
625 return (ElementPtr());
626}
627
628 ElementPtr
629fromStringstreamNull(std::istream& in, const std::string& file,
630 const int line, int& pos) {
631 // Remember position where the value starts. It will be set in the
632 // Position structure of the Element to be created.
633 const uint32_t start_pos = pos;
634 // This will move the pos to the end of the value.
635 const std::string word = wordFromStringstream(in, pos);
636 if (word == "null") {
637 return (Element::create(Element::Position(file, line, start_pos)));
638 } else {
639 throwJSONError(std::string("Bad null value: ") + word, file,
640 line, start_pos);
641 return (ElementPtr());
642 }
643}
644
645 ElementPtr
646fromStringstreamString(std::istream& in, const std::string& file, int& line,
647 int& pos) {
648 // Remember position where the value starts. It will be set in the
649 // Position structure of the Element to be created.
650 const uint32_t start_pos = pos;
651 // This will move the pos to the end of the value.
652 const std::string string_value = strFromStringstream(in, file, line, pos);
653 return (Element::create(string_value, Element::Position(file, line,
654 start_pos)));
655}
656
657 ElementPtr
658fromStringstreamList(std::istream& in, const std::string& file, int& line,
659 int& pos, unsigned level) {
660 if (level == 0) {
661 isc_throw(JSONError, "fromJSON elements nested too deeply");
662 }
663 int c = 0;
664 ElementPtr list = Element::createList(Element::Position(file, line, pos));
665 ElementPtr cur_list_element;
666
667 skipChars(in, WHITESPACE, line, pos);
668 while (c != EOF && c != ']') {
669 if (in.peek() != ']') {
670 cur_list_element =
671 Element::fromJSON(in, file, line, pos, level - 1);
672 list->add(cur_list_element);
673 c = skipTo(in, file, line, pos, ",]", WHITESPACE);
674 } else {
675 c = in.get();
676 ++pos;
677 }
678 }
679 return (list);
680}
681
682 ElementPtr
683fromStringstreamMap(std::istream& in, const std::string& file, int& line,
684 int& pos, unsigned level) {
685 if (level == 0) {
686 isc_throw(JSONError, "fromJSON elements nested too deeply");
687 }
688 ElementPtr map = Element::createMap(Element::Position(file, line, pos));
689 skipChars(in, WHITESPACE, line, pos);
690 int c = in.peek();
691 if (c == EOF) {
692 throwJSONError(std::string("Unterminated map, <string> or } expected"), file, line, pos);
693 } else if (c == '}') {
694 // empty map, skip closing curly
695 in.ignore();
696 } else {
697 while (c != EOF && c != '}') {
698 std::string key = strFromStringstream(in, file, line, pos);
699
700 skipTo(in, file, line, pos, ":", WHITESPACE);
701 // skip the :
702
703 ConstElementPtr value =
704 Element::fromJSON(in, file, line, pos, level - 1);
705 map->set(key, value);
706
707 c = skipTo(in, file, line, pos, ",}", WHITESPACE);
708 }
709 }
710 return (map);
711}
712} // end anonymous namespace
713
714std::string
715Element::typeToName(Element::types type) {
716 switch (type) {
717 case Element::integer:
718 return (std::string("integer"));
719 case Element::bigint:
720 return (std::string("bigint"));
721 case Element::real:
722 return (std::string("real"));
723 case Element::boolean:
724 return (std::string("boolean"));
725 case Element::string:
726 return (std::string("string"));
727 case Element::list:
728 return (std::string("list"));
729 case Element::map:
730 return (std::string("map"));
731 case Element::null:
732 return (std::string("null"));
733 case Element::any:
734 return (std::string("any"));
735 default:
736 return (std::string("unknown"));
737 }
738}
739
740 Element::types
741Element::nameToType(const std::string& type_name) {
742 if (type_name == "integer") {
743 return (Element::integer);
744 } else if (type_name == "bigint") {
745 return (Element::bigint);
746 } else if (type_name == "real") {
747 return (Element::real);
748 } else if (type_name == "boolean") {
749 return (Element::boolean);
750 } else if (type_name == "string") {
751 return (Element::string);
752 } else if (type_name == "list") {
753 return (Element::list);
754 } else if (type_name == "map") {
755 return (Element::map);
756 } else if (type_name == "named_set") {
757 return (Element::map);
758 } else if (type_name == "null") {
759 return (Element::null);
760 } else if (type_name == "any") {
761 return (Element::any);
762 } else {
763 isc_throw(TypeError, type_name + " is not a valid type name");
764 }
765}
766
767 ElementPtr
768 Element::fromJSON(std::istream& in, bool preproc) {
769
770 int line = 1, pos = 1;
771 stringstream filtered;
772 if (preproc) {
773 preprocess(in, filtered);
774 }
775
776 ElementPtr value = fromJSON(preproc ? filtered : in, "<istream>", line, pos);
777
778 return (value);
779}
780
781 ElementPtr
782 Element::fromJSON(std::istream& in, const std::string& file_name, bool preproc) {
783 int line = 1, pos = 1;
784 stringstream filtered;
785 if (preproc) {
786 preprocess(in, filtered);
787 }
788 return (fromJSON(preproc ? filtered : in, file_name, line, pos));
789}
790
791 ElementPtr
792 Element::fromJSON(std::istream& in, const std::string& file, int& line,
793 int& pos, unsigned level) {
794 if (level == 0) {
795 isc_throw(JSONError, "fromJSON elements nested too deeply");
796 }
797 int c = 0;
798 ElementPtr element;
799 bool el_read = false;
800 skipChars(in, WHITESPACE, line, pos);
801 while (c != EOF && !el_read) {
802 c = in.get();
803 pos++;
804 switch(c) {
805 case '1':
806 case '2':
807 case '3':
808 case '4':
809 case '5':
810 case '6':
811 case '7':
812 case '8':
813 case '9':
814 case '0':
815 case '-':
816 case '.':
817 in.putback(c);
818 --pos;
819 element = fromStringstreamNumber(in, file, line, pos);
820 el_read = true;
821 break;
822 case 't':
823 case 'f':
824 in.putback(c);
825 --pos;
826 element = fromStringstreamBool(in, file, line, pos);
827 el_read = true;
828 break;
829 case 'n':
830 in.putback(c);
831 --pos;
832 element = fromStringstreamNull(in, file, line, pos);
833 el_read = true;
834 break;
835 case '"':
836 in.putback('"');
837 --pos;
838 element = fromStringstreamString(in, file, line, pos);
839 el_read = true;
840 break;
841 case '[':
842 element = fromStringstreamList(in, file, line, pos, level);
843 el_read = true;
844 break;
845 case '{':
846 element = fromStringstreamMap(in, file, line, pos, level);
847 el_read = true;
848 break;
849 case EOF:
850 break;
851 default:
852 throwJSONError(std::string("error: unexpected character ") + std::string(1, c), file, line, pos);
853 break;
854 }
855 }
856 if (el_read) {
857 return (element);
858 } else {
859 isc_throw(JSONError, "nothing read");
860 }
861}
862
863 ElementPtr
864Element::fromJSON(const std::string& in, bool preproc) {
865 std::stringstream ss;
866 ss << in;
867
868 int line = 1, pos = 1;
869 stringstream filtered;
870 if (preproc) {
871 preprocess(ss, filtered);
872 }
873 ElementPtr result(fromJSON(preproc ? filtered : ss, "<string>", line, pos));
874 skipChars(ss, WHITESPACE, line, pos);
875 // ss must now be at end
876 if (ss.peek() != EOF) {
877 throwJSONError("Extra data", "<string>", line, pos);
878 }
879 return result;
880}
881
882 ElementPtr
883Element::fromJSONFile(const std::string& file_name, bool preproc) {
884 // zero out the errno to be safe
885 errno = 0;
886
887 std::ifstream infile(file_name.c_str(), std::ios::in | std::ios::binary);
888 if (!infile.is_open()) {
889 const char* error = strerror(errno);
890 isc_throw(InvalidOperation, "failed to read file '" << file_name
891 << "': " << error);
892 }
893
894 return (fromJSON(infile, file_name, preproc));
895}
896
897// to JSON format
898
899void
900IntElement::toJSON(std::ostream& ss, unsigned) const {
901 ss << intValue();
902}
903
904void
905BigIntElement::toJSON(std::ostream& ss, unsigned) const {
906 ss << bigIntValue();
907}
908
909void
910DoubleElement::toJSON(std::ostream& ss, unsigned) const {
911 // The default output for doubles nicely drops off trailing
912 // zeros, however this produces strings without decimal points
913 // for whole number values. When reparsed this will create
914 // IntElements not DoubleElements. Rather than used a fixed
915 // precision, we'll just tack on an ".0" when there is no
916 // fractional nor exponent parts.
917 ostringstream val_ss;
918 val_ss << doubleValue();
919 ss << val_ss.str();
920 if (val_ss.str().find_first_of(".eE") == string::npos) {
921 ss << ".0";
922 }
923}
924
925void
926BoolElement::toJSON(std::ostream& ss, unsigned) const {
927 if (boolValue()) {
928 ss << "true";
929 } else {
930 ss << "false";
931 }
932}
933
934void
935NullElement::toJSON(std::ostream& ss, unsigned) const {
936 ss << "null";
937}
938
939void
940StringElement::toJSON(std::ostream& ss, unsigned) const {
941 ss << "\"";
942 const std::string& str = stringValue();
943 for (size_t i = 0; i < str.size(); ++i) {
944 const signed char c = str[i];
945 // Escape characters as defined in JSON spec
946 // Note that we do not escape forward slash; this
947 // is allowed, but not mandatory.
948 switch (c) {
949 case '"':
950 ss << '\\' << c;
951 break;
952 case '\\':
953 ss << '\\' << c;
954 break;
955 case '\b':
956 ss << '\\' << 'b';
957 break;
958 case '\f':
959 ss << '\\' << 'f';
960 break;
961 case '\n':
962 ss << '\\' << 'n';
963 break;
964 case '\r':
965 ss << '\\' << 'r';
966 break;
967 case '\t':
968 ss << '\\' << 't';
969 break;
970 default:
971 if (c < 0x20 || c == 0x7f) {
972 std::ostringstream esc;
973 esc << "\\u"
974 << hex
975 << setw(4)
976 << setfill('0')
977 << (static_cast<unsigned>(c) & 0xff);
978 ss << esc.str();
979 } else {
980 ss << c;
981 }
982 }
983 }
984 ss << "\"";
985}
986
987void
988ListElement::toJSON(std::ostream& ss, unsigned level) const {
989 if (level == 0) {
990 isc_throw(BadValue, "toJSON got infinite recursion: "
991 "arguments include cycles");
992 }
993 ss << "[ ";
994
995 const std::vector<ElementPtr>& v = listValue();
996 bool first = true;
997 for (auto const& it : v) {
998 if (!first) {
999 ss << ", ";
1000 } else {
1001 first = false;
1002 }
1003 it->toJSON(ss, level - 1);
1004 }
1005 ss << " ]";
1006}
1007
1008void
1009MapElement::toJSON(std::ostream& ss, unsigned level) const {
1010 if (level == 0) {
1011 isc_throw(BadValue, "toJSON got infinite recursion: "
1012 "arguments include cycles");
1013 }
1014 ss << "{ ";
1015
1016 bool first = true;
1017 for (auto const& it : m) {
1018 if (!first) {
1019 ss << ", ";
1020 } else {
1021 first = false;
1022 }
1023 ss << "\"" << it.first << "\": ";
1024 if (it.second) {
1025 it.second->toJSON(ss, level - 1);
1026 } else {
1027 ss << "None";
1028 }
1029 }
1030 ss << " }";
1031}
1032
1033// throws when one of the types in the path (except the one
1034// we're looking for) is not a MapElement
1035// returns 0 if it could simply not be found
1036// should that also be an exception?
1037 ConstElementPtr
1038MapElement::find(const std::string& id) const {
1039 const size_t sep = id.find('/');
1040 if (sep == std::string::npos) {
1041 return (get(id));
1042 } else {
1043 ConstElementPtr ce = get(id.substr(0, sep));
1044 if (ce) {
1045 // ignore trailing slash
1046 if (sep + 1 != id.size()) {
1047 return (ce->find(id.substr(sep + 1)));
1048 } else {
1049 return (ce);
1050 }
1051 } else {
1052 return (ElementPtr());
1053 }
1054 }
1055}
1056
1057 ElementPtr
1058 Element::fromWire(const std::string& s) {
1059 std::stringstream ss;
1060 ss << s;
1061 int line = 0, pos = 0;
1062 return (fromJSON(ss, "<wire>", line, pos));
1063}
1064
1065 ElementPtr
1066Element::fromWire(std::stringstream& in, int) {
1067 //
1068 // Check protocol version
1069 //
1070 //for (int i = 0 ; i < 4 ; ++i) {
1071 // const unsigned char version_byte = get_byte(in);
1072 // if (PROTOCOL_VERSION[i] != version_byte) {
1073 // throw DecodeError("Protocol version incorrect");
1074 // }
1075 //}
1076 //length -= 4;
1077 int line = 0, pos = 0;
1078 return (fromJSON(in, "<wire>", line, pos));
1079}
1080
1081void
1082MapElement::set(const std::string& key, ConstElementPtr value) {
1083 m[key] = value;
1084}
1085
1086bool
1087 MapElement::find(const std::string& id, ConstElementPtr& t) const {
1088 try {
1089 ConstElementPtr p = find(id);
1090 if (p) {
1091 t = p;
1092 return (true);
1093 }
1094 } catch (const TypeError&) {
1095 // ignore
1096 }
1097 return (false);
1098}
1099
1100bool
1101IntElement::equals(const Element& other, unsigned) const {
1102 // Let's not be very picky with constraining the integer types to be the
1103 // same. Equality is sometimes checked from high-up in the Element hierarchy.
1104 // That is a context which, most of the time, does not have information on
1105 // the type of integers stored on Elements lower in the hierarchy. So it
1106 // would be difficult to differentiate between the integer types.
1107 return (other.getType() == Element::integer && i == other.intValue()) ||
1108 (other.getType() == Element::bigint && i == other.bigIntValue());
1109}
1110
1111bool
1112BigIntElement::equals(const Element& other, unsigned) const {
1113 // Let's not be very picky with constraining the integer types to be the
1114 // same. Equality is sometimes checked from high-up in the Element hierarchy.
1115 // That is a context which, most of the time, does not have information on
1116 // the type of integers stored on Elements lower in the hierarchy. So it
1117 // would be difficult to differentiate between the integer types.
1118 return (other.getType() == Element::bigint && i_ == other.bigIntValue()) ||
1119 (other.getType() == Element::integer && i_ == other.intValue());
1120}
1121
1122bool
1123DoubleElement::equals(const Element& other, unsigned) const {
1124 return (other.getType() == Element::real) &&
1125 (fabs(d - other.doubleValue()) < 1e-14);
1126}
1127
1128bool
1129BoolElement::equals(const Element& other, unsigned) const {
1130 return (other.getType() == Element::boolean) &&
1131 (b == other.boolValue());
1132}
1133
1134bool
1135NullElement::equals(const Element& other, unsigned) const {
1136 return (other.getType() == Element::null);
1137}
1138
1139bool
1140StringElement::equals(const Element& other, unsigned) const {
1141 return (other.getType() == Element::string) &&
1142 (s == other.stringValue());
1143}
1144
1145bool
1146ListElement::equals(const Element& other, unsigned level) const {
1147 if (level == 0) {
1148 isc_throw(BadValue, "equals got infinite recursion: "
1149 "arguments include cycles");
1150 }
1151 if (other.getType() == Element::list) {
1152 const size_t s = size();
1153 if (s != other.size()) {
1154 return (false);
1155 }
1156 for (size_t i = 0; i < s; ++i) {
1157 if (!get(i)->equals(*other.get(i), level - 1)) {
1158 return (false);
1159 }
1160 }
1161 return (true);
1162 } else {
1163 return (false);
1164 }
1165}
1166
1167void
1168ListElement::sort(std::string const& index /* = std::string() */) {
1169 if (l.empty()) {
1170 return;
1171 }
1172
1173 int const t(l.at(0)->getType());
1174 std::function<bool(ElementPtr, ElementPtr)> comparator;
1175 if (t == map) {
1176 if (index.empty()) {
1177 isc_throw(BadValue, "index required when sorting maps");
1178 }
1179 comparator = [&](ElementPtr const& a, ElementPtr const& b) {
1180 ConstElementPtr const& ai(a->get(index));
1181 ConstElementPtr const& bi(b->get(index));
1182 if (ai && bi) {
1183 return *ai < *bi;
1184 }
1185 return true;
1186 };
1187 } else if (t == list) {
1188 // Nested lists. Not supported.
1189 return;
1190 } else {
1191 // Assume scalars.
1192 if (!index.empty()) {
1193 isc_throw(BadValue, "index given when sorting scalars?");
1194 }
1195 comparator = [&](ElementPtr const& a, ElementPtr const& b) {
1196 return *a < *b;
1197 };
1198 }
1199
1200 std::sort(l.begin(), l.end(), comparator);
1201}
1202
1203bool
1204MapElement::equals(const Element& other, unsigned level) const {
1205 if (level == 0) {
1206 isc_throw(BadValue, "equals got infinite recursion: "
1207 "arguments include cycles");
1208 }
1209 if (other.getType() == Element::map) {
1210 if (size() != other.size()) {
1211 return (false);
1212 }
1213 for (auto const& kv : mapValue()) {
1214 auto key = kv.first;
1215 if (other.contains(key)) {
1216 if (!get(key)->equals(*other.get(key), level - 1)) {
1217 return (false);
1218 }
1219 } else {
1220 return (false);
1221 }
1222 }
1223 return (true);
1224 } else {
1225 return (false);
1226 }
1227}
1228
1229bool
1230isNull(ConstElementPtr p) {
1231 return (!p);
1232}
1233
1234void
1235removeIdentical(ElementPtr a, ConstElementPtr b) {
1236 if (!b) {
1237 return;
1238 }
1239 if (a->getType() != Element::map || b->getType() != Element::map) {
1240 isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
1241 }
1242
1243 // As maps do not allow entries with multiple keys, we can either iterate
1244 // over a checking for identical entries in b or vice-versa. As elements
1245 // are removed from a if a match is found, we choose to iterate over b to
1246 // avoid problems with element removal affecting the iterator.
1247 for (auto const& kv : b->mapValue()) {
1248 auto key = kv.first;
1249 if (a->contains(key)) {
1250 if (a->get(key)->equals(*b->get(key))) {
1251 a->remove(key);
1252 }
1253 }
1254 }
1255}
1256
1257 ConstElementPtr
1258 removeIdentical(ConstElementPtr a, ConstElementPtr b) {
1259 ElementPtr result = Element::createMap();
1260
1261 if (!b) {
1262 return (result);
1263 }
1264
1265 if (a->getType() != Element::map || b->getType() != Element::map) {
1266 isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
1267 }
1268
1269 for (auto const& kv : a->mapValue()) {
1270 auto key = kv.first;
1271 if (!b->contains(key) ||
1272 !a->get(key)->equals(*b->get(key))) {
1273 result->set(key, kv.second);
1274 }
1275 }
1276
1277 return (result);
1278}
1279
1280void
1281merge(ElementPtr element, ConstElementPtr other) {
1282 if (element->getType() != Element::map ||
1283 other->getType() != Element::map) {
1284 isc_throw(TypeError, "merge arguments not MapElements");
1285 }
1286
1287 for (auto const& kv : other->mapValue()) {
1288 auto key = kv.first;
1289 auto value = kv.second;
1290 if (value && value->getType() != Element::null) {
1291 element->set(key, value);
1292 } else if (element->contains(key)) {
1293 element->remove(key);
1294 }
1295 }
1296}
1297
1298void
1299mergeDiffAdd(ElementPtr& element, ElementPtr& other,
1300 HierarchyDescriptor& hierarchy, std::string key, size_t idx,
1301 unsigned level) {
1302 if (level == 0) {
1303 isc_throw(BadValue, "mergeDiffAdd got infinite recursion: "
1304 "arguments include cycles");
1305 }
1306 if (element->getType() != other->getType()) {
1307 isc_throw(TypeError, "mergeDiffAdd arguments not same type");
1308 }
1309
1310 if (element->getType() == Element::list) {
1311 // Store new elements in a separate container so we don't overwrite
1312 // options as we add them (if there are duplicates).
1313 ElementPtr new_elements = Element::createList();
1314 for (auto const& right : other->listValue()) {
1315 // Check if we have any description of the key in the configuration
1316 // hierarchy.
1317 auto f = hierarchy[idx].find(key);
1318 if (f != hierarchy[idx].end()) {
1319 bool found = false;
1320 ElementPtr mutable_right = boost::const_pointer_cast<Element>(right);
1321 for (auto const& left : element->listValue()) {
1322 ElementPtr mutable_left = boost::const_pointer_cast<Element>(left);
1323 // Check if the elements refer to the same configuration
1324 // entity.
1325 if (f->second.match_(mutable_left, mutable_right)) {
1326 found = true;
1327 mergeDiffAdd(mutable_left, mutable_right, hierarchy,
1328 key, idx, level - 1);
1329 }
1330 }
1331 if (!found) {
1332 new_elements->add(right);
1333 }
1334 } else {
1335 new_elements->add(right);
1336 }
1337 }
1338 // Finally add the new elements.
1339 for (auto const& right : new_elements->listValue()) {
1340 element->add(right);
1341 }
1342 return;
1343 }
1344
1345 if (element->getType() == Element::map) {
1346 for (auto const& kv : other->mapValue()) {
1347 auto current_key = kv.first;
1348 auto value = boost::const_pointer_cast<Element>(kv.second);
1349 if (value && value->getType() != Element::null) {
1350 if (element->contains(current_key) &&
1351 (value->getType() == Element::map ||
1352 value->getType() == Element::list)) {
1353 ElementPtr mutable_element = boost::const_pointer_cast<Element>(element->get(current_key));
1354 mergeDiffAdd(mutable_element, value, hierarchy,
1355 current_key, idx + 1, level - 1);
1356 } else {
1357 element->set(current_key, value);
1358 }
1359 }
1360 }
1361 return;
1362 }
1363 element = other;
1364}
1365
1366void
1367mergeDiffDel(ElementPtr& element, ElementPtr& other,
1368 HierarchyDescriptor& hierarchy, std::string key, size_t idx,
1369 unsigned level) {
1370 if (level == 0) {
1371 isc_throw(BadValue, "mergeDiffDel got infinite recursion: "
1372 "arguments include cycles");
1373 }
1374 if (element->getType() != other->getType()) {
1375 isc_throw(TypeError, "mergeDiffDel arguments not same type");
1376 }
1377
1378 if (element->getType() == Element::list) {
1379 for (auto const& value : other->listValue()) {
1380 ElementPtr mutable_right = boost::const_pointer_cast<Element>(value);
1381 for (uint32_t iter = 0; iter < element->listValue().size();) {
1382 bool removed = false;
1383 // Check if we have any description of the key in the
1384 // configuration hierarchy.
1385 auto f = hierarchy[idx].find(key);
1386 if (f != hierarchy[idx].end()) {
1387 ElementPtr mutable_left = boost::const_pointer_cast<Element>(element->listValue().at(iter));
1388 // Check if the elements refer to the same configuration
1389 // entity.
1390 if (f->second.match_(mutable_left, mutable_right)) {
1391 // Check if the user supplied data only contains
1392 // identification information, so the intent is to
1393 // delete the element, not just element data.
1394 if (f->second.no_data_(mutable_right)) {
1395 element->remove(iter);
1396 removed = true;
1397 } else {
1398 mergeDiffDel(mutable_left, mutable_right,
1399 hierarchy, key, idx, level - 1);
1400 if (mutable_left->empty()) {
1401 element->remove(iter);
1402 removed = true;
1403 }
1404 }
1405 }
1406 } else if (element->listValue().at(iter)->equals(*value)) {
1407 element->remove(iter);
1408 removed = true;
1409 }
1410 if (!removed) {
1411 ++iter;
1412 }
1413 }
1414 }
1415 return;
1416 }
1417
1418 if (element->getType() == Element::map) {
1419 // If the resulting element still contains data, we need to restore the
1420 // key parameters, so we store them here.
1421 ElementPtr new_elements = Element::createMap();
1422 for (auto const& kv : other->mapValue()) {
1423 auto current_key = kv.first;
1424 auto value = boost::const_pointer_cast<Element>(kv.second);
1425 if (value && value->getType() != Element::null) {
1426 if (element->contains(current_key)) {
1427 ElementPtr mutable_element = boost::const_pointer_cast<Element>(element->get(current_key));
1428 if (mutable_element->getType() == Element::map ||
1429 mutable_element->getType() == Element::list) {
1430 mergeDiffDel(mutable_element, value, hierarchy,
1431 current_key, idx + 1, level - 1);
1432 if (mutable_element->empty()) {
1433 element->remove(current_key);
1434 }
1435 } else {
1436 // Check if we have any description of the key in the
1437 // configuration hierarchy.
1438 auto f = hierarchy[idx].find(key);
1439 if (f != hierarchy[idx].end()) {
1440 // Check if the key is used for element
1441 // identification.
1442 if (f->second.is_key_(current_key)) {
1443 // Store the key parameter.
1444 new_elements->set(current_key, mutable_element);
1445 }
1446 }
1447 element->remove(current_key);
1448 }
1449 }
1450 }
1451 }
1452 // If the element still contains data, restore the key elements.
1453 if (element->size()) {
1454 for (auto const& kv : new_elements->mapValue()) {
1455 element->set(kv.first, kv.second);
1456 }
1457 }
1458 return;
1459 }
1460 element = ElementPtr(new NullElement());
1461}
1462
1463void
1464extend(const std::string& container, const std::string& extension,
1465 ElementPtr& element, ElementPtr& other, HierarchyDescriptor& hierarchy,
1466 std::string key, size_t idx, bool alter, unsigned level) {
1467
1468 if (level == 0) {
1469 isc_throw(BadValue, "extend got infinite recursion: "
1470 "arguments include cycles");
1471 }
1472 if (element->getType() != other->getType()) {
1473 isc_throw(TypeError, "extend arguments not same type");
1474 }
1475
1476 if (element->getType() == Element::list) {
1477 for (auto const& right : other->listValue()) {
1478 // Check if we have any description of the key in the configuration
1479 // hierarchy.
1480 auto f = hierarchy[idx].find(key);
1481 if (f != hierarchy[idx].end()) {
1482 ElementPtr mutable_right = boost::const_pointer_cast<Element>(right);
1483 for (auto const& left : element->listValue()) {
1484 ElementPtr mutable_left = boost::const_pointer_cast<Element>(left);
1485 if (container == key) {
1486 alter = true;
1487 }
1488 if (f->second.match_(mutable_left, mutable_right)) {
1489 extend(container, extension, mutable_left, mutable_right,
1490 hierarchy, key, idx, alter, level - 1);
1491 }
1492 }
1493 }
1494 }
1495 return;
1496 }
1497
1498 if (element->getType() == Element::map) {
1499 for (auto const& kv : other->mapValue()) {
1500 auto current_key = kv.first;
1501 auto value = boost::const_pointer_cast<Element>(kv.second);
1502 if (value && value->getType() != Element::null) {
1503 if (element->contains(current_key) &&
1504 (value->getType() == Element::map ||
1505 value->getType() == Element::list)) {
1506 ElementPtr mutable_element = boost::const_pointer_cast<Element>(element->get(current_key));
1507 if (container == key) {
1508 alter = true;
1509 }
1510 extend(container, extension, mutable_element, value,
1511 hierarchy, current_key, idx + 1, alter, level - 1);
1512 } else if (alter && current_key == extension) {
1513 element->set(current_key, value);
1514 }
1515 }
1516 }
1517 return;
1518 }
1519}
1520
1521 ElementPtr
1522copy(ConstElementPtr from, unsigned level) {
1523 if (!from) {
1524 isc_throw(BadValue, "copy got a null pointer");
1525 }
1526
1527 auto pos = from->getPosition();
1528 int from_type = from->getType();
1529 if (from_type == Element::integer) {
1530 return (ElementPtr(new IntElement(from->intValue(), pos)));
1531 } else if (from_type == Element::real) {
1532 return (ElementPtr(new DoubleElement(from->doubleValue(), pos)));
1533 } else if (from_type == Element::boolean) {
1534 return (ElementPtr(new BoolElement(from->boolValue(), pos)));
1535 } else if (from_type == Element::null) {
1536 return (ElementPtr(new NullElement()));
1537 } else if (from_type == Element::string) {
1538 return (ElementPtr(new StringElement(from->stringValue(), pos)));
1539 } else if (from_type == Element::list) {
1540 ElementPtr result = ElementPtr(new ListElement(pos));
1541 for (auto const& elem : from->listValue()) {
1542 if (level == 0) {
1543 result->add(elem);
1544 } else {
1545 result->add(copy(elem, level - 1));
1546 }
1547 }
1548 return (result);
1549 } else if (from_type == Element::map) {
1550 ElementPtr result = ElementPtr(new MapElement(pos));
1551 for (auto const& kv : from->mapValue()) {
1552 auto key = kv.first;
1553 auto value = kv.second;
1554 if (level == 0) {
1555 result->set(key, value);
1556 } else {
1557 result->set(key, copy(value, level - 1));
1558 }
1559 }
1560 return (result);
1561 } else {
1562 isc_throw(BadValue, "copy got an element of type: " << from_type);
1563 }
1564}
1565
1566namespace {
1567
1568// Helper function which blocks infinite recursion
1569bool
1570isEquivalent0(ConstElementPtr a, ConstElementPtr b, unsigned level) {
1571 // check looping forever on cycles
1572 if (!level) {
1573 isc_throw(BadValue, "isEquivalent got infinite recursion: "
1574 "arguments include cycles");
1575 }
1576 if (!a || !b) {
1577 isc_throw(BadValue, "isEquivalent got a null pointer");
1578 }
1579 // check types
1580 if (a->getType() != b->getType()) {
1581 return (false);
1582 }
1583 if (a->getType() == Element::list) {
1584 // check empty
1585 if (a->empty()) {
1586 return (b->empty());
1587 }
1588 // check size
1589 if (a->size() != b->size()) {
1590 return (false);
1591 }
1592
1593 // copy b into a list
1594 const size_t s = a->size();
1595 std::list<ConstElementPtr> l;
1596 for (size_t i = 0; i < s; ++i) {
1597 l.push_back(b->get(i));
1598 }
1599
1600 // iterate on a
1601 for (size_t i = 0; i < s; ++i) {
1602 ConstElementPtr item = a->get(i);
1603 // lookup this item in the list
1604 bool found = false;
1605 for (auto it = l.begin(); it != l.end(); ++it) {
1606 // if found in the list remove it
1607 if (isEquivalent0(item, *it, level - 1)) {
1608 found = true;
1609 l.erase(it);
1610 break;
1611 }
1612 }
1613 // if not found argument differs
1614 if (!found) {
1615 return (false);
1616 }
1617 }
1618
1619 // sanity check: the list must be empty
1620 if (!l.empty()) {
1621 isc_throw(Unexpected, "isEquivalent internal error");
1622 }
1623 return (true);
1624 } else if (a->getType() == Element::map) {
1625 // check sizes
1626 if (a->size() != b->size()) {
1627 return (false);
1628 }
1629 // iterate on the first map
1630 for (auto const& kv : a->mapValue()) {
1631 // get the b value for the given keyword and recurse
1632 ConstElementPtr item = b->get(kv.first);
1633 if (!item || !isEquivalent0(kv.second, item, level - 1)) {
1634 return (false);
1635 }
1636 }
1637 return (true);
1638 } else {
1639 return (a->equals(*b));
1640 }
1641}
1642
1643} // end anonymous namespace
1644
1645bool
1646isEquivalent(ConstElementPtr a, ConstElementPtr b) {
1647 return (isEquivalent0(a, b, 100));
1648}
1649
1650namespace {
1651
1652void
1653prettyPrint0(ConstElementPtr element, std::ostream& out,
1654 unsigned indent, unsigned step, unsigned level) {
1655 if (level == 0) {
1656 isc_throw(BadValue, "prettyPrint got infinite recursion: "
1657 "arguments include cycles");
1658 }
1659 if (!element) {
1660 isc_throw(BadValue, "prettyPrint got a null pointer");
1661 }
1662 if (element->getType() == Element::list) {
1663 // empty list case
1664 if (element->empty()) {
1665 out << "[ ]";
1666 return;
1667 }
1668
1669 // complex ? multiline : oneline
1670 if (!element->get(0)) {
1671 isc_throw(BadValue, "prettyPrint got a null pointer");
1672 }
1673 int first_type = element->get(0)->getType();
1674 bool complex = false;
1675 if ((first_type == Element::list) || (first_type == Element::map)) {
1676 complex = true;
1677 }
1678 std::string separator = complex ? ",\n" : ", ";
1679
1680 // open the list
1681 out << "[" << (complex ? "\n" : " ");
1682
1683 // iterate on items
1684 auto const& l = element->listValue();
1685 bool first = true;
1686 for (auto const& it : l) {
1687 // add the separator if not the first item
1688 if (!first) {
1689 out << separator;
1690 } else {
1691 first = false;
1692 }
1693 // add indentation
1694 if (complex) {
1695 out << std::string(indent + step, ' ');
1696 }
1697 // recursive call
1698 prettyPrint0(it, out, indent + step, step, level - 1);
1699 }
1700
1701 // close the list
1702 if (complex) {
1703 out << "\n" << std::string(indent, ' ');
1704 } else {
1705 out << " ";
1706 }
1707 out << "]";
1708 } else if (element->getType() == Element::map) {
1709 // empty map case
1710 if (element->size() == 0) {
1711 out << "{ }";
1712 return;
1713 }
1714
1715 // open the map
1716 out << "{\n";
1717
1718 // iterate on keyword: value
1719 auto const& m = element->mapValue();
1720 bool first = true;
1721 for (auto const& it : m) {
1722 // add the separator if not the first item
1723 if (first) {
1724 first = false;
1725 } else {
1726 out << ",\n";
1727 }
1728 // add indentation
1729 out << std::string(indent + step, ' ');
1730 // add keyword:
1731 out << "\"" << it.first << "\": ";
1732 // recursive call
1733 prettyPrint0(it.second, out, indent + step, step, level - 1);
1734 }
1735
1736 // close the map
1737 out << "\n" << std::string(indent, ' ') << "}";
1738 } else {
1739 // not a list or a map
1740 element->toJSON(out);
1741 }
1742}
1743
1744} // end anonymous namespace
1745
1746void
1747prettyPrint(ConstElementPtr element, std::ostream& out,
1748 unsigned indent, unsigned step) {
1749 prettyPrint0(element, out, indent, step, Element::MAX_NESTING_LEVEL);
1750}
1751
1752std::string
1753 prettyPrint(ConstElementPtr element, unsigned indent, unsigned step) {
1754 std::stringstream ss;
1755 prettyPrint(element, ss, indent, step);
1756 return (ss.str());
1757}
1758
1759 void Element::preprocess(std::istream& in, std::stringstream& out) {
1760
1761 std::string line;
1762
1763 while (std::getline(in, line)) {
1764 // If this is a comments line, replace it with empty line
1765 // (so the line numbers will still match
1766 if (!line.empty() && line[0] == '#') {
1767 line = "";
1768 }
1769
1770 // getline() removes end line characters. Unfortunately, we need
1771 // it for getting the line numbers right (in case we report an
1772 // error.
1773 out << line;
1774 out << "\n";
1775 }
1776}
1777
1778namespace {
1779
1780// Type of arcs.
1781typedef std::set<ConstElementPtr> Arc;
1782
1783// Helper function walking on the supposed tree.
1784bool
1785IsCircular0(ConstElementPtr element, Arc arc) {
1786 // Sanity check.
1787 if (!element) {
1788 return (false);
1789 }
1790 auto type = element->getType();
1791 // Container?
1792 if ((type != Element::list) && (type != Element::map)) {
1793 return (false);
1794 }
1795 // Empty? A cycle requires at least one element.
1796 if (element->empty()) {
1797 return (false);
1798 }
1799 // In the arc?
1800 if (arc.count(element) > 0) {
1801 return (true);
1802 }
1803 // This requires to work on a copy of the arc but it should be small.
1804 arc.insert(element);
1805 if (type == Element::list) {
1806 for (auto const& it : element->listValue()) {
1807 if (IsCircular0(it, arc)) {
1808 return (true);
1809 }
1810 }
1811 return (false);
1812 }
1813 // The argument is a map.
1814 for (auto const& it : element->mapValue()) {
1815 if (IsCircular0(it.second, arc)) {
1816 return (true);
1817 }
1818 }
1819 return (false);
1820}
1821
1822} // end anonymous namespace
1823
1824bool
1825IsCircular(ConstElementPtr element) {
1826 return (IsCircular0(element, Arc()));
1827}
1828
1829unsigned
1830getNestDepth(ConstElementPtr element, unsigned max_depth) {
1831 if (max_depth == 0U) {
1832 return (0U);
1833 }
1834 if (!element) {
1835 return (0U);
1836 }
1837 unsigned ret = 1U;
1838 if (element->getType() == Element::list) {
1839 for (auto const& i : element->listValue()) {
1840 unsigned sub = getNestDepth(i, max_depth - 1);
1841 if (sub == max_depth - 1) {
1842 return (max_depth);
1843 }
1844 if (sub + 1 > ret) {
1845 ret = sub + 1;
1846 }
1847 }
1848 } else if (element->getType() == Element::map) {
1849 for (auto const& i : element->mapValue()) {
1850 unsigned sub = getNestDepth(i.second, max_depth - 1);
1851 if (sub == max_depth - 1) {
1852 return (max_depth);
1853 }
1854 if (sub + 1 > ret) {
1855 ret = sub + 1;
1856 }
1857 }
1858
1859 }
1860 return (ret);
1861}
1862
1863} // end of isc::data namespace
1864} // end of isc namespace
bigints.h
Element::map
@ map
Definition data.h:160
Element::list
@ list
Definition data.h:159
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition exceptions/exceptions.h:143
isc::data::BigIntElement
Wrapper over int128_t.
Definition data.h:793
isc::data::BigIntElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const override
Converts the Element to JSON format and appends it to the given stringstream.
Definition data.cc:905
isc::data::BigIntElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const override
Checks whether the other Element is equal.
Definition data.cc:1112
isc::data::BoolElement
Definition data.h:858
isc::data::BoolElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1129
isc::data::BoolElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:926
isc::data::DoubleElement
Definition data.h:841
isc::data::DoubleElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:910
isc::data::DoubleElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1123
isc::data::Element
The Element class represents a piece of data, used by the command channel and configuration parts.
Definition data.h:73
isc::data::Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
isc::data::Element::getValue
virtual bool getValue(int64_t &t) const
Get the integer value.
Definition data.cc:123
isc::data::Element::typeToName
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
Definition data.cc:715
isc::data::Element::intValue
virtual int64_t intValue() const
Return the integer value.
Definition data.h:275
isc::data::Element::MAX_NESTING_LEVEL
static constexpr unsigned MAX_NESTING_LEVEL
Maximum nesting level of Element objects.
Definition data.h:86
isc::data::Element::str
std::string str() const
Returns a string representing the Element and all its child elements.
Definition data.cc:104
isc::data::Element::stringValue
virtual std::string stringValue() const
Return the string value.
Definition data.h:295
isc::data::Element::toWire
std::string toWire() const
Returns the wireformat for the Element and all its child elements.
Definition data.cc:111
isc::data::Element::types
types
The types that an Element can hold.
Definition data.h:152
isc::data::Element::bigint
@ bigint
Definition data.h:158
isc::data::Element::real
@ real
Definition data.h:154
isc::data::Element::map
@ map
Definition data.h:160
isc::data::Element::null
@ null
Definition data.h:156
isc::data::Element::integer
@ integer
Definition data.h:153
isc::data::Element::any
@ any
Definition data.h:161
isc::data::Element::boolean
@ boolean
Definition data.h:155
isc::data::Element::list
@ list
Definition data.h:159
isc::data::Element::string
@ string
Definition data.h:157
isc::data::Element::toJSON
virtual void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const =0
Converts the Element to JSON format and appends it to the given output stream.
isc::data::Element::fromWire
static ElementPtr fromWire(std::stringstream &in, int length)
These function parse the wireformat at the given stringstream (of the given length).
Definition data.cc:1066
isc::data::Element::setValue
virtual bool setValue(const long long int v)
Set the integer value.
Definition data.cc:153
isc::data::Element::fromJSONFile
static ElementPtr fromJSONFile(const std::string &file_name, bool preproc=false)
Reads contents of specified file and interprets it as JSON.
Definition data.cc:883
isc::data::Element::empty
virtual bool empty() const
Return true if there are no elements in the list.
Definition data.cc:218
isc::data::Element::remove
virtual void remove(const int i)
Removes the element at the given position.
Definition data.cc:208
isc::data::Element::contains
virtual bool contains(const std::string &name) const
Checks if there is data at the given key.
Definition data.cc:238
isc::data::Element::find
virtual ConstElementPtr find(const std::string &identifier) const
Recursively finds any data at the given identifier.
Definition data.cc:243
isc::data::Element::size
virtual size_t size() const
Returns the number of elements in the list.
Definition data.cc:213
isc::data::Element::Element
Element(types t, const Position &pos=ZERO_POSITION())
Constructor.
Definition data.h:181
isc::data::Element::mapValue
virtual const std::map< std::string, ConstElementPtr > & mapValue() const
Return the map value.
Definition data.h:306
isc::data::Element::add
virtual void add(ElementPtr element)
Adds an ElementPtr to the list.
Definition data.cc:203
isc::data::Element::listValue
virtual const std::vector< ElementPtr > & listValue() const
Return the list value.
Definition data.h:300
isc::data::Element::equals
virtual bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const =0
Test equality.
isc::data::Element::fromJSON
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition data.cc:864
isc::data::Element::get
virtual ConstElementPtr get(const int i) const
Returns the ElementPtr at the given index.
Definition data.cc:188
isc::data::Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
isc::data::Element::nameToType
static Element::types nameToType(const std::string &type_name)
Converts the string to the corresponding type Throws a TypeError if the name is unknown.
Definition data.cc:741
isc::data::Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
isc::data::Element::set
virtual void set(const size_t i, ElementPtr element)
Sets the ElementPtr at the given index.
Definition data.cc:198
isc::data::Element::doubleValue
virtual double doubleValue() const
Return the double value.
Definition data.h:285
isc::data::Element::bigIntValue
virtual isc::util::int128_t bigIntValue() const
Return the big integer value.
Definition data.h:280
isc::data::Element::getType
types getType() const
Definition data.h:191
isc::data::Element::boolValue
virtual bool boolValue() const
Return the boolean value.
Definition data.h:290
isc::data::Element::preprocess
static void preprocess(std::istream &in, std::stringstream &out)
input text preprocessor.
Definition data.cc:1759
isc::data::Element::getNonConst
virtual ElementPtr getNonConst(const int i) const
returns element as non-const pointer.
Definition data.cc:193
isc::data::Element::removeEmptyContainersRecursively
void removeEmptyContainersRecursively(unsigned level=MAX_NESTING_LEVEL)
Remove all empty maps and lists from this Element and its descendants.
Definition data.cc:57
isc::data::IntElement
Notes: IntElement type is changed to int64_t.
Definition data.h:776
isc::data::IntElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:900
isc::data::IntElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1101
isc::data::JSONError
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
Definition data.h:50
isc::data::ListElement
Definition data.h:902
isc::data::ListElement::sort
void sort(std::string const &index=std::string())
Sorts the elements inside the list.
Definition data.cc:1168
isc::data::ListElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:988
isc::data::ListElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1146
isc::data::MapElement
Definition data.h:948
isc::data::MapElement::find
ConstElementPtr find(const std::string &id) const override
Recursively finds any data at the given identifier.
Definition data.cc:1038
isc::data::MapElement::set
void set(const std::string &key, ConstElementPtr value) override
Sets the ElementPtr at the given key.
Definition data.cc:1082
isc::data::MapElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const override
Test equality.
Definition data.cc:1204
isc::data::MapElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const override
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:1009
isc::data::NullElement
Definition data.h:875
isc::data::NullElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1135
isc::data::NullElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:935
isc::data::StringElement
Definition data.h:885
isc::data::StringElement::equals
bool equals(const Element &other, unsigned level=MAX_NESTING_LEVEL) const
Test equality.
Definition data.cc:1140
isc::data::StringElement::toJSON
void toJSON(std::ostream &ss, unsigned level=MAX_NESTING_LEVEL) const
Converts the Element to JSON format and appends it to the given output stream.
Definition data.cc:940
isc::data::TypeError
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
Definition data.h:37
ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
data.h
throwTypeError
#define throwTypeError(error)
Add the position to a TypeError message should be used in place of isc_throw(TypeError,...
Definition data.h:234
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::operator==
bool operator==(const Element &a, const Element &b)
Test equality.
Definition data.cc:268
isc::data::extend
void extend(const std::string &container, const std::string &extension, ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, bool alter, unsigned level)
Extends data by adding the specified 'extension' elements from 'other' inside the 'container' element...
Definition data.cc:1464
isc::data::removeIdentical
void removeIdentical(ElementPtr a, ConstElementPtr b)
Remove all values from the first ElementPtr that are equal in the second.
Definition data.cc:1235
isc::data::merge
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element. (on the first level).
Definition data.cc:1281
isc::data::isEquivalent
bool isEquivalent(ConstElementPtr a, ConstElementPtr b)
Compares the data with other using unordered lists.
Definition data.cc:1646
isc::data::operator<
bool operator<(Element const &a, Element const &b)
Test less than.
Definition data.cc:277
isc::data::IsCircular
bool IsCircular(ConstElementPtr element)
Check if the data is circular.
Definition data.cc:1825
isc::data::prettyPrint
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
Definition data.cc:1747
isc::data::copy
ElementPtr copy(ConstElementPtr from, unsigned level)
Copy the data up to a nesting level.
Definition data.cc:1522
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::mergeDiffAdd
void mergeDiffAdd(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, unsigned level)
Merges the diff data by adding the missing elements from 'other' to 'element' (recursively).
Definition data.cc:1299
isc::data::getNestDepth
unsigned getNestDepth(ConstElementPtr element, unsigned max_depth)
Compute the nesting depth.
Definition data.cc:1830
isc::data::isNull
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a null pointer.
Definition data.cc:1230
isc::data::operator<<
std::ostream & operator<<(std::ostream &out, const Element::Position &pos)
Insert Element::Position as a string into stream.
Definition data.cc:51
isc::data::mergeDiffDel
void mergeDiffDel(ElementPtr &element, ElementPtr &other, HierarchyDescriptor &hierarchy, std::string key, size_t idx, unsigned level)
Merges the diff data by removing the data present in 'other' from 'element' (recursively).
Definition data.cc:1367
isc::data::operator!=
bool operator!=(const Element &a, const Element &b)
Test inequality.
Definition data.cc:272
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::data::HierarchyDescriptor
std::vector< FunctionMap > HierarchyDescriptor
Hierarchy descriptor of the containers in a specific Element hierarchy tree.
Definition data.h:1119
isc::util::int128_t
boost::multiprecision::checked_int128_t int128_t
Definition bigints.h:19
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition data.h:107
isc::data::Element::Position::pos_
uint32_t pos_
Position within the line.
Definition data.h:110
isc::data::Element::Position::str
std::string str() const
Returns the position in the textual format.
Definition data.cc:44
isc::data::Element::Position::line_
uint32_t line_
Line number.
Definition data.h:109
isc::data::Element::Position::file_
std::string file_
File name.
Definition data.h:108