home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / toolkt21 / c / samples / tp / fm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  3.8 KB  |  144 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)fm.c 1.3 1/22/92 16:09:18 [1/26/92] (c)IBM Corp. 1992";
  3. #endif
  4.  
  5. /*
  6.  * This class is adapted from the book
  7.  *   Class Construction in C and C++, Object Oriented Fundamentals
  8.  *   by Roger Sessions, Copyright (c) 1992 Prentice Hall.
  9.  * Reprinted with permission.
  10.  */
  11.  
  12. #define fileMgr_Class_Source
  13.  
  14. #define BUFF_SIZE 20
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "fm.ih"
  19.  
  20. /* ************************************************************ */
  21. SOM_Scope void SOMLINK fmInit(fileMgr * somSelf,
  22.                    char *newFile)
  23. {
  24.     fileMgrData *somThis = fileMgrGetData(somSelf);
  25.     fileMgrMethodDebug("fileMgr", "fileMgrInit");
  26.     if (!(_funit = fopen(newFile, "r"))) {
  27.     printf("Error: unable to open input file \"%s\".\n", newFile);
  28.     exit(3);
  29.     }
  30. }
  31.  
  32. /* ************************************************************ */
  33. SOM_Scope int SOMLINK fmGetChar(fileMgr * somSelf)
  34. {
  35.     fileMgrData *somThis = fileMgrGetData(somSelf);
  36.     int newChar;
  37.     fileMgrMethodDebug("fileMgr", "fmGetChar");
  38.  
  39.     if (_fmSize(somSelf)) {
  40.     newChar = _buffer[_getSide];
  41.     _getSide = _fmIncr(somSelf, _getSide);
  42.     }
  43.     else {
  44.     newChar = getc(_funit);
  45.     }
  46.     return newChar;
  47. }
  48.  
  49. /* ************************************************************ */
  50. SOM_Scope int SOMLINK fmPeekChar(fileMgr * somSelf,
  51.                   int offset)
  52. {
  53.     fileMgrData *somThis = fileMgrGetData(somSelf);
  54.     int newChar;
  55.     char cChar;
  56.     int n;
  57.     fileMgrMethodDebug("fileMgr", "fmPeekChar");
  58.  
  59.     for (;;) {
  60.     n = _fmSize(somSelf);
  61.     if (n > offset)
  62.         break;
  63.     newChar = getc(_funit);
  64.     cChar = (char) newChar;
  65.     _buffer[_putSide] = newChar;
  66.     _putSide = _fmIncr(somSelf, _putSide);
  67.     }
  68.     offset = _fmAdd(somSelf, _getSide, offset);
  69.     newChar = _buffer[offset];
  70.     return newChar;
  71. }
  72.  
  73. /* ************************************************************ */
  74. SOM_Scope void SOMLINK somInit(fileMgr * somSelf)
  75. {
  76.     fileMgrData *somThis = fileMgrGetData(somSelf);
  77.     fileMgrMethodDebug("fileMgr", "somInit");
  78.     parent_somInit(somSelf);
  79.  
  80.     _funit = 0;
  81.     _putSide = 0;
  82.     _getSide = 0;
  83. }
  84.  
  85. /* ************************************************************ */
  86. SOM_Scope void SOMLINK somUninit(fileMgr * somSelf)
  87. {
  88.     fileMgrData *somThis = fileMgrGetData(somSelf);
  89.     fileMgrMethodDebug("fileMgr", "somUninit");
  90.  
  91.     if (_funit)
  92.     fclose(_funit);
  93.  
  94.     parent_somUninit(somSelf);
  95. }
  96.  
  97. /* ************************************************************ */
  98. SOM_Scope void SOMLINK somDumpSelfInt(fileMgr * somSelf,
  99.                        int level)
  100. {
  101.     fileMgrData *somThis = fileMgrGetData(somSelf);
  102.     fileMgrMethodDebug("fileMgr", "somDumpSelfInt");
  103.     parent_somDumpSelfInt(somSelf, level);
  104. }
  105.  
  106. /* ************************************************************ */
  107. SOM_Scope int SOMLINK fmSize(fileMgr * somSelf)
  108. {
  109.     fileMgrData *somThis = fileMgrGetData(somSelf);
  110.     int result;
  111.     fileMgrMethodDebug("fileMgr", "size");
  112.  
  113.     if (_getSide == _putSide)
  114.     result = 0;
  115.     else if (_getSide < _putSide)
  116.     result = _putSide - _getSide;
  117.     else
  118.     result = (BUFF_SIZE - _getSide - 1) + (_putSide + 1);
  119.     return result;
  120. }
  121.  
  122. /* ************************************************************ */
  123. SOM_Scope int SOMLINK fmIncr(fileMgr * somSelf,
  124.                   int oldNum)
  125. {
  126.     fileMgrData *somThis = fileMgrGetData(somSelf);
  127.     fileMgrMethodDebug("fileMgr", "incr");
  128.  
  129.     if (oldNum == (BUFF_SIZE - 1))
  130.     return 0;
  131.     else
  132.     return (oldNum + 1);
  133. }
  134.  
  135. /* ************************************************************ */
  136. SOM_Scope int SOMLINK fmAdd(fileMgr * somSelf,
  137.                  int oldNum,
  138.                  int addNum)
  139. {
  140.     fileMgrData *somThis = fileMgrGetData(somSelf);
  141.     fileMgrMethodDebug("fileMgr", "add");
  142.     return ((oldNum + addNum) % BUFF_SIZE);
  143. }
  144.