data.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <cc/data.h>
  16. #include <cassert>
  17. #include <climits>
  18. #include <map>
  19. #include <cstdio>
  20. #include <iostream>
  21. #include <string>
  22. #include <sstream>
  23. #include <boost/algorithm/string.hpp> // for iequals
  24. #include <cmath>
  25. using namespace std;
  26. namespace isc {
  27. namespace data {
  28. std::string
  29. Element::str() const {
  30. std::stringstream ss;
  31. toJSON(ss);
  32. return (ss.str());
  33. }
  34. std::string
  35. Element::toWire() const {
  36. std::stringstream ss;
  37. toJSON(ss);
  38. return (ss.str());
  39. }
  40. void
  41. Element::toWire(std::ostream& ss) const {
  42. toJSON(ss);
  43. }
  44. bool
  45. Element::getValue(long int&) {
  46. return (false);
  47. }
  48. bool
  49. Element::getValue(double&) {
  50. return (false);
  51. }
  52. bool
  53. Element::getValue(bool&) {
  54. return (false);
  55. }
  56. bool
  57. Element::getValue(std::string&) {
  58. return (false);
  59. }
  60. bool
  61. Element::getValue(std::vector<ConstElementPtr>&) {
  62. return (false);
  63. }
  64. bool
  65. Element::getValue(std::map<std::string, ConstElementPtr>&) {
  66. return (false);
  67. }
  68. bool
  69. Element::setValue(const long int) {
  70. return (false);
  71. }
  72. bool
  73. Element::setValue(const double) {
  74. return (false);
  75. }
  76. bool
  77. Element::setValue(const bool) {
  78. return (false);
  79. }
  80. bool
  81. Element::setValue(const std::string&) {
  82. return (false);
  83. }
  84. bool
  85. Element::setValue(const std::vector<ConstElementPtr>&) {
  86. return (false);
  87. }
  88. bool
  89. Element::setValue(const std::map<std::string, ConstElementPtr>&) {
  90. return (false);
  91. }
  92. ConstElementPtr
  93. Element::get(const int) const {
  94. isc_throw(TypeError, "get(int) called on a non-list Element");
  95. }
  96. void
  97. Element::set(const size_t, ConstElementPtr) {
  98. isc_throw(TypeError, "set(int, element) called on a non-list Element");
  99. }
  100. void
  101. Element::add(ConstElementPtr) {
  102. isc_throw(TypeError, "add() called on a non-list Element");
  103. }
  104. void
  105. Element::remove(const int) {
  106. isc_throw(TypeError, "remove(int) called on a non-list Element");
  107. }
  108. size_t
  109. Element::size() const {
  110. isc_throw(TypeError, "size() called on a non-list Element");
  111. }
  112. ConstElementPtr
  113. Element::get(const std::string&) const {
  114. isc_throw(TypeError, "get(string) called on a non-map Element");
  115. }
  116. void
  117. Element::set(const std::string&, ConstElementPtr) {
  118. isc_throw(TypeError, "set(name, element) called on a non-map Element");
  119. }
  120. void
  121. Element::remove(const std::string&) {
  122. isc_throw(TypeError, "remove(string) called on a non-map Element");
  123. }
  124. bool
  125. Element::contains(const std::string&) const {
  126. isc_throw(TypeError, "contains(string) called on a non-map Element");
  127. }
  128. ConstElementPtr
  129. Element::find(const std::string&) const {
  130. isc_throw(TypeError, "find(string) called on a non-map Element");
  131. }
  132. bool
  133. Element::find(const std::string&, ConstElementPtr) const {
  134. return (false);
  135. }
  136. namespace {
  137. inline void
  138. throwJSONError(const std::string& error, const std::string& file, int line,
  139. int pos)
  140. {
  141. std::stringstream ss;
  142. ss << error << " in " + file + ":" << line << ":" << pos;
  143. isc_throw(JSONError, ss.str());
  144. }
  145. }
  146. std::ostream&
  147. operator<<(std::ostream &out, const Element& e) {
  148. return (out << e.str());
  149. }
  150. bool
  151. operator==(const Element& a, const Element& b) {
  152. return (a.equals(b));
  153. }
  154. bool operator!=(const Element& a, const Element& b) {
  155. return (!a.equals(b));
  156. };
  157. //
  158. // factory functions
  159. //
  160. ElementPtr
  161. Element::create() {
  162. return (ElementPtr(new NullElement()));
  163. }
  164. ElementPtr
  165. Element::create(const long int i) {
  166. return (ElementPtr(new IntElement(i)));
  167. }
  168. ElementPtr
  169. Element::create(const double d) {
  170. return (ElementPtr(new DoubleElement(d)));
  171. }
  172. ElementPtr
  173. Element::create(const std::string& s) {
  174. return (ElementPtr(new StringElement(s)));
  175. }
  176. ElementPtr
  177. Element::create(const bool b) {
  178. return (ElementPtr(new BoolElement(b)));
  179. }
  180. ElementPtr
  181. Element::createList() {
  182. return (ElementPtr(new ListElement()));
  183. }
  184. ElementPtr
  185. Element::createMap() {
  186. return (ElementPtr(new MapElement()));
  187. }
  188. //
  189. // helper functions for fromJSON factory
  190. //
  191. namespace {
  192. bool
  193. char_in(const char c, const char *chars) {
  194. for (size_t i = 0; i < strlen(chars); ++i) {
  195. if (chars[i] == c) {
  196. return (true);
  197. }
  198. }
  199. return (false);
  200. }
  201. void
  202. skip_chars(std::istream &in, const char *chars, int& line, int& pos) {
  203. char c = in.peek();
  204. while (char_in(c, chars) && c != EOF) {
  205. if (c == '\n') {
  206. ++line;
  207. pos = 1;
  208. } else {
  209. ++pos;
  210. }
  211. in.get();
  212. c = in.peek();
  213. }
  214. }
  215. // skip on the input stream to one of the characters in chars
  216. // if another character is found this function returns false
  217. // unless that character is specified in the optional may_skip
  218. //
  219. // the character found is left on the stream
  220. void
  221. skip_to(std::istream &in, const std::string& file, int& line,
  222. int& pos, const char* chars, const char* may_skip="")
  223. {
  224. char c = in.get();
  225. ++pos;
  226. while (c != EOF) {
  227. if (c == '\n') {
  228. pos = 1;
  229. ++line;
  230. }
  231. if (char_in(c, may_skip)) {
  232. c = in.get();
  233. ++pos;
  234. } else if (char_in(c, chars)) {
  235. while(char_in(in.peek(), may_skip)) {
  236. if (in.peek() == '\n') {
  237. pos = 1;
  238. ++line;
  239. }
  240. in.get();
  241. ++pos;
  242. }
  243. in.putback(c);
  244. --pos;
  245. return;
  246. } else {
  247. throwJSONError(std::string("'") + c + "' read, one of \"" + chars + "\" expected", file, line, pos);
  248. }
  249. }
  250. throwJSONError(std::string("EOF read, one of \"") + chars + "\" expected", file, line, pos);
  251. }
  252. // TODO: Should we check for all other official escapes here (and
  253. // error on the rest)?
  254. std::string
  255. str_from_stringstream(std::istream &in, const std::string& file, const int line,
  256. int& pos) throw (JSONError)
  257. {
  258. char c = 0;
  259. std::stringstream ss;
  260. c = in.get();
  261. ++pos;
  262. if (c == '"') {
  263. c = in.get();
  264. ++pos;
  265. } else {
  266. throwJSONError("String expected", file, line, pos);
  267. }
  268. while (c != EOF && c != '"') {
  269. if (c == '\\') {
  270. // next char must be either another \ or "
  271. // see the spec for allowed escape characters
  272. if (strchr("\"\\/\b\f\n\r\t", in.peek()) != NULL) {
  273. // drop the escape
  274. c = in.get();
  275. ++pos;
  276. } else {
  277. throwJSONError("Bad escape", file, line, pos);
  278. }
  279. }
  280. ss << c;
  281. c = in.get();
  282. ++pos;
  283. }
  284. return (ss.str());
  285. }
  286. std::string
  287. word_from_stringstream(std::istream &in, int& pos) {
  288. std::stringstream ss;
  289. while (isalpha(in.peek())) {
  290. ss << (char) in.get();
  291. }
  292. pos += ss.str().size();
  293. return (ss.str());
  294. }
  295. static std::string
  296. number_from_stringstream(std::istream &in, int& pos) {
  297. std::stringstream ss;
  298. while (isdigit(in.peek()) || in.peek() == '+' || in.peek() == '-' ||
  299. in.peek() == '.' || in.peek() == 'e' || in.peek() == 'E') {
  300. ss << (char) in.get();
  301. }
  302. pos += ss.str().size();
  303. return (ss.str());
  304. }
  305. // Should we change from IntElement and DoubleElement to NumberElement
  306. // that can also hold an e value? (and have specific getters if the
  307. // value is larger than an int can handle)
  308. ElementPtr
  309. from_stringstream_number(std::istream &in, int &pos) {
  310. long int i = 0;
  311. double d = 0.0;
  312. bool is_double = false;
  313. char *endptr;
  314. std::string number = number_from_stringstream(in, pos);
  315. i = strtol(number.c_str(), &endptr, 10);
  316. if (*endptr != '\0') {
  317. d = strtod(number.c_str(), &endptr);
  318. is_double = true;
  319. if (*endptr != '\0') {
  320. isc_throw(JSONError, std::string("Bad number: ") + number);
  321. } else {
  322. if (d == HUGE_VAL || d == -HUGE_VAL) {
  323. isc_throw(JSONError, std::string("Number overflow: ") + number);
  324. }
  325. }
  326. } else {
  327. if (i == LONG_MAX || i == LONG_MIN) {
  328. isc_throw(JSONError, std::string("Number overflow: ") + number);
  329. }
  330. }
  331. if (is_double) {
  332. return (Element::create(d));
  333. } else {
  334. return (Element::create(i));
  335. }
  336. }
  337. ElementPtr
  338. from_stringstream_bool(std::istream &in, const std::string& file,
  339. const int line, int& pos)
  340. {
  341. const std::string word = word_from_stringstream(in, pos);
  342. if (boost::iequals(word, "True")) {
  343. return (Element::create(true));
  344. } else if (boost::iequals(word, "False")) {
  345. return (Element::create(false));
  346. } else {
  347. throwJSONError(std::string("Bad boolean value: ") + word, file, line, pos);
  348. // above is a throw shortcurt, return empty is never reached
  349. return (ElementPtr());
  350. }
  351. }
  352. ElementPtr
  353. from_stringstream_null(std::istream &in, const std::string& file,
  354. const int line, int& pos)
  355. {
  356. const std::string word = word_from_stringstream(in, pos);
  357. if (boost::iequals(word, "null")) {
  358. return (Element::create());
  359. } else {
  360. throwJSONError(std::string("Bad null value: ") + word, file, line, pos);
  361. return (ElementPtr());
  362. }
  363. }
  364. ElementPtr
  365. from_stringstream_string(std::istream& in, const std::string& file, int& line,
  366. int& pos)
  367. {
  368. return (Element::create(str_from_stringstream(in, file, line, pos)));
  369. }
  370. ElementPtr
  371. from_stringstream_list(std::istream &in, const std::string& file, int& line,
  372. int& pos)
  373. {
  374. char c = 0;
  375. ElementPtr list = Element::createList();
  376. ConstElementPtr cur_list_element;
  377. skip_chars(in, " \t\n", line, pos);
  378. while (c != EOF && c != ']') {
  379. if (in.peek() != ']') {
  380. cur_list_element = Element::fromJSON(in, file, line, pos);
  381. list->add(cur_list_element);
  382. skip_to(in, file, line, pos, ",]", " \t\n");
  383. }
  384. c = in.get();
  385. pos++;
  386. }
  387. return (list);
  388. }
  389. ElementPtr
  390. from_stringstream_map(std::istream &in, const std::string& file, int& line,
  391. int& pos)
  392. {
  393. ElementPtr map = Element::createMap();
  394. skip_chars(in, " \t\n", line, pos);
  395. char c = in.peek();
  396. if (c == EOF) {
  397. throwJSONError(std::string("Unterminated map, <string> or } expected"), file, line, pos);
  398. } else if (c == '}') {
  399. // empty map, skip closing curly
  400. c = in.get();
  401. } else {
  402. while (c != EOF && c != '}') {
  403. std::string key = str_from_stringstream(in, file, line, pos);
  404. skip_to(in, file, line, pos, ":", " \t\n");
  405. // skip the :
  406. in.get();
  407. pos++;
  408. ConstElementPtr value = Element::fromJSON(in, file, line, pos);
  409. map->set(key, value);
  410. skip_to(in, file, line, pos, ",}", " \t\n");
  411. c = in.get();
  412. pos++;
  413. }
  414. }
  415. return (map);
  416. }
  417. }
  418. std::string
  419. Element::typeToName(Element::types type) {
  420. switch (type) {
  421. case Element::integer:
  422. return (std::string("integer"));
  423. case Element::real:
  424. return (std::string("real"));
  425. case Element::boolean:
  426. return (std::string("boolean"));
  427. case Element::string:
  428. return (std::string("string"));
  429. case Element::list:
  430. return (std::string("list"));
  431. case Element::map:
  432. return (std::string("map"));
  433. case Element::null:
  434. return (std::string("null"));
  435. case Element::any:
  436. return (std::string("any"));
  437. default:
  438. return (std::string("unknown"));
  439. }
  440. }
  441. Element::types
  442. Element::nameToType(const std::string& type_name) {
  443. if (type_name == "integer") {
  444. return (Element::integer);
  445. } else if (type_name == "real") {
  446. return (Element::real);
  447. } else if (type_name == "boolean") {
  448. return (Element::boolean);
  449. } else if (type_name == "string") {
  450. return (Element::string);
  451. } else if (type_name == "list") {
  452. return (Element::list);
  453. } else if (type_name == "map") {
  454. return (Element::map);
  455. } else if (type_name == "named_set") {
  456. return (Element::map);
  457. } else if (type_name == "null") {
  458. return (Element::null);
  459. } else if (type_name == "any") {
  460. return (Element::any);
  461. } else {
  462. isc_throw(TypeError, type_name + " is not a valid type name");
  463. }
  464. }
  465. ElementPtr
  466. Element::fromJSON(std::istream& in) throw(JSONError) {
  467. int line = 1, pos = 1;
  468. return (fromJSON(in, "<istream>", line, pos));
  469. }
  470. ElementPtr
  471. Element::fromJSON(std::istream& in, const std::string& file_name)
  472. throw(JSONError)
  473. {
  474. int line = 1, pos = 1;
  475. return (fromJSON(in, file_name, line, pos));
  476. }
  477. ElementPtr
  478. Element::fromJSON(std::istream &in, const std::string& file, int& line,
  479. int& pos) throw(JSONError)
  480. {
  481. char c = 0;
  482. ElementPtr element;
  483. bool el_read = false;
  484. skip_chars(in, " \n\t", line, pos);
  485. while (c != EOF && !el_read) {
  486. c = in.get();
  487. pos++;
  488. switch(c) {
  489. case '1':
  490. case '2':
  491. case '3':
  492. case '4':
  493. case '5':
  494. case '6':
  495. case '7':
  496. case '8':
  497. case '9':
  498. case '0':
  499. case '-':
  500. case '+':
  501. case '.':
  502. in.putback(c);
  503. element = from_stringstream_number(in, pos);
  504. el_read = true;
  505. break;
  506. case 't':
  507. case 'T':
  508. case 'f':
  509. case 'F':
  510. in.putback(c);
  511. element = from_stringstream_bool(in, file, line, pos);
  512. el_read = true;
  513. break;
  514. case 'n':
  515. case 'N':
  516. in.putback(c);
  517. element = from_stringstream_null(in, file, line, pos);
  518. el_read = true;
  519. break;
  520. case '"':
  521. in.putback('"');
  522. element = from_stringstream_string(in, file, line, pos);
  523. el_read = true;
  524. break;
  525. case '[':
  526. element = from_stringstream_list(in, file, line, pos);
  527. el_read = true;
  528. break;
  529. case '{':
  530. element = from_stringstream_map(in, file, line, pos);
  531. el_read = true;
  532. break;
  533. case EOF:
  534. break;
  535. default:
  536. throwJSONError(std::string("error: unexpected character ") + c, file, line, pos);
  537. break;
  538. }
  539. }
  540. if (el_read) {
  541. return (element);
  542. } else {
  543. isc_throw(JSONError, "nothing read");
  544. }
  545. }
  546. ElementPtr
  547. Element::fromJSON(const std::string &in) {
  548. std::stringstream ss;
  549. ss << in;
  550. return (fromJSON(ss, "<string>"));
  551. }
  552. // to JSON format
  553. void
  554. IntElement::toJSON(std::ostream& ss) const {
  555. ss << intValue();
  556. }
  557. void
  558. DoubleElement::toJSON(std::ostream& ss) const {
  559. ss << doubleValue();
  560. }
  561. void
  562. BoolElement::toJSON(std::ostream& ss) const {
  563. if (boolValue()) {
  564. ss << "true";
  565. } else {
  566. ss << "false";
  567. }
  568. }
  569. void
  570. NullElement::toJSON(std::ostream& ss) const {
  571. ss << "null";
  572. }
  573. void
  574. StringElement::toJSON(std::ostream& ss) const {
  575. ss << "\"";
  576. char c;
  577. const std::string& str = stringValue();
  578. for (size_t i = 0; i < str.size(); ++i) {
  579. c = str[i];
  580. // Escape characters as defined in JSON spec
  581. // Note that we do not escape forward slash; this
  582. // is allowed, but not mandatory.
  583. if (strchr("\"\\\b\f\n\r\t", c) != NULL) {
  584. ss << '\\';
  585. }
  586. ss << c;
  587. }
  588. ss << "\"";
  589. }
  590. void
  591. ListElement::toJSON(std::ostream& ss) const {
  592. ss << "[ ";
  593. const std::vector<ConstElementPtr>& v = listValue();
  594. for (std::vector<ConstElementPtr>::const_iterator it = v.begin();
  595. it != v.end(); ++it) {
  596. if (it != v.begin()) {
  597. ss << ", ";
  598. }
  599. (*it)->toJSON(ss);
  600. }
  601. ss << " ]";
  602. }
  603. void
  604. MapElement::toJSON(std::ostream& ss) const {
  605. ss << "{ ";
  606. const std::map<std::string, ConstElementPtr>& m = mapValue();
  607. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  608. it != m.end(); ++it) {
  609. if (it != m.begin()) {
  610. ss << ", ";
  611. }
  612. ss << "\"" << (*it).first << "\": ";
  613. if ((*it).second) {
  614. (*it).second->toJSON(ss);
  615. } else {
  616. ss << "None";
  617. }
  618. }
  619. ss << " }";
  620. }
  621. // throws when one of the types in the path (except the one
  622. // we're looking for) is not a MapElement
  623. // returns 0 if it could simply not be found
  624. // should that also be an exception?
  625. ConstElementPtr
  626. MapElement::find(const std::string& id) const {
  627. const size_t sep = id.find('/');
  628. if (sep == std::string::npos) {
  629. return (get(id));
  630. } else {
  631. ConstElementPtr ce = get(id.substr(0, sep));
  632. if (ce) {
  633. // ignore trailing slash
  634. if (sep + 1 != id.size()) {
  635. return (ce->find(id.substr(sep + 1)));
  636. } else {
  637. return (ce);
  638. }
  639. } else {
  640. return (ElementPtr());
  641. }
  642. }
  643. }
  644. ElementPtr
  645. Element::fromWire(const std::string& s) {
  646. std::stringstream ss;
  647. ss << s;
  648. int line = 0, pos = 0;
  649. return (fromJSON(ss, "<wire>", line, pos));
  650. }
  651. ElementPtr
  652. Element::fromWire(std::stringstream& in, int) {
  653. //
  654. // Check protocol version
  655. //
  656. //for (int i = 0 ; i < 4 ; ++i) {
  657. // const unsigned char version_byte = get_byte(in);
  658. // if (PROTOCOL_VERSION[i] != version_byte) {
  659. // throw DecodeError("Protocol version incorrect");
  660. // }
  661. //}
  662. //length -= 4;
  663. int line = 0, pos = 0;
  664. return (fromJSON(in, "<wire>", line, pos));
  665. }
  666. void
  667. MapElement::set(const std::string& key, ConstElementPtr value) {
  668. m[key] = value;
  669. }
  670. bool
  671. MapElement::find(const std::string& id, ConstElementPtr t) const {
  672. try {
  673. ConstElementPtr p = find(id);
  674. if (p) {
  675. t = p;
  676. return (true);
  677. }
  678. } catch (const TypeError&) {
  679. // ignore
  680. }
  681. return (false);
  682. }
  683. bool
  684. IntElement::equals(const Element& other) const {
  685. return (other.getType() == Element::integer) &&
  686. (i == other.intValue());
  687. }
  688. bool
  689. DoubleElement::equals(const Element& other) const {
  690. return (other.getType() == Element::real) &&
  691. (d == other.doubleValue());
  692. }
  693. bool
  694. BoolElement::equals(const Element& other) const {
  695. return (other.getType() == Element::boolean) &&
  696. (b == other.boolValue());
  697. }
  698. bool
  699. NullElement::equals(const Element& other) const {
  700. return (other.getType() == Element::null);
  701. }
  702. bool
  703. StringElement::equals(const Element& other) const {
  704. return (other.getType() == Element::string) &&
  705. (s == other.stringValue());
  706. }
  707. bool
  708. ListElement::equals(const Element& other) const {
  709. if (other.getType() == Element::list) {
  710. const size_t s = size();
  711. if (s != other.size()) {
  712. return (false);
  713. }
  714. for (size_t i = 0; i < s; ++i) {
  715. if (!get(i)->equals(*other.get(i))) {
  716. return (false);
  717. }
  718. }
  719. return (true);
  720. } else {
  721. return (false);
  722. }
  723. }
  724. bool
  725. MapElement::equals(const Element& other) const {
  726. if (other.getType() == Element::map) {
  727. const std::map<std::string, ConstElementPtr>& m = mapValue();
  728. for (std::map<std::string, ConstElementPtr>::const_iterator it =
  729. m.begin();
  730. it != m.end() ; ++it) {
  731. if (other.contains((*it).first)) {
  732. if (!get((*it).first)->equals(*other.get((*it).first))) {
  733. return (false);
  734. }
  735. } else {
  736. return (false);
  737. }
  738. }
  739. // quickly walk through the other map too, to see if there's
  740. // anything in there that we don't have. We don't need to
  741. // compare those elements; if one of them is missing we
  742. // differ (and if it's not missing the loop above has checked
  743. // it)
  744. std::map<std::string, ConstElementPtr>::const_iterator it;
  745. for (it = other.mapValue().begin();
  746. it != other.mapValue().end();
  747. ++it) {
  748. if (!contains((*it).first)) {
  749. return (false);
  750. }
  751. }
  752. return (true);
  753. } else {
  754. return (false);
  755. }
  756. }
  757. bool
  758. isNull(ConstElementPtr p) {
  759. return (!p);
  760. }
  761. void
  762. removeIdentical(ElementPtr a, ConstElementPtr b) {
  763. if (!b) {
  764. return;
  765. }
  766. if (a->getType() != Element::map || b->getType() != Element::map) {
  767. isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
  768. }
  769. // As maps do not allow entries with multiple keys, we can either iterate
  770. // over a checking for identical entries in b or vice-versa. As elements
  771. // are removed from a if a match is found, we choose to iterate over b to
  772. // avoid problems with element removal affecting the iterator.
  773. const std::map<std::string, ConstElementPtr>& m = b->mapValue();
  774. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  775. it != m.end() ; ++it) {
  776. if (a->contains((*it).first)) {
  777. if (a->get((*it).first)->equals(*b->get((*it).first))) {
  778. a->remove((*it).first);
  779. }
  780. }
  781. }
  782. }
  783. ConstElementPtr
  784. removeIdentical(ConstElementPtr a, ConstElementPtr b) {
  785. ElementPtr result = Element::createMap();
  786. if (!b) {
  787. return (result);
  788. }
  789. if (a->getType() != Element::map || b->getType() != Element::map) {
  790. isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
  791. }
  792. const std::map<std::string, ConstElementPtr>& m = a->mapValue();
  793. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  794. it != m.end() ; ++it) {
  795. if (!b->contains((*it).first) ||
  796. !a->get((*it).first)->equals(*b->get((*it).first))) {
  797. result->set((*it).first, (*it).second);
  798. }
  799. }
  800. return (result);
  801. }
  802. void
  803. merge(ElementPtr element, ConstElementPtr other) {
  804. if (element->getType() != Element::map ||
  805. other->getType() != Element::map) {
  806. isc_throw(TypeError, "merge arguments not MapElements");
  807. }
  808. const std::map<std::string, ConstElementPtr>& m = other->mapValue();
  809. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  810. it != m.end() ; ++it) {
  811. if ((*it).second && (*it).second->getType() != Element::null) {
  812. element->set((*it).first, (*it).second);
  813. } else if (element->contains((*it).first)) {
  814. element->remove((*it).first);
  815. }
  816. }
  817. }
  818. }
  819. }