home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs17.zoo / stdstrbu.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  3.7 KB  |  113 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 <stdio.h>
  20.  
  21. // This file defines the standard streambufs, corresponding to cin, cout, cerr.
  22. // We define two sets:
  23. //
  24. // __std_filebuf_0, __std_filebuf_1, __std_filebuf_2 are filebufs using
  25. // file descriptor 0/1/2.
  26. //
  27. // __stdin_stdiobuf, __stdout_stdiobuf, __stderr_stdiobuf are stdiostreams
  28. // pointing to stdin, stdout, stderr.
  29.  
  30.  
  31. // To avoid problems depending on constructor order (and for
  32. // efficiency) the standard streambufs (and streams) are
  33. // constructed statically using C-style '{ ... }' initializers.
  34. // Since you're not allowed to do this for structs that
  35. // have virtuals, we define fake streambuf and stream classes
  36. // that don't have any C++-isms, and initialize those.
  37. // To initialize the vtable field of the standard filebufs,
  38. // we use the expression 'vt_filebuf' which must evaluate to
  39. // (the address of) the virtual function table for the
  40. // filebuf class.
  41.  
  42. #if _G_NAMES_HAVE_UNDERSCORE
  43. #define UNDERSCORE "_"
  44. #else
  45. #define UNDERSCORE ""
  46. #endif
  47.  
  48. // First define the filebuf-based objects.
  49.  
  50. #if !defined(vt_filebuf)
  51. #ifndef __GNUG__
  52. // This works for cfront.
  53. #define vt_filebuf __vtbl__7filebuf
  54. extern char vt_filebuf[1];
  55. #elif _G_DOLLAR_IN_LABEL
  56. extern char vt_filebuf[1] asm(UNDERSCORE "_vt$filebuf");
  57. #else
  58. extern char vt_filebuf[1] asm(UNDERSCORE "_vt.filebuf");
  59. #endif
  60. #endif /* !defined(vt_filebuf) */
  61.  
  62. struct _fake_filebuf {
  63.     struct __streambuf s;
  64.     char* vtable;
  65.     struct __file_fields f;
  66. };
  67.  
  68. #define FILEBUF_LITERAL(CHAIN, FLAGS) \
  69.        { _IO_MAGIC+_S_LINKED+_S_IS_FILEBUF+_S_IS_BACKUPBUF+FLAGS, \
  70.      0, 0, 0, 0, 0, 0, 0, 0, CHAIN, 0, 0, 0, 0, 0}
  71.  
  72. #define DEF_FILEBUF(NAME, FD, CHAIN, FLAGS) \
  73.   _fake_filebuf NAME[1] = {{FILEBUF_LITERAL(CHAIN, FLAGS), vt_filebuf, {FD}}};
  74.  
  75. DEF_FILEBUF(__std_filebuf_0, 0, 0, _S_NO_WRITES);
  76. DEF_FILEBUF(__std_filebuf_1, 1, (streambuf*)__std_filebuf_0, _S_NO_READS);
  77. DEF_FILEBUF(__std_filebuf_2, 2, (streambuf*)__std_filebuf_1,
  78.         _S_NO_READS+_S_UNBUFFERED);
  79.  
  80. // Nest define the stdiobuf-bases objects.
  81.  
  82. #if !defined(vt_stdiobuf)
  83. #ifndef __GNUG__
  84. // This works for cfront.
  85. #define vt_stdiobuf __vtbl__7stdiobuf
  86. extern char vt_stdiobuf[1];
  87. #elif _G_DOLLAR_IN_LABEL
  88. extern char vt_stdiobuf[1] asm(UNDERSCORE "_vt$stdiobuf");
  89. #else
  90. extern char vt_stdiobuf[1] asm(UNDERSCORE "_vt.stdiobuf");
  91. #endif
  92. #endif /* !defined(vt_stdiobuf) */
  93.  
  94. struct _fake_stdiobuf {
  95.     struct __streambuf s;
  96.     char* vtable;
  97.     struct __file_fields f;
  98.     FILE *_f;
  99. };
  100.  
  101. #define DEF_STDIOBUF(NAME, FILE, FD, CHAIN, FLAGS) \
  102.     _fake_stdiobuf NAME[1] = {{ \
  103.      FILEBUF_LITERAL(CHAIN, (FLAGS)|_S_UNBUFFERED),\
  104.      vt_stdiobuf, {FD}, FILE}};
  105.  
  106. DEF_STDIOBUF(__stdin_stdiobuf, stdin, 0, (streambuf*)__std_filebuf_2,
  107.          _S_NO_WRITES);
  108. DEF_STDIOBUF(__stdout_stdiobuf, stdout, 1, (streambuf*)__stdin_stdiobuf,
  109.          _S_NO_READS);
  110. DEF_STDIOBUF(__stderr_stdiobuf, stderr, 2, (streambuf*)__stdout_stdiobuf,
  111.          _S_NO_READS);
  112. streambuf* streambuf::_list_all = (streambuf*)__stderr_stdiobuf;
  113.