From 03308e68b2877a519da31426955d914eed84e12c Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Fri, 1 Sep 2017 22:09:00 -0500 Subject: [PATCH] Updated the Xml Serializing engine to ignore the "Segment, size" tag as well as stripping braces from tag names. Forced the engine to terminate a tag if its the last line processed. --- MKVTest/Form1.cs | 6 -- MKVTest/SerializeMkvInfo.cs | 137 +++++++++++++++--------------------- 2 files changed, 56 insertions(+), 87 deletions(-) diff --git a/MKVTest/Form1.cs b/MKVTest/Form1.cs index 81c4611..4a7d77d 100644 --- a/MKVTest/Form1.cs +++ b/MKVTest/Form1.cs @@ -1,12 +1,6 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; using System.IO; -using System.Linq; using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; namespace MKVTest diff --git a/MKVTest/SerializeMkvInfo.cs b/MKVTest/SerializeMkvInfo.cs index 0e94b41..6204650 100644 --- a/MKVTest/SerializeMkvInfo.cs +++ b/MKVTest/SerializeMkvInfo.cs @@ -1,20 +1,17 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; -using System.Threading.Tasks; namespace MKVTest { internal class SerializeMkvInfo { - - public List SerializeFile(string filePath) + public List SerializeFile(string filePath) { var file = File.ReadAllLines(filePath); if (file.Length == 0) return new List(); var lastDepthLevel = 0; + var paddingOffSet = GetDepth(file[0]); var xml = new StringBuilder(); var stack = new Stack(); var test = new List(); @@ -23,13 +20,17 @@ namespace MKVTest var depth = GetDepth(file[i]); var tag = CreateTagFromString(file[i]); var padding = new StringBuilder(); - for (var j = depth; j != 0; j--) + for (var j = depth - paddingOffSet; j > 0; j--) { padding.Append(" "); } - + if (i == file.Length - 1) + { + test[test.Count - 1] = padding + "<" + tag.Name + ">" + tag.Value + ""; + continue; + } //xml.Append(padding + "<" + tag.Name + ">" + tag.Value == string.Empty ? Environment.NewLine : string.Empty); - test.Add(padding + "<" + tag.Name + ">"); + //test.Add(padding + "<" + tag.Name + ">"); //if (stack.Count != 0) //{ // var t = stack.Pop(); @@ -40,22 +41,9 @@ namespace MKVTest { if (stack.Count != 0) { - if (!string.IsNullOrEmpty(stack.Peek().Value)) - { - var t = stack.Pop(); - //xml.Append(t.Value + ""); - test[test.Count - 1] = test[test.Count - 1] + (tag.Value + ""; - continue; - } - else - { - var t = stack.Pop(); - test.Insert(i, padding + ""); - } + var t = stack.Pop(); + test[test.Count - 1] = padding + test[test.Count - 1] + t.Value + ""; } - - //xml.Append("<" + tag.Name + ">"); - //test.Add("<" + tag.Name + ">"); } else if (depth < lastDepthLevel) { @@ -63,39 +51,41 @@ namespace MKVTest if (stack.Count != 0) { var t = stack.Pop(); - //xml.Append(t.Value + ""); - test[test.Count - 1] = test[test.Count - 1] + (tag.Value + ""; - continue; - } - - } - else - { - if (stack.Count != 0 && i != 0) - { - if (!string.IsNullOrEmpty(stack.Peek().Value)) + test[test.Count - 1] = padding + "<" + t.Name + ">" + t.Value + ""; + var diff = lastDepthLevel - depth; + var subPadding = new StringBuilder(); + for (var j = 0; j < diff; j++) { - var t = stack.Pop(); - //xml.Append(t.Value + ""); - test.Add(t.Value + ""); + subPadding.Clear(); + for (var k = padding.Length - j; k != 0; k--) + { + subPadding.Append(" "); + } + if (stack.Count != 0) + { + test.Add(subPadding + ""); + } + else + { + break; + } } - test[test.Count - 1] = test[test.Count - 1] + (tag.Value + ""; - continue; } } + if(tag.Name != string.Empty) test.Add(padding + "<" + tag.Name + ">"); stack.Push(tag); lastDepthLevel = depth; } - return test; } - private XmlTag CreateTagFromString(string text, XmlTag parent = null) + private static XmlTag CreateTagFromString(string text, XmlTag parent = null) { var tag = new XmlTag(); var clearingPadding = true; var foundDelimiter = false; + var insideBraces = false; var name = new StringBuilder(); var value = new StringBuilder(); @@ -115,14 +105,32 @@ namespace MKVTest clearingPadding = false; if (foundDelimiter) { + if (insideBraces && text[i] == ')') + { + insideBraces = false; + continue; + } value.Append(text[i]); } else { - name.Append(text[i]); + if (text[i] == '(') + { + insideBraces = true; + continue; + } + if(text[i] == ')') + { + insideBraces = false; + continue; + } + if(!insideBraces) name.Append(text[i]); } } - + if (text.Contains("Segment, ")) + { + name.Clear(); + } tag.Name = FormatTagName(name.ToString()); tag.Value = value.ToString().Trim(); tag.Parent = parent; @@ -130,8 +138,9 @@ namespace MKVTest return tag; } - private string FormatTagName(string text) + private static string FormatTagName(string text) { + if (text.Length == 0) return string.Empty; var name = new StringBuilder(); var previousCharWasWhiteSpace = false; @@ -152,8 +161,9 @@ namespace MKVTest return name.ToString(); } - private int GetDepth(string line) + private static int GetDepth(string line) { + if (line.Length == 0) return -1; var depth = 0; for (var i = 0; i < line.Length; i++) @@ -170,40 +180,5 @@ namespace MKVTest return depth; } - - private bool IsClusterHead(string data) - { - var isClusterHeader = false; - - switch (data) - { - case "EBML head": - - break; - case "Chapters": - - break; - case "Segment": - - break; - case "Segment information": - - break; - case "Segment tracks": - - break; - case "A track": - - break; - case "Video track": - - break; - case "Audio track": - - break; - } - - return isClusterHeader; - } } }