[AD] file slices

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


This has been brought up a couple of times... same general concept
here: allow a user to open up a subset of an already opened file. Its
main use cases are:

* loading a resource from within a custom data file of a known size

* converting a sequential access file into a random access file

It's not much different from the core memfile function, but it does
let you work with a subset of a file without having to create a memory
buffer (first use case), and in some cases, it feels more natural than
manually creating a memfile.

I've implemented the following as a file interface:

ALLEGRO_FILE *al_fslice(ALLEGRO_FILE *fp, size_t initial_size, const
char *mode);
size_t al_get_fslice_size(ALLEGRO_FILE *fp);
uint8_t *al_get_fslice_buffer(ALLEGRO_FILE *fp);

I'm looking for feedback regarding the API and the general usefulness
given its various restrictions.

al_fslice() has the following modes that can be combined:

r: read
w: write
a: append (implies write)
m: memory buffer

If buffered, then a buffer of initial_size is created (and owned by
Allegro) and that amount of bytes are read from the file into the
buffer. All read/write operations happen inside the memory buffer,
which is expanded on write operations as needed. When flushed or
closed, the data is written to the parent file.

If not buffered, it's a wrapper over the underlying file, but the seek
operations are relative and restricted to the inital size. All write
operations are passed directly to the underlying system.

When a slice is closed, the parent file handle's position is set to
+initial_size.

The various limitations, which shouldn't be surprising:

* only one slice can be open per parent file at a time
* the parent file must be kept open while the slice is in use
* the parent file cannot be used while the slice is in use (unless
it's buffered, read-only)
* the slice mode must be sensible given the parent's mode. e.g.,
Opening the child for in-place write, but with a parent that is
append-write will give undefined behavior.
* write operations do not honor the file size limitations. If in-place
writes go outside the initial size, anything past that slice will be
overwritten. Append writes will always be placed at the end of the
physical file. (It might make sense to optionally make in-place writes
be constrained to the initial size.)

It could just be part of the memfile addon, as it provides similar
functionality.

Or another option could be: 1) remove the buffered option from file
slices and make it a core function and 2) add expandable memfiles into
the memfile addon.

--
Matthew Leverton




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/