Re: [AD] implementing seek

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


On 2010-04-07, Matthew Leverton <meffer@xxxxxxxxxx> wrote:
> I agree. But what about:
> 
> ALLEGRO_FILE *f = fopen("images.blob", "rb");
> int pos = al_fread32le(f);
> ALLEGRO_BITMAP *bmp1 = al_load_bmp_f(f);
> 
> al_fseek(f, pos, ALLEGRO_SEEK_SET);
> pos += al_fread32le(f);
> ALLEGRO_BITMAP *bmp2 = al_load_bmp_f(f);
> 
> al_fseek(f, pos, ALLEGRO_SEEK_SET);
> pos += al_fread32le(f);
> ALLEGRO_BITMAP *bmp3 = al_load_bmp_f(f);
> 
> where 'pos' is always pointing to the next position because the file
> format stored the length?

Intuitively, I would expect it to work.  But, actually, it should NOT be
guaranteed.  (You might get away with it if you know exactly the loaders
you are dealing with.)

It imposes too many constraints on the loader implementation.
You couldn't use SEEK_END at all, or even SEEK_SET with an absolute
offset (as you mention).

For a robust solution, the user should pass an ALLEGRO_FILE handle that
provides a view of exactly one file.

> So one system-wide solution would be something like al_fopen_f or even
> maybe more simply:
> 
> al_fseek(f, 1000);  // seeks to position 1000
> al_set_fseek_origin(f, al_ftell(f));  // consider the current position
> 1000 to be 0.
> al_fseek(f, 0);  // actually seeks to position 1000.
> 
> ALLEGRO_BITMAP *bmp2 = al_load_bmp_f(f); // guaranteed to work now
> 
> Basically it's just an offset that gets added to the offset value to
> SEEK_SET. Now when loading functions want to seek from the "beginning"
> of the file, the proper location is preserved.

The function you alluded to earlier was cleaner:

    ALLEGRO_FILE *al_fopen_slice(ALLEGRO_FILE *f, size_t length);

perhaps combined with memory buffering as an option, for underlying
streams that don't understand backward seeks.  Maybe also mix in
read/write permissions.

Peter




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