Changeset 382

Show
Ignore:
Timestamp:
2004-02-05 03:14:02 (5 years ago)
Author:
conrad
Message:
  • added OggzIO interface (<oggz/oggz_io.h>, src/liboggz/oggz_io.c)
  • added test cases for IO overriding
  • added OGGZ_CONTINUE, OGGZ_STOP_OK, OGGZ_STOP_ERR constants
  • added test cases for read callback stopping (OK and ERR)
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • liboggz/trunk/include/oggz/Makefile.am

    r346 r382  
    33# Include files to install 
    44includedir = $(prefix)/include/oggz 
    5 include_HEADERS = oggz.h oggz_constants.h oggz_table.h 
     5include_HEADERS = oggz.h oggz_constants.h oggz_io.h oggz_table.h 
    66 
  • liboggz/trunk/include/oggz/oggz.h

    r372 r382  
    10951095long oggz_serialno_new (OGGZ * oggz); 
    10961096 
     1097#include <oggz/oggz_io.h> 
    10971098 
    10981099#endif /* __OGGZ_H__ */ 
  • liboggz/trunk/include/oggz/oggz_constants.h

    r346 r382  
    6262   * once all bos pages have been delivered. */ 
    6363  OGGZ_AUTO         = 0x20 
     64}; 
     65 
     66enum OggzStopCtl { 
     67  /** Continue calling read callbacks */ 
     68  OGGZ_CONTINUE     = 0, 
     69 
     70  /** Stop calling callbacks, but retain buffered packet data */ 
     71  OGGZ_STOP_OK      = 1, 
     72 
     73  /** Stop calling callbacks, and purge buffered packet data */ 
     74  OGGZ_STOP_ERR     = -1 
    6475}; 
    6576 
  • liboggz/trunk/include/oggz/oggz_io.h

    r373 r382  
    6363 * \retval "<  0" An error condition 
    6464 */ 
    65 typedef long (*OggzIORead) (void * user_handle, void * buf, long n); 
     65typedef size_t (*OggzIORead) (void * user_handle, void * buf, size_t n); 
    6666 
    6767/** 
     
    7676 * \retval "<  0" An error condition 
    7777 */ 
    78 typedef long (*OggzIOWrite) (void * user_handle, void * buf, long n); 
     78typedef size_t (*OggzIOWrite) (void * user_handle, void * buf, size_t n); 
    7979 
    8080/** 
     
    8787 * \retval ">= 0" The offset seeked to 
    8888 * \retval "<  0" An error condition 
    89  */ 
    90 typedef off_t (*OggzIOSeek) (void * user_handle, off_t offset, int whence); 
     89 * 
     90 * \note If you provide an OggzIOSeek function, you MUST also provide 
     91 * an OggzIOTell function, or else all your seeks will fail. 
     92 */ 
     93typedef int (*OggzIOSeek) (void * user_handle, long offset, int whence); 
    9194 
    9295/** 
     
    99102 * \retval "<  0" An error condition 
    100103 */ 
    101 typedef off_t (*OggzIOTell) (void * user_handle); 
     104typedef long (*OggzIOTell) (void * user_handle); 
    102105 
    103106/** 
     
    166169 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ 
    167170 * \retval OGGZ_ERR_INVALID Operation not suitable for this OGGZ 
     171 * 
     172 * \note If you provide an OggzIOSeek function, you MUST also provide 
     173 * an OggzIOTell function, or else all your seeks will fail. 
    168174 */ 
    169175int oggz_io_set_seek (OGGZ * oggz, OggzIOSeek seek, void * user_handle); 
  • liboggz/trunk/src/liboggz/Makefile.am

    r355 r382  
    1313        oggz.c \ 
    1414        oggz_private.h oggz_byteorder.h oggz_compat.h oggz_macros.h \ 
     15        oggz_io.c \ 
    1516        oggz_read.c oggz_write.c \ 
    1617        oggz_auto.c oggz_auto.h \ 
  • liboggz/trunk/src/liboggz/oggz.c

    r355 r382  
    8080  oggz->flags = flags; 
    8181  oggz->file = NULL; 
     82  oggz->io = NULL; 
    8283 
    8384  oggz->offset = 0; 
     
    142143  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ; 
    143144 
    144   if (oggz->file == NULL) return OGGZ_ERR_INVALID; 
    145  
    146   if (fflush (oggz->file) == EOF) { 
    147     return OGGZ_ERR_SYSTEM; 
    148   } 
    149  
    150   return 0; 
     145  return oggz_io_flush (oggz); 
    151146} 
    152147 
  • liboggz/trunk/src/liboggz/oggz_private.h

    r372 r382  
    4343 
    4444typedef struct _OGGZ OGGZ; 
     45typedef struct _OggzIO OggzIO; 
    4546typedef struct _OggzReader OggzReader; 
    4647typedef struct _OggzWriter OggzWriter; 
     
    5758 
    5859typedef int (*OggzWriteHungry) (OGGZ * oggz, int empty, void * user_data); 
     60 
     61/* oggz_io */ 
     62typedef size_t (*OggzIORead) (void * user_handle, void * buf, size_t n); 
     63typedef size_t (*OggzIOWrite) (void * user_handle, void * buf, size_t n); 
     64typedef int (*OggzIOSeek) (void * user_handle, long offset, int whence); 
     65typedef long (*OggzIOTell) (void * user_handle); 
     66typedef int (*OggzIOFlush) (void * user_handle); 
    5967 
    6068typedef struct { 
     
    140148}; 
    141149 
     150struct _OggzIO { 
     151  OggzIORead read; 
     152  void * read_user_handle; 
     153 
     154  OggzIOWrite write; 
     155  void * write_user_handle; 
     156 
     157  OggzIOSeek seek; 
     158  void * seek_user_handle; 
     159 
     160  OggzIOTell tell; 
     161  void * tell_user_handle; 
     162 
     163  OggzIOFlush flush; 
     164  void * flush_user_handle; 
     165}; 
     166 
    142167struct _OGGZ { 
    143168  int flags; 
    144169  FILE * file; 
     170  OggzIO * io; 
    145171 
    146172  ogg_packet current_packet; 
     
    185211int oggz_auto (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data); 
    186212 
     213/* oggz_io */ 
     214size_t oggz_io_read (OGGZ * oggz, void * buf, size_t n); 
     215size_t oggz_io_write (OGGZ * oggz, void * buf, size_t n); 
     216int oggz_io_seek (OGGZ * oggz, long offset, int whence); 
     217long oggz_io_tell (OGGZ * oggz); 
     218int oggz_io_flush (OGGZ * oggz); 
     219 
    187220#endif /* __OGGZ_PRIVATE_H__ */ 
  • liboggz/trunk/src/liboggz/oggz_read.c

    r381 r382  
    158158#if _UMMODIFIED_ 
    159159      buffer = ogg_sync_buffer (&reader->ogg_sync, CHUNKSIZE); 
    160       if ((bytes = fread (buffer, 1, CHUNKSIZE, oggz->file)) == 0) { 
     160      if ((bytes = oggz_io_read (oggz, buffer, CHUNKSIZE)) == 0) { 
    161161        if (ferror (oggz->file)) { 
    162162          oggz_set_error (oggz, OGGZ_ERR_SYSTEM); 
     
    189189  /* Calculate the byte offset of the page which was found */ 
    190190  if (bytes > 0) { 
    191     oggz->offset = ftell (oggz->file) - bytes + page_offset; 
     191    oggz->offset = oggz_io_tell (oggz) - bytes + page_offset; 
    192192    ret = oggz->offset; 
    193193  } else { 
     
    276276    } 
    277277 
     278    /* If we've got a stop already, don't read more data in */ 
     279    if (cb_ret != 0) return cb_ret; 
     280 
    278281    if(oggz_get_next_page_7 (oggz, &og) < 0) 
    279282      return -404; /* eof. leave unitialized */ 
     
    349352    bytes = MIN (remaining, 4096); 
    350353    buffer = ogg_sync_buffer (&reader->ogg_sync, bytes); 
    351     if ((bytes_read = (long)fread (buffer, 1, bytes, oggz->file)) == 0) { 
     354    if ((bytes_read = (long) oggz_io_read (oggz, buffer, bytes)) == 0) { 
    352355 
    353356      if (ferror (oggz->file)) { 
     
    433436  oggz_off_t offset_at; 
    434437 
    435   offset_at = ftell (oggz->file); 
     438  offset_at = oggz_io_tell (oggz); 
    436439 
    437440  return offset_at; 
     
    447450  oggz_off_t offset_at; 
    448451 
    449   if (fseek (oggz->file, offset, whence) == -1) { 
    450     if (errno == ESPIPE) { 
    451       /*oggz_set_error (oggz, OGGZ_ERR_NOSEEK);*/ 
    452     } else { 
    453       /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/ 
    454     } 
    455     return -1; 
    456   } 
    457  
    458   offset_at = ftell (oggz->file); 
     452  if (oggz_io_seek (oggz, offset, whence) == -1) { 
     453    return -1; 
     454  } 
     455 
     456  offset_at = oggz_io_tell (oggz); 
    459457 
    460458  oggz->offset = offset_at; 
     
    555553 
    556554      buffer = ogg_sync_buffer (&reader->ogg_sync, CHUNKSIZE); 
    557       if ((bytes = (long)fread (buffer, 1, CHUNKSIZE, oggz->file)) == 0) { 
     555      if ((bytes = (long) oggz_io_read (oggz, buffer, CHUNKSIZE)) == 0) { 
    558556        if (ferror (oggz->file)) { 
    559557          /*oggz_set_error (oggz, OGGZ_ERR_SYSTEM);*/ 
  • liboggz/trunk/src/liboggz/oggz_write.c

    r355 r382  
    456456    nwritten = write (fd, og->header + writer->page_offset, h); 
    457457#else 
    458     nwritten = (long)fwrite (og->header + writer->page_offset, 1, h, oggz->file); 
    459 #endif 
     458    nwritten = (long)oggz_io_write (oggz, og->header + writer->page_offset, h); 
     459#endif 
     460 
     461#ifdef DEBUG 
    460462    if (nwritten < h) { 
    461463      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, h); 
    462464    } 
     465#endif 
    463466    writer->page_offset += h; 
    464467    n -= h; 
     
    473476                      og->body + (writer->page_offset - og->header_len), b); 
    474477#else 
    475     nwritten = (long)fwrite (og->body + (writer->page_offset - og->header_len), 
    476                        1, b, oggz->file); 
    477 #endif 
     478    nwritten = (long)oggz_io_write (oggz, og->body + (writer->page_offset - og->header_len), b); 
     479#endif 
     480#ifdef DEBUG 
    478481    if (nwritten < b) { 
    479482      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, b); 
    480483    } 
     484#endif 
    481485    writer->page_offset += b; 
    482486    n -= b; 
  • liboggz/trunk/src/tests/Makefile.am

    r343 r382  
    1818endif 
    1919 
    20 TESTS = $(write_tests) 
     20if OGGZ_CONFIG_READ 
     21if OGGZ_CONFIG_WRITE 
     22rw_tests = read-generated read-stop-ok read-stop-err \ 
     23        io-read io-seek io-write 
     24endif 
     25endif 
     26 
     27TESTS = $(write_tests) $(rw_tests) 
    2128 
    2229noinst_PROGRAMS = $(TESTS) 
     
    4956write_bad_packetno_SOURCES = write-bad-packetno.c 
    5057write_bad_packetno_LDADD = $(OGGZ_LIBS) 
     58 
     59read_generated_SOURCES = read-generated.c 
     60read_generated_LDADD = $(OGGZ_LIBS) 
     61 
     62read_stop_ok_SOURCES = read-stop-ok.c 
     63read_stop_ok_LDADD = $(OGGZ_LIBS) 
     64 
     65read_stop_err_SOURCES = read-stop-err.c 
     66read_stop_err_LDADD = $(OGGZ_LIBS) 
     67 
     68io_read_SOURCES = io-read.c 
     69io_read_LDADD = $(OGGZ_LIBS) 
     70 
     71io_seek_SOURCES = io-seek.c 
     72io_seek_LDADD = $(OGGZ_LIBS) 
     73 
     74io_write_SOURCES = io-write.c 
     75io_write_LDADD = $(OGGZ_LIBS) 
  • liboggz/trunk/src/tests/oggz_tests.h

    r343 r382  
    3131*/ 
    3232 
     33#include "config.h" 
     34 
    3335#include <stdio.h> 
    3436#include <stdlib.h> 
     37 
     38#ifdef HAVE_INTTYPES_H 
     39#  include <inttypes.h> 
     40#else 
     41#  define PRId64 "I64d" 
     42#endif 
     43 
     44/* FIXME: on Mac OS X, off_t is 64-bits.  Obviously we want a nicer 
     45 * way to do it than this, but a quick fix is a good fix */ 
     46#ifdef __APPLE__ 
     47#  define PRI_off_t "q" 
     48#else 
     49#  define PRI_off_t "l" 
     50#endif 
    3551 
    3652#define INFO(str) \ 
     
    4258#define FAIL(str) \ 
    4359  { printf ("%s:%d: %s\n", __FILE__, __LINE__, (str)); exit(1); } 
     60 
     61#undef MIN 
     62#define MIN(a,b) ((a)<(b)?(a):(b))