/* * Copyright (c) 2014-2017, Eren Okka * Copyright (c) 2016-2017, Paul Miller * Copyright (c) 2017-2018, Tyler Bratton * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ namespace AnitomySharp; /// /// A library capable of parsing Anime filenames /// This code is a C++ to C# port of Anitomy>, /// using the already existing Java port AnitomyJ as a reference. /// public class Anitomy { private Anitomy() {} /// /// Parses an anime into its constituent elements. /// /// the anime file name /// the options to parse with, use Parse(filename) to use default options /// the list of parsed elements public static IEnumerable Parse(string filename, Options options) { List elements = []; List tokens = []; if (options.ParseFileExtension) { string extension = ""; if (RemoveExtensionFromFilename(ref filename, ref extension)) { elements.Add(new Element(ElementCategory.ElementFileExtension, extension)); } } if (string.IsNullOrEmpty(filename)) { return elements; } elements.Add(new Element(ElementCategory.ElementFileName, filename)); bool isTokenized = new Tokenizer(filename, elements, options, tokens).Tokenize(); if (!isTokenized) { return elements; } new Parser(elements, options, tokens).Parse(); return elements; } /// /// Parses an anime into its consituent elements. /// /// the anime file name /// the list of parsed elements public static IEnumerable Parse(string filename) { return Parse(filename, new Options()); } /// /// Removes the extension from the /// /// the ref that will be updated with the new filename /// the ref that will be updated with the file extension /// if the extension was successfully separated from the filename private static bool RemoveExtensionFromFilename(ref string filename, ref string extension) { int position; if (string.IsNullOrEmpty(filename) || (position = filename.LastIndexOf('.')) == -1) { return false; } extension = filename[(position + 1)..]; if (extension.Length > 4 || !extension.All(char.IsLetterOrDigit)) { return false; } string keyword = KeywordManager.Normalize(extension); if (!KeywordManager.Contains(ElementCategory.ElementFileExtension, keyword)) { return false; } filename = filename[..position]; return true; } }