From 0627964072831d25fb4477511da38e4212d6e7e7 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 30 Sep 2016 20:15:04 -0700 Subject: [PATCH] [DatFile] Make getting attributes easier --- SabreTools.Helper/Objects/Dat/DatFile.cs | 79 ++---------------------- 1 file changed, 4 insertions(+), 75 deletions(-) diff --git a/SabreTools.Helper/Objects/Dat/DatFile.cs b/SabreTools.Helper/Objects/Dat/DatFile.cs index 28ac15a7..99359b60 100644 --- a/SabreTools.Helper/Objects/Dat/DatFile.cs +++ b/SabreTools.Helper/Objects/Dat/DatFile.cs @@ -3648,83 +3648,12 @@ namespace SabreTools.Helper { Dictionary attribs = new Dictionary(); - // If the line ends with a '/', remove it - if (line.EndsWith("/")) + MatchCollection mc = Regex.Matches(line, @"(\S*?)=""(.*?)"""); + foreach (Match match in mc) { - line = line.TrimEnd('/'); + attribs.Add(match.Groups[1].Value, match.Groups[2].Value); } - - string[] gc = line.Trim().Split(' '); - - // Loop over all attributes and add them if possible - string attrib = "", val = ""; - for (int i = 1; i < gc.Length; i++) - { - //If the item is empty, we automatically skip it because it's a fluke - if (gc[i].Trim() == String.Empty) - { - continue; - } - - // Contains an equal sign, even number of quotes, not in attribute - else if (gc[i].Contains("=") && Regex.Matches(gc[i], "\"").Count % 2 == 0 && attrib == "") - { - string[] split = gc[i].Split('='); - attrib = split[0]; - val = gc[i].Substring(split[0].Length + 1).Replace("\"", ""); - attribs.Add(attrib, val); - attrib = ""; - val = ""; - } - - // Contains an equal sign, even number of quotes, in attribute - else if (gc[i].Contains("=") && Regex.Matches(gc[i], "\"").Count % 2 == 0 && attrib != "") - { - val += " " + gc[i]; - } - - // Contains an equal sign, odd number of quotes, not in attribute - else if (gc[i].Contains("=") && Regex.Matches(gc[i], "\"").Count % 2 == 1 && attrib == "") - { - string[] split = gc[i].Split('='); - attrib = split[0]; - val = gc[i].Substring(split[0].Length + 1).Replace("\"", ""); - } - - // Contains no equal sign, even number of quotes, not in attribute - else if (!gc[i].Contains("=") && Regex.Matches(gc[i], "\"").Count % 2 == 0 && attrib == "") - { - attribs.Add(gc[i], null); - } - - // Contains no equal sign, even number of quotes, in attribute - else if (!gc[i].Contains("=") && Regex.Matches(gc[i], "\"").Count % 2 == 0 && attrib != "") - { - val += " " + gc[i]; - } - - // Contains no equal sign, odd number of quotes, not in attribute - else if (!gc[i].Contains("=") && Regex.Matches(gc[i], "\"").Count % 2 == 1 && attrib == "") - { - val += " " + gc[i]; - } - - // Contains odd number of quotes, in attribute - else if (Regex.Matches(gc[i], "\"").Count % 2 == 1 && attrib != "") - { - val += " " + gc[i].Replace("\"", ""); - attribs.Add(attrib, val); - attrib = ""; - val = ""; - } - } - - // There can be leftovers, add them - if (attrib != "" && val != "") - { - attribs.Add(attrib, val); - } - + return attribs; }