diff options
author | 2016-07-15 22:24:34 +1000 | |
---|---|---|
committer | 2016-07-15 22:24:34 +1000 | |
commit | ce0162de56e22d0bfde739492ea653a1a0686144 (patch) | |
tree | 9737bb369d82e2156e9681c901974bbc1581244b /src/frontend/mame/ui/datfile.cpp | |
parent | 200adb341a40f2981ce34f7470bfda7c20b5f21f (diff) |
UI refactoring: [Vas Crabb]
* more const where it should be
* don't unnecessarily copy big maps
* don't make things members when they shouldn't be
* get rid of a couple more function statics
* move custom render and tab navigate up to base class
Diffstat (limited to 'src/frontend/mame/ui/datfile.cpp')
-rw-r--r-- | src/frontend/mame/ui/datfile.cpp | 199 |
1 files changed, 107 insertions, 92 deletions
diff --git a/src/frontend/mame/ui/datfile.cpp b/src/frontend/mame/ui/datfile.cpp index d5c77e30893..17552f34d68 100644 --- a/src/frontend/mame/ui/datfile.cpp +++ b/src/frontend/mame/ui/datfile.cpp @@ -9,11 +9,15 @@ ***************************************************************************/ #include "emu.h" +#include "ui/datfile.h" + #include "drivenum.h" #include "ui/moptions.h" -#include "ui/datfile.h" #include "ui/utils.h" +#include <utility> + + namespace ui { namespace { //------------------------------------------------- @@ -59,7 +63,7 @@ std::string datfile_manager::m_story_rev; std::string datfile_manager::m_ginit_rev; bool datfile_manager::first_run = true; -#define opendatsfile(f) if (parseopen(#f".dat")) { init_##f(); parseclose(); } +#define opendatsfile(f) do { fileptr datfile = parseopen(#f".dat"); if (datfile) init_##f(std::move(datfile)); } while (false) //------------------------------------------------- // ctor @@ -84,10 +88,10 @@ datfile_manager::datfile_manager(running_machine &machine, ui_options &moptions) //------------------------------------------------- // initialize sysinfo.dat index //------------------------------------------------- -void datfile_manager::init_sysinfo() +void datfile_manager::init_sysinfo(fileptr &&fp) { int swcount = 0; - auto count = index_datafile(m_sysidx, swcount, m_sysinfo_rev, TAG_SYSINFO_R, '.'); + auto count = index_datafile(std::move(fp), m_sysidx, swcount, m_sysinfo_rev, TAG_SYSINFO_R, '.'); osd_printf_verbose("Sysinfo.dat games found = %i\n", count); osd_printf_verbose("Rev = %s\n", m_sysinfo_rev.c_str()); } @@ -95,20 +99,20 @@ void datfile_manager::init_sysinfo() //------------------------------------------------- // initialize story.dat index //------------------------------------------------- -void datfile_manager::init_story() +void datfile_manager::init_story(fileptr &&fp) { int swcount = 0; - auto count = index_datafile(m_storyidx, swcount, m_story_rev, TAG_STORY_R, 's'); + auto count = index_datafile(std::move(fp), m_storyidx, swcount, m_story_rev, TAG_STORY_R, 's'); osd_printf_verbose("Story.dat games found = %i\n", count); } //------------------------------------------------- // initialize history.dat index //------------------------------------------------- -void datfile_manager::init_history() +void datfile_manager::init_history(fileptr &&fp) { int swcount = 0; - auto count = index_datafile(m_histidx, swcount, m_history_rev, TAG_HISTORY_R, ' '); + auto count = index_datafile(std::move(fp), m_histidx, swcount, m_history_rev, TAG_HISTORY_R, ' '); osd_printf_verbose("History.dat systems found = %i\n", count); osd_printf_verbose("History.dat software packages found = %i\n", swcount); osd_printf_verbose("Rev = %s\n", m_history_rev.c_str()); @@ -117,11 +121,11 @@ void datfile_manager::init_history() //------------------------------------------------- // initialize gameinit.dat index //------------------------------------------------- -void datfile_manager::init_gameinit() +void datfile_manager::init_gameinit(fileptr &&fp) { int swcount = 0; drvindex tmp; - auto count = index_mame_mess_info(m_ginitidx, tmp, swcount); + auto count = index_mame_mess_info(std::move(fp), m_ginitidx, tmp, swcount); osd_printf_verbose("Gameinit.dat games found = %i\n", count); osd_printf_verbose("Rev = %s\n", m_ginit_rev.c_str()); } @@ -129,10 +133,10 @@ void datfile_manager::init_gameinit() //------------------------------------------------- // initialize mameinfo.dat index //------------------------------------------------- -void datfile_manager::init_mameinfo() +void datfile_manager::init_mameinfo(fileptr &&fp) { int drvcount = 0; - auto count = index_mame_mess_info(m_mameidx, m_drvidx, drvcount); + auto count = index_mame_mess_info(std::move(fp), m_mameidx, m_drvidx, drvcount); osd_printf_verbose("Mameinfo.dat games found = %i\n", count); osd_printf_verbose("Mameinfo.dat drivers found = %d\n", drvcount); osd_printf_verbose("Rev = %s\n", m_mame_rev.c_str()); @@ -141,10 +145,10 @@ void datfile_manager::init_mameinfo() //------------------------------------------------- // initialize messinfo.dat index //------------------------------------------------- -void datfile_manager::init_messinfo() +void datfile_manager::init_messinfo(fileptr &&fp) { int drvcount = 0; - auto count = index_mame_mess_info(m_messidx, m_messdrvidx, drvcount); + auto count = index_mame_mess_info(std::move(fp), m_messidx, m_messdrvidx, drvcount); osd_printf_verbose("Messinfo.dat games found = %i\n", count); osd_printf_verbose("Messinfo.dat drivers found = %d\n", drvcount); osd_printf_verbose("Rev = %s\n", m_mess_rev.c_str()); @@ -153,26 +157,31 @@ void datfile_manager::init_messinfo() //------------------------------------------------- // initialize command.dat index //------------------------------------------------- -void datfile_manager::init_command() +void datfile_manager::init_command(fileptr &&fp) { int swcount = 0; std::string tmp; - auto count = index_datafile(m_cmdidx, swcount, tmp, std::string(), 'c'); + auto count = index_datafile(std::move(fp), m_cmdidx, swcount, tmp, std::string(), 'c'); osd_printf_verbose("Command.dat games found = %i\n", count); } -bool datfile_manager::has_software(std::string const &softlist, std::string const &softname, std::string const &parentname) +bool datfile_manager::has_software(std::string const &softlist, std::string const &softname, std::string const &parentname) const +{ + return bool(find_software(softlist, softname, parentname)); +} + +long const *datfile_manager::find_software(std::string const &softlist, std::string const &softname, std::string const &parentname) const { // Find software in software list index - auto const found(m_swindex.find(softlist)); - if (found == m_swindex.end()) - return false; + auto const software(m_swindex.find(softlist)); + if (software == m_swindex.end()) + return nullptr; - m_itemsiter = found->second.find(softname); - if ((m_itemsiter == found->second.end()) && !parentname.empty()) - m_itemsiter = found->second.find(parentname); + auto itemsiter = software->second.find(softname); + if ((itemsiter == software->second.end()) && !parentname.empty()) + itemsiter = software->second.find(parentname); - return m_itemsiter != found->second.end(); + return (itemsiter != software->second.end()) ? &itemsiter->second : nullptr; } //------------------------------------------------- @@ -180,18 +189,22 @@ bool datfile_manager::has_software(std::string const &softlist, std::string cons //------------------------------------------------- void datfile_manager::load_software_info(std::string const &softlist, std::string &buffer, std::string const &softname, std::string const &parentname) { + if (m_swindex.empty()) + return; + // Load history text - if (!m_swindex.empty() && parseopen("history.dat")) + fileptr const datfile = parseopen("history.dat"); + if (datfile) { // Find software in software list index - if (!has_software(softlist, softname, parentname)) + long const *const s_offset = find_software(softlist, softname, parentname); + if (!s_offset) return; - auto s_offset = m_itemsiter->second; char rbuf[64 * 1024]; - fseek(fp, s_offset, SEEK_SET); + std::fseek(datfile.get(), *s_offset, SEEK_SET); std::string readbuf; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, datfile.get()) != nullptr) { readbuf = chartrimcarriage(rbuf); @@ -202,7 +215,6 @@ void datfile_manager::load_software_info(std::string const &softlist, std::strin // add this string to the buffer buffer.append(readbuf).append("\n"); } - parseclose(); } } @@ -211,66 +223,69 @@ void datfile_manager::load_software_info(std::string const &softlist, std::strin //------------------------------------------------- void datfile_manager::load_data_info(const game_driver *drv, std::string &buffer, int type) { - dataindex index_idx; - drvindex driver_idx; - std::string tag, filename; + dataindex const *index_idx = nullptr; + drvindex const *driver_idx = nullptr; + std::string const *tag; + std::string filename; switch (type) { case UI_HISTORY_LOAD: filename = "history.dat"; - tag = TAG_BIO; - index_idx = m_histidx; + tag = &TAG_BIO; + index_idx = &m_histidx; break; case UI_MAMEINFO_LOAD: filename = "mameinfo.dat"; - tag = TAG_MAME; - index_idx = m_mameidx; - driver_idx = m_drvidx; + tag = &TAG_MAME; + index_idx = &m_mameidx; + driver_idx = &m_drvidx; break; case UI_SYSINFO_LOAD: filename = "sysinfo.dat"; - tag = TAG_BIO; - index_idx = m_sysidx; + tag = &TAG_BIO; + index_idx = &m_sysidx; break; case UI_MESSINFO_LOAD: filename = "messinfo.dat"; - tag = TAG_MAME; - index_idx = m_messidx; - driver_idx = m_messdrvidx; + tag = &TAG_MAME; + index_idx = &m_messidx; + driver_idx = &m_messdrvidx; break; case UI_STORY_LOAD: filename = "story.dat"; - tag = TAG_STORY; - index_idx = m_storyidx; + tag = &TAG_STORY; + index_idx = &m_storyidx; break; case UI_GINIT_LOAD: filename = "gameinit.dat"; - tag = TAG_MAME; - index_idx = m_ginitidx; + tag = &TAG_MAME; + index_idx = &m_ginitidx; break; + default: + assert(false); + return; } - if (parseopen(filename.c_str())) + fileptr const datfile = parseopen(filename.c_str()); + if (datfile) { - load_data_text(drv, buffer, index_idx, tag); + load_data_text(datfile.get(), drv, buffer, *index_idx, *tag); // load driver info - if (!driver_idx.empty()) - load_driver_text(drv, buffer, driver_idx, TAG_DRIVER); + if (driver_idx && !driver_idx->empty()) + load_driver_text(datfile.get(), drv, buffer, *driver_idx, TAG_DRIVER); // cleanup mameinfo and sysinfo double line spacing - if ((tag == TAG_MAME && type != UI_GINIT_LOAD) || type == UI_SYSINFO_LOAD) + if (((*tag == TAG_MAME) && (type != UI_GINIT_LOAD)) || (type == UI_SYSINFO_LOAD)) strreplace(buffer, "\n\n", "\n"); - - parseclose(); } } //------------------------------------------------- // load a game text into the buffer //------------------------------------------------- -void datfile_manager::load_data_text(const game_driver *drv, std::string &buffer, dataindex &idx, std::string const &tag) +void datfile_manager::load_data_text(FILE *fp, game_driver const *drv, std::string &buffer, dataindex const &idx, std::string const &tag) { auto itemsiter = idx.find(drv); if (itemsiter == idx.end()) @@ -288,10 +303,10 @@ void datfile_manager::load_data_text(const game_driver *drv, std::string &buffer } auto s_offset = itemsiter->second; - fseek(fp, s_offset, SEEK_SET); + std::fseek(fp, s_offset, SEEK_SET); char rbuf[64 * 1024]; std::string readbuf; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, fp) != nullptr) { readbuf = chartrimcarriage(rbuf); @@ -312,7 +327,7 @@ void datfile_manager::load_data_text(const game_driver *drv, std::string &buffer // load a driver name and offset into an // indexed array //------------------------------------------------- -void datfile_manager::load_driver_text(const game_driver *drv, std::string &buffer, drvindex &idx, std::string const &tag) +void datfile_manager::load_driver_text(FILE *fp, game_driver const *drv, std::string &buffer, drvindex const &idx, std::string const &tag) { std::string s(core_filename_extract_base(drv->source_file)); auto index = idx.find(s); @@ -323,10 +338,10 @@ void datfile_manager::load_driver_text(const game_driver *drv, std::string &buff buffer.append("\n--- DRIVER INFO ---\n").append("Driver: ").append(s).append("\n\n"); auto s_offset = index->second; - fseek(fp, s_offset, SEEK_SET); + std::fseek(fp, s_offset, SEEK_SET); char rbuf[64 * 1024]; std::string readbuf; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, fp) != nullptr) { readbuf = chartrimcarriage(rbuf); @@ -347,7 +362,7 @@ void datfile_manager::load_driver_text(const game_driver *drv, std::string &buff // load a game name and offset into an // indexed array (mameinfo) //------------------------------------------------- -int datfile_manager::index_mame_mess_info(dataindex &index, drvindex &index_drv, int &drvcount) +int datfile_manager::index_mame_mess_info(fileptr &&fp, dataindex &index, drvindex &index_drv, int &drvcount) { size_t foundtag; auto t_mame = TAG_MAMEINFO_R.size(); @@ -357,7 +372,7 @@ int datfile_manager::index_mame_mess_info(dataindex &index, drvindex &index_drv, char rbuf[64 * 1024]; std::string readbuf, xid, name; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, fp.get()) != nullptr) { readbuf = chartrimcarriage(rbuf); if (m_mame_rev.empty() && readbuf.compare(0, t_mame, TAG_MAMEINFO_R) == 0) @@ -378,7 +393,7 @@ int datfile_manager::index_mame_mess_info(dataindex &index, drvindex &index_drv, else if (readbuf.compare(0, t_info, TAG_INFO) == 0) { // TAG_INFO - fgets(rbuf, 64 * 1024, fp); + std::fgets(rbuf, 64 * 1024, fp.get()); xid = chartrimcarriage(rbuf); name = readbuf.substr(t_info + 1); if (xid == TAG_MAME) @@ -386,11 +401,11 @@ int datfile_manager::index_mame_mess_info(dataindex &index, drvindex &index_drv, // validate driver auto game_index = driver_list::find(name.c_str()); if (game_index != -1) - index.emplace(&driver_list::driver(game_index), ftell(fp)); + index.emplace(&driver_list::driver(game_index), std::ftell(fp.get())); } else if (xid == TAG_DRIVER) { - index_drv.emplace(name, ftell(fp)); + index_drv.emplace(name, std::ftell(fp.get())); drvcount++; } } @@ -402,14 +417,14 @@ int datfile_manager::index_mame_mess_info(dataindex &index, drvindex &index_drv, // load a game name and offset into an // indexed array //------------------------------------------------- -int datfile_manager::index_datafile(dataindex &index, int &swcount, std::string &rev, std::string const &tag, char sep) +int datfile_manager::index_datafile(fileptr &&fp, dataindex &index, int &swcount, std::string &rev, std::string const &tag, char sep) { std::string readbuf; auto const tag_size = tag.size(); auto const t_info = TAG_INFO.size(); auto const t_bio = TAG_BIO.size(); char rbuf[64 * 1024]; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, fp.get()) != nullptr) { readbuf = chartrimcarriage(rbuf); @@ -433,13 +448,13 @@ int datfile_manager::index_datafile(dataindex &index, int &swcount, std::string { auto game_index = driver_list::find(e.c_str()); if (game_index != -1) - index.emplace(&driver_list::driver(game_index), ftell(fp)); + index.emplace(&driver_list::driver(game_index), std::ftell(fp.get())); } } else if (!readbuf.empty() && readbuf[0] == DATAFILE_TAG[0]) { // search for software info - fgets(rbuf, 64 * 1024, fp); + std::fgets(rbuf, 64 * 1024, fp.get()); std::string readbuf_2(chartrimcarriage(rbuf)); if (readbuf_2.compare(0, t_bio, TAG_BIO) == 0) { @@ -450,7 +465,7 @@ int datfile_manager::index_datafile(dataindex &index, int &swcount, std::string std::vector<std::string> token_roms = tokenize(s_roms, ','); for (auto & li : token_list) for (auto & ro : token_roms) - m_swindex[li].emplace(ro, ftell(fp)); + m_swindex[li].emplace(ro, std::ftell(fp.get())); swcount++; } } @@ -461,25 +476,25 @@ int datfile_manager::index_datafile(dataindex &index, int &swcount, std::string //--------------------------------------------------------- // parseopen - Open up file for reading //--------------------------------------------------------- -bool datfile_manager::parseopen(const char *filename) +datfile_manager::fileptr datfile_manager::parseopen(const char *filename) { emu_file file(m_options.history_path(), OPEN_FLAG_READ); if (file.open(filename) != osd_file::error::NONE) - return false; + return fileptr(nullptr, &std::fclose); - m_fullpath = file.fullpath(); + std::string const fullpath = file.fullpath(); file.close(); - fp = fopen(m_fullpath.c_str(), "rb"); + fileptr result(std::fopen(fullpath.c_str(), "rb"), &std::fclose); - fgetc(fp); - fseek(fp, 0, SEEK_SET); - return true; + fgetc(result.get()); + fseek(result.get(), 0, SEEK_SET); + return result; } //------------------------------------------------- // create the menu index //------------------------------------------------- -void datfile_manager::index_menuidx(const game_driver *drv, dataindex const &idx, drvindex &index) +void datfile_manager::index_menuidx(fileptr &&fp, const game_driver *drv, dataindex const &idx, drvindex &index) { auto itemsiter = idx.find(drv); if (itemsiter == idx.end()) @@ -495,11 +510,11 @@ void datfile_manager::index_menuidx(const game_driver *drv, dataindex const &idx // seek to correct point in datafile auto const s_offset = itemsiter->second; - fseek(fp, s_offset, SEEK_SET); + std::fseek(fp.get(), s_offset, SEEK_SET); auto const tinfo = TAG_INFO.size(); char rbuf[64 * 1024]; std::string readbuf; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, fp.get()) != nullptr) { readbuf = chartrimcarriage(rbuf); @@ -509,9 +524,9 @@ void datfile_manager::index_menuidx(const game_driver *drv, dataindex const &idx // TAG_COMMAND identifies the driver if (readbuf == TAG_COMMAND) { - fgets(rbuf, 64 * 1024, fp); + std::fgets(rbuf, 64 * 1024, fp.get()); chartrimcarriage(rbuf); - index.emplace(rbuf, ftell(fp)); + index.emplace(rbuf, std::ftell(fp.get())); } } } @@ -519,16 +534,17 @@ void datfile_manager::index_menuidx(const game_driver *drv, dataindex const &idx //------------------------------------------------- // load command text into the buffer //------------------------------------------------- -void datfile_manager::load_command_info(std::string &buffer, std::string &sel) +void datfile_manager::load_command_info(std::string &buffer, std::string const &sel) { - if (parseopen("command.dat")) + fileptr const datfile = parseopen("command.dat"); + if (datfile) { // open and seek to correct point in datafile - auto offset = m_menuidx.at(sel); - fseek(fp, offset, SEEK_SET); + auto const offset = m_menuidx.at(sel); + std::fseek(datfile.get(), offset, SEEK_SET); char rbuf[64 * 1024]; std::string readbuf; - while (fgets(rbuf, 64 * 1024, fp) != nullptr) + while (std::fgets(rbuf, 64 * 1024, datfile.get()) != nullptr) { readbuf = chartrimcarriage(rbuf); @@ -543,7 +559,6 @@ void datfile_manager::load_command_info(std::string &buffer, std::string &sel) // add this string to the buffer buffer.append(readbuf).append("\n");; } - parseclose(); } } @@ -552,14 +567,14 @@ void datfile_manager::load_command_info(std::string &buffer, std::string &sel) //------------------------------------------------- void datfile_manager::command_sub_menu(const game_driver *drv, std::vector<std::string> &menuitems) { - if (parseopen("command.dat")) + fileptr datfile = parseopen("command.dat"); + if (datfile) { m_menuidx.clear(); - index_menuidx(drv, m_cmdidx, m_menuidx); + index_menuidx(std::move(datfile), drv, m_cmdidx, m_menuidx); menuitems.reserve(m_menuidx.size()); - for (auto & elem : m_menuidx) + for (auto const &elem : m_menuidx) menuitems.push_back(elem.first); - parseclose(); } } |