home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__inc / streambu.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  5.5 KB  |  237 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _streambuf_h
  20. #ifdef __GNUG__
  21. #pragma once
  22. #pragma interface
  23. #endif
  24. #define _streambuf_h 1
  25.  
  26. /* streambufs. 
  27.    basic streambufs and filebufs are as in Stroustrup, ch 8,
  28.    but the base class contains virtual extensions that allow
  29.    most capabilities of libg++ Files to be used as streambufs
  30.    via `Filebufs'.
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <builtin.h>
  35. #include <xfmodes.h>
  36. #ifdef atarist
  37. #include <bool.h>
  38. #endif
  39.  
  40. // see below for NO_LINE_BUFFER_STREAMBUFS
  41.  
  42. #ifndef BUFSIZE
  43. #ifdef BUFSIZ
  44. #define BUFSIZE BUFSIZ
  45. #else
  46. #define BUFSIZE 1024
  47. #endif
  48. #endif
  49.  
  50. enum open_mode // filebuf open modes
  51.   input=0, 
  52.   output=1, 
  53.   append=2 
  54. #ifdef atarist
  55.   ,
  56.   input_bin =  _atari_bin+input,
  57.   output_bin = _atari_bin+output,
  58.   append_bin = _atari_bin+append,
  59.  
  60.   input_text  = _atari_text+input,
  61.   output_text = _atari_text+output,
  62.   append_text = _atari_text+append
  63. #endif
  64. }; 
  65.  
  66. class streambuf
  67. {
  68. public:
  69.   char*       base;          // start of buffer
  70.   char*       pptr;          // put-pointer (and gptr fence)
  71.   char*       gptr;          // get-pointer
  72.   char*       eptr;          // last valid addr in buffer
  73.  
  74.   char        alloc;         // true if we own freestore alloced buffer
  75.  
  76. #ifdef atarist
  77.   bool        _bin_mode;     // text or bin mode
  78. #endif
  79.  
  80.               streambuf();
  81. #ifdef atarist
  82.               streambuf(char* buf, size_t buflen);
  83. #else
  84.               streambuf(char* buf, int buflen);
  85. #endif
  86.  
  87.   virtual    ~streambuf();
  88.  
  89. #ifdef atarist
  90.   size_t         doallocate();
  91.   size_t         allocate();
  92. #else
  93.   int         doallocate();
  94.   int         allocate();
  95. #endif
  96.  
  97.   int         must_overflow(int ch); // true if should call overflow
  98.  
  99.   virtual int overflow(int c = EOF); // flush -- return EOF if fail
  100.   virtual int underflow();           // fill -- return EOF if fail
  101.  
  102.   int         sgetc();          // get one char (as int) or EOF
  103.   int         snextc();         // get and advance
  104.   void        stossc();         // advance only
  105.  
  106.   int         sputbackc(char);   // unget
  107.  
  108.   int         sputc(int c = EOF); // write one char
  109.  
  110. #ifdef atarist
  111.   virtual streambuf*  setbuf(char* buf, size_t buflen, size_t preloaded_count = 0);
  112. #else
  113.   virtual streambuf*  setbuf(char* buf, int buflen, int preloaded_count = 0);
  114.                                 // (not virtual in AT&T)
  115. #endif
  116.  
  117. // the following aren't in AT&T version:
  118.  
  119.   int         sputs(const char* s);           // write null-terminated str
  120. #ifdef atarist
  121.   int         sputsn(const char* s, size_t len);
  122.   int         _atari_putc(int ch);            // kludge for bin/text modes
  123. #else
  124.   int         sputsn(const char* s, int len); // write len bytes
  125. #endif
  126.  
  127.   virtual const char* name();
  128.  
  129.  
  130.   virtual streambuf*  open(const char* name, open_mode m);
  131.   virtual streambuf*  open(const char* filename, io_mode m, access_mode a);
  132.   virtual streambuf*  open(const char* filename, const char* m);
  133.   virtual streambuf*  open(int  filedesc, io_mode m);
  134.   virtual streambuf*  open(FILE* fileptr);
  135.  
  136.   virtual int         is_open();
  137.   virtual int         close();
  138.  
  139.   virtual void        error();
  140. };
  141.  
  142.  
  143. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  144.  
  145.  
  146. inline int streambuf::must_overflow(int ch)
  147. {
  148. #ifndef NO_LINE_BUFFER_STREAMBUF
  149.   return pptr >= eptr || ch == '\n';
  150. #else
  151.   return pptr >= eptr;
  152. #endif
  153. }
  154.  
  155.  
  156. #ifdef atarist
  157. inline size_t streambuf::allocate()
  158. {
  159.   return (base == 0)? doallocate() : 0; 
  160. }
  161. #else
  162. inline int streambuf::allocate()
  163. {
  164.   return (base == 0)? doallocate() : 0; 
  165. }
  166. #endif
  167.  
  168. inline int streambuf::sgetc()
  169. {
  170. #ifndef atarist
  171.   return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
  172. #else
  173.   int ch;
  174.   do {
  175.     ch = ((gptr >= pptr) ? underflow() : int((unsigned char)(*gptr)));
  176.     if ((!_bin_mode) && (ch == '\r'))
  177.       ch = ((++gptr >= pptr) ? underflow() : int((unsigned char)(*gptr)));
  178.   } while ((!_bin_mode) && (ch == '\r'));
  179.   return(ch);
  180. #endif
  181. }
  182.  
  183.  
  184. inline int streambuf::snextc()
  185. {
  186. #ifndef atarist
  187.   ++gptr;
  188.   return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
  189. #else
  190.   int ch;
  191.   do {
  192.     ++gptr;
  193.     ch = ((gptr >= pptr) ? underflow() : int((unsigned char)(*gptr)));
  194.   } while ((!_bin_mode) && (ch == '\r'));
  195.   return ch;
  196. #endif
  197. }
  198.  
  199.  
  200. inline void streambuf::stossc()
  201. {
  202.   if (gptr >= pptr) underflow(); else gptr++;
  203. }
  204.  
  205.  
  206. inline int streambuf::sputbackc(char ch)
  207. {
  208.   return (gptr > base)? int((unsigned char)(*--gptr = ch)) : EOF;
  209. }
  210.  
  211. inline int streambuf::sputc(int ch)
  212. {
  213.   return must_overflow(ch)? overflow(ch) : 
  214. #ifdef atarist
  215.                             _atari_putc(ch);
  216. #else
  217.                             int((unsigned char)(*pptr++ = char(ch)));
  218. #endif
  219. }
  220.  
  221. #ifdef atarist
  222.  
  223. inline int streambuf::_atari_putc(int ch)
  224. {
  225.   if ((!_bin_mode) && (ch == '\n'))
  226.     *pptr++ = '\r';
  227.   *pptr++ = ch;
  228.   return(int(ch));
  229. }
  230.  
  231. #endif
  232.  
  233. #endif
  234.  
  235. #endif
  236.