home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libio / stdstreams.cc < prev    next >
C/C++ Source or Header  |  1995-06-15  |  5KB  |  150 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 this library; see the file COPYING.  If not, write to the Free
  17. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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. #include "libioP.h"
  28. #include "streambuf.h"
  29. #include <stdio.h>
  30.  
  31. // The ANSI draft requires that operations on cin/cout/cerr can be
  32. // mixed with operations on stdin/stdout/stderr on a character by
  33. // character basis.  This normally requires that the streambuf's
  34. // used by cin/cout/cerr be stdiostreams.  However, if the stdio
  35. // implementation is the one that is built using this library,
  36. // then we don't need to, since in that case stdin/stdout/stderr
  37. // are identical to _IO_stdin/_IO_stdout/_IO_stderr.
  38.  
  39. #include "libio.h"
  40.  
  41. #ifdef _STDIO_USES_IOSTREAM
  42. #define CIN_SBUF _IO_stdin_
  43. #define COUT_SBUF _IO_stdout_
  44. #define CERR_SBUF _IO_stderr_
  45. static int use_stdiobuf = 0;
  46. #else
  47. #define CIN_SBUF _IO_stdin_buf
  48. #define COUT_SBUF _IO_stdout_buf
  49. #define CERR_SBUF _IO_stderr_buf
  50. static int use_stdiobuf = 1;
  51. #endif
  52.  
  53. #define cin CIN
  54. #define cout COUT
  55. #define cerr CERR
  56. #define clog CLOG
  57. #include "iostream.h"
  58. #undef cin
  59. #undef cout
  60. #undef cerr
  61. #undef clog
  62.  
  63. #ifdef __GNUG__
  64. #define PAD 0 /* g++ allows 0-length arrays. */
  65. #else
  66. #define PAD 1
  67. #endif
  68. struct _fake_istream {
  69.     struct myfields {
  70. #ifdef __GNUC__
  71.     _ios_fields *vb; /* pointer to virtual base class ios */
  72.     _IO_ssize_t _gcount;
  73. #else
  74.     /* This is supposedly correct for cfront. */
  75.     _IO_ssize_t _gcount;
  76.     void *vptr;
  77.     _ios_fields *vb; /* pointer to virtual base class ios */
  78. #endif
  79.     } mine;
  80.     _ios_fields base;
  81.     char filler[sizeof(struct istream)-sizeof(struct _ios_fields)+PAD];
  82. };
  83. struct _fake_ostream {
  84.     struct myfields {
  85. #ifndef __GNUC__
  86.     void *vptr;
  87. #endif
  88.     _ios_fields *vb; /* pointer to virtual base class ios */
  89.     } mine;
  90.     _ios_fields base;
  91.     char filler[sizeof(struct ostream)-sizeof(struct _ios_fields)+PAD];
  92. };
  93.  
  94.  
  95. #ifdef _IO_NEW_STREAMS
  96. #define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
  97.  (streambuf*)&SBUF, TIE, 0, ios::skipws|ios::dec|EXTRA_FLAGS, ' ',0,0,6
  98. #else
  99. #define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
  100.  (streambuf*)&SBUF, TIE, 0, ios::dont_close|ios::dec|ios::skipws|EXTRA_FLAGS, ' ',0,0,6
  101. #endif
  102.  
  103. #ifdef __GNUC__
  104. #define OSTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
  105.   _fake_ostream NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  106. #define ISTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
  107.   _fake_istream NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  108. #else
  109. #define OSTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
  110.   _fake_ostream NAME = { {0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  111. #define ISTREAM_DEF(NAME, SBUF, TIE, EXTRA_FLAGS) \
  112.   _fake_istream NAME = {{0, 0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS)}};
  113. #endif
  114.  
  115. OSTREAM_DEF(cout, COUT_SBUF, NULL, 0)
  116. OSTREAM_DEF(cerr, CERR_SBUF,(ostream*)&cout, ios::unitbuf)
  117. ISTREAM_DEF(cin, CIN_SBUF,  (ostream*)&cout, 0)
  118.  
  119. /* Only for (partial) compatibility with AT&T's library. */
  120. OSTREAM_DEF(clog, CERR_SBUF, (ostream*)&cout, 0)
  121.  
  122. // Switches between using _IO_std{in,out,err} and __std{in,out,err}_buf
  123. // for standard streams.  This does not normally need to be called
  124. // explicitly, but is provided for AT&T compatibility.
  125.  
  126. int ios::sync_with_stdio(int new_state)
  127. {
  128. #ifdef _STDIO_USES_IOSTREAM
  129.     // It is always synced.
  130.     return 0;
  131. #else
  132.     if (new_state == use_stdiobuf) // The usual case now.
  133.     return use_stdiobuf;
  134.     if (new_state) {
  135.     cin.base._strbuf = (streambuf*)&_IO_stdin_buf;
  136.     cout.base._strbuf = (streambuf*)&_IO_stdout_buf;
  137.     cerr.base._strbuf = (streambuf*)&_IO_stderr_buf;
  138.     clog.base._strbuf = (streambuf*)&_IO_stderr_buf;
  139.     } else {
  140.     cin.base._strbuf = (streambuf*)_IO_stdin;
  141.     cout.base._strbuf = (streambuf*)_IO_stdout;
  142.     cerr.base._strbuf = (streambuf*)_IO_stderr;
  143.     clog.base._strbuf = (streambuf*)_IO_stderr;
  144.     }
  145.     int old_state = use_stdiobuf;
  146.     use_stdiobuf = new_state;
  147.     return old_state;
  148. #endif
  149. }
  150.