home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / iostream.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  8KB  |  223 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  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
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef _IOSTREAM_H
  19. #ifdef __GNUG__
  20. #pragma interface
  21. #endif
  22. #define _IOSTREAM_H
  23.  
  24. #include <streambuf.h>
  25.  
  26. class istream; class ostream;
  27. typedef ios& (*__manip)(ios&);
  28. typedef istream& (*__imanip)(istream&);
  29. typedef ostream& (*__omanip)(ostream&);
  30.  
  31. extern istream& ws(istream& ins);
  32. extern ostream& flush(ostream& outs);
  33. extern ostream& endl(ostream& outs);
  34. extern ostream& ends(ostream& outs);
  35.  
  36. class ostream : virtual public ios
  37. {
  38.     // NOTE: If fields are changed, you must fix _fake_ostream in stdstreams.C!
  39.     void do_osfx();
  40.   public:
  41.     ostream() { }
  42.     ostream(streambuf* sb, ostream* tied=NULL);
  43.     int opfx() {
  44.     if (!good()) return 0; else { if (_tie) _tie->flush(); return 1;} }
  45.     void osfx() { if (flags() & (ios::unitbuf|ios::stdio))
  46.               do_osfx(); }
  47.     streambuf* ostreambuf() const { return _strbuf; }
  48.     ostream& flush();
  49.     ostream& put(char c) { _strbuf->sputc(c); return *this; }
  50.     ostream& put(unsigned char c) { return put((char)c); }
  51.  
  52.     ostream& write(const char *s, int n);
  53.     ostream& write(const unsigned char *s, int n) { return write((const char*)s, n);}
  54. #ifndef _G_BROKEN_SIGNED_CHAR
  55.     ostream& put(signed char c) { return put((char)c); }
  56.     ostream& write(const signed char *s, int n) { return write((const char*)s, n);}
  57. #endif
  58.     ostream& write(const void *s, int n) { return write((const char*)s, n);}
  59.     ostream& seekp(streampos);
  60.     ostream& seekp(streamoff, _seek_dir);
  61.     streampos tellp();
  62.     ostream& form(const char *format ...);
  63.     ostream& vform(const char *format, _G_va_list args);
  64.  
  65.     ostream& operator<<(char c);
  66.     ostream& operator<<(unsigned char c) { return (*this) << (char)c; }
  67. #ifndef _G_BROKEN_SIGNED_CHAR
  68.     ostream& operator<<(signed char c) { return (*this) << (char)c; }
  69. #endif
  70.     ostream& operator<<(const char *s);
  71.     ostream& operator<<(const unsigned char *s)
  72.     { return (*this) << (const char*)s; }
  73. #ifndef _G_BROKEN_SIGNED_CHAR
  74.     ostream& operator<<(const signed char *s)
  75.     { return (*this) << (const char*)s; }
  76. #endif
  77.     ostream& operator<<(void *p);
  78.     ostream& operator<<(int n);
  79.     ostream& operator<<(unsigned int n);
  80.     ostream& operator<<(long n);
  81.     ostream& operator<<(unsigned long n);
  82. #ifdef __GNUG__
  83.     ostream& operator<<(long long n);
  84.     ostream& operator<<(unsigned long long n);
  85. #endif
  86.     ostream& operator<<(short n) {return operator<<((int)n);}
  87.     ostream& operator<<(unsigned short n) {return operator<<((unsigned int)n);}
  88.     ostream& operator<<(double n);
  89.     ostream& operator<<(float n) { return operator<<((double)n); }
  90.     ostream& operator<<(__omanip func) { return (*func)(*this); }
  91.     ostream& operator<<(__manip func) {(*func)(*this); return *this;}
  92.     ostream& operator<<(streambuf*);
  93. };
  94.  
  95. class istream : virtual public ios
  96. {
  97.     // NOTE: If fields are changed, you must fix _fake_istream in stdstreams.C!
  98.     _G_ssize_t _gcount;
  99.  
  100.     int _skip_ws();
  101.   public:
  102.     istream() { _gcount = 0; }
  103.     istream(streambuf* sb, ostream*tied=NULL);
  104.     streambuf* istreambuf() const { return _strbuf; }
  105.     istream& get(char* ptr, int len, char delim = '\n');
  106.     istream& get(unsigned char* ptr, int len, char delim = '\n')
  107.     { return get((char*)ptr, len, delim); }
  108.     istream& get(char& c);
  109.     istream& get(unsigned char& c) { return get((char&)c); }
  110.     istream& getline(char* ptr, int len, char delim = '\n');
  111.     istream& getline(unsigned char* ptr, int len, char delim = '\n')
  112.     { return getline((char*)ptr, len, delim); }
  113. #ifndef _G_BROKEN_SIGNED_CHAR
  114.     istream& get(signed char& c)  { return get((char&)c); }
  115.     istream& get(signed char* ptr, int len, char delim = '\n')
  116.     { return get((char*)ptr, len, delim); }
  117.     istream& getline(signed char* ptr, int len, char delim = '\n')
  118.     { return getline((char*)ptr, len, delim); }
  119. #endif
  120.     istream& read(char *ptr, int n);
  121.     istream& read(unsigned char *ptr, int n) { return read((char*)ptr, n); }
  122. #ifndef _G_BROKEN_SIGNED_CHAR
  123.     istream& read(signed char *ptr, int n) { return read((char*)ptr, n); }
  124. #endif
  125.     istream& read(void *ptr, int n) { return read((char*)ptr, n); }
  126.     // Should get() and/or peek() set failbit and/or eofbit? FIXME!
  127.     istream& get(streambuf& sb, char delim = '\n');
  128.     istream& gets(char **s, char delim = '\n');
  129.     int ipfx(int need) {
  130.     if (!good()) { set(ios::failbit); return 0; }
  131.     if (_tie && (need == 0 || rdbuf()->in_avail() < need)) _tie->flush();
  132.     if (!need && (flags() & ios::skipws)) return _skip_ws();
  133.     return 1;
  134.     }
  135.     int ipfx0() { // Optimized version of ipfx(0).
  136.     if (!good()) { set(ios::failbit); return 0; }
  137.     if (_tie) _tie->flush();
  138.     if (flags() & ios::skipws) return _skip_ws();
  139.     return 1;
  140.     }
  141.     int ipfx1() { // Optimized version of ipfx(1).
  142.     if (!good()) { set(ios::failbit); return 0; }
  143.     if (_tie && rdbuf()->in_avail() == 0) _tie->flush();
  144.     return 1;
  145.     }
  146.     int get() { if (!ipfx1()) return EOF;
  147.         int ch = _strbuf->sbumpc();
  148.         if (ch == EOF) set(ios::eofbit);
  149.         return ch; }
  150.     int peek() { if (!ipfx1()) return EOF;
  151.         int ch = _strbuf->sgetc();
  152.         if (ch == EOF) set(ios::eofbit);
  153.         return ch; }
  154.     _G_ssize_t gcount() { return _gcount; }
  155.     istream& ignore(int n=1, int delim = EOF);
  156.     istream& seekg(streampos);
  157.     istream& seekg(streamoff, _seek_dir);
  158.     streampos tellg();
  159.     istream& putback(char ch) {
  160.     if (good() && _strbuf->sputbackc(ch) == EOF) clear(ios::badbit);
  161.     return *this;}
  162.     istream& unget() {
  163.     if (good() && _strbuf->sungetc() == EOF) clear(ios::badbit);
  164.     return *this;}
  165.     istream& scan(const char *format ...);
  166.     istream& vscan(const char *format, _G_va_list args);
  167. #ifdef _STREAM_COMPAT
  168.     istream& unget(char ch) { return putback(ch); }
  169.     int skip(int i);
  170. #endif
  171.  
  172.     istream& operator>>(char*);
  173.     istream& operator>>(unsigned char* p) { return operator>>((char*)p); }
  174. #ifndef _G_BROKEN_SIGNED_CHAR
  175.     istream& operator>>(signed char*p) { return operator>>((char*)p); }
  176. #endif
  177.     istream& operator>>(char& c);
  178.     istream& operator>>(unsigned char& c) {return operator>>((char&)c);}
  179. #ifndef _G_BROKEN_SIGNED_CHAR
  180.     istream& operator>>(signed char& c) {return operator>>((char&)c);}
  181. #endif
  182.     istream& operator>>(int&);
  183.     istream& operator>>(long&);
  184. #ifdef __GNUG__
  185.     istream& operator>>(long long&);
  186. #endif
  187.     istream& operator>>(short&);
  188.     istream& operator>>(unsigned int&);
  189.     istream& operator>>(unsigned long&);
  190. #ifdef __GNUG__
  191.     istream& operator>>(unsigned long long&);
  192. #endif
  193.     istream& operator>>(unsigned short&);
  194.     istream& operator>>(float&);
  195.     istream& operator>>(double&);
  196.     istream& operator>>( __manip func) {(*func)(*this); return *this;}
  197.     istream& operator>>(__imanip func) { return (*func)(*this); }
  198.     istream& operator>>(streambuf*);
  199. };
  200.  
  201.  
  202. class iostream : public istream, public ostream
  203. {
  204.     _G_ssize_t _gcount;
  205.   public:
  206.     iostream() { _gcount = 0; }
  207.     iostream(streambuf* sb, ostream*tied=NULL);
  208. };
  209.  
  210. extern istream cin;
  211. extern ostream cout, cerr, clog; // clog->rdbuf() == cerr->rdbuf()
  212.  
  213. struct Iostream_init { } ;  // Compatibility hack for AT&T library.
  214.  
  215. inline ios& dec(ios& i)
  216. { i.setf(ios::dec, ios::dec|ios::hex|ios::oct); return i; }
  217. inline ios& hex(ios& i)
  218. { i.setf(ios::hex, ios::dec|ios::hex|ios::oct); return i; }
  219. inline ios& oct(ios& i)
  220. { i.setf(ios::oct, ios::dec|ios::hex|ios::oct); return i; }
  221.  
  222. #endif /*!_IOSTREAM_H*/
  223.