65 lines
2.6 KiB
C#
65 lines
2.6 KiB
C#
using View_by_Distance.Shared.Models.Properties;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
namespace View_by_Distance.Shared.Models.Stateless;
|
|
|
|
internal abstract class Id
|
|
{
|
|
|
|
internal static bool NameWithoutExtensionIsIdFormat(string fileNameWithoutExtension)
|
|
{
|
|
bool result;
|
|
int intMinValueLength = int.MinValue.ToString().Length;
|
|
if (fileNameWithoutExtension.Length < 5 || fileNameWithoutExtension.Length > intMinValueLength)
|
|
result = false;
|
|
else
|
|
{
|
|
bool skipOneAllAreNumbers = fileNameWithoutExtension[1..].All(l => char.IsNumber(l));
|
|
result = (skipOneAllAreNumbers && fileNameWithoutExtension[0] == '-') || (skipOneAllAreNumbers && char.IsNumber(fileNameWithoutExtension[0]));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal static NameWithoutExtension GetNameWithoutExtension(IMetadataConfiguration configuration, string file)
|
|
{
|
|
NameWithoutExtension result;
|
|
int? id;
|
|
short? multiplier;
|
|
char negativeMarker;
|
|
int absoluteValueOfId;
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
|
short sortOrderOnlyLengthIndex = IId.GetSortOrderOnlyLengthIndex(configuration.Offset);
|
|
bool nameWithoutExtensionIsIdFormat = IId.NameWithoutExtensionIsIdFormat(fileNameWithoutExtension);
|
|
bool nameWithoutExtensionIsPaddedIdFormat = IId.NameWithoutExtensionIsPaddedIdFormat(fileNameWithoutExtension, sortOrderOnlyLengthIndex);
|
|
if (!nameWithoutExtensionIsIdFormat && !nameWithoutExtensionIsPaddedIdFormat)
|
|
id = null;
|
|
else if (nameWithoutExtensionIsIdFormat)
|
|
{
|
|
if (!int.TryParse(fileNameWithoutExtension, out absoluteValueOfId))
|
|
id = null;
|
|
else
|
|
id = absoluteValueOfId;
|
|
}
|
|
else
|
|
{
|
|
negativeMarker = fileNameWithoutExtension[sortOrderOnlyLengthIndex - 2];
|
|
if (negativeMarker == '7')
|
|
multiplier = 1;
|
|
else if (negativeMarker == '3')
|
|
multiplier = -1;
|
|
else
|
|
multiplier = null;
|
|
if (!int.TryParse(fileNameWithoutExtension[sortOrderOnlyLengthIndex..], out absoluteValueOfId))
|
|
id = null;
|
|
else
|
|
{
|
|
id = absoluteValueOfId * multiplier;
|
|
if (id is null || !fileNameWithoutExtension.EndsWith(id.Value.ToString()[1..]))
|
|
id = null;
|
|
}
|
|
}
|
|
result = new(fileNameWithoutExtension, id, nameWithoutExtensionIsIdFormat, nameWithoutExtensionIsPaddedIdFormat);
|
|
return result;
|
|
}
|
|
|
|
} |