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

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