home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libio / iostream.cc < prev    next >
C/C++ Source or Header  |  1994-06-16  |  18KB  |  814 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /* Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #ifdef __GNUC__
  28. #pragma implementation
  29. #endif
  30. #define _STREAM_COMPAT
  31. #include <iostream.h>
  32. #include "libioP.h"
  33. #include <stdio.h>  /* Needed for sprintf */
  34. #include <ctype.h>
  35. #include <string.h>
  36. #include <limits.h>
  37. #include "floatio.h"
  38.  
  39. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  40.  
  41. //#define isspace(ch) ((ch)==' ' || (ch)=='\t' || (ch)=='\n')
  42.  
  43. istream::istream(streambuf *sb, ostream* tied)
  44. {
  45.   init (sb, tied);
  46.   _flags |= ios::dont_close;
  47.   _gcount = 0;
  48. }
  49.  
  50. int skip_ws(streambuf* sb)
  51. {
  52.     int ch;
  53.     for (;;) {
  54.     ch = sb->sbumpc();
  55.     if (ch == EOF || !isspace(ch))
  56.         return ch;
  57.     }
  58. }
  59.  
  60. istream& istream::get(char& c)
  61. {
  62.     if (ipfx1()) {
  63.     int ch = _strbuf->sbumpc();
  64.     if (ch == EOF) {
  65.       set(ios::eofbit|ios::failbit);
  66.       _gcount = 0;
  67.     }
  68.     else {
  69.       c = (char)ch;
  70.       _gcount = 1;
  71.     }
  72.     }
  73.     return *this;
  74. }
  75.  
  76. int istream::peek()
  77. {
  78.   if (!good())
  79.     return EOF;
  80.   if (_tie && rdbuf()->in_avail() == 0)
  81.     _tie->flush();
  82.   int ch = _strbuf->sgetc();
  83.   if (ch == EOF)
  84.     set(ios::eofbit);
  85.   return ch;
  86. }
  87.  
  88. istream& istream::ignore(int n /* = 1 */, int delim /* = EOF */)
  89. {
  90.     if (ipfx1()) {
  91.     register streambuf* sb = _strbuf;
  92.     if (delim == EOF) {
  93.         _gcount = sb->ignore(n);
  94.         return *this;
  95.     }
  96.     _gcount = 0;
  97.     for (;;) {
  98. #if 0
  99.         if (n != MAXINT) // FIXME
  100. #endif
  101.         if (--n < 0)
  102.         break;
  103.         int ch = sb->sbumpc();
  104.         if (ch == EOF) {
  105.         set(ios::eofbit|ios::failbit);
  106.         break;
  107.         }
  108.         _gcount++;
  109.         if (ch == delim)
  110.         break;
  111.     }
  112.     }
  113.     return *this;
  114. }
  115.  
  116. istream& istream::read(char *s, int n)
  117. {
  118.     if (ipfx1()) {
  119.     _gcount = _strbuf->sgetn(s, n);
  120.     if (_gcount != n)
  121.         set(ios::failbit|ios::eofbit);
  122.     }
  123.     return *this;
  124. }
  125.  
  126. istream& istream::seekg(streampos pos)
  127. {
  128.     pos = _strbuf->sseekpos(pos, ios::in);
  129.     if (pos == streampos(EOF))
  130.     set(ios::badbit);
  131.     return *this;
  132. }
  133.  
  134. istream& istream::seekg(streamoff off, _seek_dir dir)
  135. {
  136.   streampos pos
  137.     = _IO_seekoff (_strbuf, off,
  138.            (_IO_seekflags)
  139.            ((int)dir | _IO_seek_not_out | _IO_seek_pos_ignored));
  140.   if (pos == streampos(EOF))
  141.     set(ios::badbit);
  142.   return *this;
  143. }
  144.  
  145. streampos istream::tellg()
  146. {
  147. #if 0
  148.     streampos pos = _strbuf->sseekoff(0, ios::cur, ios::in);
  149. #else
  150.     streampos pos
  151.       = _IO_seekoff (_strbuf, 0,
  152.              (_IO_seekflags)(_IO_seek_cur | _IO_seek_not_out));
  153. #endif
  154.     if (pos == streampos(EOF))
  155.     set(ios::badbit);
  156.     return pos;
  157. }
  158.  
  159. istream& istream::operator>>(char& c)
  160. {
  161.     if (ipfx0()) {
  162.     int ch = _strbuf->sbumpc();
  163.     if (ch == EOF)
  164.         set(ios::eofbit|ios::failbit);
  165.     else
  166.         c = (char)ch;
  167.     }
  168.     return *this;
  169. }
  170.  
  171. istream& istream::operator>>(char* ptr)
  172. {
  173.   register char *p = ptr;
  174.   int w = width(0);
  175.   if (ipfx0()) {
  176.     register streambuf* sb = _strbuf;
  177.     for (;;)
  178.       {
  179.     int ch = sb->sbumpc();
  180.     if (ch == EOF)
  181.       {
  182.         set(p == ptr ? (ios::eofbit|ios::failbit) : (ios::eofbit));
  183.         break;
  184.       }
  185.     else if (isspace(ch))
  186.       {
  187.         sb->sputbackc(ch);
  188.         break;
  189.       }
  190.     else if (w == 1)
  191.       {
  192.         set(ios::failbit);
  193.         sb->sputbackc(ch);
  194.         break;
  195.       }
  196.     else *p++ = ch;
  197.     w--;
  198.       }
  199.   }
  200.   *p = '\0';
  201.   return *this;
  202. }
  203.  
  204. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  205. #define LONGEST long long
  206. #else
  207. #define LONGEST long
  208. #endif
  209.  
  210. static int read_int(istream& stream, unsigned LONGEST& val, int& neg)
  211. {
  212.     if (!stream.ipfx0())
  213.       return 0;
  214.     register streambuf* sb = stream.rdbuf();
  215.     int base = 10;
  216.     int ndigits = 0;
  217.     register int ch = skip_ws(sb);
  218.     if (ch == EOF)
  219.     goto eof_fail;
  220.     neg = 0;
  221.     if (ch == '+') {
  222.     ch = skip_ws(sb);
  223.     }
  224.     else if (ch == '-') {
  225.     neg = 1;
  226.     ch = skip_ws(sb);
  227.     }
  228.     if (ch == EOF) goto eof_fail;
  229.     if (!(stream.flags() & ios::basefield)) {
  230.     if (ch == '0') {
  231.         ch = sb->sbumpc();
  232.         if (ch == EOF) {
  233.         val = 0;
  234.         return 1;
  235.         }
  236.         if (ch == 'x' || ch == 'X') {
  237.         base = 16;
  238.         ch = sb->sbumpc();
  239.         if (ch == EOF) goto eof_fail;
  240.         }
  241.         else {
  242.         sb->sputbackc(ch);
  243.         base = 8;
  244.         ch = '0';
  245.         }
  246.     }
  247.     }
  248.     else if ((stream.flags() & ios::basefield) == ios::hex)
  249.     base = 16;
  250.     else if ((stream.flags() & ios::basefield) == ios::oct)
  251.     base = 8;
  252.     val = 0;
  253.     for (;;) {
  254.     if (ch == EOF)
  255.         break;
  256.     int digit;
  257.     if (ch >= '0' && ch <= '9')
  258.         digit = ch - '0';
  259.     else if (ch >= 'A' && ch <= 'F')
  260.         digit = ch - 'A' + 10;
  261.     else if (ch >= 'a' && ch <= 'f')
  262.         digit = ch - 'a' + 10;
  263.     else
  264.         digit = 999;
  265.     if (digit >= base) {
  266.         sb->sputbackc(ch);
  267.         if (ndigits == 0)
  268.         goto fail;
  269.         else
  270.         return 1;
  271.     }
  272.     ndigits++;
  273.     val = base * val + digit;
  274.     ch = sb->sbumpc();
  275.     }
  276.     return 1;
  277.   fail:
  278.     stream.set(ios::failbit);
  279.     return 0;
  280.   eof_fail:
  281.     stream.set(ios::failbit|ios::eofbit);
  282.     return 0;
  283. }
  284.  
  285. #define READ_INT(TYPE) \
  286. istream& istream::operator>>(TYPE& i)\
  287. {\
  288.     unsigned LONGEST val; int neg;\
  289.     if (read_int(*this, val, neg)) {\
  290.     if (neg) val = -val;\
  291.     i = (TYPE)val;\
  292.     }\
  293.     return *this;\
  294. }
  295.  
  296. READ_INT(short)
  297. READ_INT(unsigned short)
  298. READ_INT(int)
  299. READ_INT(unsigned int)
  300. READ_INT(long)
  301. READ_INT(unsigned long)
  302. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  303. READ_INT(long long)
  304. READ_INT(unsigned long long)
  305. #endif
  306. #if _G_HAVE_BOOL
  307. READ_INT(bool)
  308. #endif
  309.  
  310. istream& istream::operator>>(double& x)
  311. {
  312.     if (ipfx0())
  313.     scan("%lg", &x);
  314.     return *this;
  315. }
  316.  
  317. istream& istream::operator>>(float& x)
  318. {
  319.     if (ipfx0())
  320.     scan("%g", &x);
  321.     return *this;
  322. }
  323.  
  324. istream& istream::operator>>(register streambuf* sbuf)
  325. {
  326.     if (ipfx0()) {
  327.     register streambuf* inbuf = rdbuf();
  328.     // FIXME: Should optimize!
  329.     for (;;) {
  330.         register int ch = inbuf->sbumpc();
  331.         if (ch == EOF) {
  332.         set(ios::eofbit);
  333.         break;
  334.         }
  335.         if (sbuf->sputc(ch) == EOF) {
  336.         set(ios::failbit);
  337.         break;
  338.         }
  339.     }
  340.     }
  341.     return *this;
  342. }
  343.  
  344. ostream& ostream::operator<<(char c)
  345. {
  346.     if (opfx()) {
  347. #if 1
  348.     // This is what the cfront implementation does.
  349.     if (_strbuf->sputc(c) == EOF)
  350.       goto failed;
  351. #else
  352.     // This is what cfront documentation and current ANSI drafts say.
  353.     int w = width(0);
  354.     char fill_char = fill();
  355.     register int padding = w > 0 ? w - 1 : 0;
  356.     register streambuf *sb = _strbuf;
  357.     if (!(flags() & ios::left) && padding) // Default adjustment.
  358.         if (_IO_padn(sb, fill_char, padding) < padding)
  359.           goto failed;
  360.     if (sb->sputc(c) == EOF)
  361.       goto failed;
  362.     if (flags() & ios::left && padding) // Left adjustment.
  363.         if (_IO_padn(sb, fill_char, padding) < padding)
  364.           goto failed;
  365. #endif
  366.     osfx();
  367.     }
  368.     return *this;
  369.   failed:
  370.     set(ios::badbit);
  371.     osfx();
  372.     return *this;
  373. }
  374.  
  375. /* Write VAL on STREAM.
  376.    If SIGN<0, val is the absolute value of a negative number.
  377.    If SIGN>0, val is a signed non-negative number.
  378.    If SIGN==0, val is unsigned. */
  379.  
  380. static void write_int(ostream& stream, unsigned LONGEST val, int sign)
  381. {
  382. #define WRITE_BUF_SIZE (10 + sizeof(unsigned LONGEST) * 3)
  383.     char buf[WRITE_BUF_SIZE];
  384.     register char *buf_ptr = buf+WRITE_BUF_SIZE; // End of buf.
  385.     char *show_base = "";
  386.     int show_base_len = 0;
  387.     int show_pos = 0; // If 1, print a '+'.
  388.  
  389.     // Now do the actual conversion, placin