home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / basq / source / poweru / cache.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-16  |  2.1 KB  |  130 lines

  1.     #define    CACHEOK        0
  2.     #define    BADCACHE    1
  3.  
  4.     #include <io.h>
  5.     #include <fcntl.h>
  6.     #include <sys\stat.h>
  7.     #include <malloc.h>
  8.  
  9. class readablecache
  10.     {
  11.     protected:
  12.  
  13.         char far            *bpoint;
  14.         int                        buffercount;
  15.         unsigned int    handle;
  16.  
  17.     public:
  18.  
  19.         char                    status;
  20.  
  21.         readablecache(int);
  22.         char    readitem();
  23.         long    lseek(long,int);
  24.         long    filelength();
  25.         ~readablecache();
  26.     };
  27.  
  28. class writeablecache
  29.     {
  30.     protected:
  31.  
  32.         char far            *bpoint;
  33.         int                        buffercount;
  34.         unsigned int    handle;
  35.  
  36.     public:
  37.  
  38.         char                    status;
  39.  
  40.         writeablecache(int);
  41.         void writeitem(char);
  42.         ~writeablecache();
  43.     };
  44.  
  45.  
  46. readablecache::readablecache(int handlec)
  47.     {
  48.     handle=handlec;
  49.     buffercount=32000;
  50.     if((bpoint=(char far *) _fcalloc(32000,sizeof(char)))==NULL) status=BADCACHE;
  51.     else status=CACHEOK;
  52.     }
  53.  
  54. readablecache::~readablecache()
  55.     {
  56.     _ffree(bpoint);
  57.     }
  58.  
  59. char readablecache::readitem()
  60.     {
  61.     char        buf;
  62.     int            rdbytes;
  63.  
  64. bcj:    if(status!=BADCACHE)
  65.         {
  66.         if(buffercount==32000)
  67.             {
  68.             rdbytes=read(handle,bpoint,32000);
  69.             if(rdbytes==-1||rdbytes==0)
  70.                 {
  71.                 status=BADCACHE;
  72.                 goto bcj;
  73.                 }
  74.             buffercount=0;
  75.             }
  76.         buf=bpoint[buffercount];
  77.         status=CACHEOK;
  78.         buffercount++;
  79.         }
  80.     else buf=0;
  81.     return buf;
  82.     }
  83.  
  84. long readablecache::lseek(long offset,int origin)
  85.     {
  86.     buffercount=32000;
  87.     return ::lseek(handle,offset,origin);
  88.     }
  89.  
  90. long readablecache::filelength()
  91.     {
  92.     return ::filelength(handle);
  93.     }
  94.  
  95. writeablecache::writeablecache(int handlec)
  96.     {
  97.     handle=handlec;
  98.     buffercount=0;
  99.     if((bpoint=(char far *) _fcalloc(32000,sizeof(char)))==NULL) status=BADCACHE;
  100.     else status=CACHEOK;
  101.     }
  102.  
  103. writeablecache::~writeablecache()
  104.     {
  105. bcj:    if(status!=BADCACHE)
  106.         {
  107.         if(write(handle,bpoint,buffercount)==buffercount) status=BADCACHE;
  108.         goto bcj;
  109.         }
  110.     _ffree(bpoint);
  111.     }
  112.  
  113. void writeablecache::writeitem(char item)
  114.     {
  115. bcj:    if(status!=BADCACHE)
  116.         {
  117.         if(buffercount==32000)
  118.             {
  119.             buffercount=0;
  120.             if(write(handle,bpoint,32000)==-1)
  121.                 {
  122.                 status=BADCACHE;
  123.                 goto bcj;
  124.                 }
  125.             }
  126.         bpoint[buffercount]=item;
  127.         buffercount++;
  128.         }
  129.     }
  130.