From 4d6487b1bcb4db090f13af01d1c6f151c41a0365 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sat, 25 Jan 2020 17:53:32 -0330 Subject: [PATCH] Use regular expressions to match console type in ROM filename. --- src/emucore/Console.cxx | 52 +++++++++++++++++------------------------ src/emucore/Console.hxx | 7 ------ 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index 6dc7158db..5b4cc882e 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -17,6 +17,7 @@ #include #include +#include #include "AtariVox.hxx" #include "Booster.hxx" @@ -275,44 +276,35 @@ void Console::redetectFrameLayout() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string Console::formatFromFilename() const { - std::map Pattern = { - {"NTSC50", "NTSC50"}, {"NTSC 50", "NTSC50"}, {"NTSC-50", "NTSC50"}, - {"PAL60", "PAL60"}, {"PAL 60", "PAL60"}, {"PAL-60", "PAL60"}, - {"SECAM60", "SECAM60"}, {"SECAM 60", "SECAM60"}, {"SECAM-60", "SECAM60"}, - {"NTSC60", "NTSC" }, {"NTSC", "NTSC"}, // also finds "NTSC 60" and "NTSC-60" - {"PAL50", "PAL" }, {"PAL", "PAL"}, // also finds "PAL 50" and "PAL-50" - {"SECAM50", "SECAM"}, {"SECAM", "SECAM"}, // also finds "SECAM 50" and "SECAM-50" - }; - string filename = myOSystem.romFile().getNameWithExt(""); // get filename *without* extension + static const BSPF::array2D Pattern = {{ + { R"([ _\-(\[<]+NTSC[ -]?50)", "NTSC50" }, + { R"([ _\-(\[<]+PAL[ -]?60)", "PAL60" }, + { R"([ _\-(\[<]+SECAM[ -]?60)", "SECAM60" }, + { R"([ _\-(\[<]+NTSC[ _\-)\]>]?)", "NTSC" }, + { R"([ _\-(\[<]+PAL[ _\-)\]>]?)", "PAL" }, + { R"([ _\-(\[<]+SECAM[ _\-)\]>]?)", "SECAM" } + }}; - for (const auto& item : Pattern) + // Get filename *without* extension, and search using regex's above + const string& filename = myOSystem.romFile().getNameWithExt(""); + for(size_t i = 0; i < Pattern.size(); ++i) { - size_t pos = filename.find(item.first); - if (pos != string::npos) + try { - // avoid false positives - if (pos == filename.length() - (item.first).length() || // pattern at the very end - ((pos == 0 || isWhiteSpace(filename.at(pos - 1))) && // pattern within withspaces - isWhiteSpace(filename.at(pos + (item.first).length())))) - return item.second; + std::regex rgx(Pattern[i][0]); + if(std::regex_search(filename, rgx)) + return Pattern[i][1]; + } + catch(...) + { + continue; } } - // nothing found + + // Nothing found return "AUTO"; } -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool Console::isWhiteSpace(const char s) const -{ - const string WHITESPACES = " _-()[]<>"; - - for (size_t i = 0; i < WHITESPACES.length(); ++i) - if (s == WHITESPACES[i]) - return true; - - return false; -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Console::save(Serializer& out) const { diff --git a/src/emucore/Console.hxx b/src/emucore/Console.hxx index 05ea929aa..eb85025a6 100644 --- a/src/emucore/Console.hxx +++ b/src/emucore/Console.hxx @@ -348,13 +348,6 @@ class Console : public Serializable, public ConsoleIO */ string formatFromFilename() const; - /** - Check if the given character is a whitespace. - @param s Character to check - @return True if whitespace character - */ - bool isWhiteSpace(const char s) const; - /** Create the audio queue */