summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame
diff options
context:
space:
mode:
author Vas Crabb <cuavas@users.noreply.github.com>2021-10-05 03:34:45 +1100
committer GitHub <noreply@github.com>2021-10-05 03:34:45 +1100
commitaeb9eae87469e67bc6a91caf3840c34c11d959fc (patch)
tree45bffedf374dbce47caca633ad7bda547592e6c9 /src/frontend/mame
parent33723892a3f06e678d817b36aef84364c32848ec (diff)
util: Further API cleanups: (#8661)
* Turned `core_file` into an implementation of `random_read_write`. * Turned PNG errors into a standard error category. * Added a helper for generating what look like derived classes on-the-fly.
Diffstat (limited to 'src/frontend/mame')
-rw-r--r--src/frontend/mame/media_ident.cpp48
-rw-r--r--src/frontend/mame/ui/icorender.cpp44
2 files changed, 54 insertions, 38 deletions
diff --git a/src/frontend/mame/media_ident.cpp b/src/frontend/mame/media_ident.cpp
index fbc160f3bcf..b1a18ed7789 100644
--- a/src/frontend/mame/media_ident.cpp
+++ b/src/frontend/mame/media_ident.cpp
@@ -238,8 +238,7 @@ void media_identifier::digest_file(std::vector<file_info> &info, char const *pat
if (!util::core_file::open(path, OPEN_FLAG_READ, file))
{
jed_data jed;
- auto ptr = util::core_file_read(std::move(file));
- if (ptr && JEDERR_NONE == jed_parse(*ptr, &jed))
+ if (JEDERR_NONE == jed_parse(*file, &jed))
{
try
{
@@ -261,30 +260,35 @@ void media_identifier::digest_file(std::vector<file_info> &info, char const *pat
// load the file and process if it opens and has a valid length
util::core_file::ptr file;
- if (!util::core_file::open(path, OPEN_FLAG_READ, file) && file)
+ if (util::core_file::open(path, OPEN_FLAG_READ, file) || !file)
{
- util::hash_collection hashes;
- hashes.begin(util::hash_collection::HASH_TYPES_CRC_SHA1);
- std::uint8_t buf[1024];
- for (std::uint64_t remaining = file->size(); remaining; )
- {
- std::uint32_t const block = std::min<std::uint64_t>(remaining, sizeof(buf));
- if (file->read(buf, block) < block)
- {
- osd_printf_error("%s: error reading file\n", path);
- return;
- }
- remaining -= block;
- hashes.buffer(buf, block);
- }
- hashes.end();
- info.emplace_back(path, file->size(), std::move(hashes), file_flavour::RAW);
- m_total++;
+ osd_printf_error("%s: error opening file\n", path);
+ return;
}
- else
+ std::uint64_t length;
+ if (file->length(length))
{
- osd_printf_error("%s: error opening file\n", path);
+ osd_printf_error("%s: error getting file length\n", path);
+ return;
}
+ util::hash_collection hashes;
+ hashes.begin(util::hash_collection::HASH_TYPES_CRC_SHA1);
+ std::uint8_t buf[1024];
+ for (std::uint64_t remaining = length; remaining; )
+ {
+ std::size_t const block = std::min<std::uint64_t>(remaining, sizeof(buf));
+ std::size_t actual;
+ if (file->read(buf, block, actual) || !actual)
+ {
+ osd_printf_error("%s: error reading file\n", path);
+ return;
+ }
+ remaining -= actual;
+ hashes.buffer(buf, actual);
+ }
+ hashes.end();
+ info.emplace_back(path, length, std::move(hashes), file_flavour::RAW);
+ m_total++;
}
}
diff --git a/src/frontend/mame/ui/icorender.cpp b/src/frontend/mame/ui/icorender.cpp
index 5bda0836838..363e64bad8b 100644
--- a/src/frontend/mame/ui/icorender.cpp
+++ b/src/frontend/mame/ui/icorender.cpp
@@ -80,10 +80,9 @@ bool load_ico_png(util::core_file &fp, icon_dir_entry_t const &dir, bitmap_argb3
if (9U >= dir.size)
return false;
fp.seek(dir.offset, SEEK_SET);
- util::png_error const err(util::png_read_bitmap(fp, bitmap));
- switch (err)
+ std::error_condition const err(util::png_read_bitmap(fp, bitmap));
+ if (!err)
{
- case util::png_error::NONE:
// found valid PNG image
assert(bitmap.valid());
if ((dir.get_width() == bitmap.width()) && ((dir.get_height() == bitmap.height())))
@@ -100,16 +99,20 @@ bool load_ico_png(util::core_file &fp, icon_dir_entry_t const &dir, bitmap_argb3
dir.get_height());
}
return true;
-
- case util::png_error::BAD_SIGNATURE:
+ }
+ else if (util::png_error::BAD_SIGNATURE == err)
+ {
// doesn't look like PNG data - just fall back to DIB without the file header
return false;
-
- default:
+ }
+ else
+ {
// invalid PNG data or I/O error
LOG(
- "Error %u reading PNG image data from ICO file at offset %u (directory size %u)\n",
- unsigned(err),
+ "Error %s:%d %s reading PNG image data from ICO file at offset %u (directory size %u)\n",
+ err.category().name(),
+ err.value(),
+ err.message(),
dir.offset,
dir.size);
return false;
@@ -175,9 +178,13 @@ bool load_ico_image(util::core_file &fp, unsigned index, icon_dir_entry_t const
bool load_ico_image(util::core_file &fp, unsigned count, unsigned index, bitmap_argb32 &bitmap)
{
// read the directory entry
+ std::error_condition err;
+ size_t actual;
icon_dir_entry_t dir;
- fp.seek(sizeof(icon_dir_t) + (sizeof(icon_dir_entry_t) * index), SEEK_SET);
- if (fp.read(&dir, sizeof(dir)) != sizeof(dir))
+ err = fp.seek(sizeof(icon_dir_t) + (sizeof(icon_dir_entry_t) * index), SEEK_SET);
+ if (!err)
+ err = fp.read(&dir, sizeof(dir), actual);
+ if (err || (sizeof(dir) != actual))
{
LOG("Failed to read ICO file directory entry %u\n", index);
return false;
@@ -204,9 +211,13 @@ bool load_ico_image(util::core_file &fp, unsigned count, unsigned index, bitmap_
int images_in_ico(util::core_file &fp)
{
// read and check the icon file header
+ std::error_condition err;
+ size_t actual;
icon_dir_t header;
- fp.seek(0, SEEK_SET);
- if (fp.read(&header, sizeof(header)) != sizeof(header))
+ err = fp.seek(0, SEEK_SET);
+ if (!err)
+ err = fp.read(&header, sizeof(header), actual);
+ if (err || (sizeof(header) != actual))
{
LOG("Failed to read ICO file header\n");
return -1;
@@ -272,9 +283,10 @@ void render_load_ico_highest_detail(util::core_file &fp, bitmap_argb32 &bitmap)
{
// now load all the directory entries
size_t const dir_bytes(sizeof(icon_dir_entry_t) * count);
- std::unique_ptr<icon_dir_entry_t []> dir(new icon_dir_entry_t [count]);
- std::unique_ptr<unsigned []> index(new unsigned [count]);
- if (count && (fp.read(dir.get(), dir_bytes) != dir_bytes))
+ std::unique_ptr<icon_dir_entry_t []> dir(new (std::nothrow) icon_dir_entry_t [count]);
+ std::unique_ptr<unsigned []> index(new (std::nothrow) unsigned [count]);
+ size_t actual;
+ if (count && (!dir || !index || fp.read(dir.get(), dir_bytes, actual) || (dir_bytes != actual)))
{
LOG("Failed to read ICO file directory entries\n");
}