20#include "cello_path.h"
24juce::ValueTree findRoot (juce::ValueTree& origin)
26 auto current { origin };
29 auto parent { current.getParent () };
30 if (!parent.isValid ())
36juce::ValueTree findAncestor (juce::ValueTree& origin,
37 const juce::Identifier ancestorType)
39 auto current { origin };
43 auto parent { current.getParent () };
45 if (!parent.isValid ())
48 if (parent.hasType (ancestorType))
60 juce::UndoManager* undo)
63 if (pathSegments.size () == 0)
66 if (!origin.isValid ())
72 if (pathSegments.size () > 1)
75 if (!juce::Identifier::isValidIdentifier (pathSegments[0]))
79 return juce::ValueTree (pathSegments[0]);
82 auto currentTree { origin };
88 if (pathSegments.size () == 1 &&
89 (pathSegments[0] == currentTree.getType ().toString ()))
92 for (
int i { 0 }; i < pathSegments.size () && currentTree.isValid (); ++i)
94 const auto& segment { pathSegments[i] };
95 const auto isLastSegment { i == (pathSegments.size () - 1) };
98 currentTree = findRoot (origin);
100 else if (segment == parent)
102 currentTree = currentTree.getParent ();
104 else if ((segment == current) || (segment.isEmpty ()))
108 else if (segment.startsWith (ancestor))
110 currentTree = findAncestor (
111 currentTree, segment.trimCharactersAtStart (juce::String { ancestor }));
116 auto childTree { currentTree.getChildWithName (segment) };
119 currentTree = childTree;
123 if (!childTree.isValid ())
128 childTree = juce::ValueTree (segment);
129 currentTree.appendChild (childTree, undo);
133 currentTree = childTree;
144juce::StringArray Path::parsePathSegments (
const juce::String& pathString)
146 auto segments { juce::StringArray::fromTokens (pathString,
sep,
"") };
147 for (
auto& segment : segments)
150 segment = segment.trim ();
154 if (pathString.startsWith (
sep))
155 segments.insert (0,
sep);
161#include "test/test_cello_path.inl"
SearchType
Definition cello_path.h:64
@ query
only search, do not create anything.
Definition cello_path.h:65
@ createAll
create final tree and all intermediate trees needed to reach it.
Definition cello_path.h:67
juce::ValueTree findValueTree(juce::ValueTree &origin, SearchType searchType, juce::UndoManager *undo=nullptr)
Navigate the path from origin to a tree that is expected at the end of the current path specification...
Definition cello_path.cpp:59
@ created
performing a search created a new tree
Definition cello_path.h:74
@ notFound
unable to find the requested tree
Definition cello_path.h:72
@ found
the sought tree existed already and was found
Definition cello_path.h:73
static const juce::String sep
Path separator.
Definition cello_path.h:53