public Key Parse(string script, NameValueCollection args) { this.args = args; this.input = new StringReader(script); NextChar(); Key root = new Key(); while (!EOF) { char ch = SkipWhitespace(); if (!EOF) { ParseKey(root); } } return root; }
void ParseKey(Key parent) { char ch = SkipWhitespace(); if (ch == '}') return; Key key = new Key(); string name = ParseIdentifier("{="); if (name == "val") { // this is not a sub key, it is just value in the parent key. string id = ParseIdentifier("="); object value = ParseValue(); parent.AddValue(id, value); return; } if (name == "NoRemove") { key.Removal = Removal.NoRemove; name = ParseIdentifier("{="); } else if (name == "ForceRemove") { key.Removal = Removal.ForceRemove; name = ParseIdentifier("{="); } key.Name = name; ch = SkipWhitespace(); if (ch == '=') { object def = ParseValue(); key.DefaultValue = def; ch = SkipWhitespace(); } if (ch == '{') { ch = NextChar(); while (!EOF && ch != '}') { ParseKey(key); ch = SkipWhitespace(); } if (ch != '}') { Error("Expected '{0}'", "}"); } NextChar(); // consume closing brace } parent.AddChild(key); }
void Unregister(Key key, RegistryKey reg) { if (key.values != null) { foreach (string name in key.values.Keys) { reg.DeleteValue(name, false); } } if (key.children != null) { foreach (Key child in key.children.Values) { if (child.Removal == Removal.ForceRemove) { QuietDeleteSubKeyTree(reg, child.Name); } else { using (RegistryKey subKey = reg.OpenSubKey(child.Name, true)) { if (subKey != null) { Unregister(child, subKey); } } } } } }
void Register(Key key, RegistryKey reg) { if (key.DefaultValue != null) { object value = key.DefaultValue; reg.SetValue(null, value); } if (key.values != null) { foreach (string name in key.values.Keys) { object value = key.values[name]; reg.SetValue(name, value); } } if (key.children != null) { foreach (Key child in key.children.Values) { using (RegistryKey subKey = reg.CreateSubKey(child.Name)) { Register(child, subKey); } } } }
public void Merge(Key key) { if (key.values != null) { foreach (string var in key.values.Keys) { this.AddValue(var, key.values[var]); } } if (key.children != null) { foreach (string name in key.children.Keys) { AddChild((Key)key.children[name]); } } }
public void AddChild(Key child) { if (children == null) children = new Hashtable(); if (children.Contains(child.Name)) { // need to merge them Key existingChild = (Key)children[child.Name]; existingChild.Merge(child); } else { children.Add(child.Name, child); } }