home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / pjp / filebuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-02  |  3.3 KB  |  109 lines

  1. ------------- Listing 2: The file filebuf.c ------------------
  2.  
  3. // filebuf -- filebuf basic members
  4. #include <stdio.h>
  5. #include <fstream>
  6.  
  7. filebuf::~filebuf()
  8.         {       // destruct a filebuf
  9.         if (_Closef)
  10.                 close();
  11.         }
  12.  
  13. filebuf *filebuf::close()
  14.         {       // close a file
  15.         if (_File != 0 && fclose(_File) == 0)
  16.                 {       // note successful close
  17.                 _Init();
  18.                 return (this);
  19.                 }
  20.         else
  21.                 return (0);
  22.         }
  23.  
  24. int filebuf::overflow(int ch)
  25.         {       // try to write output
  26.         return (pptr() != 0 && pptr() < epptr()
  27.                 ? (*_Pn()++ = ch)
  28.                 : _File == 0 ? EOF : ch == EOF ? 0 : fputc(ch, _File));
  29.         }
  30.  
  31. int filebuf::pbackfail(int ch)
  32.         {       // try to pushback a character
  33.         return (gptr() != 0 && eback() < gptr() && ch == gptr()[-1]
  34.                 ? *--_Gn()
  35.                 : _File == 0 || ch == EOF ? EOF : ungetc(ch, _File));
  36.         }
  37.  
  38. int filebuf::underflow()
  39.         {       // try to peek at input
  40.         return (gptr() != 0 && gptr() < egptr()
  41.                 ? *_Gn()
  42.                 : _File == 0 ? EOF : ungetc(fgetc(_File), _File));
  43.         }
  44.  
  45. int filebuf::uflow()
  46.         {       // try to consume input
  47.         return (gptr() != 0 && gptr() < egptr()
  48.                 ? *_Gn()++
  49.                 : _File == 0 ? EOF : fgetc(_File));
  50.         }
  51.  
  52. streamsize filebuf::xsgetn(char *s, streamsize n)
  53.         {       // read n characters
  54.         return (_File == 0 ? 0 : fread(s, 1, n, _File));
  55.         }
  56.  
  57. streamsize filebuf::xsputn(const char *s, streamsize n)
  58.         {       // write n characters
  59.         return (_File == 0 ? 0 : fwrite(s, 1, n, _File));
  60.         }
  61.  
  62. streampos filebuf::seekoff(streamoff off, ios::seekdir way,
  63.         ios::openmode)
  64.         {       // seek by specified offset
  65.         return (streampos(_File == 0
  66.                 || fseek(_File, off, way) != 0
  67.                  ? _BADOFF : streamoff(ftell(_File))));
  68.         }
  69.  
  70. streampos filebuf::seekpos(streampos sp, ios::openmode)
  71.         {       // seek to memorized position
  72.         return (_File == 0 || fsetpos(_File, sp._Fpos()) != 0
  73.                 || fseek(_File, sp.offset(), SEEK_CUR) != 0
  74.                 || fgetpos(_File, sp._Fpos()) != 0
  75.                 ? streampos(_BADOFF)
  76.                 : streampos(0, sp._Fpos()));
  77.         }
  78.  
  79. streambuf *filebuf::setbuf(char *s, streamsize n)
  80.         {       // provide a file buffer
  81.         return (_File == 0 || setvbuf(_File, s, _IOFBF, n) != 0
  82.                 ? 0 : this);
  83.         }
  84.  
  85. int filebuf::sync()
  86.         {       // synchronize buffer with file
  87.         return (_File == 0 ? 0 : fflush(_File));
  88.         }
  89.  
  90. FILE *filebuf::_Init(FILE *fp, bool closef)
  91.         {       // initialize buffer pointers
  92. #if _HAS_PJP_CLIB
  93.         if (fp == 0)
  94.                 streambuf::_Init();
  95.         else
  96.                 streambuf::_Init((char **)&fp->_Buf,
  97.                         (char **)&fp->_Next,
  98.                         (char **)&fp->_Rend,
  99.                         (char **)&fp->_Buf,
  100.                         (char **)&fp->_Next,
  101.                         (char **)&fp->_Wend);
  102. #else
  103.         streambuf::_Init();
  104. #endif
  105.         _Closef = closef;
  106.         _File = fp;
  107.         return (_File);
  108.         }
  109.