diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/frontend/mame/cheat.cpp | 13 | ||||
-rw-r--r-- | src/frontend/mame/cheat.h | 14 | ||||
-rw-r--r-- | src/lib/util/xmlfile.cpp | 156 | ||||
-rw-r--r-- | src/lib/util/xmlfile.h | 30 |
4 files changed, 59 insertions, 154 deletions
diff --git a/src/frontend/mame/cheat.cpp b/src/frontend/mame/cheat.cpp index 2841ec1d7ce..3c2b2e1844b 100644 --- a/src/frontend/mame/cheat.cpp +++ b/src/frontend/mame/cheat.cpp @@ -74,7 +74,6 @@ #include "emu.h" #include "emuopts.h" -#include "xmlfile.h" #include "mame.h" #include "ui/ui.h" #include "ui/menu.h" @@ -109,16 +108,16 @@ inline std::string number_and_format::format() const switch (m_format) { default: - case XML_INT_FORMAT_DECIMAL: + case xml_data_node::int_format::DECIMAL: return string_format("%d", uint32_t(m_value)); - case XML_INT_FORMAT_DECIMAL_POUND: + case xml_data_node::int_format::DECIMAL_HASH: return string_format("#%d", uint32_t(m_value)); - case XML_INT_FORMAT_HEX_DOLLAR: + case xml_data_node::int_format::HEX_DOLLAR: return string_format("$%X", uint32_t(m_value)); - case XML_INT_FORMAT_HEX_C: + case xml_data_node::int_format::HEX_C: return string_format("0x%X", uint32_t(m_value)); } } @@ -153,8 +152,8 @@ cheat_parameter::cheat_parameter(cheat_manager &manager, symbol_table &symbols, throw emu_fatalerror("%s.xml(%d): item is value\n", filename, itemnode->line); // extract the parameters - uint64_t value = itemnode->get_attribute_int("value", 0); - int format = itemnode->get_attribute_int_format("value"); + uint64_t const value = itemnode->get_attribute_int("value", 0); + xml_data_node::int_format const format = itemnode->get_attribute_int_format("value"); // allocate and append a new item auto curitem = std::make_unique<item>(itemnode->get_value(), value, format); diff --git a/src/frontend/mame/cheat.h b/src/frontend/mame/cheat.h index aa08106b15a..bc983c160f1 100644 --- a/src/frontend/mame/cheat.h +++ b/src/frontend/mame/cheat.h @@ -16,6 +16,7 @@ #include "debug/express.h" #include "debug/debugcpu.h" #include "ui/text.h" +#include "xmlfile.h" //************************************************************************** @@ -49,7 +50,7 @@ class number_and_format { public: // construction/destruction - number_and_format(uint64_t value = 0, int format = 0) + number_and_format(uint64_t value = 0, xml_data_node::int_format format = xml_data_node::int_format::DECIMAL) : m_value(value) , m_format(format) { @@ -64,8 +65,8 @@ public: private: // internal state - uint64_t m_value; - int m_format; + uint64_t m_value; + xml_data_node::int_format m_format; }; @@ -98,9 +99,10 @@ private: { public: // construction/destruction - item(const char *text, uint64_t value, int valformat) - : m_text(text), - m_value(value, valformat) { } + item(const char *text, uint64_t value, xml_data_node::int_format valformat) + : m_text(text) + , m_value(value, valformat) + { } // getters const number_and_format &value() const { return m_value; } diff --git a/src/lib/util/xmlfile.cpp b/src/lib/util/xmlfile.cpp index 08512611668..5c9096608bf 100644 --- a/src/lib/util/xmlfile.cpp +++ b/src/lib/util/xmlfile.cpp @@ -11,6 +11,7 @@ #include <assert.h> #include "xmlfile.h" + #include <expat.h> #include <algorithm> @@ -54,61 +55,6 @@ static void expat_element_end(void *data, const XML_Char *name); /*************************************************************************** - CORE IMPLEMENTATION -***************************************************************************/ - -/*------------------------------------------------- - copystring - make an allocated copy of a - string --------------------------------------------------*/ - -static char *copystring(const char *input) -{ - char *newstr; - - /* nullptr just passes through */ - if (input == nullptr) - return nullptr; - - /* make a lower-case copy if the allocation worked */ - newstr = (char *)malloc(strlen(input) + 1); - if (newstr != nullptr) - strcpy(newstr, input); - - return newstr; -} - - -/*------------------------------------------------- - copystring_lower - make an allocated copy of - a string and convert it to lowercase along - the way --------------------------------------------------*/ - -static char *copystring_lower(const char *input) -{ - char *newstr; - int i; - - /* nullptr just passes through */ - if (input == nullptr) - return nullptr; - - /* make a lower-case copy if the allocation worked */ - newstr = (char *)malloc(strlen(input) + 1); - if (newstr != nullptr) - { - for (i = 0; input[i] != 0; i++) - newstr[i] = tolower((uint8_t)input[i]); - newstr[i] = 0; - } - - return newstr; -} - - - -/*************************************************************************** XML FILE OBJECTS ***************************************************************************/ @@ -161,8 +107,8 @@ xml_data_node *xml_data_node::file_read(util::core_file &file, xml_parse_options XML_ParserFree(info.parser); return nullptr; } - - } while (!done); + } + while (!done); /* free the parser */ XML_ParserFree(info.parser); @@ -252,8 +198,8 @@ xml_data_node::xml_data_node() : line(0) , m_next(nullptr) , m_first_child(nullptr) - , m_name(nullptr) - , m_value(nullptr) + , m_name() + , m_value() , m_parent(nullptr) , m_attributes() { @@ -263,11 +209,12 @@ xml_data_node::xml_data_node(xml_data_node *parent, const char *name, const char : line(0) , m_next(nullptr) , m_first_child(nullptr) - , m_name(::copystring_lower(name)) - , m_value(::copystring(value)) + , m_name(name) + , m_value(value) , m_parent(parent) , m_attributes() { + std::transform(m_name.begin(), m_name.end(), m_name.begin(), [] (char ch) { return std::tolower(uint8_t(ch)); }); } @@ -278,12 +225,6 @@ xml_data_node::xml_data_node(xml_data_node *parent, const char *name, const char xml_data_node::~xml_data_node() { - /* free name/value */ - if (m_name) - std::free((void *)m_name); - if (m_value) - std::free((void *)m_value); - /* free the children */ for (xml_data_node *nchild = nullptr; m_first_child; m_first_child = nchild) { @@ -296,67 +237,29 @@ xml_data_node::~xml_data_node() void xml_data_node::set_value(char const *value) { - if (m_value) - std::free((void *)m_value); - - m_value = ::copystring(value); + m_value.assign(value); } void xml_data_node::append_value(char const *value, int length) { - if (length) - { - /* determine how much data we currently have */ - int const oldlen = m_value ? int(std::strlen(m_value)) : 0; - - /* realloc */ - char *const newdata = reinterpret_cast<char *>(std::malloc(oldlen + length + 1)); - if (newdata) - { - if (m_value) - { - std::memcpy(newdata, m_value, oldlen); - std::free((void *)m_value); - } - m_value = newdata; - - /* copy in the new data and NUL-terminate */ - std::memcpy(&newdata[oldlen], value, length); - newdata[oldlen + length] = '\0'; - } - } + m_value.append(value, length); } void xml_data_node::trim_whitespace() { - if (m_value) - { - char *start = m_value; - char *end = start + strlen(m_value); - - /* first strip leading spaces */ - while (*start && std::isspace(uint8_t(*start))) - start++; - - /* then strip trailing spaces */ - while ((end > start) && std::isspace(uint8_t(end[-1]))) - end--; + /* first strip leading spaces */ + std::string::const_iterator start = m_value.begin(); + while ((m_value.end() != start) && std::isspace(uint8_t(*start))) + ++start; + m_value.replace(m_value.begin(), start, 0U, '\0'); - if (start == end) - { - /* if nothing left, just free it */ - std::free(m_value); - m_value = nullptr; - } - else - { - /* otherwise, memmove the data */ - std::memmove(m_value, start, end - start); - m_value[end - start] = '\0'; - } - } + /* then strip trailing spaces */ + std::string::const_iterator end = m_value.end(); + while ((m_value.begin() != end) && std::isspace(uint8_t(*std::prev(end)))) + --end; + m_value.replace(end, m_value.end(), 0U, '\0'); } @@ -655,19 +558,20 @@ int xml_data_node::get_attribute_int(const char *attribute, int defvalue) const format of the given integer attribute -------------------------------------------------*/ -int xml_data_node::get_attribute_int_format(const char *attribute) const +xml_data_node::int_format xml_data_node::get_attribute_int_format(const char *attribute) const { char const *const string = get_attribute_string(attribute, nullptr); - if (string == nullptr) - return XML_INT_FORMAT_DECIMAL; - if (string[0] == '$') - return XML_INT_FORMAT_HEX_DOLLAR; - if (string[0] == '0' && string[1] == 'x') - return XML_INT_FORMAT_HEX_C; + if (!string) + return int_format::DECIMAL; + else if (string[0] == '$') + return int_format::HEX_DOLLAR; + else if (string[0] == '0' && string[1] == 'x') + return int_format::HEX_C; if (string[0] == '#') - return XML_INT_FORMAT_DECIMAL_POUND; - return XML_INT_FORMAT_DECIMAL; + return int_format::DECIMAL_HASH; + else + return int_format::DECIMAL; } diff --git a/src/lib/util/xmlfile.h b/src/lib/util/xmlfile.h index 6696b636774..e6d33970292 100644 --- a/src/lib/util/xmlfile.h +++ b/src/lib/util/xmlfile.h @@ -31,16 +31,6 @@ enum }; -enum -{ - XML_INT_FORMAT_DECIMAL, - XML_INT_FORMAT_DECIMAL_POUND, - XML_INT_FORMAT_HEX_DOLLAR, - XML_INT_FORMAT_HEX_C -}; - - - /*************************************************************************** TYPE DEFINITIONS ***************************************************************************/ @@ -71,6 +61,16 @@ struct xml_parse_options class xml_data_node { public: + enum class int_format + { + DECIMAL, + DECIMAL_HASH, + HEX_DOLLAR, + HEX_C + }; + + + /* ----- XML file objects ----- */ // create a new empty xml file object @@ -91,9 +91,9 @@ public: /* ----- XML node management ----- */ - char const *get_name() const { return m_name; } + char const *get_name() const { return m_name.empty() ? nullptr : m_name.c_str(); } - char const *get_value() const { return m_value; } + char const *get_value() const { return m_value.empty() ? nullptr : m_value.c_str(); } void set_value(char const *value); void append_value(char const *value, int length); void trim_whitespace(); @@ -151,7 +151,7 @@ public: int get_attribute_int(const char *attribute, int defvalue) const; // return the format of the given integer attribute - int get_attribute_int_format(const char *attribute) const; + int_format get_attribute_int_format(const char *attribute) const; // return the float value of an attribute, or the specified default if not present float get_attribute_float(const char *attribute, float defvalue) const; @@ -206,8 +206,8 @@ private: xml_data_node * m_next; xml_data_node * m_first_child; - char const * m_name; - char * m_value; + std::string m_name; + std::string m_value; xml_data_node * m_parent; std::list<attribute_node> m_attributes; }; |