diff --git a/DATabase/Helper/Converters.cs b/DATabase/Helper/Converters.cs index 14f04c7f..7af929a5 100644 --- a/DATabase/Helper/Converters.cs +++ b/DATabase/Helper/Converters.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Text.RegularExpressions; using System.Xml.Linq; @@ -11,8 +12,7 @@ namespace SabreTools.Helper { // Regex matching patterns private static string _headerPattern = @"(^.*?) \($"; - private static string _romPattern = @"^\s+((?:rom)|(?:disk)) \( (name) ""(.*?)"" (?:(size) (.*?) )?(?:(crc) (.*?))?(?:(md5) (.*?) )?(?:(sha1) (.*?) )?\)"; - private static string _itemPattern = @"^\s+(.*?) ""(.*?)"""; + private static string _itemPattern = @"^\s+(\S*?) (.*)"; private static string _endPattern = @"^\s*\)\s*$"; /// @@ -49,19 +49,64 @@ namespace SabreTools.Helper } // If the line is a rom or disk and we're in a block - else if (Regex.IsMatch(line, _romPattern) && block) - { - GroupCollection gc = Regex.Match(line, _romPattern).Groups; + else if ((line.Trim().StartsWith("rom (") || line.Trim().StartsWith("disk (")) && block) + { + string[] gc = line.Trim().Split(' '); - XElement temp = new XElement(gc[1].Value); + XElement temp = new XElement(gc[0]); // Loop over all attributes and add them if possible - for (int i = 2; i < gc.Count - 1; i++) + bool quote = false; + string attrib = "", val = ""; + for (int i = 2; i < gc.Length; i++) { - if (i + 1 < gc.Count && gc[i].Value != "") + // Even number of quotes, not in a quote, not in attribute + if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && !quote && attrib == "") { - temp.SetAttributeValue(gc[i].Value, gc[i+1].Value); - i++; + attrib = gc[i].Replace("\"", ""); + } + // Even number of quotes, not in a quote, in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && !quote && attrib != "") + { + temp.SetAttributeValue(attrib, gc[i].Replace("\"", "")); + + attrib = ""; + } + // Even number of quotes, in a quote, not in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && quote && attrib == "") + { + // Attributes can't have quoted names + } + // Even number of quotes, in a quote, in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 0 && quote && attrib != "") + { + val += " " + gc[i]; + } + // Odd number of quotes, not in a quote, not in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && !quote && attrib == "") + { + // Attributes can't have quoted names + } + // Odd number of quotes, not in a quote, in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && !quote && attrib != "") + { + val = gc[i].Replace("\"", ""); + quote = true; + } + // Odd number of quotes, in a quote, not in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && quote && attrib == "") + { + quote = false; + } + // Odd number of quotes, in a quote, in attribute + else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && quote && attrib != "") + { + val += " " + gc[i].Replace("\"", ""); + temp.SetAttributeValue(attrib, val); + + quote = false; + attrib = ""; + val = ""; } } @@ -74,12 +119,12 @@ namespace SabreTools.Helper if (gc[1].Value == "name" && elem.Name != "header") { - elem.SetAttributeValue(gc[1].Value, gc[2].Value); - elem.Add(new XElement("description", gc[2].Value)); + elem.SetAttributeValue(gc[1].Value, gc[2].Value.Replace("\"", "")); + elem.Add(new XElement("description", gc[2].Value.Replace("\"", ""))); } else { - elem.Add(new XElement(gc[1].Value, gc[2].Value)); + elem.Add(new XElement(gc[1].Value, gc[2].Value.Replace("\"", ""))); } }