From 0341f8ee4cb7706abfdc486d6741e6103e6af02a Mon Sep 17 00:00:00 2001 From: AJR Date: Sun, 12 Feb 2023 19:15:55 -0500 Subject: xmlfile.cpp: Replace implementation of normalize_string with similar one in infoxml.cpp that returns a new std::string instead of a static buffer --- src/frontend/mame/clifront.cpp | 32 +++++++++-------- src/frontend/mame/infoxml.cpp | 81 +++++++++++++++++------------------------- src/lib/util/xmlfile.cpp | 29 +++++++-------- src/lib/util/xmlfile.h | 3 +- 4 files changed, 64 insertions(+), 81 deletions(-) diff --git a/src/frontend/mame/clifront.cpp b/src/frontend/mame/clifront.cpp index 5fff54b46ca..a395fd3e88b 100644 --- a/src/frontend/mame/clifront.cpp +++ b/src/frontend/mame/clifront.cpp @@ -1189,37 +1189,39 @@ const char cli_frontend::s_softlist_xml_dtd[] = void cli_frontend::output_single_softlist(std::ostream &out, software_list_device &swlistdev) { - util::stream_format(out, "\t\n", swlistdev.list_name(), util::xml::normalize_string(swlistdev.description().c_str())); + using util::xml::normalize_string; + + util::stream_format(out, "\t\n", swlistdev.list_name(), normalize_string(swlistdev.description())); for (const software_info &swinfo : swlistdev.get_info()) { - util::stream_format(out, "\t\t\n"; - util::stream_format(out, "\t\t\t%s\n", util::xml::normalize_string(swinfo.longname().c_str())); - util::stream_format(out, "\t\t\t%s\n", util::xml::normalize_string(swinfo.year().c_str())); - util::stream_format(out, "\t\t\t%s\n", util::xml::normalize_string(swinfo.publisher().c_str())); + util::stream_format(out, "\t\t\t%s\n", normalize_string(swinfo.longname())); + util::stream_format(out, "\t\t\t%s\n", normalize_string(swinfo.year())); + util::stream_format(out, "\t\t\t%s\n", normalize_string(swinfo.publisher())); for (const auto &flist : swinfo.info()) - util::stream_format(out, "\t\t\t\n", flist.name(), util::xml::normalize_string(flist.value().c_str())); + util::stream_format(out, "\t\t\t\n", flist.name(), normalize_string(flist.value())); for (const auto &flist : swinfo.shared_features()) - util::stream_format(out, "\t\t\t\n", flist.name(), util::xml::normalize_string(flist.value().c_str())); + util::stream_format(out, "\t\t\t\n", flist.name(), normalize_string(flist.value())); for (const software_part &part : swinfo.parts()) { - util::stream_format(out, "\t\t\t\n"; for (const auto &flist : part.features()) - util::stream_format(out, "\t\t\t\t\n", flist.name(), util::xml::normalize_string(flist.value().c_str())); + util::stream_format(out, "\t\t\t\t\n", flist.name(), normalize_string(flist.value())); // TODO: display ROM region information for (const rom_entry *region = part.romdata().data(); region; region = rom_next_region(region)) @@ -1227,18 +1229,18 @@ void cli_frontend::output_single_softlist(std::ostream &out, software_list_devic int is_disk = ROMREGION_ISDISKDATA(region); if (!is_disk) - util::stream_format(out, "\t\t\t\t\n", util::xml::normalize_string(region->name().c_str()), region->get_length()); + util::stream_format(out, "\t\t\t\t\n", normalize_string(region->name()), region->get_length()); else - util::stream_format(out, "\t\t\t\t\n", util::xml::normalize_string(region->name().c_str())); + util::stream_format(out, "\t\t\t\t\n", normalize_string(region->name())); for (const rom_entry *rom = rom_first_file(region); rom && !ROMENTRY_ISREGIONEND(rom); rom++) { if (ROMENTRY_ISFILE(rom)) { if (!is_disk) - util::stream_format(out, "\t\t\t\t\thashdata()); diff --git a/src/frontend/mame/infoxml.cpp b/src/frontend/mame/infoxml.cpp index 03b23c9ca01..fa0d998aa1b 100644 --- a/src/frontend/mame/infoxml.cpp +++ b/src/frontend/mame/infoxml.cpp @@ -104,9 +104,6 @@ private: typedef std::set, device_type_compare> device_type_set; -std::string normalize_string(const char *string); -std::string normalize_string(std::string_view string); - // internal helper void output_header(std::ostream &out, bool dtd); void output_footer(std::ostream &out); @@ -580,39 +577,6 @@ void info_xml_creator::output(std::ostream &out, const std::function': result.append(">"); break; - default: result.append(1, ch); break; - } - } - - return result; -} - - //------------------------------------------------- // device_filter::filter - apply the filter, if // present @@ -669,6 +633,7 @@ void output_header(std::ostream &out, bool dtd) } // top-level tag + assert(emulator_info::get_build_version() != nullptr); util::stream_format(out, "<%s build=\"%s\" debug=\"" #ifdef MAME_DEBUG @@ -678,7 +643,7 @@ void output_header(std::ostream &out, bool dtd) #endif "\" mameconfig=\"%d\">\n", XML_ROOT, - normalize_string(emulator_info::get_build_version()), + util::xml::normalize_string(emulator_info::get_build_version()), configuration_manager::CONFIG_VERSION); } @@ -701,6 +666,8 @@ void output_footer(std::ostream &out) void output_one(std::ostream &out, driver_enumerator &drivlist, const game_driver &driver, device_type_set *devtypes) { + using util::xml::normalize_string; + machine_config config(driver, drivlist.options()); device_enumerator iter(config.root_device()); @@ -821,6 +788,8 @@ void output_one(std::ostream &out, driver_enumerator &drivlist, const game_drive void output_one_device(std::ostream &out, machine_config &config, device_t &device, const char *devtag) { + using util::xml::normalize_string; + bool has_speaker = false, has_input = false; // check if the device adds speakers to the system sound_interface_enumerator snditer(device); @@ -939,7 +908,7 @@ void output_device_refs(std::ostream &out, device_t &root) { for (device_t &device : device_enumerator(root)) if (&device != &root) - util::stream_format(out, "\t\t\n", normalize_string(device.shortname())); + util::stream_format(out, "\t\t\n", util::xml::normalize_string(device.shortname())); } @@ -956,7 +925,7 @@ void output_sampleof(std::ostream &out, device_t &device) samples_iterator sampiter(samples); if (sampiter.altbasename() != nullptr) { - util::stream_format(out, " sampleof=\"%s\"", normalize_string(sampiter.altbasename())); + util::stream_format(out, " sampleof=\"%s\"", util::xml::normalize_string(sampiter.altbasename())); // must stop here, as there can only be one attribute of the same name return; @@ -984,8 +953,8 @@ void output_bios(std::ostream &out, device_t const &device) { // output extracted name and descriptions' out << "\t\t\n"; @@ -1056,6 +1025,8 @@ void output_rom(std::ostream &out, machine_config &config, driver_list const *dr tiny_rom_entry const *region(nullptr); for (tiny_rom_entry const *rom = device.rom_region(); rom && !ROMENTRY_ISEND(rom); ++rom) { + using util::xml::normalize_string; + if (ROMENTRY_ISREGION(rom)) region = rom; else if (ROMENTRY_ISSYSTEM_BIOS(rom)) @@ -1150,7 +1121,7 @@ void output_sample(std::ostream &out, device_t &device) continue; // output the sample name - util::stream_format(out, "\t\t\n", normalize_string(samplename)); + util::stream_format(out, "\t\t\n", util::xml::normalize_string(samplename)); } } } @@ -1163,6 +1134,8 @@ void output_sample(std::ostream &out, device_t &device) void output_chips(std::ostream &out, device_t &device, const char *root_tag) { + using util::xml::normalize_string; + // iterate over executable devices for (device_execute_interface &exec : execute_interface_enumerator(device)) { @@ -1215,7 +1188,7 @@ void output_display(std::ostream &out, device_t &device, machine_flags::type con std::string newtag(screendev.tag()), oldtag(":"); newtag = newtag.substr(newtag.find(oldtag.append(root_tag)) + oldtag.length()); - util::stream_format(out, "\t\t\n", normalize_string(condition.tag()), condition.mask(), rel, condition.value()); + util::stream_format(out, "\n", util::xml::normalize_string(condition.tag()), condition.mask(), rel, condition.value()); } //------------------------------------------------- @@ -1743,6 +1716,8 @@ void output_input(std::ostream &out, const ioport_list &portlist) for (auto & elem : control_info) if (elem.type != nullptr) { + using util::xml::normalize_string; + //printf("type %s - player %d - buttons %d\n", elem.type, elem.player, elem.nbuttons); if (elem.analog) { @@ -1836,10 +1811,13 @@ void output_switches(std::ostream &out, const ioport_list &portlist, const char for (ioport_field const &field : port.second->fields()) if (field.type() == type) { + using util::xml::normalize_string; + std::string newtag(port.second->tag()), oldtag(":"); newtag = newtag.substr(newtag.find(oldtag.append(root_tag)) + oldtag.length()); // output the switch name information + assert(field.specific_name() != nullptr); std::string const normalized_field_name(normalize_string(field.specific_name())); std::string const normalized_newtag(normalize_string(newtag)); util::stream_format(out, "\t\t<%s name=\"%s\" tag=\"%s\" mask=\"%u\">\n", outertag, normalized_field_name, normalized_newtag, field.mask()); @@ -1887,7 +1865,7 @@ void output_ports(std::ostream &out, const ioport_list &portlist) // cycle through ports for (auto &port : portlist) { - util::stream_format(out, "\t\t\n", normalize_string(port.second->tag())); + util::stream_format(out, "\t\t\n", util::xml::normalize_string(port.second->tag())); for (ioport_field const &field : port.second->fields()) { if (field.is_analog()) @@ -1910,7 +1888,7 @@ void output_adjusters(std::ostream &out, const ioport_list &portlist) for (ioport_field const &field : port.second->fields()) if (field.type() == IPT_ADJUSTER) { - util::stream_format(out, "\t\t\n", normalize_string(field.specific_name()), field.defvalue()); + util::stream_format(out, "\t\t\n", util::xml::normalize_string(field.specific_name()), field.defvalue()); } } @@ -2016,11 +1994,14 @@ void output_images(std::ostream &out, device_t &device, const char *root_tag) { if (strcmp(imagedev.device().tag(), device.tag())) { + using util::xml::normalize_string; + bool loadable = imagedev.user_loadable(); std::string newtag(imagedev.device().tag()), oldtag(":"); newtag = newtag.substr(newtag.find(oldtag.append(root_tag)) + oldtag.length()); // print m_output device type + assert(imagedev.image_type_name() != nullptr); util::stream_format(out, "\t\t(swlist) == &root) { assert(swlist.list_name().empty()); @@ -2165,11 +2150,11 @@ void output_ramoptions(std::ostream &out, device_t &root) { assert(!havedefault); havedefault = true; - util::stream_format(out, "\t\t%u\n", normalize_string(option.first), option.second); + util::stream_format(out, "\t\t%u\n", util::xml::normalize_string(option.first), option.second); } else { - util::stream_format(out, "\t\t%u\n", normalize_string(option.first), option.second); + util::stream_format(out, "\t\t%u\n", util::xml::normalize_string(option.first), option.second); } } if (!havedefault) diff --git a/src/lib/util/xmlfile.cpp b/src/lib/util/xmlfile.cpp index be7bab597ca..39604a29a87 100644 --- a/src/lib/util/xmlfile.cpp +++ b/src/lib/util/xmlfile.cpp @@ -782,29 +782,24 @@ void data_node::set_attribute_float(const char *name, float value) // to ensure it doesn't contain embedded tags //------------------------------------------------- -const char *normalize_string(const char *string) +std::string normalize_string(std::string_view string) { - static char buffer[1024]; - char *d = &buffer[0]; + std::string result; + result.reserve(string.length()); - if (string != nullptr) + for (char ch : string) { - while (*string) + switch (ch) { - switch (*string) - { - case '\"' : d += sprintf(d, """); break; - case '&' : d += sprintf(d, "&"); break; - case '<' : d += sprintf(d, "<"); break; - case '>' : d += sprintf(d, ">"); break; - default: - *d++ = *string; - } - ++string; + case '\"': result.append("""); break; + case '&': result.append("&"); break; + case '<': result.append("<"); break; + case '>': result.append(">"); break; + default: result.append(1, ch); break; } } - *d++ = 0; - return buffer; + + return result; } diff --git a/src/lib/util/xmlfile.h b/src/lib/util/xmlfile.h index 8d0f2b8c36c..8949ede2d6d 100644 --- a/src/lib/util/xmlfile.h +++ b/src/lib/util/xmlfile.h @@ -16,6 +16,7 @@ #include #include +#include #include @@ -245,7 +246,7 @@ private: /* ----- miscellaneous interfaces ----- */ /* normalize a string into something that can be written to an XML file */ -const char *normalize_string(const char *string); +std::string normalize_string(std::string_view string); } // namespace util::xml -- cgit v1.2.3