diff options
author | 2016-12-11 18:15:41 +1100 | |
---|---|---|
committer | 2016-12-11 18:15:41 +1100 | |
commit | c8f19544676e2954bf2bb4ca03aa90d1d1148fc8 (patch) | |
tree | dd802a7f701c5ef7d3a5270a19f5a295a8ed8e7f /src/emu/config.cpp | |
parent | f38bc880f0fe3eb73740f8b5c00c359402afb3c4 (diff) |
XML refactoring:
* move stuff to namespace util::xml
* scope down some enums
* split config load/save delegate types
* make config load take const so it can't mangle data
Diffstat (limited to 'src/emu/config.cpp')
-rw-r--r-- | src/emu/config.cpp | 108 |
1 files changed, 48 insertions, 60 deletions
diff --git a/src/emu/config.cpp b/src/emu/config.cpp index 8e3357adfd6..fb1204fd622 100644 --- a/src/emu/config.cpp +++ b/src/emu/config.cpp @@ -36,7 +36,7 @@ configuration_manager::configuration_manager(running_machine &machine) * *************************************/ -void configuration_manager::config_register(const char* nodename, config_saveload_delegate load, config_saveload_delegate save) +void configuration_manager::config_register(const char* nodename, config_load_delegate load, config_save_delegate save) { config_element element; element.name = nodename; @@ -61,7 +61,7 @@ int configuration_manager::load_settings() /* loop over all registrants and call their init function */ for (auto type : m_typelist) - type.load(config_type::CONFIG_TYPE_INIT, nullptr); + type.load(config_type::INIT, nullptr); /* now load the controller file */ if (controller[0] != 0) @@ -74,7 +74,7 @@ int configuration_manager::load_settings() throw emu_fatalerror("Could not load controller file %s.cfg", controller); /* load the XML */ - if (!load_xml(file, config_type::CONFIG_TYPE_CONTROLLER)) + if (!load_xml(file, config_type::CONTROLLER)) throw emu_fatalerror("Could not load controller file %s.cfg", controller); } @@ -82,16 +82,16 @@ int configuration_manager::load_settings() emu_file file(machine().options().cfg_directory(), OPEN_FLAG_READ); osd_file::error filerr = file.open("default.cfg"); if (filerr == osd_file::error::NONE) - load_xml(file, config_type::CONFIG_TYPE_DEFAULT); + load_xml(file, config_type::DEFAULT); /* finally, load the game-specific file */ filerr = file.open(machine().basename(), ".cfg"); if (filerr == osd_file::error::NONE) - loaded = load_xml(file, config_type::CONFIG_TYPE_GAME); + loaded = load_xml(file, config_type::GAME); /* loop over all registrants and call their final function */ for (auto type : m_typelist) - type.load(config_type::CONFIG_TYPE_FINAL, nullptr); + type.load(config_type::FINAL, nullptr); /* if we didn't find a saved config, return 0 so the main core knows that it */ /* is the first time the game is run and it should diplay the disclaimer. */ @@ -103,22 +103,22 @@ void configuration_manager::save_settings() { /* loop over all registrants and call their init function */ for (auto type : m_typelist) - type.save(config_type::CONFIG_TYPE_INIT, nullptr); + type.save(config_type::INIT, nullptr); /* save the defaults file */ emu_file file(machine().options().cfg_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); osd_file::error filerr = file.open("default.cfg"); if (filerr == osd_file::error::NONE) - save_xml(file, config_type::CONFIG_TYPE_DEFAULT); + save_xml(file, config_type::DEFAULT); /* finally, save the game-specific file */ filerr = file.open(machine().basename(), ".cfg"); if (filerr == osd_file::error::NONE) - save_xml(file, config_type::CONFIG_TYPE_GAME); + save_xml(file, config_type::GAME); /* loop over all registrants and call their final function */ for (auto type : m_typelist) - type.save(config_type::CONFIG_TYPE_FINAL, nullptr); + type.save(config_type::FINAL, nullptr); } @@ -131,27 +131,25 @@ void configuration_manager::save_settings() int configuration_manager::load_xml(emu_file &file, config_type which_type) { - xml_data_node *root, *confignode, *systemnode; - const char *srcfile; - int version, count; - /* read the file */ - root = xml_data_node::file_read(file, nullptr); + std::unique_ptr<util::xml::data_node, void (*)(util::xml::data_node *)> const root( + util::xml::data_node::file_read(file, nullptr), + [] (util::xml::data_node *node) { node->file_free(); }); if (!root) - goto error; + return 0; /* find the config node */ - confignode = root->get_child("mameconfig"); + util::xml::data_node const *const confignode = root->get_child("mameconfig"); if (!confignode) - goto error; + return 0; /* validate the config data version */ - version = confignode->get_attribute_int("version", 0); + int const version = confignode->get_attribute_int("version", 0); if (version != CONFIG_VERSION) - goto error; + return 0; /* strip off all the path crap from the source filename */ - srcfile = strrchr(machine().system().source_file, '/'); + const char *srcfile = strrchr(machine().system().source_file, '/'); if (!srcfile) srcfile = strrchr(machine().system().source_file, '\\'); if (!srcfile) @@ -162,8 +160,8 @@ int configuration_manager::load_xml(emu_file &file, config_type which_type) srcfile++; /* loop over all system nodes in the file */ - count = 0; - for (systemnode = confignode->get_child("system"); systemnode; systemnode = systemnode->get_next_sibling("system")) + int count = 0; + for (util::xml::data_node const *systemnode = confignode->get_child("system"); systemnode; systemnode = systemnode->get_next_sibling("system")) { /* look up the name of the system here; skip if none */ const char *name = systemnode->get_attribute_string("name", ""); @@ -171,19 +169,19 @@ int configuration_manager::load_xml(emu_file &file, config_type which_type) /* based on the file type, determine whether we have a match */ switch (which_type) { - case config_type::CONFIG_TYPE_GAME: - /* only match on the specific game name */ - if (strcmp(name, machine().system().name) != 0) - continue; - break; - - case config_type::CONFIG_TYPE_DEFAULT: - /* only match on default */ - if (strcmp(name, "default") != 0) - continue; - break; - - case config_type::CONFIG_TYPE_CONTROLLER: + case config_type::GAME: + /* only match on the specific game name */ + if (strcmp(name, machine().system().name) != 0) + continue; + break; + + case config_type::DEFAULT: + /* only match on default */ + if (strcmp(name, "default") != 0) + continue; + break; + + case config_type::CONTROLLER: { int clone_of; /* match on: default, game name, source file name, parent name, grandparent name */ @@ -195,8 +193,9 @@ int configuration_manager::load_xml(emu_file &file, config_type which_type) continue; break; } - default: - break; + + default: + break; } /* log that we are processing this entry */ @@ -211,16 +210,9 @@ int configuration_manager::load_xml(emu_file &file, config_type which_type) /* error if this isn't a valid game match */ if (count == 0) - goto error; + return 0; - /* free the parser */ - root->file_free(); return 1; - -error: - if (root) - root->file_free(); - return 0; } @@ -233,32 +225,33 @@ error: int configuration_manager::save_xml(emu_file &file, config_type which_type) { - xml_data_node *const root = xml_data_node::file_create(); - xml_data_node *confignode, *systemnode; + std::unique_ptr<util::xml::data_node, void (*)(util::xml::data_node *)> const root( + util::xml::data_node::file_create(), + [] (util::xml::data_node *node) { node->file_free(); }); /* if we don't have a root, bail */ if (!root) return 0; /* create a config node */ - confignode = root->add_child("mameconfig", nullptr); + util::xml::data_node *const confignode = root->add_child("mameconfig", nullptr); if (!confignode) - goto error; + return 0; confignode->set_attribute_int("version", CONFIG_VERSION); /* create a system node */ - systemnode = confignode->add_child("system", nullptr); + util::xml::data_node *const systemnode = confignode->add_child("system", nullptr); if (!systemnode) - goto error; - systemnode->set_attribute("name", (which_type == config_type::CONFIG_TYPE_DEFAULT) ? "default" : machine().system().name); + return 0; + systemnode->set_attribute("name", (which_type == config_type::DEFAULT) ? "default" : machine().system().name); /* create the input node and write it out */ /* loop over all registrants and call their save function */ for (auto type : m_typelist) { - xml_data_node *curnode = systemnode->add_child(type.name.c_str(), nullptr); + util::xml::data_node *const curnode = systemnode->add_child(type.name.c_str(), nullptr); if (!curnode) - goto error; + return 0; type.save(which_type, curnode); /* if nothing was added, just nuke the node */ @@ -270,10 +263,5 @@ int configuration_manager::save_xml(emu_file &file, config_type which_type) root->file_write(file); /* free and get out of here */ - root->file_free(); return 1; - -error: - root->file_free(); - return 0; } |