home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / BufferedFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-16  |  5.1 KB  |  198 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3. BufferedFile.c
  4.  
  5. */
  6.  
  7. struct BufferedFile_S {
  8.     long                            errval;
  9.     short                           FileNum;
  10.     EString_t                       buffer;
  11.     unsigned long                   buffersize;
  12.     unsigned long                   bufferindex;
  13.     unsigned long                   Fullness;
  14. };
  15. typedef struct BufferedFile_S   BufferedFile_t;
  16. typedef BufferedFile_t P__H    *BufferedFileVia_t;
  17.  
  18. long                            FillFileBuffer(BufferedFileVia_t thef);
  19. void                            FlushFileBuffer(BufferedFileVia_t thef);
  20.  char                   GetBufChar(MemBufVia_t theb);
  21.  short                  GetBufWord(MemBufVia_t theb);
  22.  long                   GetBufLong(MemBufVia_t theb);
  23. void                            PutBufChar(MemBufVia_t theb,  char c);
  24. void                            PutBufWord(MemBufVia_t theb,  short wd);
  25. void                            PutBufLong(MemBufVia_t theb,  long lg);
  26.  char                   GetFileChar(BufferedFileVia_t thef);
  27. void                            PutFileChar(BufferedFileVia_t thef,  char c);
  28. void                            PutFileWord(BufferedFileVia_t thef,  short wd);
  29. void                            PutFileLong(BufferedFileVia_t thef,  long wd);
  30. BufferedFileVia_t               OpenFile(char *name, short volrefnum, long dirID, char *mode);
  31. void                            CloseFile(BufferedFileVia_t thef);
  32.  short                  GetFileWord(BufferedFileVia_t thef);
  33.  long                   GetFileLong(BufferedFileVia_t thef);
  34. long
  35. FillFileBuffer(BufferedFileVia_t thef)
  36. /* For input files, this fills the buffer by reading the file */
  37. {
  38.     long                            count;
  39.     #ifdef OLDMEM
  40.     HLock((Handle) Via(thef)->buffer);
  41.     #endif
  42.     count = Via(thef)->buffersize;
  43.     Via(thef)->errval = FSRead(Via(thef)->FileNum, &count, (char *) Via(Via(thef)->buffer));
  44.     Via(thef)->Fullness = count;
  45.     Via(thef)->bufferindex = 0;
  46.     #ifdef OLDMEM
  47.     HUnlock((Handle) Via(thef)->buffer);
  48.     #endif
  49.     return count;
  50. }
  51.  
  52. void
  53. FlushFileBuffer(BufferedFileVia_t thef)
  54. /* For output files, this dumps the contents of the buffer */
  55. {
  56.     long                            count;
  57.     #ifdef OLDMEM
  58.     HLock((Handle) Via(thef)->buffer);
  59.     #endif
  60.     count = Via(thef)->bufferindex;
  61.     Via(thef)->errval = FSWrite(Via(thef)->FileNum, &count, (char *) Via(Via(thef)->buffer));
  62.     Via(thef)->bufferindex = 0;
  63.     #ifdef OLDMEM
  64.     HUnlock((Handle) Via(thef)->buffer);
  65.     #endif
  66. }
  67.  
  68.  char
  69. GetBufChar(MemBufVia_t theb)
  70. {
  71.     return Via(Via(theb)->buf)[Via(theb)->ndx++];
  72. }
  73.  
  74.  short
  75. GetBufWord(MemBufVia_t theb)
  76. {
  77.      short                  wd;
  78.     wd = GetBufChar(theb) << 8;
  79.     wd |= GetBufChar(theb);
  80.     return wd;
  81. }
  82.  
  83.  long
  84. GetBufLong(MemBufVia_t theb)
  85. {
  86.      long                   lg;
  87.     lg = GetBufWord(theb) << 16;
  88.     lg |= GetBufWord(theb);
  89.     return lg;
  90. }
  91.  
  92. void
  93. PutBufChar(MemBufVia_t theb,  char c)
  94. {
  95.     Via(Via(theb)->buf)[Via(theb)->ndx++] = c;
  96. }
  97.  
  98. void
  99. PutBufWord(MemBufVia_t theb,  short wd)
  100. {
  101.     PutBufChar(theb, wd >> 8);
  102.     PutBufChar(theb, wd);
  103. }
  104.  
  105. void
  106. PutBufLong(MemBufVia_t theb,  long lg)
  107. {
  108.     PutBufWord(theb, lg >> 16);
  109.     PutBufWord(theb, lg);
  110. }
  111.  
  112.  char
  113. GetFileChar(BufferedFileVia_t thef)
  114. {
  115.     if (Via(thef)->bufferindex >= Via(thef)->Fullness)
  116.     FillFileBuffer(thef);
  117.     if (!Via(thef)->Fullness)
  118.     Via(thef)->errval = -1;
  119.     return Via(Via(thef)->buffer)[Via(thef)->bufferindex++];
  120. }
  121.  
  122. void
  123. PutFileChar(BufferedFileVia_t thef,  char c)
  124. {
  125.     Via(Via(thef)->buffer)[Via(thef)->bufferindex++] = c;
  126.     if (Via(thef)->bufferindex >= Via(thef)->buffersize)
  127.     FlushFileBuffer(thef);
  128. }
  129.  
  130. void
  131. PutFileWord(BufferedFileVia_t thef,  short wd)
  132. {
  133.     PutFileChar(thef, wd >> 8);
  134.     PutFileChar(thef, wd);
  135. }
  136.  
  137. void
  138. PutFileLong(BufferedFileVia_t thef,  long wd)
  139. {
  140.     PutFileWord(thef, wd >> 16);
  141.     PutFileWord(thef, wd);
  142. }
  143.  
  144. BufferedFileVia_t
  145. OpenFile(char *name, short volrefnum, long dirID, char *mode)
  146. {
  147.     /* name coming in must be a C string */
  148.     BufferedFileVia_t               raw;
  149.     Str255                          pname;
  150.  
  151.     raw = Ealloc(sizeof(BufferedFile_t));
  152.     strcpy((char *) pname, name);
  153.     c2pstr(pname);
  154.     if (!strcmp(mode, "w")) {
  155.     HDelete(volrefnum, dirID, pname);
  156.     HCreate(volrefnum, dirID, pname, MakeOSType("Jn15"), MakeOSType("TEXT"));
  157.     Via(raw)->errval = HOpen(volrefnum, dirID, pname, 2, &(Via(raw)->FileNum));
  158.     } else {
  159.     Via(raw)->errval = HOpen(volrefnum, dirID, pname, 1, &(Via(raw)->FileNum));
  160.     }
  161.     if (Via(raw)->errval)
  162.     return NULL;
  163.     Via(raw)->buffersize = 10000;
  164.     Via(raw)->buffer = Ealloc(Via(raw)->buffersize);
  165.     Via(raw)->bufferindex = 0;
  166.     Via(raw)->errval = 0;
  167.     FillFileBuffer(raw);
  168.     return raw;
  169. }
  170.  
  171. void
  172. CloseFile(BufferedFileVia_t thef)
  173. {
  174.     FlushFileBuffer(thef);
  175.     FSClose(Via(thef)->FileNum);
  176.     Efree(Via(thef)->buffer);
  177.     Efree(thef);
  178. }
  179.  
  180.  short
  181. GetFileWord(BufferedFileVia_t thef)
  182. {
  183.     int                             wd;
  184.     wd = GetFileChar(thef);
  185.     wd = (wd << 8) | GetFileChar(thef);
  186.     return wd;
  187. }
  188.  
  189.  long
  190. GetFileLong(BufferedFileVia_t thef)
  191. {
  192.      long                   lg;
  193.     lg = GetFileWord(thef);
  194.     lg = (lg << 16) | GetFileWord(thef);
  195.     return lg;
  196. }
  197.  
  198.