summaryrefslogtreecommitdiffstats
path: root/src/lib/formats/flopimg.cpp
diff options
context:
space:
mode:
author Vas Crabb <cuavas@users.noreply.github.com>2021-08-22 09:06:15 +1000
committer GitHub <noreply@github.com>2021-08-22 09:06:15 +1000
commite8bbea1fc6e94e14768509d322f6c624403ffb36 (patch)
tree74dd1606a900d83de8aecff17a6737af4113308d /src/lib/formats/flopimg.cpp
parente319bde5fc3696d7f48f62b15b6366c4377fe5d1 (diff)
formats, osd, util: Started refactoring file I/O stuff. (#8456)
Added more modern generic I/O interfaces with implementation backed by stdio, osd_file and core_file, replacing io_generic. Also replaced core_file's build-in zlib compression with a filter. unzip.cpp, un7z.cpp: Added option to supply abstract I/O interface rather than filename. Converted osd_file, core_file, archive_file, chd_file and device_image_interface to use std::error_condition rather than their own error enums. Allow mounting TI-99 RPK from inside archives.
Diffstat (limited to 'src/lib/formats/flopimg.cpp')
-rw-r--r--src/lib/formats/flopimg.cpp77
1 files changed, 45 insertions, 32 deletions
diff --git a/src/lib/formats/flopimg.cpp b/src/lib/formats/flopimg.cpp
index 48c2fa1b610..55d00d1bb83 100644
--- a/src/lib/formats/flopimg.cpp
+++ b/src/lib/formats/flopimg.cpp
@@ -11,9 +11,10 @@
#include "flopimg.h"
#include "imageutl.h"
-#include "osdcore.h"
#include "ioprocs.h"
+#include "osdcore.h"
+
#include <cassert>
#include <cctype>
#include <climits>
@@ -30,7 +31,7 @@ using util::BIT;
struct floppy_image_legacy
{
- struct io_generic io = { 0 };
+ util::random_read_write::ptr io = nullptr;
const struct FloppyFormat *floppy_option = nullptr;
struct FloppyCallbacks format = { 0 };
@@ -69,22 +70,20 @@ OPTION_GUIDE_START(floppy_option_guide)
OPTION_GUIDE_END
-static void floppy_close_internal(floppy_image_legacy *floppy, bool close_file);
+static void floppy_close_internal(floppy_image_legacy *floppy);
/*********************************************************************
opening, closing and creating of floppy images
*********************************************************************/
/* basic floppy_image_legacy initialization common to floppy_open() and floppy_create() */
-static floppy_image_legacy *floppy_init(void *fp, const struct io_procs *procs, int flags)
+static floppy_image_legacy *floppy_init(util::random_read_write::ptr &&io, int flags)
{
floppy_image_legacy *floppy;
floppy = new floppy_image_legacy;
- floppy->io.file = fp;
- floppy->io.procs = procs;
- floppy->io.filler = 0xFF;
+ floppy->io = std::move(io);
floppy->flags = (uint8_t) flags;
return floppy;
}
@@ -93,7 +92,7 @@ static floppy_image_legacy *floppy_init(void *fp, const struct io_procs *procs,
/* main code for identifying and maybe opening a disk image; not exposed
* directly because this function is big and hideous */
-static floperr_t floppy_open_internal(void *fp, const struct io_procs *procs, const std::string &extension,
+static floperr_t floppy_open_internal(util::random_read_write::ptr &&io, const std::string &extension,
const struct FloppyFormat *floppy_options, int max_options, int flags, floppy_image_legacy **outfloppy,
int *outoption)
{
@@ -104,7 +103,7 @@ static floperr_t floppy_open_internal(void *fp, const struct io_procs *procs, co
int vote;
size_t i;
- floppy = floppy_init(fp, procs, flags);
+ floppy = floppy_init(std::move(io), flags);
if (!floppy)
{
err = FLOPPY_ERROR_OUTOFMEMORY;
@@ -163,7 +162,7 @@ done:
/* if we have a floppy disk and we either errored or are not keeping it, close it */
if (floppy && (!outfloppy || err))
{
- floppy_close_internal(floppy, false);
+ floppy_close_internal(floppy);
floppy = nullptr;
}
@@ -176,31 +175,31 @@ done:
-floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension,
+floperr_t floppy_identify(util::random_read_write::ptr &&io, const char *extension,
const struct FloppyFormat *formats, int *identified_format)
{
- return floppy_open_internal(fp, procs, extension, formats, INT_MAX, FLOPPY_FLAGS_READONLY, nullptr, identified_format);
+ return floppy_open_internal(std::move(io), extension, formats, INT_MAX, FLOPPY_FLAGS_READONLY, nullptr, identified_format);
}
-floperr_t floppy_open(void *fp, const struct io_procs *procs, const std::string &extension,
+floperr_t floppy_open(util::random_read_write::ptr &&io, const std::string &extension,
const struct FloppyFormat *format, int flags, floppy_image_legacy **outfloppy)
{
- return floppy_open_internal(fp, procs, extension, format, 1, flags, outfloppy, nullptr);
+ return floppy_open_internal(std::move(io), extension, format, 1, flags, outfloppy, nullptr);
}
-floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const std::string &extension,
+floperr_t floppy_open_choices(util::random_read_write::ptr &&io, const std::string &extension,
const struct FloppyFormat *formats, int flags, floppy_image_legacy **outfloppy)
{
- return floppy_open_internal(fp, procs, extension, formats, INT_MAX, flags, outfloppy, nullptr);
+ return floppy_open_internal(std::move(io), extension, formats, INT_MAX, flags, outfloppy, nullptr);
}
-floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, util::option_resolution *parameters, floppy_image_legacy **outfloppy)
+floperr_t floppy_create(util::random_read_write::ptr &&io, const struct FloppyFormat *format, util::option_resolution *parameters, floppy_image_legacy **outfloppy)
{
floppy_image_legacy *floppy = nullptr;
floperr_t err;
@@ -210,7 +209,7 @@ floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct Flo
assert(format);
/* create the new image */
- floppy = floppy_init(fp, procs, 0);
+ floppy = floppy_init(std::move(io), 0);
if (!floppy)
{
err = FLOPPY_ERROR_OUTOFMEMORY;
@@ -266,28 +265,26 @@ floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct Flo
done:
if (err && floppy)
{
- floppy_close_internal(floppy, false);
+ floppy_close_internal(floppy);
floppy = nullptr;
}
if (outfloppy)
*outfloppy = floppy;
else if (floppy)
- floppy_close_internal(floppy, false);
+ floppy_close_internal(floppy);
return err;
}
-static void floppy_close_internal(floppy_image_legacy *floppy, bool close_file)
+static void floppy_close_internal(floppy_image_legacy *floppy)
{
if (floppy) {
floppy_track_unload(floppy);
if(floppy->floppy_option && floppy->floppy_option->destruct)
floppy->floppy_option->destruct(floppy, floppy->floppy_option);
- if (close_file)
- io_generic_close(&floppy->io);
delete floppy;
}
@@ -297,7 +294,7 @@ static void floppy_close_internal(floppy_image_legacy *floppy, bool close_file)
void floppy_close(floppy_image_legacy *floppy)
{
- floppy_close_internal(floppy, true);
+ floppy_close_internal(floppy);
}
@@ -332,14 +329,16 @@ void *floppy_create_tag(floppy_image_legacy *floppy, size_t tagsize)
uint8_t floppy_get_filler(floppy_image_legacy *floppy)
{
- return floppy->io.filler;
+ // FIXME: remove this function, it's here for legacy reasons
+ // the caller actually sets the filler byte - in practice it's always 0xff but there's no actual guarantee
+ return 0xff;
}
-void floppy_set_filler(floppy_image_legacy *floppy, uint8_t filler)
+util::random_read_write &floppy_get_io(floppy_image_legacy *floppy)
{
- floppy->io.filler = filler;
+ return *floppy->io;
}
@@ -350,28 +349,42 @@ void floppy_set_filler(floppy_image_legacy *floppy, uint8_t filler)
void floppy_image_read(floppy_image_legacy *floppy, void *buffer, uint64_t offset, size_t length)
{
- io_generic_read(&floppy->io, buffer, offset, length);
+ size_t actual;
+ floppy->io->read_at(offset, buffer, length, actual);
}
void floppy_image_write(floppy_image_legacy *floppy, const void *buffer, uint64_t offset, size_t length)
{
- io_generic_write(&floppy->io, buffer, offset, length);
+ size_t actual;
+ floppy->io->write_at(offset, buffer, length, actual);
}
void floppy_image_write_filler(floppy_image_legacy *floppy, uint8_t filler, uint64_t offset, size_t length)
{
- io_generic_write_filler(&floppy->io, filler, offset, length);
+ uint8_t buffer[512];
+ memset(buffer, filler, std::min(sizeof(buffer), length));
+
+ while (length)
+ {
+ size_t const block = std::min(sizeof(buffer), length);
+ size_t actual;
+ floppy->io->write_at(offset, buffer, block, actual);
+ offset += block;
+ length -= block;
+ }
}
uint64_t floppy_image_size(floppy_image_legacy *floppy)
{
- return io_generic_size(&floppy->io);
+ uint64_t result;
+ floppy->io->length(result);
+ return result;
}
@@ -976,7 +989,7 @@ bool floppy_image_format_t::has_variant(const std::vector<uint32_t> &variants, u
return false;
}
-bool floppy_image_format_t::save(io_generic *, const std::vector<uint32_t> &, floppy_image *)
+bool floppy_image_format_t::save(util::random_read_write &io, const std::vector<uint32_t> &, floppy_image *)
{
return false;
}