//gets the string from a leaf node at end of a path private String findLeafValue(String path) { String result = null; String leafname = ""; ENAMLStem subpath = null; int leafpos = path.LastIndexOf('.'); if (leafpos != -1) { leafname = path.Substring(leafpos + 1); //split the leaf node name from the end of the path path = path.Substring(0, leafpos); subpath = getSubPath(path); //get stem node that is parent to this leaf node } else { leafname = path; subpath = root; } if ((subpath != null) && (subpath.children.ContainsKey(leafname))) { ENAMLNode leaf = subpath.children[leafname]; //then get the leaf node child if (leaf != null && leaf is ENAMLLeaf) //these should both be true, check anyway { result = ((ENAMLLeaf)leaf).value; //and return it's value } } return(result); }
private void setLeafValue(String path, ENAMLStem subtree, String val) { int dotpos = path.IndexOf('.'); if (dotpos != -1) //path is name.subpath { String name = path.Substring(0, dotpos); String subpath = path.Substring(dotpos + 1); if (!subtree.children.ContainsKey(name)) { subtree.children[name] = new ENAMLStem(subtree.fullpath + '.' + name); } setLeafValue(subpath, (ENAMLStem)subtree.children[name], val); } else { if (!subtree.children.ContainsKey(path)) { subtree.children[path] = new ENAMLLeaf(subtree.fullpath + '.' + path, val); } else { ENAMLNode pathend = subtree.children[path]; //last node in a path has to be a leaf node if (pathend is ENAMLLeaf) { ((ENAMLLeaf)pathend).value = val; //to (re)store val in } else { throw new ENAMLPathException("path [" + pathend.fullpath + "is not an endpoint"); } } } }
private void storeSubTree(List <string> lines, ENAMLStem stem, String indent) { List <string> childNameList = new List <string>(stem.children.Keys); foreach (String childname in childNameList) { storeNode(lines, stem.children[childname], indent, childname); } }
//returns an empty list if the path is invalid or ends in a leaf (ie has no children) //that way you can do a foreach loop on the list w/o first having to test for a null return val public List <String> getPathKeys(String path) { List <String> keyList = new List <string>(); ENAMLStem subpath = getSubPath(path); if (subpath != null) { foreach (string key in subpath.children.Keys) { keyList.Add(key); } } return(keyList); }
private ENAMLStem parseSubtree(string[] lines, ref int lineNum, string path) { ENAMLStem curStem = new ENAMLStem(path); int indentLevel = -1; while (lineNum < lines.Length) { String line = lines[lineNum++].TrimEnd(wspace); if (line.Length == 0 || line[0] == '#') //skip blank lines & comments { continue; } int indent = 0; while ((indent < line.Length) && (line[indent] == ' ')) { indent++; //get line indent } if (indentLevel == -1) { indentLevel = indent; //if first line of subtree, get indent level } if (indent < indentLevel) { lineNum--; //this line is not in subgroup so back up one line return(curStem); //and we're done with this level, go back to parent } else { line = line.TrimStart(wspace); //we have the indent count, remove the leading spaces int colonpos = line.IndexOf(':'); String name = line.Substring(0, colonpos).Trim(); String subpath = path + "." + name; if (colonpos + 1 != line.Length) //nnn : xxx { String val = line.Substring(colonpos + 1).Trim(); curStem.children.Add(name, new ENAMLLeaf(subpath, val)); } else { ENAMLStem substem = parseSubtree(lines, ref lineNum, subpath); //nnn : - start of a subgroup curStem.children.Add(name, substem); } } } return(curStem); }
//- path management --------------------------------------------------- //check if the path actually exists public bool doesPathExist(String path) { int endpos = path.LastIndexOf('.'); if (endpos != -1) //if we have a path xxx.yyy.zzz { string subpath = path.Substring(0, endpos); //get the xxx.yyy part ENAMLStem stem = getSubPath(subpath); //and find the stem node for that path if (stem != null) { string childname = path.Substring(endpos + 1); return(stem.children.ContainsKey(childname)); //then check if it has zzz node (stem or leaf) } } else { return(root.children.ContainsKey(path)); //if we just have xxx, then check if child of root } return(false); }
private void removeSubPath(ENAMLStem subpath, string path, string endName) { if (path.Length == 0) { if (subpath != null && subpath.children.ContainsKey(endName)) { subpath.children.Remove(endName); //from the example above. we're at ccc now, so remove ddd } } else { int pathpos = path.IndexOf('.'); string name = (pathpos != -1) ? path.Substring(0, pathpos) : path; //split aaa.bbb.ccc into aaa and bbb.ccc string rest = (pathpos != -1) ? path.Substring(pathpos + 1) : ""; ENAMLStem node = (ENAMLStem)subpath.children[name]; //get node aaa removeSubPath(node, rest, endName); //and remove subpath ddd from bbb.ccc (etc...) if (node.children.Count == 0) //if aaa has no children now { subpath.children.Remove(name); //then remove it too } } }
public EnamlData() { root = new ENAMLStem("root"); }