data.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. //
  45. // The following methods are effectively empty, and their parameters are
  46. // unused. To silence compilers that warn unused function parameters,
  47. // we specify a (compiler dependent) special keyword when available.
  48. // It's defined in config.h, and to avoid including this header file from
  49. // installed files we define the methods here.
  50. //
  51. bool
  52. Element::getValue(long int&) {
  53. return (false);
  54. }
  55. bool
  56. Element::getValue(double&) {
  57. return (false);
  58. }
  59. bool
  60. Element::getValue(bool&) {
  61. return (false);
  62. }
  63. bool
  64. Element::getValue(std::string&) {
  65. return (false);
  66. }
  67. bool
  68. Element::getValue(std::vector<ConstElementPtr>&) {
  69. return (false);
  70. }
  71. bool
  72. Element::getValue(std::map<std::string, ConstElementPtr>&) {
  73. return (false);
  74. }
  75. bool
  76. Element::setValue(const long int) {
  77. return (false);
  78. }
  79. bool
  80. Element::setValue(const double) {
  81. return (false);
  82. }
  83. bool
  84. Element::setValue(const bool) {
  85. return (false);
  86. }
  87. bool
  88. Element::setValue(const std::string&) {
  89. return (false);
  90. }
  91. bool
  92. Element::setValue(const std::vector<ConstElementPtr>&) {
  93. return (false);
  94. }
  95. bool
  96. Element::setValue(const std::map<std::string, ConstElementPtr>&) {
  97. return (false);
  98. }
  99. ConstElementPtr
  100. Element::get(const int) const {
  101. isc_throw(TypeError, "get(int) called on a non-list Element");
  102. }
  103. void
  104. Element::set(const size_t, ConstElementPtr) {
  105. isc_throw(TypeError, "set(int, element) called on a non-list Element");
  106. }
  107. void
  108. Element::add(ConstElementPtr) {
  109. isc_throw(TypeError, "add() called on a non-list Element");
  110. }
  111. void
  112. Element::remove(const int) {
  113. isc_throw(TypeError, "remove(int) called on a non-list Element");
  114. }
  115. size_t
  116. Element::size() const {
  117. isc_throw(TypeError, "size() called on a non-list Element");
  118. }
  119. ConstElementPtr
  120. Element::get(const std::string&) const {
  121. isc_throw(TypeError, "get(string) called on a non-map Element");
  122. }
  123. void
  124. Element::set(const std::string&, ConstElementPtr) {
  125. isc_throw(TypeError, "set(name, element) called on a non-map Element");
  126. }
  127. void
  128. Element::remove(const std::string&) {
  129. isc_throw(TypeError, "remove(string) called on a non-map Element");
  130. }
  131. bool
  132. Element::contains(const std::string&) const {
  133. isc_throw(TypeError, "contains(string) called on a non-map Element");
  134. }
  135. ConstElementPtr
  136. Element::find(const std::string&) const {
  137. isc_throw(TypeError, "find(string) called on a non-map Element");
  138. }
  139. bool
  140. Element::find(const std::string&, ConstElementPtr) const {
  141. return (false);
  142. }
  143. namespace {
  144. inline void
  145. throwJSONError(const std::string& error, const std::string& file, int line,
  146. int pos)
  147. {
  148. std::stringstream ss;
  149. ss << error << " in " + file + ":" << line << ":" << pos;
  150. isc_throw(JSONError, ss.str());
  151. }
  152. }
  153. std::ostream&
  154. operator<<(std::ostream &out, const Element& e) {
  155. return (out << e.str());
  156. }
  157. bool
  158. operator==(const Element& a, const Element& b) {
  159. return (a.equals(b));
  160. }
  161. bool operator!=(const Element& a, const Element& b) {
  162. return (!a.equals(b));
  163. };
  164. //
  165. // factory functions
  166. //
  167. ElementPtr
  168. Element::create() {
  169. return (ElementPtr(new NullElement()));
  170. }
  171. ElementPtr
  172. Element::create(const long int i) {
  173. return (ElementPtr(new IntElement(i)));
  174. }
  175. ElementPtr
  176. Element::create(const double d) {
  177. return (ElementPtr(new DoubleElement(d)));
  178. }
  179. ElementPtr
  180. Element::create(const std::string& s) {
  181. return (ElementPtr(new StringElement(s)));
  182. }
  183. ElementPtr
  184. Element::create(const bool b) {
  185. return (ElementPtr(new BoolElement(b)));
  186. }
  187. ElementPtr
  188. Element::createList() {
  189. return (ElementPtr(new ListElement()));
  190. }
  191. ElementPtr
  192. Element::createMap() {
  193. return (ElementPtr(new MapElement()));
  194. }
  195. //
  196. // helper functions for fromJSON factory
  197. //
  198. namespace {
  199. bool
  200. char_in(const char c, const char *chars) {
  201. for (size_t i = 0; i < strlen(chars); ++i) {
  202. if (chars[i] == c) {
  203. return (true);
  204. }
  205. }
  206. return (false);
  207. }
  208. void
  209. skip_chars(std::istream &in, const char *chars, int& line, int& pos) {
  210. char c = in.peek();
  211. while (char_in(c, chars) && c != EOF) {
  212. if (c == '\n') {
  213. ++line;
  214. pos = 1;
  215. } else {
  216. ++pos;
  217. }
  218. in.get();
  219. c = in.peek();
  220. }
  221. }
  222. // skip on the input stream to one of the characters in chars
  223. // if another character is found this function returns false
  224. // unless that character is specified in the optional may_skip
  225. //
  226. // the character found is left on the stream
  227. void
  228. skip_to(std::istream &in, const std::string& file, int& line,
  229. int& pos, const char* chars, const char* may_skip="")
  230. {
  231. char c = in.get();
  232. ++pos;
  233. while (c != EOF) {
  234. if (c == '\n') {
  235. pos = 1;
  236. ++line;
  237. }
  238. if (char_in(c, may_skip)) {
  239. c = in.get();
  240. ++pos;
  241. } else if (char_in(c, chars)) {
  242. while(char_in(in.peek(), may_skip)) {
  243. if (in.peek() == '\n') {
  244. pos = 1;
  245. ++line;
  246. }
  247. in.get();
  248. ++pos;
  249. }
  250. in.putback(c);
  251. --pos;
  252. return;
  253. } else {
  254. throwJSONError(std::string("'") + c + "' read, one of \"" + chars + "\" expected", file, line, pos);
  255. }
  256. }
  257. throwJSONError(std::string("EOF read, one of \"") + chars + "\" expected", file, line, pos);
  258. }
  259. // TODO: Should we check for all other official escapes here (and
  260. // error on the rest)?
  261. std::string
  262. str_from_stringstream(std::istream &in, const std::string& file, const int line,
  263. int& pos) throw (JSONError)
  264. {
  265. char c = 0;
  266. std::stringstream ss;
  267. c = in.get();
  268. ++pos;
  269. if (c == '"') {
  270. c = in.get();
  271. ++pos;
  272. } else {
  273. throwJSONError("String expected", file, line, pos);
  274. }
  275. while (c != EOF && c != '"') {
  276. ss << c;
  277. if (c == '\\' && in.peek() == '"') {
  278. ss << in.get();
  279. ++pos;
  280. }
  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 == "null") {
  456. return (Element::null);
  457. } else if (type_name == "any") {
  458. return (Element::any);
  459. } else {
  460. isc_throw(TypeError, type_name + " is not a valid type name");
  461. }
  462. }
  463. ElementPtr
  464. Element::fromJSON(std::istream& in) throw(JSONError) {
  465. int line = 1, pos = 1;
  466. return (fromJSON(in, "<istream>", line, pos));
  467. }
  468. ElementPtr
  469. Element::fromJSON(std::istream& in, const std::string& file_name)
  470. throw(JSONError)
  471. {
  472. int line = 1, pos = 1;
  473. return (fromJSON(in, file_name, line, pos));
  474. }
  475. ElementPtr
  476. Element::fromJSON(std::istream &in, const std::string& file, int& line,
  477. int& pos) throw(JSONError)
  478. {
  479. char c = 0;
  480. ElementPtr element;
  481. bool el_read = false;
  482. skip_chars(in, " \n\t", line, pos);
  483. while (c != EOF && !el_read) {
  484. c = in.get();
  485. pos++;
  486. switch(c) {
  487. case '1':
  488. case '2':
  489. case '3':
  490. case '4':
  491. case '5':
  492. case '6':
  493. case '7':
  494. case '8':
  495. case '9':
  496. case '0':
  497. case '-':
  498. case '+':
  499. case '.':
  500. in.putback(c);
  501. element = from_stringstream_number(in, pos);
  502. el_read = true;
  503. break;
  504. case 't':
  505. case 'T':
  506. case 'f':
  507. case 'F':
  508. in.putback(c);
  509. element = from_stringstream_bool(in, file, line, pos);
  510. el_read = true;
  511. break;
  512. case 'n':
  513. case 'N':
  514. in.putback(c);
  515. element = from_stringstream_null(in, file, line, pos);
  516. el_read = true;
  517. break;
  518. case '"':
  519. in.putback('"');
  520. element = from_stringstream_string(in, file, line, pos);
  521. el_read = true;
  522. break;
  523. case '[':
  524. element = from_stringstream_list(in, file, line, pos);
  525. el_read = true;
  526. break;
  527. case '{':
  528. element = from_stringstream_map(in, file, line, pos);
  529. el_read = true;
  530. break;
  531. case EOF:
  532. break;
  533. default:
  534. throwJSONError(std::string("error: unexpected character ") + c, file, line, pos);
  535. break;
  536. }
  537. }
  538. if (el_read) {
  539. return (element);
  540. } else {
  541. isc_throw(JSONError, "nothing read");
  542. }
  543. }
  544. ElementPtr
  545. Element::fromJSON(const std::string &in) {
  546. std::stringstream ss;
  547. ss << in;
  548. return (fromJSON(ss, "<string>"));
  549. }
  550. // to JSON format
  551. void
  552. IntElement::toJSON(std::ostream& ss) const {
  553. ss << intValue();
  554. }
  555. void
  556. DoubleElement::toJSON(std::ostream& ss) const {
  557. ss << doubleValue();
  558. }
  559. void
  560. BoolElement::toJSON(std::ostream& ss) const {
  561. if (boolValue()) {
  562. ss << "true";
  563. } else {
  564. ss << "false";
  565. }
  566. }
  567. void
  568. NullElement::toJSON(std::ostream& ss) const {
  569. ss << "null";
  570. }
  571. void
  572. StringElement::toJSON(std::ostream& ss) const {
  573. ss << "\"";
  574. ss << stringValue();
  575. ss << "\"";
  576. }
  577. void
  578. ListElement::toJSON(std::ostream& ss) const {
  579. ss << "[ ";
  580. const std::vector<ConstElementPtr>& v = listValue();
  581. for (std::vector<ConstElementPtr>::const_iterator it = v.begin();
  582. it != v.end(); ++it) {
  583. if (it != v.begin()) {
  584. ss << ", ";
  585. }
  586. (*it)->toJSON(ss);
  587. }
  588. ss << " ]";
  589. }
  590. void
  591. MapElement::toJSON(std::ostream& ss) const {
  592. ss << "{ ";
  593. const std::map<std::string, ConstElementPtr>& m = mapValue();
  594. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  595. it != m.end(); ++it) {
  596. if (it != m.begin()) {
  597. ss << ", ";
  598. }
  599. ss << "\"" << (*it).first << "\": ";
  600. if ((*it).second) {
  601. (*it).second->toJSON(ss);
  602. } else {
  603. ss << "None";
  604. }
  605. }
  606. ss << " }";
  607. }
  608. // throws when one of the types in the path (except the one
  609. // we're looking for) is not a MapElement
  610. // returns 0 if it could simply not be found
  611. // should that also be an exception?
  612. ConstElementPtr
  613. MapElement::find(const std::string& id) const {
  614. const size_t sep = id.find('/');
  615. if (sep == std::string::npos) {
  616. return (get(id));
  617. } else {
  618. ConstElementPtr ce = get(id.substr(0, sep));
  619. if (ce) {
  620. // ignore trailing slash
  621. if (sep + 1 != id.size()) {
  622. return (ce->find(id.substr(sep + 1)));
  623. } else {
  624. return (ce);
  625. }
  626. } else {
  627. return (ElementPtr());
  628. }
  629. }
  630. }
  631. ElementPtr
  632. Element::fromWire(const std::string& s) {
  633. std::stringstream ss;
  634. ss << s;
  635. int line = 0, pos = 0;
  636. return (fromJSON(ss, "<wire>", line, pos));
  637. }
  638. ElementPtr
  639. Element::fromWire(std::stringstream& in, int) {
  640. //
  641. // Check protocol version
  642. //
  643. //for (int i = 0 ; i < 4 ; ++i) {
  644. // const unsigned char version_byte = get_byte(in);
  645. // if (PROTOCOL_VERSION[i] != version_byte) {
  646. // throw DecodeError("Protocol version incorrect");
  647. // }
  648. //}
  649. //length -= 4;
  650. int line = 0, pos = 0;
  651. return (fromJSON(in, "<wire>", line, pos));
  652. }
  653. void
  654. MapElement::set(const std::string& key, ConstElementPtr value) {
  655. m[key] = value;
  656. }
  657. bool
  658. MapElement::find(const std::string& id, ConstElementPtr t) const {
  659. try {
  660. ConstElementPtr p = find(id);
  661. if (p) {
  662. t = p;
  663. return (true);
  664. }
  665. } catch (const TypeError& e) {
  666. // ignore
  667. }
  668. return (false);
  669. }
  670. bool
  671. IntElement::equals(const Element& other) const {
  672. return (other.getType() == Element::integer) &&
  673. (i == other.intValue());
  674. }
  675. bool
  676. DoubleElement::equals(const Element& other) const {
  677. return (other.getType() == Element::real) &&
  678. (d == other.doubleValue());
  679. }
  680. bool
  681. BoolElement::equals(const Element& other) const {
  682. return (other.getType() == Element::boolean) &&
  683. (b == other.boolValue());
  684. }
  685. bool
  686. NullElement::equals(const Element& other) const {
  687. return (other.getType() == Element::null);
  688. }
  689. bool
  690. StringElement::equals(const Element& other) const {
  691. return (other.getType() == Element::string) &&
  692. (s == other.stringValue());
  693. }
  694. bool
  695. ListElement::equals(const Element& other) const {
  696. if (other.getType() == Element::list) {
  697. const int s = size();
  698. if (s != other.size()) {
  699. return (false);
  700. }
  701. for (int i = 0; i < s; ++i) {
  702. if (!get(i)->equals(*other.get(i))) {
  703. return (false);
  704. }
  705. }
  706. return (true);
  707. } else {
  708. return (false);
  709. }
  710. }
  711. bool
  712. MapElement::equals(const Element& other) const {
  713. if (other.getType() == Element::map) {
  714. const std::map<std::string, ConstElementPtr>& m = mapValue();
  715. for (std::map<std::string, ConstElementPtr>::const_iterator it =
  716. m.begin();
  717. it != m.end() ; ++it) {
  718. if (other.contains((*it).first)) {
  719. if (!get((*it).first)->equals(*other.get((*it).first))) {
  720. return (false);
  721. }
  722. } else {
  723. return (false);
  724. }
  725. }
  726. // quickly walk through the other map too, to see if there's
  727. // anything in there that we don't have. We don't need to
  728. // compare those elements; if one of them is missing we
  729. // differ (and if it's not missing the loop above has checked
  730. // it)
  731. std::map<std::string, ConstElementPtr>::const_iterator it;
  732. for (it = other.mapValue().begin();
  733. it != other.mapValue().end();
  734. ++it) {
  735. if (!contains((*it).first)) {
  736. return (false);
  737. }
  738. }
  739. return (true);
  740. } else {
  741. return (false);
  742. }
  743. }
  744. bool
  745. isNull(ConstElementPtr p) {
  746. return (!p);
  747. }
  748. void
  749. removeIdentical(ElementPtr a, ConstElementPtr b) {
  750. if (!b) {
  751. return;
  752. }
  753. if (a->getType() != Element::map || b->getType() != Element::map) {
  754. isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
  755. }
  756. const std::map<std::string, ConstElementPtr>& m = a->mapValue();
  757. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  758. it != m.end() ; ++it) {
  759. if (b->contains((*it).first)) {
  760. if (a->get((*it).first)->equals(*b->get((*it).first))) {
  761. a->remove((*it).first);
  762. }
  763. }
  764. }
  765. }
  766. ConstElementPtr
  767. removeIdentical(ConstElementPtr a, ConstElementPtr b) {
  768. ElementPtr result = Element::createMap();
  769. if (!b) {
  770. return (result);
  771. }
  772. if (a->getType() != Element::map || b->getType() != Element::map) {
  773. isc_throw(TypeError, "Non-map Elements passed to removeIdentical");
  774. }
  775. const std::map<std::string, ConstElementPtr>& m = a->mapValue();
  776. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  777. it != m.end() ; ++it) {
  778. if (!b->contains((*it).first) ||
  779. !a->get((*it).first)->equals(*b->get((*it).first))) {
  780. result->set((*it).first, (*it).second);
  781. }
  782. }
  783. return (result);
  784. }
  785. void
  786. merge(ElementPtr element, ConstElementPtr other) {
  787. if (element->getType() != Element::map ||
  788. other->getType() != Element::map) {
  789. isc_throw(TypeError, "merge arguments not MapElements");
  790. }
  791. const std::map<std::string, ConstElementPtr>& m = other->mapValue();
  792. for (std::map<std::string, ConstElementPtr>::const_iterator it = m.begin();
  793. it != m.end() ; ++it) {
  794. if ((*it).second && (*it).second->getType() != Element::null) {
  795. element->set((*it).first, (*it).second);
  796. } else if (element->contains((*it).first)) {
  797. element->remove((*it).first);
  798. }
  799. }
  800. }
  801. }
  802. }