home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / iostream (.txt) < prev    next >
GNU Info File  |  1993-06-12  |  12KB  |  252 lines

  1. This is Info file iostream, produced by Makeinfo-1.47 from the input
  2. file iostream.tex.
  3. START-INFO-DIR-ENTRY
  4. * iostream: (iostream).             The C++ input/output facility.
  5. END-INFO-DIR-ENTRY
  6. File: iostream,  Node: Top,  Next: Introduction,  Prev: (DIR),  Up: (DIR)
  7. * Menu:
  8. * Introduction::
  9. * Using the iostream layer::
  10. * Using the streambuf layer::
  11. * stdio - C-style input/output::
  12. * Streambuf internals::
  13. Indices:
  14. * Function and Variable Index::
  15. * Concept Index::
  16. File: iostream,  Node: Introduction,  Next: Using the iostream layer,  Prev: Top,  Up: Top
  17. Introduction
  18. ************
  19.    The `iostream' library was written by Per Bothner.
  20.    Various people have found bugs or come with suggestions. Hongjiu Lu
  21. has worked hard to use the library as the default stdio implementation
  22. for Linux, and has provided much stress-testing of the library.
  23.    Some code was derived from parts of BSD 4.4, which is copyright
  24. University of California at Berkeley.
  25. File: iostream,  Node: Using the iostream layer,  Next: Using the streambuf layer,  Prev: Introduction,  Up: Top
  26. Using the iostream layer
  27. ************************
  28. File: iostream,  Node: Using the streambuf layer,  Next: stdio - C-style input/output,  Prev: Using the iostream layer,  Up: Top
  29. Using the streambuf layer
  30. *************************
  31. * Menu:
  32. * Backing up::
  33. File: iostream,  Node: Backing up,  Up: Using the streambuf layer
  34. Backing up
  35. ==========
  36.    The GNU iostream library allows you to ask streambuf to remember the
  37. current position, and then later after you've read further be able to
  38. go back to it.  Your're guaranteed to be able to backup arbitrary
  39. amounts, even on unbuffered files or multiple buffers worth, as long as
  40. you tell the library advance. This unbounded backup is very useful for
  41. scanning and parsing applications.  This example shows a typical
  42. scenario:
  43.      // Read either "dog", "hound", or "hounddog".
  44.      // If "dog" is found, return 1.
  45.      // If "hound" is found, return 2.
  46.      // If "hounddog" is found, return 3.
  47.      // If non of these are found, return -1.
  48.      int my_scan(streambuf* sb)
  49.      {
  50.          streammarker fence(sb);
  51.          char buffer[20];
  52.          // Try reading "hounddog":
  53.          if (sb->sgetn(buffer, 8) == 8 && strncmp(buffer, "hounddog", 8) == 0)
  54.            return 3;
  55.          // No, no "hounddog":  Backup to 'fence' ...
  56.          sb->seekmark(fence); //
  57.          // ... and try reading "dog":
  58.          if (sb->sgetn(buffer, 3) == 3 && strncmp(buffer, "dog", 3) == 0)
  59.            return 1;
  60.          // No, no "dog" either:  Backup to 'fence' ...
  61.          sb->seekmark(fence); //
  62.          // ... and try reading "hound":
  63.          if (sb->sgetn(buffer, 5) == 5 && strncmp(buffer, "hound", 5) == 0)
  64.            return 2;
  65.          // No, no "hound" either:  Backup to 'fence' and signal failure.
  66.          sb->seekmark(fence); // Backup to 'fence'..
  67.          return -1;
  68.      }
  69.  -- Constructor:  streammarker::streammarker (streambuf* SBUF)
  70.      Create a `streammarker' associated with SBUF that remembers the
  71.      current postion of the get pointer.
  72.  -- Method: int streammarker::delta (streammarker& MARK2)
  73.      Return the difference between the get positions corresponding to
  74.      `*this' and MARK2 (which must point into the same `streambuffer'
  75.      as `this').
  76.  -- Method: int streammarker::delta ()
  77.      Return the position relative to the streambuffer's current get
  78.      position.
  79.  -- Method: int streambuffer::seekmark (streammarker& MARK)
  80.      Move the get pointer to where it (logicly) was when MARK was
  81.      constructed.
  82. File: iostream,  Node: stdio - C-style input/output,  Next: Streambuf internals,  Prev: Using the streambuf layer,  Up: Top
  83. stdio: C input/output
  84. *********************
  85.    Iostreams is distributed with a complete implementation of the ANSI C
  86. stdio facility.  It is implemented using streambufs.
  87.    The stdio package is intended as a replacement for the whatever stdio
  88. is in your C library.  It can co-exist with C libraries that have
  89. alternate implementations of stdio, but there may be some problems.
  90. Since stdio works best when you build libc to contain it, and that may
  91. be inconvenient, it is not installed by default.
  92.    Extensions beyond ANSI:
  93.    * A stdio FILE is identical to a streambuf. Hence there is no need
  94.      to worry about synchronizing C and C++ input/output - they are by
  95.      definition always synchronized.
  96.    * If you create a new streambuf sub-class (in C++), you can use it
  97.      as a FILE from C.  Thus the system is extensible using the standard
  98.      streambuf protocol.
  99.    * You can arbitrarily mix reading and writing, without having to seek
  100.      in between.
  101.    * Unbounded ungetc() buffer.
  102. File: iostream,  Node: Streambuf internals,  Next: Function and Variable Index,  Prev: stdio - C-style input/output,  Up: Top
  103. Streambuf internals
  104. *******************
  105. * Menu:
  106. * Buffer management::
  107. * Filebuf internals::
  108. File: iostream,  Node: Buffer management,  Next: Filebuf internals,  Up: Streambuf internals
  109. Buffer management
  110. =================
  111. Areas
  112. -----
  113.    Streambuf buffer management is fairly sophisticated (this is a nice
  114. way to say "complicated").  The standard protocol has the following
  115. "areas":
  116.    * The "put area" contains characters waiting for output.
  117.    * The "get area" contains characters available for reading.
  118.    * The "reserve area" is available to virtual methods. Usually, the
  119.      get and/or put areas are part of the reserve area.
  120.    The GNU streambuf design extends this by supporting two get areas:
  121.    * The "main get area" contains characters that have been read in
  122.      from the character source, but not yet read by the application.
  123.    * The "backup area" contains previously read data that is being
  124.      saved because of a user request, or that have been "unread"
  125.      (putback).
  126.    The backup and the main get area are logically contiguous:  That is,
  127. the first character of the main get area follows the last character of
  128. the backup area.
  129.    The "current get area" is whichever one of the backup or main get
  130. areas that is currently being read from. The other of the two is the
  131. "non-current get area".
  132. Pointers
  133. --------
  134.    The following `char*' pointers define the various areas. (Note that
  135. if a pointer points to the 'end' of an area, it means that it points to
  136. the character after the area.)
  137.  -- Method: char* streambuffer::base ()
  138.      The start of the reserve area.
  139.  -- Method: char* streambuffer::ebuf ()
  140.      The end of the reserve area.
  141.  -- Method: char* streambuffer::pbase ()
  142.      The start of the put area.
  143.  -- Method: char* streambuffer::pptr ()
  144.      The current put position. If `pptr() < epptr()', then the next
  145.      write will overwrite `*pptr()', and increment `pptr()'.
  146.  -- Method: char* streambuffer::epptr ()
  147.      The end of the put area.
  148.  -- Method: char* streambuffer::eback ()
  149.      The start of the current get area.
  150.  -- Method: char* streambuffer::gptr ()
  151.      The current get position. If `gptr() < egptr()', then the next
  152.      read will read `*gptr()', and increment `gptr()'.
  153.  -- Method: char* streambuffer::egptr ()
  154.      The end of the current get area.
  155.  -- Method: char* streambuffer::Gbase ()
  156.      The start of the main get area.
  157.  -- Method: char* streambuffer::eGptr ()
  158.      The end of the main get area.
  159.  -- Method: char* streambuffer::Bbase ()
  160.      The start of the backup area.
  161.  -- Method: char* streambuffer::Bptr ()
  162.      The start of the used part of the backup area. The area (`Bptr()'
  163.      .. `eBptr()') contains data that has been pushed back, while
  164.      (`Bbase()' .. `eBptr()') contains unused space available for
  165.      future putbacks.
  166.  -- Method: char* streambuffer::eBptr ()
  167.      The end of the backup area.
  168.  -- Method: char* streambuffer::Nbase ()
  169.      The start of the non-current get area (either `main_gbase' or
  170.      `backup_gbase').
  171.  -- Method: char* streambuffer::eNptr ()
  172.      The end of the non-current get area.
  173. File: iostream,  Node: Filebuf internals,  Prev: Buffer management,  Up: Streambuf internals
  174. Filebuf internals
  175. =================
  176.    The `filebuf' is used a lot, so it is importamt that it be
  177. efficient.  It is also supports rather complex semantics. so let us
  178. examine its implementation.
  179. Tied read and write pointers
  180. ----------------------------
  181.    The streambuf model allows completely independent read and write
  182. pointers. However, a `filebuf' has only a single logical pointer used
  183. for both reads and writes.  Since the `streambuf' protocol uses
  184. `gptr()' for reading and `pptr()' for writing, we map the logical file
  185. pointer into either `gptr()' or `pptr()' at different times.
  186.    * Reading is allowed when `gptr() < egptr()', which we call get mode.
  187.    * Writing is allowed when `pptr() < epptr()', which we call put mode.
  188. A `filebuf' cannot be in put get mode and put mode at the same time.
  189.    We have upto two buffers:
  190.    * The backeup area, defined by `Bbase()', `Bptr()', and `eBptr()'.
  191.      This can be empty.
  192.    * The reserve area, which also contains the main get area. For an
  193.      unbuffered file, the (`shortbuf()'..`shortbuf()+1') is used, where
  194.      `shortbuf()' points to a 1-byte buffer that is part of the
  195.      `filebuf'.
  196.    The file system's idea of the current position is `eGptr()'.
  197.    Character that have been written into a buffer but not yet written
  198. out (flushed) to the file systems are those between `pbase()' and
  199. `pptr()'.
  200.    The end of the valid data bytes is: `pptr() > eGptr() && pptr() <
  201. ebuf() ? pptr() : eGptr()'.
  202.    If the `filebuf' is unbuffered or line buffered, the `eptr()' is
  203. `pbase()'.  This forces a call to `overflow()' on each put of a
  204. character. The logical `epptr()' is `epptr() ? ebuf() : NULL'. (If the
  205. buffer is read-only, set `pbase()', `pptr()', and `epptr()' to `NULL'.)
  206. File: iostream,  Node: Function and Variable Index,  Next: Concept Index,  Prev: Streambuf internals,  Up: Top
  207. Function and Variable Index,Concept Index,,Top
  208. **********************************************
  209. * Menu:
  210. * streambuffer::Bbase:                  Buffer management.
  211. * streambuffer::Bptr:                   Buffer management.
  212. * streambuffer::Gbase:                  Buffer management.
  213. * streambuffer::Nbase:                  Buffer management.
  214. * streambuffer::base:                   Buffer management.
  215. * streambuffer::eBptr:                  Buffer management.
  216. * streambuffer::eGptr:                  Buffer management.
  217. * streambuffer::eNptr:                  Buffer management.
  218. * streambuffer::eback:                  Buffer management.
  219. * streambuffer::ebuf:                   Buffer management.
  220. * streambuffer::egptr:                  Buffer management.
  221. * streambuffer::epptr:                  Buffer management.
  222. * streambuffer::gptr:                   Buffer management.
  223. * streambuffer::pbase:                  Buffer management.
  224. * streambuffer::pptr:                   Buffer management.
  225. * streambuffer::seekmark:               Backing up.
  226. * streammarker::delta:                  Backing up.
  227. * streammarker::delta:                  Backing up.
  228. * streammarker::streammarker:           Backing up.
  229. File: iostream,  Node: Concept Index,  Prev: Function and Variable Index,  Up: Top
  230. Concept Index,,Function and Variable Index,Top
  231. **********************************************
  232. * Menu:
  233. * backup area:                          Buffer management.
  234. * get area:                             Buffer management.
  235. * main get area:                        Buffer management.
  236. * put area:                             Buffer management.
  237. * reserve area:                         Buffer management.
  238. Tag Table:
  239. Node: Top
  240. Node: Introduction
  241. Node: Using the iostream layer
  242. Node: Using the streambuf layer
  243. Node: Backing up
  244. Node: stdio - C-style input/output
  245. Node: Streambuf internals
  246. Node: Buffer management
  247. Node: Filebuf internals
  248. Node: Function and Variable Index
  249. Node: Concept Index
  250. 11075
  251. End Tag Table
  252.