1
1

jsonElement.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * jsonElement.cpp for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #include "jsonElement.hh"
  7. #include "jsonContainer.hh"
  8. #include "jsonObjectEntry.hh"
  9. JSonElement::JSonElement(JSonElement *p): parent(p)
  10. { }
  11. JSonElement::~JSonElement()
  12. { }
  13. void JSonElement::setParent(JSonElement *p)
  14. {
  15. parent = p;
  16. }
  17. unsigned int JSonElement::getLevel() const
  18. {
  19. unsigned int level = 0;
  20. for (const JSonElement *parent = this; parent; parent = parent->parent)
  21. level++;
  22. return level;
  23. }
  24. JSonElement *JSonElement::getParent()
  25. {
  26. return parent;
  27. }
  28. const JSonElement *JSonElement::getParent() const
  29. {
  30. return parent;
  31. }
  32. const JSonElement *JSonElement::findPrev() const
  33. {
  34. const JSonElement *item = this;
  35. const JSonElement *parent = item->getParent();
  36. if (parent == nullptr || !dynamic_cast<const JSonContainer*>(parent))
  37. return nullptr; // Root node, can't have brothers
  38. std::list<JSonElement *>::const_iterator it = ((const JSonContainer*)parent)->cbegin();
  39. const JSonElement *prevElem = *it;
  40. if (prevElem == item)
  41. return nullptr; // First item
  42. while ((++it) != ((const JSonContainer*)parent)->cend())
  43. {
  44. if (*it == item)
  45. return prevElem;
  46. prevElem = *it;
  47. }
  48. return nullptr;
  49. }
  50. const JSonElement* JSonElement::findNext() const
  51. {
  52. const JSonElement *item = this;
  53. const JSonElement *parent = item->getParent();
  54. if (parent == nullptr || !dynamic_cast<const JSonContainer*>(parent))
  55. return nullptr; // Root node, can't have brothers
  56. JSonContainer::const_iterator it = ((const JSonContainer*)parent)->cbegin();
  57. while (it != ((const JSonContainer*)parent)->cend())
  58. {
  59. if (*it == item)
  60. {
  61. it++;
  62. if (it == ((const JSonContainer*)parent)->cend())
  63. return parent->findNext(); // Last item
  64. return *it;
  65. }
  66. it++;
  67. }
  68. return parent->findNext();
  69. }
  70. bool JSonElement::match(const std::string &search_pattern) const
  71. {
  72. const std::string str = stringify();
  73. return str.find(search_pattern) != str.npos;
  74. }