home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 334.lha / DkbAnim / gio.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  5KB  |  128 lines

  1. /*----------------------------------------------------------------------*/
  2. /* GIO.C  Generic I/O Speed Up Package                         1/23/86  */
  3. /* See GIOCall.C for an example of usage.                */
  4. /* Read not speeded-up yet.  Only one Write file buffered at a time.    */
  5. /* Note: The speed-up provided is ONLY significant for code such as IFF */
  6. /* which does numerous small Writes and Seeks.                */
  7. /*                                                                  */
  8. /* By Jerry Morrison and Steve Shaw, Electronic Arts.               */
  9. /* This software is in the public domain.                           */
  10. /*                                                                  */
  11. /* This version for the Commodore-Amiga computer.                   */
  12. /*                                                                   */
  13. /*----------------------------------------------------------------------*/
  14. #include "iff/gio.h"    /* See comments here for explanation.*/
  15.  
  16. #if GIO_ACTIVE
  17.  
  18. #define local static
  19.  
  20.  
  21. local BPTR wFile      = NULL;
  22. local BYTE *wBuffer   = NULL;
  23. local LONG wNBytes    = 0; /* buffer size in bytes.*/
  24. local LONG wIndex     = 0; /* index of next available byte.*/
  25. local LONG wWaterline = 0; /* Count of # bytes to be written.
  26.                 * Different than wIndex because of GSeek.*/
  27.  
  28. /*----------- GOpen ----------------------------------------------------*/
  29. LONG GOpen(filename, openmode)   char *filename;  LONG openmode; {
  30.     return( Open(filename, openmode) );
  31.     }
  32.  
  33. /*----------- GClose ---------------------------------------------------*/
  34. LONG GClose(file)  BPTR file; {
  35.     LONG signal = 0, signal2;
  36.     if (file == wFile)
  37.     signal = GWriteUndeclare(file);
  38.     signal2 = Close(file);    /* Call Close even if trouble with write.*/
  39.     if (signal2 < 0)
  40.     signal = signal2;
  41.     return( signal );
  42.     }
  43.  
  44. /*----------- GRead ----------------------------------------------------*/
  45. LONG GRead(file, buffer, nBytes)   BPTR file;  BYTE *buffer;  LONG nBytes; {
  46.     LONG signal = 0;
  47.     /* We don't yet read directly from the buffer, so flush it to disk and
  48.      * let the DOS fetch it back. */
  49.     if (file == wFile)
  50.     signal = GWriteFlush(file);
  51.     if (signal >= 0)
  52.     signal = Read(file, buffer, nBytes);
  53.     return( signal );
  54.     }
  55.  
  56. /* ---------- GWriteFlush ----------------------------------------------*/
  57. LONG GWriteFlush(file)  BPTR file; {
  58.     LONG gWrite = 0;
  59.     if (wFile != NULL  &&  wBuffer != NULL  &&  wIndex > 0)
  60.     gWrite = Write(wFile, wBuffer, wWaterline);
  61.     wWaterline = wIndex = 0;    /* No matter what, make sure this happens.*/
  62.     return( gWrite );
  63.     }
  64.  
  65. /* ---------- GWriteDeclare --------------------------------------------*/
  66. LONG GWriteDeclare(file, buffer, nBytes)
  67.     BPTR file;  BYTE *buffer;  LONG nBytes; {
  68.     LONG gWrite = GWriteFlush(wFile);  /* Finish any existing usage.*/
  69.     if ( file==NULL  ||  (file==wFile  &&  buffer==NULL)  ||  nBytes<=3) {
  70.     wFile = NULL;   wBuffer = NULL;     wNBytes = 0; }
  71.     else {
  72.     wFile = file;   wBuffer = buffer;   wNBytes = nBytes; }
  73.     return( gWrite );
  74.     }
  75.  
  76. /* ---------- GWrite ---------------------------------------------------*/
  77. LONG GWrite(file, buffer, nBytes)   BPTR file;  BYTE *buffer;  LONG nBytes; {
  78.     LONG gWrite = 0;
  79.  
  80.     if (file == wFile  &&  wBuffer != NULL) {
  81.     if (wNBytes >= wIndex + nBytes) {
  82.         /* Append to wBuffer.*/
  83.         movmem(buffer, wBuffer+wIndex, nBytes);
  84.         wIndex += nBytes;
  85.         if (wIndex > wWaterline)
  86.         wWaterline = wIndex;
  87.         nBytes = 0;        /* Indicate data has been swallowed.*/
  88.         }
  89.     else {
  90.         wWaterline = wIndex;     /* We are about to overwrite any
  91.         * data above wIndex, up to at least the buffer end.*/
  92.         gWrite = GWriteFlush(file);  /* Write data out in proper order.*/
  93.         }
  94.     }
  95.     if (nBytes > 0  &&  gWrite >= 0)
  96.     gWrite += Write(file, buffer, nBytes);
  97.     return( gWrite );
  98.     }
  99.  
  100. /* ---------- GSeek ----------------------------------------------------*/
  101. LONG GSeek(file, position, mode)
  102.     BPTR file;   LONG position;   LONG mode; {
  103.     LONG gSeek = -2;
  104.     LONG newWIndex = wIndex + position;
  105.  
  106.     if (file == wFile  &&  wBuffer != NULL) {
  107.     if (mode == OFFSET_CURRENT  &&
  108.         newWIndex >= 0  &&  newWIndex <= wWaterline) {
  109.         gSeek = wIndex;     /* Okay; return *OLD* position */
  110.         wIndex = newWIndex;
  111.         }
  112.     else {
  113.         /* We don't even try to optimize the other cases.*/
  114.         gSeek = GWriteFlush(file);
  115.         if (gSeek >= 0)   gSeek = -2;  /* OK so far */
  116.         }
  117.     }
  118.     if (gSeek == -2)
  119.     gSeek = Seek(file, position, mode);
  120.     return( gSeek );
  121.     }
  122.  
  123. #else /* not GIO_ACTIVE */
  124.  
  125. void GIODummy() { }    /* to keep the compiler happy */
  126.  
  127. #endif GIO_ACTIVE
  128.