Re: [AD] memfile

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


On 2010-12-21, Matthew Leverton <meffer@xxxxxxxxxx> wrote:
> On Mon, Dec 20, 2010 at 4:40 PM, Peter Wang <novalazy@xxxxxxxxxx> wrote:
> > On 2010-12-20, Matthew Leverton <meffer@xxxxxxxxxx> wrote:
> >> Is the memfile addon excluded from documentation / installation for
> >> any reason? (Or at least, last I checked, it was not there.)
> >
> > You can if you want.
> >
> Attached is an implementation of ungetc for memfile and an additional
> mode parameter for the al_open_memfile. It also includes
> documentation. (Not included is that the memfile.h header file should
> be renamed to allegro_memfile.h.)
> 
> For fungetc, I used a buffer (hardcoded to 16 bytes). I think it
> basically follows the standard ungetc, except that it allows and
> reports negative positions. (My man pages for ungetc say that behavior
> is undefined... it happens to stop at 0, but allows the following getc
> to work as long as there's no intermediate seeking or telling.)
> 
> Seeking or writing is relative to the "virtual" position, but
> immediately clears the entire ungetc buffer, regardless if the seek or
> write actually extends past the ungetc buffer. Trying to write before
> position 0 causes it to write at 0. I think this is all the same
> behavior as the standard file implementation, with the exception of
> some undefined things regarding negative positions.
> 
> The mode parameter accepts any combination of "r" and "w". If you open
> the file for neither read or write, then you cannot do anything useful
> with it. The file always opens at position 0. Trying to read or write
> without proper permission just returns 0.
> 
> Note that I swapped the order of the first two parameters. All the I/O
> functions go buffer, size... so I think it's more consistent if
> open_memfile is the same way.

Looks good.

> @@ -21,27 +26,59 @@
>  static size_t memfile_fread(ALLEGRO_FILE *fp, void *ptr, size_t size)
>  {
>     ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
> -   size_t n;
> -
> -   if (mf->size - mf->pos < (int64_t)size) { 
> -      /* partial read */
> -      n = mf->size - mf->pos;
> -      mf->eof = true;
> +   size_t bytes_unget = 0;
> +   size_t bytes_read = 0;
> +   
> +   if (!mf->readable) {
> +      return 0;
>     }

Maybe set EPERM here.

>  static size_t memfile_fwrite(ALLEGRO_FILE *fp, const void *ptr, size_t size)
>  {
>     ALLEGRO_FILE_MEMFILE *mf = al_get_file_userdata(fp);
>     size_t n;
> -
> +   
> +   if (!mf->writable) {
> +      return 0;
> +   }

EPERM


Remember to svn add memfile.txt if you didn't.

Peter




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