[DatFile, Utilities] Extensions

This commit is contained in:
Matt Nadareski
2017-12-05 13:26:25 -08:00
parent f561f189ae
commit 58c5939514
4 changed files with 104 additions and 33 deletions

View File

@@ -2953,13 +2953,7 @@ namespace SabreTools.Library.DatFiles
} }
// Check the file extension first as a safeguard // Check the file extension first as a safeguard
string ext = Path.GetExtension(filename).ToLowerInvariant(); if (!Utilities.HasValidDatExtension(filename))
if (ext.StartsWith("."))
{
ext = ext.Substring(1);
}
if (ext != "dat" && ext != "csv" && ext != "md5" && ext != "sfv" && ext != "sha1" && ext != "sha256"
&& ext != "sha384" && ext != "sha512" && ext != "tsv" && ext != "txt" && ext != "xml")
{ {
return; return;
} }
@@ -4597,14 +4591,14 @@ namespace SabreTools.Library.DatFiles
List<string> newExtA = new List<string>(); List<string> newExtA = new List<string>();
foreach (string s in extA) foreach (string s in extA)
{ {
newExtA.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant()); newExtA.Add((s.StartsWith(".") ? s.Substring(1) : s).ToUpperInvariant());
} }
string newExtAString = string.Join(",", newExtA); string newExtAString = string.Join(",", newExtA);
List<string> newExtB = new List<string>(); List<string> newExtB = new List<string>();
foreach (string s in extB) foreach (string s in extB)
{ {
newExtB.Add((s.StartsWith(".") ? s : "." + s).ToUpperInvariant()); newExtB.Add((s.StartsWith(".") ? s.Substring(1) : s).ToUpperInvariant());
} }
string newExtBString = string.Join(",", newExtB); string newExtBString = string.Join(",", newExtB);
@@ -4653,11 +4647,11 @@ namespace SabreTools.Library.DatFiles
List<DatItem> items = this[key]; List<DatItem> items = this[key];
foreach (DatItem item in items) foreach (DatItem item in items)
{ {
if (newExtA.Contains(Path.GetExtension(item.Name.ToUpperInvariant()))) if (newExtA.Contains(Utilities.GetExtension(item.Name.ToUpperInvariant())))
{ {
datdataA.Add(key, item); datdataA.Add(key, item);
} }
else if (newExtB.Contains(Path.GetExtension(item.Name.ToUpperInvariant()))) else if (newExtB.Contains(Utilities.GetExtension(item.Name.ToUpperInvariant())))
{ {
datdataB.Add(key, item); datdataB.Add(key, item);
} }

View File

@@ -293,11 +293,7 @@ namespace SabreTools.Library.DatFiles
} }
else if (rom.Type == ItemType.Rom) else if (rom.Type == ItemType.Rom)
{ {
string tempext = Path.GetExtension(((Rom)rom).Name); string tempext = "." + Utilities.GetExtension(((Rom)rom).Name);
if (!tempext.StartsWith("."))
{
tempext = "." + tempext;
}
state += "\t\t\t<files>\n" state += "\t\t\t<files>\n"
+ (((Rom)rom).CRC != null + (((Rom)rom).CRC != null

View File

@@ -61,7 +61,7 @@ namespace SabreTools.Library.Tools
_tofile = tofile; _tofile = tofile;
_warnings = false; _warnings = false;
_errors = false; _errors = false;
_filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ")" + Path.GetExtension(filename); _filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ")." + Utilities.GetExtension(filename);
_filter = filter; _filter = filter;
if (!Directory.Exists(_basepath)) if (!Directory.Exists(_basepath))

View File

@@ -865,15 +865,7 @@ namespace SabreTools.Library.Tools
} }
// First line of defense is going to be the extension, for better or worse // First line of defense is going to be the extension, for better or worse
string ext = Path.GetExtension(input).ToLowerInvariant(); if (!HasValidArchiveExtension(input))
if (ext.StartsWith("."))
{
ext = ext.Substring(1);
}
if (ext != "7z" && ext != "gz" && ext != "lzma" && ext != "rar"
&& ext != "rev" && ext != "r00" && ext != "r01" && ext != "tar"
&& ext != "tgz" && ext != "tlz" && ext != "zip" && ext != "zipx")
{ {
return outtype; return outtype;
} }
@@ -933,17 +925,14 @@ namespace SabreTools.Library.Tools
public static DatFormat GetDatFormat(string filename) public static DatFormat GetDatFormat(string filename)
{ {
// Limit the output formats based on extension // Limit the output formats based on extension
string ext = Path.GetExtension(filename).ToLowerInvariant(); if (!HasValidDatExtension(filename))
if (ext.StartsWith("."))
{
ext = ext.Substring(1);
}
if (ext != "csv" && ext != "dat" && ext != "md5" && ext != "sfv" && ext != "sha1"
&& ext != "sha384" && ext != "sha512" && ext != "tsv" && ext != "txt" && ext != "xml")
{ {
return 0; return 0;
} }
// Get the extension from the filename
string ext = GetExtension(filename);
// Read the input file, if possible // Read the input file, if possible
Globals.Logger.Verbose("Attempting to read file to get format: {0}", filename); Globals.Logger.Verbose("Attempting to read file to get format: {0}", filename);
@@ -2113,6 +2102,37 @@ namespace SabreTools.Library.Tools
return outDir; return outDir;
} }
/// <summary>
/// Get the extension from the path, if possible
/// </summary>
/// <param name="path">Path to get extension from</param>
/// <returns>Extension, if possible</returns>
public static string GetExtension(string path)
{
// Check null or empty first
if (String.IsNullOrWhiteSpace(path))
{
return null;
}
// Get the extension from the path, if possible
string ext = Path.GetExtension(path)?.ToLowerInvariant();
// Check if the extension is null or empty
if (String.IsNullOrWhiteSpace(ext))
{
return null;
}
// Make sure that extensions are valid
if (ext.StartsWith("."))
{
ext = ext.Substring(1);
}
return ext;
}
/// <summary> /// <summary>
/// Get the proper output path for a given input file and output directory /// Get the proper output path for a given input file and output directory
/// </summary> /// </summary>
@@ -2256,6 +2276,67 @@ namespace SabreTools.Library.Tools
return size; return size;
} }
/// <summary>
/// Get if the given path has a valid DAT extension
/// </summary>
/// <param name="path">Path to check</param>
/// <returns>True if the extension is valid, false otherwise</returns>
public static bool HasValidArchiveExtension(string path)
{
// Get the extension from the path, if possible
string ext = GetExtension(path);
// Check against the list of known archive extensions
switch (ext)
{
case "7z":
case "gz":
case "lzma":
case "rar":
case "rev":
case "r00":
case "r01":
case "tar":
case "tgz":
case "tlz":
case "zip":
case "zipx":
return true;
default:
return false;
}
}
/// <summary>
/// Get if the given path has a valid archive extension
/// </summary>
/// <param name="path">Path to check</param>
/// <returns>True if the extension is valid, false otherwise</returns>
public static bool HasValidDatExtension(string path)
{
// Get the extension from the path, if possible
string ext = GetExtension(path);
// Check against the list of known DAT extensions
switch (ext)
{
case "csv":
case "dat":
case "md5":
case "sfv":
case "sha1":
case "sha256":
case "sha384":
case "sha512":
case "tsv":
case "txt":
case "xml":
return true;
default:
return false;
}
}
/// <summary> /// <summary>
/// Get if a string contains Unicode characters /// Get if a string contains Unicode characters
/// </summary> /// </summary>