home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs20.zoo / stdstrea.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  4.7 KB  |  147 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1992 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. #include <ioprivat.h>
  19. #include <g_config.h>
  20.  
  21. // The ANSI draft requires that operations on cin/cout/cerr can be
  22. // mixed with operations on stdin/stdout/stderr on a character by
  23. // character basis.  This normally requires that the streambuf's
  24. // used by cin/cout/cerr be stdiostreams.  However, if the stdio
  25. // implementation is the one that is built using this library,
  26. // then we don't need to, since in that case stdin/stdout/stderr
  27. // are identical to &__std_filebuf_0/&__std_filebuf_1/&__std_filebuf_2.
  28.  
  29. #ifdef _STDIO_USES_IOSTREAM
  30. #define USE_FILEBUF
  31. #endif
  32.  
  33. #if defined(NAMES_HAVE_UNDERSCORE) || defined(_G_NAMES_HAVE_UNDERSCORE)
  34. #define UNDERSCORE "_"
  35. #else
  36. #define UNDERSCORE ""
  37. #endif
  38.  
  39. #ifdef USE_FILEBUF
  40. #define CIN_SBUF __std_filebuf_0
  41. #define COUT_SBUF __std_filebuf_1
  42. #define CERR_SBUF __std_filebuf_2
  43. static int use_stdiobuf = 0;
  44. #else
  45. #define CIN_SBUF __stdin_stdiobuf
  46. #define COUT_SBUF __stdout_stdiobuf
  47. #define CERR_SBUF __stderr_stdiobuf
  48. static int use_stdiobuf = 1;
  49. #endif
  50.  
  51. struct _fake_filebuf;
  52. extern _fake_filebuf __std_filebuf_0, __std_filebuf_1, __std_filebuf_2;
  53. struct _fake_stdiobuf;
  54. extern _fake_stdiobuf __stdin_stdiobuf, __stdout_stdiobuf, __stderr_stdiobuf;
  55.  
  56. #define cin CIN
  57. #define cout COUT
  58. #define cerr CERR
  59. #define clog CLOG
  60. #include <iostream.h>
  61. #undef cin
  62. #undef cout
  63. #undef cerr
  64. #undef clog
  65.  
  66. #ifdef __GNUC__
  67. #define PAD 0 /* g++ allows 0-length arrays. */
  68. #else
  69. #define PAD 1
  70. #endif
  71. struct _fake_istream {
  72.     struct myfields {
  73. #ifdef __GNUC__
  74.     _ios_fields *vb; /* pointer to virtual base class ios */
  75.     _G_ssize_t _gcount;
  76. #else
  77.     /* This is supposedly correct for cfront. */
  78.     _G_ssize_t _gcount;
  79.     void *vptr;
  80.     _ios_fields *vb; /* pointer to virtual base class ios */
  81. #endif
  82.     } mine;
  83.     _ios_fields base;
  84.     char filler[sizeof(struct istream)-sizeof(struct _ios_fields)+PAD];
  85. };
  86. struct _fake_ostream {
  87.     struct myfields {
  88. #ifndef __GNUC__
  89.     void *vptr;
  90. #endif
  91.     _ios_fields *vb; /* pointer to virtual base class ios */
  92.     } mine;
  93.     _ios_fields base;
  94.     char filler[sizeof(struct ostream)-sizeof(struct _ios_fields)+PAD];
  95. };
  96.  
  97. #define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
  98.  (streambuf*)&SBUF, TIE, 0, ios::dont_close|ios::skipws|EXTRA_FLAGS, ' ',0,0,6
  99.  
  100. #ifdef __GNUC__
  101. #define OSTREAM_DEF(TYPE, NAME, SBUF, TIE, EXTRA_FLAGS) \
  102.   TYPE NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  103. #define ISTREAM_DEF(TYPE, NAME, SBUF, TIE, EXTRA_FLAGS) \
  104.   TYPE NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  105. #else
  106. #define OSTREAM_DEF(TYPE, NAME, SBUF, TIE, EXTRA_FLAGS) \
  107.   TYPE NAME = { {0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  108. #define ISTREAM_DEF(TYPE, NAME, SBUF, TIE, EXTRA_FLAGS) \
  109.   TYPE NAME = { {0, 0, &NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  110. #endif
  111.  
  112. OSTREAM_DEF(_fake_ostream, cout, COUT_SBUF, NULL, 0)
  113. OSTREAM_DEF(_fake_ostream, cerr, CERR_SBUF, (ostream*)&cout, ios::unitbuf)
  114. ISTREAM_DEF(_fake_istream, cin, CIN_SBUF,  (ostream*)&cout, 0)
  115.  
  116. /* Only for (partial) compatibility with AT&T's library. */
  117. OSTREAM_DEF(_fake_ostream, clog, CERR_SBUF, (ostream*)&cout, 0)
  118.  
  119. // Switches between using __std_filebuf_{0,1,2} and
  120. // __std{in,out,err}_stdiobuf for standard streams.  This is
  121. // normally not needed, but is provided for AT&T compatibility.
  122.  
  123. int ios::sync_with_stdio(int new_state)
  124. {
  125. #ifdef _STDIO_USES_IOSTREAM
  126.     // It is always synced.
  127.     return 0;
  128. #else
  129.     if (new_state == use_stdiobuf) // The usual case now.
  130.     return use_stdiobuf;
  131.     if (new_state) {
  132.     cout.base._strbuf = (streambuf*)&__stdout_stdiobuf;
  133.     cin.base._strbuf = (streambuf*)&__stdin_stdiobuf;
  134.     cerr.base._strbuf = (streambuf*)&__stderr_stdiobuf;
  135.     clog.base._strbuf = (streambuf*)&__stderr_stdiobuf;
  136.     } else {
  137.     cout.base._strbuf = (streambuf*)&__std_filebuf_1;
  138.     cin.base._strbuf = (streambuf*)&__std_filebuf_0;
  139.     cerr.base._strbuf = (streambuf*)&__std_filebuf_2;
  140.     clog.base._strbuf = (streambuf*)&__std_filebuf_2;
  141.     }
  142.     int old_state = use_stdiobuf;
  143.     use_stdiobuf = new_state;
  144.     return old_state;
  145. #endif
  146. }
  147.