123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- // $XConsortium: iostream.C /main/4 1996/08/21 15:54:53 drk $
- #include "utility/c_iostream.h"
- #include <stdio.h>
- #include <ctype.h>
- istream&
- istream::seekg(streampos delta, ios::seek_dir d)
- {
- if ( fail() ) return *this;
- if ( d != ios::beg || sbuf -> seekg(delta) == EOF ) {
- set_bad();
- set_fail();
- }
- return *this;
- }
- int istream::get()
- {
- return sbuf -> get();
- }
- istream& istream::get(char& c)
- {
- if ( fail() ) return *this;
- int i = sbuf -> get();
- if ( i == EOF ) {
- set_fail();
- return *this;
- }
- c = i;
- return *this;
- }
- istream&
- istream::putback(char c)
- {
- sbuf -> putback(c);
- return *this;
- }
- istream& istream::getline(char* b, int lim, char delim)
- {
- return _getline(b, lim, delim, 1);
- }
- istream& istream::_getline(char* b, int lim, int delim, int fill_zero)
- {
- if ( fail() ) return *this;
- if ( sbuf -> examine() == EOF ) {
- set_fail();
- return *this;
- }
- sbuf -> clear_gcount();
- int i;
- int count;
- for ( count = 0 ; count < lim-1; count++ ) {
- i = sbuf -> get();
- if ( i == EOF || i == delim ) {
- //fprintf(stderr, "prematual break in _getline(): i=%d, count = %d\n", i, count);
- break;
- }
- b[count] = char(i);
- }
- if ( fill_zero )
- b[count] = 0;
- return *this;
- }
- istream&
- istream::read(char* s, int n)
- {
- return _getline(s, n+1, EOF, 0);
- }
- int
- istream::gcount()
- {
- return sbuf -> gcount();
- }
- istream& istream::operator>>(char& c)
- {
- int x;
- if ( (x=sbuf->examine()) == EOF ) return *this;
- c = (char)x;
- sbuf -> get();
- return *this;
- }
- int istream::eatw()
- {
- if ( fail() ) return EOF;
- int c = sbuf->examine();
- if (c == EOF) set_fail();
-
- while (isspace(c) && c != EOF) {
- sbuf->get();
- c = sbuf->examine();
- }
- if ( c == EOF ) set_fail();
- return c;
- }
- istream& istream::operator>>(char* s)
- {
- int c;
- if ( (c=eatw()) == EOF ) return *this;
- do {
- *s++ = c;
- sbuf->get();
- c = sbuf -> examine();
- } while (!isspace(c) && c != EOF) ;
-
- *s = '\0';
- if (c == EOF) {
- set_fail();
- }
- return *this;
- }
- istream&
- istream::operator>>(unsigned short& x)
- {
- unsigned int l = 0;
- *this >> l;
- x = (unsigned short)l;
- return *this;
- }
- istream&
- istream::operator>>(unsigned int& n)
- {
- int x;
- if ( (x=eatw()) == EOF ) return *this;
- n = 0;
- if ( isdigit(x) ) {
- do {
- sbuf -> get();
- n = n*10+x-'0';
- } while (isdigit(x=sbuf->examine()));
- } else {
- set_fail();
- return *this;
- }
- return *this;
- }
- istream&
- istream::operator>>(int& x)
- {
- long l = 0;
- *this >> l;
- x = int(l);
- return *this;
- }
- istream&
- istream::operator>>(long& n)
- {
- int x;
- if ( (x=eatw()) == EOF ) return *this;
- int sign = '+';
- switch (x) {
- case '+':
- case '-':
- sign = x;
- sbuf -> get();
- x = sbuf -> examine();
- break;
- case EOF:
- set_fail();
- return *this;
- }
- n = 0;
- if ( isdigit(x) ) {
- do {
- sbuf -> get();
- n = n*10+x-'0';
- } while (isdigit(x=sbuf->examine()));
- if (sign=='-')
- n = -n;
- } else {
- set_fail();
- return *this;
- }
- return *this;
- }
- /////////////////////
- ostream&
- ostream::operator<<(void* a)
- {
- if ( fail() ) return *this;
- long x = (long)a;
- *this << x;
- return *this;
- }
- ostream&
- ostream::operator<<(const char* str)
- {
- if ( str == 0 ) {
- set_bad();
- return *this;
- }
- while (*str) {
- if (sbuf->put(*str++) == EOF) {
- set_fail();
- break;
- }
- }
- return *this;
- }
- ostream&
- ostream::operator<<(char c)
- {
- if ( fail() ) return *this;
- sbuf -> put(c);
- return *this;
- }
- ostream&
- ostream::operator<<(int i)
- {
- long x = i;
- *this << x;
- return *this;
- }
- ostream&
- ostream::operator<<(unsigned int l)
- {
- long x = l;
- *this << x;
- return *this;
- }
- ostream&
- ostream::operator<<(long x)
- {
- if ( fail() ) return *this;
- char buf[32];
- char *p = buf;
- if (x < 0) {
- sbuf->put('-');
- x = -x;
- }
- do {
- *p++ = '0' + char(x%10);
- x /= 10;
- } while (x > 0);
- do {
- if (sbuf->put(*--p) == EOF) {
- set_fail();
- break;
- }
- } while (p != buf);
- return *this;
- }
- ostream&
- ostream::operator<< (ostream& (*f)(ostream&))
- {
- return (*f)(*this) ;
- }
- ostream& ostream::put(char c)
- {
- if ( fail() ) return *this;
- sbuf -> put(c);
- return *this;
- }
- ostream& ostream::flush()
- {
- if ( fail() ) return *this;
- sbuf -> flush();
- return *this;
- }
- ostream&
- ostream::write(const char* s, int n)
- {
- for ( int i=0; i<n; i++ ) {
- if ( sbuf->put(s[i]) == EOF )
- break;
- }
- return *this;
- }
- ostream& endl(ostream& out)
- {
- return out << '\n';
- }
- istream::istream(streambuf* sb) : ios(sb)
- {
- sbuf = sb;
- }
- ostream::ostream(streambuf* sb) : ios(sb)
- {
- sbuf = sb;
- }
- iostream::iostream(streambuf* sb) : istream(sb), ostream(sb)
- {
- sbuf = sb;
- }
|