private void Open_Click(object sender, RoutedEventArgs e) { if (Data.DataContext == null) { RootNode = ReadSFS(Filename.Text); PopulateGrid(); Filename.IsEnabled = false; BrowseButton.IsEnabled = false; DeleteButton.IsEnabled = true; SaveButton.IsEnabled = true; ((Button)sender).Content = "Unload"; } else { Data.DataContext = null; RootNode = null; Filename.IsEnabled = true; BrowseButton.IsEnabled = true; DeleteButton.IsEnabled = false; SaveButton.IsEnabled = false; ((Button)sender).Content = "Load"; } }
private void WriteSFS(Stream output, SFSNode current, int indent) { Indent(output, indent); output.Write(current.NodeName); output.Write("\r\n"); Indent(output, indent); output.Write("{\r\n"); foreach (var pair in current.Attributes) { Indent(output, indent + 1); output.Write(pair.Key); output.Write(" = "); output.Write(pair.Value); output.Write("\r\n"); } foreach (SFSNode child in current.Children) { WriteSFS(output, child, indent + 1); } Indent(output, indent); output.Write("}\r\n"); }
private SFSNode ReadSFS(string inputSfs) { string inStr = string.Empty; using (FileStream input = new FileStream(inputSfs, FileMode.Open, FileAccess.Read, FileShare.Read)) { System.Diagnostics.Debug.Assert(input.Length <= (long)int.MaxValue); byte[] bytes = new byte[input.Length]; input.Read(bytes, 0, (int)input.Length); inStr = Encoding.UTF8.GetString(bytes).Replace("\r", string.Empty); } SFSNode root = null; Stack<SFSNode> stack = new Stack<SFSNode>(); string[] lines = inStr.Split('\n'); string nodeName = null; foreach (string line in lines) { if (line.EndsWith("{")) { SFSNode newNode = new SFSNode(nodeName); if (root == null) { root = newNode; } else { stack.Peek().Children.Add(newNode); } stack.Push(newNode); } else if (line.EndsWith("}")) { stack.Pop(); } else if (line.Contains(" = ")) { string[] parts = line.Split(new string[] { " = " }, StringSplitOptions.None); stack.Peek().Attributes[parts[0].Trim()] = parts[1]; } else { nodeName = line.Trim(); } } return root; }