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