home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / djgpp / include / streambu.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-03  |  4.0 KB  |  166 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #ifndef _streambuf_h
  25. #ifdef __GNUG__
  26. #pragma once
  27. #pragma interface
  28. #endif
  29. #define _streambuf_h 1
  30.  
  31. /* streambufs. 
  32.    basic streambufs and filebufs are as in Stroustrup, ch 8,
  33.    but the base class contains virtual extensions that allow
  34.    most capabilities of libg++ Files to be used as streambufs
  35.    via `Filebufs'.
  36. */
  37.  
  38. #include <stdio.h>
  39. #include <builtin.h>
  40. #include <Fmodes.h>
  41.  
  42. // see below for NO_LINE_BUFFER_STREAMBUFS
  43.  
  44. #ifndef BUFSIZE
  45. #ifdef BUFSIZ
  46. #define BUFSIZE BUFSIZ
  47. #else
  48. #define BUFSIZE 1024
  49. #endif
  50. #endif
  51.  
  52. enum open_mode // filebuf open modes
  53.   input=0, 
  54.   output=1, 
  55.   append=2 
  56. }; 
  57.  
  58. class streambuf
  59. {
  60. public:
  61.   char*       base;          // start of buffer
  62.   char*       pptr;          // put-pointer (and gptr fence)
  63.   char*       gptr;          // get-pointer
  64.   char*       eptr;          // last valid addr in buffer
  65.  
  66.   char        alloc;         // true if we own freestore alloced buffer
  67.  
  68.               streambuf();
  69.               streambuf(char* buf, int buflen);
  70.  
  71.   virtual    ~streambuf();
  72.  
  73.   int         doallocate();
  74.   int         allocate();
  75.  
  76.  
  77.   int         must_overflow(int ch); // true if should call overflow
  78.  
  79.   virtual int overflow(int c = EOF); // flush -- return EOF if fail
  80.   virtual int underflow();           // fill -- return EOF if fail
  81.  
  82.   int         sgetc();          // get one char (as int) or EOF
  83.   int         snextc();         // get and advance
  84.   void        stossc();         // advance only
  85.  
  86.   int         sputbackc(char);   // unget
  87.  
  88.   int         sputc(int c = EOF); // write one char
  89.  
  90.   virtual streambuf*  setbuf(char* buf, int buflen, int preloaded_count = 0);
  91.                                 // (not virtual in AT&T)
  92.  
  93. // the following aren't in AT&T version:
  94.  
  95.   int         sputs(const char* s);           // write null-terminated str
  96.   int         sputsn(const char* s, int len); // write len bytes
  97.  
  98.   virtual const char* name();
  99.  
  100.  
  101.   virtual streambuf*  open(const char* name, open_mode m);
  102.   virtual streambuf*  open(const char* filename, io_mode m, access_mode a);
  103.   virtual streambuf*  open(const char* filename, const char* m);
  104.   virtual streambuf*  open(int  filedesc, io_mode m);
  105.   virtual streambuf*  open(FILE* fileptr);
  106.  
  107.   virtual int         is_open();
  108.   virtual int         close();
  109.  
  110.   virtual void        error();
  111. };
  112.  
  113.  
  114. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  115.  
  116.  
  117. inline int streambuf::must_overflow(int ch)
  118. {
  119. #ifndef NO_LINE_BUFFER_STREAMBUF
  120.   return pptr >= eptr || ch == '\n';
  121. #else
  122.   return pptr >= eptr;
  123. #endif
  124. }
  125.  
  126.  
  127. inline int streambuf::allocate()
  128. {
  129.   return (base == 0)? doallocate() : 0; 
  130. }
  131.  
  132. inline int streambuf::sgetc()
  133. {
  134.   return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
  135. }
  136.  
  137.  
  138. inline int streambuf::snextc()
  139. {
  140.   ++gptr;
  141.   return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
  142. }
  143.  
  144.  
  145. inline void streambuf::stossc()
  146. {
  147.   if (gptr >= pptr) underflow(); else gptr++;
  148. }
  149.  
  150.  
  151. inline int streambuf::sputbackc(char ch)
  152. {
  153.   return (gptr > base)? int((unsigned char)(*--gptr = ch)) : EOF;
  154. }
  155.  
  156. inline int streambuf::sputc(int ch)
  157. {
  158.   return must_overflow(ch)? overflow(ch) : 
  159.                             int((unsigned char)(*pptr++ = char(ch)));
  160. }
  161.  
  162. #endif
  163.  
  164. #endif
  165.