From 9f5325a31145baa173c90d2f3b08ac363b4f53a5 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Wed, 10 Aug 2016 07:22:46 -0400 Subject: [PATCH] device_image_interface::software_name_split() ==> softlist.cpp:software_name_parse() Also consolidated with code that performed a quick pass to identify whether a piece of text is a software name --- src/emu/diimage.cpp | 52 ++------------------------------ src/emu/diimage.h | 1 - src/emu/image.cpp | 10 ++---- src/emu/softlist.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/emu/softlist.h | 5 +++ 5 files changed, 82 insertions(+), 58 deletions(-) diff --git a/src/emu/diimage.cpp b/src/emu/diimage.cpp index 935d1df52b9..c29bf3e891b 100644 --- a/src/emu/diimage.cpp +++ b/src/emu/diimage.cpp @@ -1254,53 +1254,6 @@ void device_image_interface::update_names(const device_type device_type, const c } } -//------------------------------------------------- -// software_name_split - helper that splits a -// software_list:software:part string into -// separate software_list, software, and part -// strings. -// -// str1:str2:str3 => swlist_name - str1, swname - str2, swpart - str3 -// str1:str2 => swlist_name - nullptr, swname - str1, swpart - str2 -// str1 => swlist_name - nullptr, swname - str1, swpart - nullptr -// -// Notice however that we could also have been -// passed a string swlist_name:swname, and thus -// some special check has to be performed in this -// case. -//------------------------------------------------- - -void device_image_interface::software_name_split(const std::string &swlist_swname, std::string &swlist_name, std::string &swname, std::string &swpart) -{ - // reset all output parameters - swlist_name.clear(); - swname.clear(); - swpart.clear(); - - // if no colon, this is the swname by itself - auto split1 = swlist_swname.find_first_of(':'); - if (split1 == std::string::npos) - { - swname = swlist_swname; - return; - } - - // if one colon, it is the swname and swpart alone - auto split2 = swlist_swname.find_first_of(':', split1 + 1); - if (split2 == std::string::npos) - { - swname = swlist_swname.substr(0, split1); - swpart = swlist_swname.substr(split1 + 1); - return; - } - - // if two colons present, split into 3 parts - swlist_name = swlist_swname.substr(0, split1); - swname = swlist_swname.substr(split1 + 1, split2 - (split1 + 1)); - swpart = swlist_swname.substr(split2 + 1); -} - - //------------------------------------------------- // find_software_item //------------------------------------------------- @@ -1309,7 +1262,8 @@ const software_part *device_image_interface::find_software_item(const std::strin { // split full software name into software list name and short software name std::string swlist_name, swinfo_name, swpart_name; - software_name_split(path, swlist_name, swinfo_name, swpart_name); + if (!software_name_parse(path, &swlist_name, &swinfo_name, &swpart_name)) + return nullptr; // determine interface const char *interface = nullptr; @@ -1343,7 +1297,7 @@ const software_part *device_image_interface::find_software_item(const std::strin const software_info *info = swlistdev.find(swpart_name.c_str()); if (info != nullptr) { - const software_part *part = info->find_part(nullptr, interface); + const software_part *part = info->find_part("", interface); if (part != nullptr) { if (dev != nullptr) diff --git a/src/emu/diimage.h b/src/emu/diimage.h index d2ea51c47e7..aac698230b8 100644 --- a/src/emu/diimage.h +++ b/src/emu/diimage.h @@ -238,7 +238,6 @@ public: bool load_software(software_list_device &swlist, const char *swname, const rom_entry *entry); int reopen_for_write(const std::string &path); - static void software_name_split(const std::string &swlist_swname, std::string &swlist_name, std::string &swname, std::string &swpart); static void static_set_user_loadable(device_t &device, bool user_loadable) { device_image_interface *img; if (!device.interface(img)) diff --git a/src/emu/image.cpp b/src/emu/image.cpp index 5fbc5844b9d..06998ad37e6 100644 --- a/src/emu/image.cpp +++ b/src/emu/image.cpp @@ -10,19 +10,13 @@ ***************************************************************************/ #include -#include #include "emu.h" #include "emuopts.h" #include "image.h" #include "config.h" #include "xmlfile.h" - -//************************************************************************** -// STATIC VARIABLES -//************************************************************************** - -static std::regex s_potenial_softlist_regex("\\w+(\\:\\w+\\:\\w+)?"); +#include "softlist.h" //************************************************************************** @@ -54,7 +48,7 @@ image_manager::image_manager(running_machine &machine) image.set_init_phase(); // try as a softlist - if (std::regex_match(image_name, s_potenial_softlist_regex)) + if (software_name_parse(image_name)) result = image.load_software(image_name); // failing that, try as an image diff --git a/src/emu/softlist.cpp b/src/emu/softlist.cpp index 67178a1b4bb..277449c5492 100644 --- a/src/emu/softlist.cpp +++ b/src/emu/softlist.cpp @@ -8,10 +8,20 @@ ***************************************************************************/ +#include + #include "softlist.h" #include "hash.h" #include "expat.h" + +//************************************************************************** +// STATIC VARIABLES +//************************************************************************** + +static std::regex s_potenial_softlist_regex("\\w+(\\:\\w+)*"); + + //************************************************************************** // FEATURE LIST ITEM //************************************************************************** @@ -813,3 +823,65 @@ void softlist_parser::parse_soft_end(const char *tagname) } } + +//------------------------------------------------- +// software_name_parse - helper that splits a +// software_list:software:part string into +// separate software_list, software, and part +// strings. +// +// str1:str2:str3 => swlist_name - str1, swname - str2, swpart - str3 +// str1:str2 => swlist_name - nullptr, swname - str1, swpart - str2 +// str1 => swlist_name - nullptr, swname - str1, swpart - nullptr +// +// Notice however that we could also have been +// passed a string swlist_name:swname, and thus +// some special check has to be performed in this +// case. +//------------------------------------------------- + +bool software_name_parse(const std::string &text, std::string *swlist_name, std::string *swname, std::string *swpart) +{ + // first, sanity check the arguments + if (!std::regex_match(text, s_potenial_softlist_regex)) + return false; + + // reset all output parameters (if specified of course) + if (swlist_name != nullptr) + swlist_name->clear(); + if (swname != nullptr) + swname->clear(); + if (swpart != nullptr) + swpart->clear(); + + // if no colon, this is the swname by itself + auto split1 = text.find_first_of(':'); + if (split1 == std::string::npos) + { + if (swname != nullptr) + *swname = text; + return true; + } + + // if one colon, it is the swname and swpart alone + auto split2 = text.find_first_of(':', split1 + 1); + if (split2 == std::string::npos) + { + if (swname != nullptr) + *swname = text.substr(0, split1); + if (swpart != nullptr) + *swpart = text.substr(split1 + 1); + return true; + } + + // if two colons present, split into 3 parts + if (swlist_name != nullptr) + *swlist_name = text.substr(0, split1); + if (swname != nullptr) + *swname = text.substr(split1 + 1, split2 - (split1 + 1)); + if (swpart != nullptr) + *swpart = text.substr(split2 + 1); + return true; +} + + diff --git a/src/emu/softlist.h b/src/emu/softlist.h index 19ebc73e973..b517ab31458 100644 --- a/src/emu/softlist.h +++ b/src/emu/softlist.h @@ -207,5 +207,10 @@ private: }; +// ----- Helpers ----- + +// parses a software name (e.g. - 'apple2e:agentusa:flop1') into its consituent parts (returns false if cannot parse) +bool software_name_parse(const std::string &text, std::string *swlist_name = nullptr, std::string *swname = nullptr, std::string *swpart = nullptr); + #endif // __SOFTLIST_H_