Refactor DAT name creation

This commit is contained in:
Matt Nadareski
2016-05-23 14:05:25 -07:00
parent 23c07252d9
commit 7afdf4adc5
2 changed files with 8 additions and 22 deletions

View File

@@ -71,11 +71,8 @@ namespace SabreTools.Helper
// Create the output directory if it doesn't already exist // Create the output directory if it doesn't already exist
Directory.CreateDirectory(outDir); Directory.CreateDirectory(outDir);
// Get the outfile and clean the path // Get the outfile name
string outfile = outDir + datdata.Description + (datdata.OutputFormat == OutputFormat.Xml || datdata.OutputFormat == OutputFormat.SabreDat ? ".xml" : ".dat"); string outfile = Style.CreateOutfileName(outDir, datdata);
outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
outfile);
logger.User("Opening file for writing: " + outfile); logger.User("Opening file for writing: " + outfile);

View File

@@ -139,25 +139,14 @@ namespace SabreTools.Helper
return input; return input;
} }
// http://stackoverflow.com/questions/203528/what-is-the-simplest-way-to-get-indented-xml-with-line-breaks-from-xmldocument public static string CreateOutfileName(string outDir, DatData datdata)
// http://www.timvw.be/2007/01/08/generating-utf-8-with-systemxmlxmlwriter/
public static string Beautify(XmlDocument doc)
{ {
MemoryStream ms = new MemoryStream(); string outfile = outDir + datdata.Description + (datdata.OutputFormat == OutputFormat.Xml || datdata.OutputFormat == OutputFormat.SabreDat ? ".xml" : ".dat");
XmlWriterSettings settings = new XmlWriterSettings outfile = (outfile.Contains(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString()) ?
{ outfile.Replace(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar.ToString(), Path.DirectorySeparatorChar.ToString()) :
Encoding = new UTF8Encoding(false), outfile);
Indent = true,
IndentChars = "\t",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(ms, settings)) return outfile;
{
doc.Save(writer);
}
return Encoding.UTF8.GetString(ms.ToArray());
} }
} }
} }