home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / gpplib22.zoo / iosrc / iostream.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-12  |  17.7 KB  |  807 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) : ios(sb, tied)
  44. {
  45.     _flags |= ios::dont_close;
  46.     _gcount = 0;
  47. }
  48.  
  49. int skip_ws(streambuf* sb)
  50. {
  51.     int ch;
  52.     for (;;) {
  53.     ch = sb->sbumpc();
  54.     if (ch == EOF || !isspace(ch))
  55.         return ch;
  56.     }
  57. }
  58.  
  59. istream& istream::get(char& c)
  60. {
  61.     if (ipfx1()) {
  62.     int ch = _strbuf->sbumpc();
  63.     if (ch == EOF) {
  64.       set(ios::eofbit|ios::failbit);
  65.       _gcount = 0;
  66.     }
  67.     else {
  68.       c = (char)ch;
  69.       _gcount = 1;
  70.     }
  71.     }
  72.     return *this;
  73. }
  74.  
  75. int istream::peek()
  76. {
  77.   if (!good())
  78.     return EOF;
  79.   if (_tie && rdbuf()->in_avail() == 0)
  80.     _tie->flush();
  81.   int ch = _strbuf->sgetc();
  82.   if (ch == EOF)
  83.     set(ios::eofbit);
  84.   return ch;
  85. }
  86.  
  87. istream& istream::ignore(int n /* = 1 */, int delim /* = EOF */)
  88. {
  89.     if (ipfx1()) {
  90.     register streambuf* sb = _strbuf;
  91.     if (delim == EOF) {
  92.         _gcount = sb->ignore(n);
  93.         return *this;
  94.     }
  95.     _gcount = 0;
  96.     for (;;) {
  97. #if 0
  98.         if (n != MAXINT) // FIXME
  99. #endif
  100.         if (--n < 0)
  101.         break;
  102.         int ch = sb->sbumpc();
  103.         if (ch == EOF) {
  104.         set(ios::eofbit|ios::failbit);
  105.         break;
  106.         }
  107.         _gcount++;
  108.         if (ch == delim)
  109.         break;
  110.     }
  111.     }
  112.     return *this;
  113. }
  114.  
  115. istream& istream::read(char *s, int n)
  116. {
  117.     if (ipfx1()) {
  118.     _gcount = _strbuf->sgetn(s, n);
  119.     if (_gcount != n)
  120.         set(ios::failbit);
  121.     }
  122.     return *this;
  123. }
  124.  
  125. istream& istream::seekg(streampos pos)
  126. {
  127.     pos = _strbuf->sseekpos(pos, ios::in);
  128.     if (pos == streampos(EOF))
  129.     set(ios::badbit);
  130.     return *this;
  131. }
  132.  
  133. istream& istream::seekg(streamoff off, _seek_dir dir)
  134. {
  135.   streampos pos
  136.     = _IO_seekoff (_strbuf, off,
  137.            (_IO_seekflags)
  138.            ((int)dir | _IO_seek_not_out | _IO_seek_pos_ignored));
  139.   if (pos == streampos(EOF))
  140.     set(ios::badbit);
  141.   return *this;
  142. }
  143.  
  144. streampos istream::tellg()
  145. {
  146. #if 0
  147.     streampos pos = _strbuf->sseekoff(0, ios::cur, ios::in);
  148. #else
  149.     streampos pos
  150.       = _IO_seekoff (_strbuf, 0,
  151.              (_IO_seekflags)(_IO_seek_cur | _IO_seek_not_out));
  152. #endif
  153.     if (pos == streampos(EOF))
  154.     set(ios::badbit);
  155.     return pos;
  156. }
  157.  
  158. istream& istream::operator>>(char& c)
  159. {
  160.     if (ipfx0()) {
  161.     int ch = _strbuf->sbumpc();
  162.     if (ch == EOF)
  163.         set(ios::eofbit|ios::failbit);
  164.     else
  165.         c = (char)ch;
  166.     }
  167.     return *this;
  168. }
  169.  
  170. istream& istream::operator>>(char* ptr)
  171. {
  172.   register char *p = ptr;
  173.   int w = width(0);
  174.   if (ipfx0()) {
  175.     register streambuf* sb = _strbuf;
  176.     for (;;)
  177.       {
  178.     int ch = sb->sbumpc();
  179.     if (ch == EOF)
  180.       {
  181.         set(p == ptr ? (ios::eofbit|ios::failbit) : (ios::eofbit));
  182.         break;
  183.       }
  184.     else if (isspace(ch))
  185.       {
  186.         sb->sputbackc(ch);
  187.         break;
  188.       }
  189.     else if (w == 1)
  190.       {
  191.         set(ios::failbit);
  192.         sb->sputbackc(ch);
  193.         break;
  194.       }
  195.     else *p++ = ch;
  196.     w--;
  197.       }
  198.   }
  199.   *p = '\0';
  200.   return *this;
  201. }
  202.  
  203. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  204. #define LONGEST long long
  205. #else
  206. #define LONGEST long
  207. #endif
  208.  
  209. static int read_int(istream& stream, unsigned LONGEST& val, int& neg)
  210. {
  211.     if (!stream.ipfx0())
  212.       return 0;
  213.     register streambuf* sb = stream.rdbuf();
  214.     int base = 10;
  215.     int ndigits = 0;
  216.     register int ch = skip_ws(sb);
  217.     if (ch == EOF)
  218.     goto eof_fail;
  219.     neg = 0;
  220.     if (ch == '+') {
  221.     ch = skip_ws(sb);
  222.     }
  223.     else if (ch == '-') {
  224.     neg = 1;
  225.     ch = skip_ws(sb);
  226.     }
  227.     if (ch == EOF) goto eof_fail;
  228.     if (!(stream.flags() & ios::basefield)) {
  229.     if (ch == '0') {
  230.         ch = sb->sbumpc();
  231.         if (ch == EOF) {
  232.         val = 0;
  233.         return 1;
  234.         }
  235.         if (ch == 'x' || ch == 'X') {
  236.         base = 16;
  237.         ch = sb->sbumpc();
  238.         if (ch == EOF) goto eof_fail;
  239.         }
  240.         else {
  241.         sb->sputbackc(ch);
  242.         base = 8;
  243.         ch = '0';
  244.         }
  245.     }
  246.     }
  247.     else if ((stream.flags() & ios::basefield) == ios::hex)
  248.     base = 16;
  249.     else if ((stream.flags() & ios::basefield) == ios::oct)
  250.     base = 8;
  251.     val = 0;
  252.     for (;;) {
  253.     if (ch == EOF)
  254.         break;
  255.     int digit;
  256.     if (ch >= '0' && ch <= '9')
  257.         digit = ch - '0';
  258.     else if (ch >= 'A' && ch <= 'F')
  259.         digit = ch - 'A' + 10;
  260.     else if (ch >= 'a' && ch <= 'f')
  261.         digit = ch - 'a' + 10;
  262.     else
  263.         digit = 999;
  264.     if (digit >= base) {
  265.         sb->sputbackc(ch);
  266.         if (ndigits == 0)
  267.         goto fail;
  268.         else
  269.         return 1;
  270.     }
  271.     ndigits++;
  272.     val = base * val + digit;
  273.     ch = sb->sbumpc();
  274.     }
  275.     return 1;
  276.   fail:
  277.     stream.set(ios::failbit);
  278.     return 0;
  279.   eof_fail:
  280.     stream.set(ios::failbit|ios::eofbit);
  281.     return 0;
  282. }
  283.  
  284. #define READ_INT(TYPE) \
  285. istream& istream::operator>>(TYPE& i)\
  286. {\
  287.     unsigned LONGEST val; int neg;\
  288.     if (read_int(*this, val, neg)) {\
  289.     if (neg) val = -val;\
  290.     i = (TYPE)val;\
  291.     }\
  292.     return *this;\
  293. }
  294.  
  295. READ_INT(short)
  296. READ_INT(unsigned short)
  297. READ_INT(int)
  298. READ_INT(unsigned int)
  299. READ_INT(long)
  300. READ_INT(unsigned long)
  301. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  302. READ_INT(long long)
  303. READ_INT(unsigned long long)
  304. #endif
  305.  
  306. istream& istream::operator>>(double& x)
  307. {
  308.     if (ipfx0())
  309.     scan("%lg", &x);
  310.     return *this;
  311. }
  312.  
  313. istream& istream::operator>>(float& x)
  314. {
  315.     if (ipfx0())
  316.     scan("%g", &x);
  317.     return *this;
  318. }
  319.  
  320. istream& istream::operator>>(register streambuf* sbuf)
  321. {
  322.     if (ipfx0()) {
  323.     register streambuf* inbuf = rdbuf();
  324.     // FIXME: Should optimize!
  325.     for (;;) {
  326.         register int ch = inbuf->sbumpc();
  327.         if (ch == EOF) {
  328.         set(ios::eofbit);
  329.         break;
  330.         }
  331.         if (sbuf->sputc(ch) == EOF) {
  332.         set(ios::failbit);
  333.         break;
  334.         }
  335.     }
  336.     }
  337.     return *this;
  338. }
  339.  
  340. ostream& ostream::operator<<(char c)
  341. {
  342.     if (opfx()) {
  343. #if 1
  344.     // This is what the cfront implementation does.
  345.     if (_strbuf->sputc(c) == EOF)
  346.       goto failed;
  347. #else
  348.     // This is what cfront documentation and current ANSI drafts say.
  349.     int w = width(0);
  350.     char fill_char = fill();
  351.     register int padding = w > 0 ? w - 1 : 0;
  352.     register streambuf *sb = _strbuf;
  353.     if (!(flags() & ios::left) && padding) // Default adjustment.
  354.         if (_IO_padn(sb, fill_char, padding) < padding)
  355.           goto failed;
  356.     if (sb->sputc(c) == EOF)
  357.       goto failed;
  358.     if (flags() & ios::left && padding) // Left adjustment.
  359.         if (_IO_padn(sb, fill_char, padding) < padding)
  360.           goto failed;
  361. #endif
  362.     osfx();
  363.     }
  364.     return *this;
  365.   failed:
  366.     set(ios::badbit);
  367.     osfx();
  368.     return *this;
  369. }
  370.  
  371. /* Write VAL on STREAM.
  372.    If SIGN<0, val is the absolute value of a negative number.
  373.    If SIGN>0, val is a signed non-negative number.
  374.    If SIGN==0, val is unsigned. */
  375.  
  376. static void write_int(ostream& stream, unsigned LONGEST val, int sign)
  377. {
  378. #define WRITE_BUF_SIZE (10 + sizeof(unsigned LONGEST) * 3)
  379.     char buf[WRITE_BUF_SIZE];
  380.     register char *buf_ptr = buf+WRITE_BUF_SIZE; // End of buf.
  381.     char *show_base = "";
  382.     int show_base_len = 0;
  383.     int show_pos = 0; // If 1, print a '+'.
  384.  
  385.     // Now do the actual conversion, placing the result at the *end* of buf.
  386.     // Note that we use separate code for decimal, octal, and hex,
  387.     // so we can divide by optimizable constants.
  388.     if ((stream.flags() & ios::basefield) == ios::oct) { // Octal
  389.     do {
  390.         *--buf_ptr = (val & 7) + '0';
  391.         val = val >> 3;
  392.     } while (val != 0);
  393.     if ((stream.flags() & ios::showbase) && (val != 0))
  394.         *--buf_ptr = '0';
  395.     }
  396.     else if ((stream.flags() & ios::basefield) == ios::hex) { // Hex
  397.     char *xdigs = (stream.flags() & ios::uppercase) ? "0123456789ABCDEF0X"
  398.         : "0123456789abcdef0x";
  399.     do {
  400.         *--buf_ptr = xdigs[val & 15];
  401.         val = val >> 4;
  402.     } while (val != 0);
  403.     if ((stream.flags() & ios::showbase) && (val != 0)) {
  404.         show_base = xdigs + 16; // Either "0X" or "0x".
  405.         show_base_len = 2;
  406.     }
  407.     }
  408.     else { // Decimal
  409. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  410.     // Optimization:  Only use long long when we need to.
  411.     while (val > UINT_MAX) {
  412.         *--buf_ptr = (val % 10) + '0';
  413.         val /= 10;
  414.     }
  415.     // Use more efficient (int) arithmetic for the rest.
  416.     register unsigned int ival = (unsigned int)val;
  417. #else
  418.     register unsigned LONGEST ival = val;
  419. #endif
  420.     do {
  421.         *--buf_ptr = (ival % 10) + '0';
  422.         ival /= 10;
  423.     } while (ival != 0);
  424.     if (sign > 0 && (stream.flags() & ios::showpos))
  425.         show_pos=1;
  426.     }
  427.  
  428.     int buf_len = buf+WRITE_BUF_SIZE - buf_ptr;
  429.     int w = stream.width(0);
  430.  
  431.     // Calculate padding.
  432.     int len = buf_len+show_pos;
  433.     if (sign < 0) len++;
  434.     len += show_base_len;
  435.     int padding = len > w ? 0 : w - len;
  436.  
  437.     // Do actual output.
  438.     register streambuf* sbuf = stream.rdbuf();
  439.     ios::fmtflags pad_kind =
  440.     stream.flags() & (ios::left|ios::right|ios::internal);
  441.     char fill_char = stream.fill();
  442.     if (padding > 0
  443.     && pad_kind != (ios::fmtflags)ios::left
  444.     && pad_kind != (ios::fmtflags)ios::internal) // Default (right) adjust.
  445.     if (_IO_padn(sbuf, fill_char, padding) < padding)
  446.       goto failed;
  447.     if (sign < 0 || show_pos)
  448.       {
  449.     char ch = sign < 0 ? '-' : '+';
  450.     if (sbuf->sputc(ch) < 0)
  451.       goto failed;
  452.       }
  453.     if (show_base_len)
  454.     if (_IO_sputn(sbuf, show_base, show_base_len) <= 0)
  455.       goto failed;
  456.     if (pad_kind == (ios::fmtflags)ios::internal && padding > 0)
  457.       if (_IO_padn(sbuf, fill_char, padding) < padding)
  458.     goto failed;
  459.     if (_IO_sputn (sbuf, buf_ptr, buf_len) != buf_len)
  460.       goto failed;
  461.     if (pad_kind == (ios::fmtflags)ios::left && padding > 0) // Left adjustment
  462.       if (_IO_padn(sbuf, fill_char, padding) < padding)
  463.     goto failed;
  464.     stream.osfx();
  465.     return;
  466.   failed:
  467.     stream.set(ios::badbit);
  468.     stream.osfx();
  469. }
  470.  
  471. ostream& ostream::operator<<(int n)
  472. {
  473.     if (opfx()) {
  474.     int sign = 1;
  475.     if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  476.         n = -n, sign = -1;
  477.     write_int(*this, n, sign);
  478.     }
  479.     return *this;
  480. }
  481.  
  482. ostream& ostream::operator<<(unsigned int n)
  483. {
  484.     if (opfx())
  485.     write_int(*this, n, 0);
  486.     return *this;
  487. }
  488.  
  489.  
  490. ostream& ostream::operator<<(long n)
  491. {
  492.     if (opfx()) {
  493.     int sign = 1;
  494.     unsigned int abs_n = (unsigned)n;
  495.     if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  496.         abs_n = -((unsigned)n), sign = -1;
  497.     write_int(*this, abs_n, sign);
  498.     }
  499.     return *this;
  500. }
  501.  
  502. ostream& ostream::operator<<(unsigned long n)
  503. {
  504.     if (opfx())
  505.     write_int(*this, n, 0);
  506.     return *this;
  507. }
  508.  
  509. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  510. ostream& ostream::operator<<(long long n)
  511. {
  512.     if (opfx()) {
  513.     int sign = 1;
  514.     unsigned long long abs_n = (unsigned long long)n;
  515.     if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  516.         abs_n = -((unsigned long long)n), sign = -1;
  517.     write_int(*this, abs_n, sign);
  518.     }
  519.     return *this;
  520. }
  521.  
  522.  
  523. ostream& ostream::operator<<(unsigned long long n)
  524. {
  525.     if (opfx())
  526.     write_int(*this, n, 0);
  527.     return *this;
  528. }
  529. #endif /*__GNUC__*/
  530.  
  531. ostream& ostream::operator<<(double n)
  532. {
  533.     if (opfx()) {
  534.     // Uses __cvt_double (renamed from static cvt), in Chris Torek's
  535.     // stdio implementation.  The setup code uses the same logic
  536.     // as in __vsbprintf.C (also based on Torek's code).
  537.     int format_char;
  538. #if 0
  539.     if (flags() ios::showpos) sign = '+';
  540. #endif
  541.     if ((flags() & ios::floatfield) == ios::fixed)
  542.         format_char = 'f';
  543.     else if ((flags() & ios::floatfield) == ios::scientific)
  544.         format_char = flags() & ios::uppercase ? 'E' : 'e';
  545.     else
  546.         format_char = flags() & ios::uppercase ? 'G' : 'g';
  547.  
  548.     int fpprec = 0; // 'Extra' (suppressed) floating precision.
  549.     int prec = precision();
  550.     if (prec > MAXFRACT) {
  551.         if (flags() & (ios::fixed|ios::scientific) & ios::showpos)
  552.         fpprec = prec - MAXFRACT;
  553.         prec = MAXFRACT;
  554.     }
  555.     else if (prec <= 0 && !(flags() & ios::fixed))
  556.       prec = 6; /* default */
  557.  
  558.     // Do actual conversion.
  559. #ifdef USE_DTOA
  560.     if (_IO_outfloat(n, rdbuf(), format_char, width(0),
  561.                prec, flags(), 0, fill()) < 0)
  562.         set(ios::badbit|ios::failbit); // ??
  563. #else
  564.     int negative;
  565.     char buf[BUF];
  566.     int sign = '\0';
  567.     char *cp = buf;
  568.     *cp = 0;
  569.     int size = __cvt_double(n, prec,
  570.                 flags() & ios::showpoint ? 0x80 : 0,
  571.                 &negative,
  572.                 format_char, cp, buf + sizeof(buf));
  573.     if (negative) sign = '-';
  574.     if (*cp == 0)
  575.         cp++;
  576.  
  577.     // Calculate padding.
  578.     int fieldsize = size + fpprec;
  579.     if (sign) fieldsize++;
  580.     int padding = 0;
  581.     int w = width(0);
  582.     if (fieldsize < w)
  583.         padding = w - fieldsize;
  584.  
  585.     // Do actual output.
  586.     register streambuf* sbuf = rdbuf();
  587.     register i;
  588.     char fill_char = fill();
  589.     ios::fmtflags pad_kind =
  590.         flags() & (ios::left|ios::right|ios::internal);
  591.     if (pad_kind != (ios::fmtflags)ios::left // Default (right) adjust.
  592.         && pad_kind != (ios::fmtflags)ios::internal)
  593.         for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);
  594.     if (sign)
  595.         sbuf->sputc(sign);
  596.     if (pad_kind == (ios::fmtflags)ios::internal)
  597.         for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);
  598.     
  599.     // Emit the actual concented field, followed by extra zeros.
  600.     _IO_sputn (sbuf, cp, size);
  601.     for (i = fpprec; --i >= 0; ) sbuf->sputc('0');
  602.  
  603.     if (pad_kind == (ios::fmtflags)ios::left) // Left adjustment
  604.         for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);
  605. #endif
  606.     osfx();
  607.     }
  608.     return *this;
  609. }
  610.  
  611. ostream& ostream::operator<<(const char *s)
  612. {
  613.   if (opfx())
  614.     {
  615.       if (s == NULL)
  616.     s = "(null)";
  617.       int len = strlen(s);
  618.       int w = width(0);
  619. // FIXME: Should we: if (w && len>w) len = w;
  620.       char fill_char = fill();
  621.       register streambuf *sbuf = rdbuf();
  622.       register int padding = w > len ? w - len : 0;
  623.       if (!(flags() & ios::left) && padding > 0) // Default adjustment.
  624.     if (_IO_padn(sbuf, fill_char, padding) != padding)
  625.       goto failed;
  626.       if (_IO_sputn (sbuf, s, len) != len)
  627.     goto failed;
  628.       if (flags() & ios::left && padding > 0) // Left adjustment.
  629.     if (_IO_padn(sbuf, fill_char, padding) != padding)
  630.       goto failed;
  631.       osfx();
  632.     }
  633.   return *this;
  634.  failed:
  635.   set(ios::badbit);
  636.   osfx();
  637.   return *this;
  638. }
  639.  
  640. #if 0
  641. ostream& ostream::operator<<(const void *p)
  642. { Is in osform.cc, to avoid pulling in all of _IO_vfprintf by this file. */ }
  643. #endif
  644.  
  645. ostream& ostream::operator<<(register streambuf* sbuf)
  646. {
  647.   if (opfx())
  648.     {
  649.       char buffer[_IO_BUFSIZ];
  650.       register streambuf* outbuf = _strbuf;
  651.       for (;;)
  652.     {
  653.       _IO_size_t count = _IO_sgetn(sbuf, buffer, _IO_BUFSIZ);
  654.       if (count <= 0)
  655.         break;
  656.       if (_IO_sputn(outbuf, buffer, count) != count)
  657.         {
  658.           set(ios::badbit);
  659.           break;
  660.         }
  661.     }
  662.       osfx();
  663.     }
  664.   return *this;
  665. }
  666.  
  667. ostream::ostream(streambuf* sb, ostream* tied) : ios(sb, tied)
  668. {
  669.     _flags |= ios::dont_close;
  670. }
  671.  
  672. ostream& ostream::seekp(streampos pos)
  673. {
  674.     pos = _strbuf->sseekpos(pos, ios::out);
  675.     if (pos == streampos(EOF))
  676.     set(ios::badbit);
  677.     return *this;
  678. }
  679.  
  680. ostream& ostream::seekp(streamoff off, _seek_dir dir)
  681. {
  682.   streampos pos
  683.     = _IO_seekoff (_strbuf, off,
  684.            (_IO_seekflags)
  685.            ((int)dir | _IO_seek_not_in | _IO_seek_pos_ignored));
  686.   if (pos == streampos(EOF))
  687.     set(ios::badbit);
  688.   return *this;
  689. }
  690.  
  691. streampos ostream::tellp()
  692. {
  693. #if 1
  694.     streampos pos
  695.       = _IO_seekoff (_strbuf, 0,
  696.              (_IO_seekflags)(_IO_seek_cur | _IO_seek_not_in));
  697. #else
  698.     streampos pos = _strbuf->sseekoff(0, ios::cur, ios::out);
  699. #endif
  700.     if (pos == streampos(EOF))
  701.     set(ios::badbit);
  702.     return pos;
  703. }
  704.  
  705. ostream& ostream::flush()
  706. {
  707.     if (_strbuf->_jumps->__sync(_strbuf))
  708.     set(ios::badbit);
  709.     return *this;
  710. }
  711.  
  712. ostream& flush(ostream& outs)
  713. {
  714.   return outs.flush();
  715. }
  716.  
  717. istream& ws(istream& ins)
  718. {
  719.     if (ins.ipfx1()) {
  720.     int ch = skip_ws(ins._strbuf);
  721.     if (ch == EOF)
  722.         ins.set(ios::eofbit);
  723.     else
  724.         ins._strbuf->sputbackc(ch);
  725.     }
  726.     return ins;
  727. }
  728.  
  729. // Skip white-space.  Return 0 on failure (EOF), or 1 on success.
  730. // Differs from ws() manipulator in that failbit is set on EOF.
  731. // Called by ipfx() and ipfx0() if needed.
  732.  
  733. int istream::_skip_ws()
  734. {
  735.     int ch = skip_ws(_strbuf);
  736.     if (ch == EOF) {
  737.     set(ios::eofbit|ios::failbit);
  738.     return 0;
  739.     }
  740.     else {
  741.     _strbuf->sputbackc(ch);
  742.     return 1;
  743.     }
  744. }
  745.  
  746. ostream& ends(ostream& outs)
  747. {
  748.     outs.put('\0');
  749.     return outs;
  750. }
  751.  
  752. ostream& endl(ostream& outs)
  753. {
  754.     return flush(outs.put('\n'));
  755. }
  756.  
  757. ostream& ostream::write(const char *s, int n)
  758. {
  759.     if (opfx()) {
  760.     if (_IO_sputn(_strbuf, s, n) != n)
  761.         set(ios::failbit);
  762.     }
  763.     return *this;
  764. }
  765.  
  766. void ostream::do_osfx()
  767. {
  768.     if (flags() & ios::unitbuf)
  769.     flush();
  770.     if (flags() & ios::stdio) {
  771.     fflush(stdout);
  772.     fflush(stderr);
  773.     }
  774. }
  775.  
  776. iostream::iostream(streambuf* sb, ostream* tied) : ios(sb, tied)
  777. {
  778.   _flags |= ios::dont_close;
  779. }
  780.  
  781. // NOTE: extension for compatibility with old libg++.
  782. // Not really compatible with fistream::close().
  783. #ifdef _STREAM_COMPAT
  784. void ios::close()
  785. {
  786.   if (!(_flags & (unsigned int)ios::dont_close))
  787.     delete rdbuf();
  788.   else if (_strbuf->_flags & _IO_IS_FILEBUF)
  789.     ((struct filebuf*)rdbuf())->close();
  790.   else if (_strbuf != NULL)
  791.     rdbuf()->sync();
  792.   _flags |= ios::dont_close;
  793.   _strbuf = NULL;
  794.   _state = badbit;
  795. }
  796.  
  797. int istream::skip(int i)
  798. {
  799.     int old = (_flags & ios::skipws) != 0;
  800.     if (i)
  801.     _flags |= ios::skipws;
  802.     else
  803.     _flags &= ~ios::skipws;
  804.     return old;
  805. }
  806. #endif
  807.