summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/fileio.cpp
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2016-03-13 13:54:57 +1100
committer Vas Crabb <vas@vastheman.com>2016-03-14 18:55:00 +1100
commit42fbb9c3967fcda37f2d9ae17d5796a79cf027b9 (patch)
treea43047ee00431e5e22bfc5f8f612ff775586e415 /src/emu/fileio.cpp
parent5fc27747031b6ed6f6c0582502098ebfb960be67 (diff)
Make osd_file a polymorphic class that's held with smart pointers
Make avi_file a class that's held with smart pointers, encapsulate various AVI I/O structures Make zip_file and _7z_file classes rather than having free functions everywhere Hide zip/7z class implementation behind an interface, no longer need to call close() to send back to the cache Don't dump as much crap in global namespace Add solaris PTY implementation Improve variable expansion for SDL OSD - supports ~/$FOO/${BAR} syntax Rearrange stuff so the same things are in file module for all OSDs Move file stuff into its own module 7z/zip open and destruct are still not thread-safe due to lack of interlocks around cache access Directory functions still need to be moved to file module SDL OSD may not initialise WinSock on Windows
Diffstat (limited to 'src/emu/fileio.cpp')
-rw-r--r--src/emu/fileio.cpp150
1 files changed, 70 insertions, 80 deletions
diff --git a/src/emu/fileio.cpp b/src/emu/fileio.cpp
index 79bf746fbfa..b8e0c433575 100644
--- a/src/emu/fileio.cpp
+++ b/src/emu/fileio.cpp
@@ -146,7 +146,7 @@ emu_file::emu_file(UINT32 openflags)
m_openflags(openflags),
m_zipfile(nullptr),
m_ziplength(0),
- m__7zfile(nullptr),
+ m__7zfile(),
m__7zlength(0),
m_remove_on_close(false),
m_restrict_to_mediapath(false)
@@ -164,7 +164,7 @@ emu_file::emu_file(const char *searchpath, UINT32 openflags)
m_openflags(openflags),
m_zipfile(nullptr),
m_ziplength(0),
- m__7zfile(nullptr),
+ m__7zfile(),
m__7zlength(0),
m_remove_on_close(false),
m_restrict_to_mediapath(false)
@@ -255,7 +255,7 @@ hash_collection &emu_file::hashes(const char *types)
// open - open a file by searching paths
//-------------------------------------------------
-file_error emu_file::open(const char *name)
+osd_file::error emu_file::open(const char *name)
{
// remember the filename and CRC info
m_filename = name;
@@ -267,28 +267,28 @@ file_error emu_file::open(const char *name)
return open_next();
}
-file_error emu_file::open(const char *name1, const char *name2)
+osd_file::error emu_file::open(const char *name1, const char *name2)
{
// concatenate the strings and do a standard open
std::string name = std::string(name1).append(name2);
return open(name.c_str());
}
-file_error emu_file::open(const char *name1, const char *name2, const char *name3)
+osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3)
{
// concatenate the strings and do a standard open
std::string name = std::string(name1).append(name2).append(name3);
return open(name.c_str());
}
-file_error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4)
+osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4)
{
// concatenate the strings and do a standard open
std::string name = std::string(name1).append(name2).append(name3).append(name4);
return open(name.c_str());
}
-file_error emu_file::open(const char *name, UINT32 crc)
+osd_file::error emu_file::open(const char *name, UINT32 crc)
{
// remember the filename and CRC info
m_filename = name;
@@ -300,21 +300,21 @@ file_error emu_file::open(const char *name, UINT32 crc)
return open_next();
}
-file_error emu_file::open(const char *name1, const char *name2, UINT32 crc)
+osd_file::error emu_file::open(const char *name1, const char *name2, UINT32 crc)
{
// concatenate the strings and do a standard open
std::string name = std::string(name1).append(name2);
return open(name.c_str(), crc);
}
-file_error emu_file::open(const char *name1, const char *name2, const char *name3, UINT32 crc)
+osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3, UINT32 crc)
{
// concatenate the strings and do a standard open
std::string name = std::string(name1).append(name2).append(name3);
return open(name.c_str(), crc);
}
-file_error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4, UINT32 crc)
+osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4, UINT32 crc)
{
// concatenate the strings and do a standard open
std::string name = std::string(name1).append(name2).append(name3).append(name4);
@@ -327,19 +327,19 @@ file_error emu_file::open(const char *name1, const char *name2, const char *name
// the filename by iterating over paths
//-------------------------------------------------
-file_error emu_file::open_next()
+osd_file::error emu_file::open_next()
{
// if we're open from a previous attempt, close up now
if (m_file != nullptr)
close();
// loop over paths
- file_error filerr = FILERR_NOT_FOUND;
+ osd_file::error filerr = osd_file::error::NOT_FOUND;
while (m_iterator.next(m_fullpath, m_filename.c_str()))
{
// attempt to open the file directly
filerr = util::core_file::open(m_fullpath.c_str(), m_openflags, m_file);
- if (filerr == FILERR_NONE)
+ if (filerr == osd_file::error::NONE)
break;
// if we're opening for read-only we have other options
@@ -348,13 +348,13 @@ file_error emu_file::open_next()
std::string tempfullpath = m_fullpath;
filerr = attempt_zipped();
- if (filerr == FILERR_NONE)
+ if (filerr == osd_file::error::NONE)
break;
m_fullpath = tempfullpath;
filerr = attempt__7zped();
- if (filerr == FILERR_NONE)
+ if (filerr == osd_file::error::NONE)
break;
}
}
@@ -367,7 +367,7 @@ file_error emu_file::open_next()
// just an array of data in RAM
//-------------------------------------------------
-file_error emu_file::open_ram(const void *data, UINT32 length)
+osd_file::error emu_file::open_ram(const void *data, UINT32 length)
{
// set a fake filename and CRC
m_filename = "RAM";
@@ -386,21 +386,15 @@ file_error emu_file::open_ram(const void *data, UINT32 length)
void emu_file::close()
{
// close files and free memory
- if (m__7zfile != nullptr)
- _7z_file_close(m__7zfile);
- m__7zfile = nullptr;
-
- if (m_zipfile != nullptr)
- zip_file_close(m_zipfile);
- m_zipfile = nullptr;
-
+ m__7zfile.reset();
+ m_zipfile.reset();
m_file.reset();
m__7zdata.clear();
m_zipdata.clear();
if (m_remove_on_close)
- osd_rmfile(m_fullpath.c_str());
+ osd_file::remove(m_fullpath);
m_remove_on_close = false;
// reset our hashes and path as well
@@ -415,7 +409,7 @@ void emu_file::close()
// compression, or up to 9 for max compression
//-------------------------------------------------
-file_error emu_file::compress(int level)
+osd_file::error emu_file::compress(int level)
{
return m_file->compress(level);
}
@@ -429,10 +423,10 @@ file_error emu_file::compress(int level)
bool emu_file::compressed_file_ready(void)
{
// load the ZIP file now if we haven't yet
- if (m__7zfile != nullptr && load__7zped_file() != FILERR_NONE)
+ if (m__7zfile != nullptr && load__7zped_file() != osd_file::error::NONE)
return true;
- if (m_zipfile != nullptr && load_zipped_file() != FILERR_NONE)
+ if (m_zipfile != nullptr && load_zipped_file() != osd_file::error::NONE)
return true;
return false;
@@ -657,7 +651,7 @@ bool emu_file::part_of_mediapath(std::string path)
// attempt_zipped - attempt to open a ZIPped file
//-------------------------------------------------
-file_error emu_file::attempt_zipped()
+osd_file::error emu_file::attempt_zipped()
{
std::string filename;
@@ -667,11 +661,11 @@ file_error emu_file::attempt_zipped()
// find the final path separator
int dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]);
if (dirsep == -1)
- return FILERR_NOT_FOUND;
+ return osd_file::error::NOT_FOUND;
if (restrict_to_mediapath())
if ( !part_of_mediapath(m_fullpath) )
- return FILERR_NOT_FOUND;
+ return osd_file::error::NOT_FOUND;
// insert the part from the right of the separator into the head of the filename
if (filename.length() > 0)
@@ -682,49 +676,49 @@ file_error emu_file::attempt_zipped()
m_fullpath = m_fullpath.substr(0, dirsep).append(".zip");
// attempt to open the ZIP file
- zip_file *zip;
- zip_error ziperr = zip_file_open(m_fullpath.c_str(), &zip);
+ zip_file::ptr zip;
+ zip_file::error ziperr = zip_file::open(m_fullpath, zip);
// chop the .zip back off the filename before continuing
m_fullpath = m_fullpath.substr(0, dirsep);
// if we failed to open this file, continue scanning
- if (ziperr != ZIPERR_NONE)
+ if (ziperr != zip_file::error::NONE)
continue;
// see if we can find a file with the right name and (if available) crc
- const zip_file_header *header;
- for (header = zip_file_first_file(zip); header != nullptr; header = zip_file_next_file(zip))
+ const zip_file::file_header *header;
+ for (header = zip->first_file(); header != nullptr; header = zip->next_file())
if (zip_filename_match(*header, filename) && (!(m_openflags & OPEN_FLAG_HAS_CRC) || header->crc == m_crc))
break;
// if that failed, look for a file with the right crc, but the wrong filename
if (header == nullptr && (m_openflags & OPEN_FLAG_HAS_CRC))
- for (header = zip_file_first_file(zip); header != nullptr; header = zip_file_next_file(zip))
+ for (header = zip->first_file(); header != nullptr; header = zip->next_file())
if (header->crc == m_crc && !zip_header_is_path(*header))
break;
// if that failed, look for a file with the right name; reporting a bad checksum
// is more helpful and less confusing than reporting "rom not found"
if (header == nullptr)
- for (header = zip_file_first_file(zip); header != nullptr; header = zip_file_next_file(zip))
+ for (header = zip->first_file(); header != nullptr; header = zip->next_file())
if (zip_filename_match(*header, filename))
break;
// if we got it, read the data
if (header != nullptr)
{
- m_zipfile = zip;
+ m_zipfile = std::move(zip);
m_ziplength = header->uncompressed_length;
// build a hash with just the CRC
m_hashes.reset();
m_hashes.add_crc(header->crc);
- return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? FILERR_NONE : load_zipped_file();
+ return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file();
}
// close up the ZIP file and try the next level
- zip_file_close(zip);
+ zip.reset();
}
}
@@ -733,7 +727,7 @@ file_error emu_file::attempt_zipped()
// load_zipped_file - load a ZIPped file
//-------------------------------------------------
-file_error emu_file::load_zipped_file()
+osd_file::error emu_file::load_zipped_file()
{
assert(m_file == nullptr);
assert(m_zipdata.empty());
@@ -743,25 +737,24 @@ file_error emu_file::load_zipped_file()
m_zipdata.resize(m_ziplength);
// read the data into our buffer and return
- zip_error ziperr = zip_file_decompress(m_zipfile, &m_zipdata[0], m_zipdata.size());
- if (ziperr != ZIPERR_NONE)
+ zip_file::error ziperr = m_zipfile->decompress(&m_zipdata[0], m_zipdata.size());
+ if (ziperr != zip_file::error::NONE)
{
m_zipdata.clear();
- return FILERR_FAILURE;
+ return osd_file::error::FAILURE;
}
// convert to RAM file
- file_error filerr = util::core_file::open_ram(&m_zipdata[0], m_zipdata.size(), m_openflags, m_file);
- if (filerr != FILERR_NONE)
+ osd_file::error filerr = util::core_file::open_ram(&m_zipdata[0], m_zipdata.size(), m_openflags, m_file);
+ if (filerr != osd_file::error::NONE)
{
m_zipdata.clear();
- return FILERR_FAILURE;
+ return osd_file::error::FAILURE;
}
// close out the ZIP file
- zip_file_close(m_zipfile);
- m_zipfile = nullptr;
- return FILERR_NONE;
+ m_zipfile.reset();
+ return osd_file::error::NONE;
}
@@ -770,7 +763,7 @@ file_error emu_file::load_zipped_file()
// to expected filename, ignoring any directory
//-------------------------------------------------
-bool emu_file::zip_filename_match(const zip_file_header &header, const std::string &filename)
+bool emu_file::zip_filename_match(const zip_file::file_header &header, const std::string &filename)
{
const char *zipfile = header.filename + header.filename_length - filename.length();
return (zipfile >= header.filename && core_stricmp(filename.c_str(),zipfile) == 0 && (zipfile == header.filename || zipfile[-1] == '/'));
@@ -782,7 +775,7 @@ bool emu_file::zip_filename_match(const zip_file_header &header, const std::stri
// in header is a path
//-------------------------------------------------
-bool emu_file::zip_header_is_path(const zip_file_header &header)
+bool emu_file::zip_header_is_path(const zip_file::file_header &header)
{
const char *zipfile = header.filename + header.filename_length - 1;
return (zipfile >= header.filename && zipfile[0] == '/');
@@ -792,7 +785,7 @@ bool emu_file::zip_header_is_path(const zip_file_header &header)
// attempt__7zped - attempt to open a .7z file
//-------------------------------------------------
-file_error emu_file::attempt__7zped()
+osd_file::error emu_file::attempt__7zped()
{
std::string filename;
@@ -802,11 +795,11 @@ file_error emu_file::attempt__7zped()
// find the final path separator
int dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]);
if (dirsep == -1)
- return FILERR_NOT_FOUND;
+ return osd_file::error::NOT_FOUND;
if (restrict_to_mediapath())
if ( !part_of_mediapath(m_fullpath) )
- return FILERR_NOT_FOUND;
+ return osd_file::error::NOT_FOUND;
// insert the part from the right of the separator into the head of the filename
if (filename.length() > 0)
@@ -817,43 +810,41 @@ file_error emu_file::attempt__7zped()
m_fullpath = m_fullpath.substr(0, dirsep).append(".7z");
// attempt to open the _7Z file
- _7z_file *_7z;
- _7z_error _7zerr = _7z_file_open(m_fullpath.c_str(), &_7z);
+ _7z_file::ptr _7z;
+ _7z_file::error _7zerr = _7z_file::open(m_fullpath, _7z);
// chop the ._7z back off the filename before continuing
m_fullpath = m_fullpath.substr(0, dirsep);
// if we failed to open this file, continue scanning
- if (_7zerr != _7ZERR_NONE)
+ if (_7zerr != _7z_file::error::NONE)
continue;
int fileno = -1;
// see if we can find a file with the right name and (if available) crc
- if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z_search_crc_match(_7z, m_crc, filename.c_str(), filename.length(), true, true);
+ if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z->search(m_crc, filename);
// if that failed, look for a file with the right crc, but the wrong filename
- if (fileno==-1)
- if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z_search_crc_match(_7z, m_crc, filename.c_str(), filename.length(), true, false);
+ if ((fileno == -1) && (m_openflags & OPEN_FLAG_HAS_CRC)) fileno = _7z->search(m_crc);
// if that failed, look for a file with the right name; reporting a bad checksum
// is more helpful and less confusing than reporting "rom not found"
- if (fileno==-1)
- fileno = _7z_search_crc_match(_7z, m_crc, filename.c_str(), filename.length(), false, true);
+ if (fileno == -1) fileno = _7z->search(filename);
if (fileno != -1)
{
- m__7zfile = _7z;
- m__7zlength = _7z->uncompressed_length;
+ m__7zfile = std::move(_7z);
+ m__7zlength = _7z->current_uncompressed_length();
// build a hash with just the CRC
m_hashes.reset();
- m_hashes.add_crc(_7z->crc);
- return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? FILERR_NONE : load__7zped_file();
+ m_hashes.add_crc(_7z->current_crc());
+ return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load__7zped_file();
}
// close up the _7Z file and try the next level
- _7z_file_close(_7z);
+ _7z.reset();
}
}
@@ -862,33 +853,32 @@ file_error emu_file::attempt__7zped()
// load__7zped_file - load a _7Zped file
//-------------------------------------------------
-file_error emu_file::load__7zped_file()
+osd_file::error emu_file::load__7zped_file()
{
assert(m_file == nullptr);
assert(m__7zdata.empty());
- assert(m__7zfile != nullptr);
+ assert(m__7zfile);
// allocate some memory
m__7zdata.resize(m__7zlength);
// read the data into our buffer and return
- _7z_error _7zerr = _7z_file_decompress(m__7zfile, &m__7zdata[0], m__7zdata.size());
- if (_7zerr != _7ZERR_NONE)
+ _7z_file::error _7zerr = m__7zfile->decompress(&m__7zdata[0], m__7zdata.size());
+ if (_7zerr != _7z_file::error::NONE)
{
m__7zdata.clear();
- return FILERR_FAILURE;
+ return osd_file::error::FAILURE;
}
// convert to RAM file
- file_error filerr = util::core_file::open_ram(&m__7zdata[0], m__7zdata.size(), m_openflags, m_file);
- if (filerr != FILERR_NONE)
+ osd_file::error filerr = util::core_file::open_ram(&m__7zdata[0], m__7zdata.size(), m_openflags, m_file);
+ if (filerr != osd_file::error::NONE)
{
m__7zdata.clear();
- return FILERR_FAILURE;
+ return osd_file::error::FAILURE;
}
// close out the _7Z file
- _7z_file_close(m__7zfile);
- m__7zfile = nullptr;
- return FILERR_NONE;
+ m__7zfile.reset();
+ return osd_file::error::NONE;
}