diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/emu/fileio.cpp | 143 | ||||
-rw-r--r-- | src/emu/fileio.h | 35 | ||||
-rw-r--r-- | src/frontend/mame/cheat.cpp | 4 |
3 files changed, 110 insertions, 72 deletions
diff --git a/src/emu/fileio.cpp b/src/emu/fileio.cpp index 12643534c74..4f4578856a1 100644 --- a/src/emu/fileio.cpp +++ b/src/emu/fileio.cpp @@ -22,75 +22,106 @@ const UINT32 OPEN_FLAG_HAS_CRC = 0x10000; //************************************************************************** //------------------------------------------------- -// path_iterator - constructor +// path_iterator - constructors //------------------------------------------------- -path_iterator::path_iterator(const char *rawsearchpath) - : m_base(rawsearchpath), - m_current(m_base), - m_index(0) +path_iterator::path_iterator(std::string &&searchpath) + : m_searchpath(std::move(searchpath)) + , m_current(m_searchpath.cbegin()) + , m_is_first(true) { } +path_iterator::path_iterator(std::string const &searchpath) + : m_searchpath(searchpath) + , m_current(m_searchpath.cbegin()) + , m_is_first(true) +{ +} + +path_iterator::path_iterator(path_iterator &&that) +{ + operator=(std::move(that)); +} + +path_iterator::path_iterator(path_iterator const &that) + : m_searchpath(that.m_searchpath) + , m_current(std::next(m_searchpath.cbegin(), std::distance(that.m_searchpath.cbegin(), that.m_current))) + , m_is_first(that.m_is_first) +{ +} + + +//------------------------------------------------- +// path_iterator - assignement operators +//------------------------------------------------- + +path_iterator &path_iterator::operator=(path_iterator &&that) +{ + auto const current(std::distance(that.m_searchpath.cbegin(), that.m_current)); + m_searchpath = std::move(that.m_searchpath); + m_current = std::next(m_searchpath.cbegin(), current); + m_is_first = that.m_is_first; + return *this; +} + +path_iterator &path_iterator::operator=(path_iterator const &that) +{ + m_searchpath = that.m_searchpath; + m_current = std::next(m_searchpath.cbegin(), std::distance(that.m_searchpath.cbegin(), that.m_current)); + m_is_first = that.m_is_first; + return *this; +} + //------------------------------------------------- -// path_iterator_get_next - get the next entry -// in a multipath sequence +// path_iterator::next - get the next entry in a +// multipath sequence //------------------------------------------------- bool path_iterator::next(std::string &buffer, const char *name) { // if none left, return FALSE to indicate we are done - if (m_index != 0 && *m_current == 0) + if (!m_is_first && (m_searchpath.cend() == m_current)) return false; - // copy up to the next semicolon - const char *semi = strchr(m_current, ';'); - if (semi == nullptr) - semi = m_current + strlen(m_current); - buffer.assign(m_current, semi - m_current); - m_current = (*semi == 0) ? semi : semi + 1; + // copy up to the next separator + auto const sep(std::find(m_current, m_searchpath.cend(), ';')); // FIXME this should be a macro - UNIX prefers : + buffer.assign(m_current, sep); + m_current = sep; + if (m_searchpath.cend() != m_current) + ++m_current; // append the name if we have one - if (name != nullptr) + if (name) { // compute the full pathname - if (buffer.length() > 0) + if (!buffer.empty() && (*buffer.rbegin() != *PATH_SEPARATOR)) buffer.append(PATH_SEPARATOR); buffer.append(name); } // bump the index and return TRUE - m_index++; + m_is_first = false; return true; } - -//************************************************************************** -// FILE ENUMERATOR -//************************************************************************** - //------------------------------------------------- -// file_enumerator - constructor +// path_iteratr::reset - let's go again //------------------------------------------------- -file_enumerator::file_enumerator(const char *searchpath) - : m_iterator(searchpath), - m_curdir(nullptr)/*, - m_buflen(0)*/ +void path_iterator::reset() { + m_current = m_searchpath.cbegin(); + m_is_first = true; } -//------------------------------------------------- -// ~file_enumerator - destructor -//------------------------------------------------- - -file_enumerator::~file_enumerator() -{ -} +//************************************************************************** +// FILE ENUMERATOR +//************************************************************************** //------------------------------------------------- // next - return information about the next file @@ -100,7 +131,7 @@ file_enumerator::~file_enumerator() const osd::directory::entry *file_enumerator::next() { // loop over potentially empty directories - while (1) + while (true) { // if no open directory, get the next path while (!m_curdir) @@ -114,8 +145,8 @@ const osd::directory::entry *file_enumerator::next() } // get the next entry from the current directory - const osd::directory::entry *result = m_curdir->read(); - if (result != nullptr) + const osd::directory::entry *const result = m_curdir->read(); + if (result) return result; // we're done; close this directory @@ -134,31 +165,31 @@ const osd::directory::entry *file_enumerator::next() //------------------------------------------------- emu_file::emu_file(UINT32 openflags) - : m_file(nullptr), - m_iterator(""), - m_mediapaths(""), - m_crc(0), - m_openflags(openflags), - m_zipfile(nullptr), - m_ziplength(0), - m_remove_on_close(false), - m_restrict_to_mediapath(false) + : m_file() + , m_iterator(std::string()) + , m_mediapaths(std::string()) + , m_crc(0) + , m_openflags(openflags) + , m_zipfile(nullptr) + , m_ziplength(0) + , m_remove_on_close(false) + , m_restrict_to_mediapath(false) { // sanity check the open flags if ((m_openflags & OPEN_FLAG_HAS_CRC) && (m_openflags & OPEN_FLAG_WRITE)) throw emu_fatalerror("Attempted to open a file for write with OPEN_FLAG_HAS_CRC"); } -emu_file::emu_file(const char *searchpath, UINT32 openflags) - : m_file(nullptr), - m_iterator(searchpath), - m_mediapaths(searchpath), - m_crc(0), - m_openflags(openflags), - m_zipfile(nullptr), - m_ziplength(0), - m_remove_on_close(false), - m_restrict_to_mediapath(false) +emu_file::emu_file(std::string &&searchpath, UINT32 openflags) + : m_file() + , m_iterator(searchpath) + , m_mediapaths(std::move(searchpath)) + , m_crc(0) + , m_openflags(openflags) + , m_zipfile(nullptr) + , m_ziplength(0) + , m_remove_on_close(false) + , m_restrict_to_mediapath(false) { // sanity check the open flags if ((m_openflags & OPEN_FLAG_HAS_CRC) && (m_openflags & OPEN_FLAG_WRITE)) diff --git a/src/emu/fileio.h b/src/emu/fileio.h index 69479bc5a98..218821f7aa5 100644 --- a/src/emu/fileio.h +++ b/src/emu/fileio.h @@ -34,20 +34,25 @@ namespace util { class archive_file; } class path_iterator { public: - // construction/destruction - path_iterator(const char *searchpath); + // constructors + path_iterator(std::string &&searchpath); + path_iterator(std::string const &searchpath); + path_iterator(path_iterator &&that); + path_iterator(path_iterator const &that); - // getters - bool next(std::string &buffer, const char *name = nullptr); + // assignment operators + path_iterator &operator=(path_iterator &&that); + path_iterator &operator=(path_iterator const &that); - // reset - void reset() { m_current = m_base; m_index = 0; } + // main interface + bool next(std::string &buffer, const char *name = nullptr); + void reset(); private: // internal state - const char * m_base; - const char * m_current; - int m_index; + std::string m_searchpath; + std::string::const_iterator m_current; + bool m_is_first; }; @@ -59,8 +64,11 @@ class file_enumerator { public: // construction/destruction - file_enumerator(const char *searchpath); - ~file_enumerator(); + template <typename... T> file_enumerator(T &&... args) : m_iterator(std::forward<T>(args)...) { } + file_enumerator(file_enumerator &&) = default; + file_enumerator(file_enumerator const &) = delete; + file_enumerator &operator=(file_enumerator &&) = default; + file_enumerator &operator=(file_enumerator const &) = delete; // iterator const osd::directory::entry *next(); @@ -70,7 +78,6 @@ private: path_iterator m_iterator; osd::directory::ptr m_curdir; std::string m_pathbuffer; - //int m_buflen; }; @@ -82,7 +89,7 @@ class emu_file public: // file open/creation emu_file(UINT32 openflags); - emu_file(const char *searchpath, UINT32 openflags); + emu_file(std::string &&searchpath, UINT32 openflags); virtual ~emu_file(); // getters @@ -155,7 +162,7 @@ private: path_iterator m_mediapaths; // media-path iterator UINT32 m_crc; // file's CRC UINT32 m_openflags; // flags we used for the open - util::hash_collection m_hashes; // collection of hashes + util::hash_collection m_hashes; // collection of hashes std::unique_ptr<util::archive_file> m_zipfile; // ZIP file pointer dynamic_buffer m_zipdata; // ZIP file data diff --git a/src/frontend/mame/cheat.cpp b/src/frontend/mame/cheat.cpp index 13c90f7a283..3812666fe83 100644 --- a/src/frontend/mame/cheat.cpp +++ b/src/frontend/mame/cheat.cpp @@ -1381,13 +1381,13 @@ void cheat_manager::load_cheats(const char *filename) { xml_data_node *rootnode = nullptr; std::string searchstr(machine().options().cheat_path()); - path_iterator path(searchstr.c_str()); + path_iterator path(searchstr); std::string curpath; while (path.next(curpath)) { searchstr.append(";").append(curpath).append(PATH_SEPARATOR).append("cheat"); } - emu_file cheatfile(searchstr.c_str(), OPEN_FLAG_READ); + emu_file cheatfile(std::move(searchstr), OPEN_FLAG_READ); try { // open the file with the proper name |