File indexing completed on 2025-08-05 08:16:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "PHNodeIterator.h"
0011
0012 #include "PHCompositeNode.h"
0013 #include "PHNode.h"
0014 #include "PHNodeOperation.h"
0015 #include "PHPointerListIterator.h"
0016 #include "phooldefs.h"
0017
0018 #include <boost/algorithm/string.hpp>
0019
0020 #include <vector>
0021
0022 PHNodeIterator::PHNodeIterator(PHCompositeNode* node)
0023 : currentNode(node)
0024 {
0025 }
0026
0027 PHPointerList<PHNode>&
0028 PHNodeIterator::ls()
0029 {
0030 PHPointerListIterator<PHNode> iter(currentNode->subNodes);
0031 subNodeList.clear();
0032 PHNode* thisNode;
0033 while ((thisNode = iter()))
0034 {
0035 subNodeList.append(thisNode);
0036 }
0037 return subNodeList;
0038 }
0039
0040 void PHNodeIterator::print()
0041 {
0042 currentNode->print();
0043 }
0044
0045
0046 PHNode* PHNodeIterator::findFirst(const std::string& requiredType, const std::string& requiredName)
0047 {
0048 PHPointerListIterator<PHNode> iter(currentNode->subNodes);
0049 PHNode* thisNode;
0050 while ((thisNode = iter()))
0051 {
0052 if (thisNode->getType() == requiredType && thisNode->getName() == requiredName)
0053 {
0054 return thisNode;
0055 }
0056
0057 if (thisNode->getType() == "PHCompositeNode")
0058 {
0059 PHNodeIterator nodeIter(dynamic_cast<PHCompositeNode*>(thisNode));
0060 PHNode* nodeFoundInSubTree = nodeIter.findFirst(requiredType, requiredName);
0061 if (nodeFoundInSubTree)
0062 {
0063 return nodeFoundInSubTree;
0064 }
0065 }
0066 }
0067 return nullptr;
0068 }
0069
0070
0071 PHNode* PHNodeIterator::findFirst(const std::string& requiredName)
0072 {
0073 PHPointerListIterator<PHNode> iter(currentNode->subNodes);
0074 PHNode* thisNode;
0075 while ((thisNode = iter()))
0076 {
0077 if (thisNode->getName() == requiredName)
0078 {
0079 return thisNode;
0080 }
0081
0082 if (thisNode->getType() == "PHCompositeNode")
0083 {
0084 PHNodeIterator nodeIter(dynamic_cast<PHCompositeNode*>(thisNode));
0085 PHNode* nodeFoundInSubTree = nodeIter.findFirst(requiredName);
0086 if (nodeFoundInSubTree)
0087 {
0088 return nodeFoundInSubTree;
0089 }
0090 }
0091 }
0092 return nullptr;
0093 }
0094
0095 bool PHNodeIterator::cd(const std::string& pathString)
0096 {
0097 bool success = true;
0098 if (pathString.empty())
0099 {
0100 while (currentNode->getParent())
0101 {
0102 currentNode = dynamic_cast<PHCompositeNode*>(currentNode->getParent());
0103 }
0104 }
0105 else
0106 {
0107 std::vector<std::string> splitpath;
0108 boost::split(splitpath, pathString, boost::is_any_of(phooldefs::nodetreepathdelim));
0109 bool pathFound;
0110 PHNode* subNode;
0111 for (const auto& iter : splitpath)
0112 {
0113 if (iter == "..")
0114 {
0115 if (currentNode->getParent())
0116 {
0117 currentNode = dynamic_cast<PHCompositeNode*>(currentNode->getParent());
0118 }
0119 else
0120 {
0121 success = false;
0122 }
0123 }
0124 else
0125 {
0126 PHPointerListIterator<PHNode> subNodeIter(currentNode->subNodes);
0127 pathFound = false;
0128 while ((subNode = subNodeIter()))
0129 {
0130 if (subNode->getType() == "PHCompositeNode" && subNode->getName() == iter)
0131 {
0132 currentNode = dynamic_cast<PHCompositeNode*>(subNode);
0133 pathFound = true;
0134 }
0135 }
0136 if (!pathFound)
0137 {
0138 success = false;
0139 }
0140 }
0141 }
0142 }
0143 return success;
0144 }
0145
0146 bool PHNodeIterator::addNode(PHNode* newNode)
0147 {
0148 return currentNode->addNode(newNode);
0149 }
0150
0151
0152 void PHNodeIterator::forEach(PHNodeOperation& operation)
0153 {
0154 operation(currentNode);
0155 PHPointerListIterator<PHNode> iter(currentNode->subNodes);
0156 PHNode* thisNode;
0157 while ((thisNode = iter()))
0158 {
0159 if (thisNode->getType() == "PHCompositeNode")
0160 {
0161 PHNodeIterator subNodeIter(dynamic_cast<PHCompositeNode*>(thisNode));
0162 subNodeIter.forEach(operation);
0163 }
0164 else
0165 {
0166 operation(thisNode);
0167 }
0168 }
0169 }
0170
0171 void PHNodeIterator::for_each(PHNodeOperation& operation)
0172 {
0173 forEach(operation);
0174 }