home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / win3 / patches / symantec / rtlinc.exe / IOSTREAM.H < prev    next >
C/C++ Source or Header  |  1993-06-10  |  43KB  |  1,194 lines

  1. // IOStreams Package
  2. // Steve Teale April 1992
  3. // Copyright Symantec Corp 1990-1992. All Rights Reserved.
  4.  
  5. #ifndef __IOSTREAM_H
  6. #define __IOSTREAM_H
  7. #include <stddef.h>
  8. #include <2comp.h>
  9.  
  10. #define seek_dir relative_to
  11.  
  12. #ifndef EOF
  13. const int EOF = -1;
  14. #endif
  15.  
  16. const int _ios_default_decimal_precision = 6;
  17. const int _ios_n_extended_format_words = 10;
  18. // This is the number of extended format state words reserved for use
  19. // by derived classes and user inserters. This value should be reasonably
  20. // small, as it inflates the size of each instance of ios.
  21.  
  22. #pragma pack(__DEFALIGN)
  23. class streampos {
  24. friend class streamoff;
  25. public:
  26.     streampos(long elems, size_t elemsize = 1) : ne(elems), es(elemsize) {}
  27.     operator long() const { return (ne == -1)? EOF: ne*es; }
  28. private:
  29.     long ne;
  30.     size_t es;
  31. };
  32.  
  33. class streamoff {
  34. public:
  35.     streamoff(long elems, size_t elemsize = 1) : ne(elems), es(elemsize) {}
  36.     streamoff(streampos &a) : ne(a.ne), es(a.es) {}
  37.     size_t stepsize() const { return es; }
  38.     long steps() const { return ne == -1? 0: ne; }
  39.     streamoff& operator += (long o) { ne += o; return *this; }
  40.     streamoff& operator -= (long o) { ne -= o; return *this; }
  41.     operator long() const { return steps()*es; }
  42.  
  43. private:
  44.     long ne;
  45.     size_t es;
  46. };
  47.  
  48. class streambuf;
  49. class ostream;
  50. class istream;
  51.  
  52.  
  53. class ios {
  54.  
  55. // This is the base class for istream and ostream, and all of their
  56. // derivations.
  57.  
  58. friend ostream &endl(ostream &);
  59.  
  60. public:
  61.     enum io_state {
  62.         goodbit=0,
  63. // No errors - everything hunky dory!
  64.         eofbit=1,
  65. // Normally set when underflow failed because there was no more file.
  66.         failbit=2,
  67. // An error has ocurred, but it is probably recoverable, and the
  68. // stream is still in a useable state
  69.         badbit=4
  70. // A fatal error has ocurred
  71.     };
  72.  
  73. // This is called seek_dir in the AT & T version, which is misleading.
  74. // A define is included above for compatibility. These enumerators are
  75. // used to specify relative seeks in streams.
  76.     enum relative_to {
  77.         beg,
  78. // For seek operations relative to the beginning of the stream (file),
  79.  
  80.         cur,
  81. // relative to the current position in the stream (file),
  82.  
  83.         end
  84. // and relative to the end of the stream (file)
  85.     };
  86.  
  87. // The following enumeration applies to file related derivatives.
  88.     enum open_mode {
  89.         in=0x1,
  90. // Input allowed
  91.  
  92.         out=0x2,
  93. // Output allowed
  94.  
  95.         ate=0x4,
  96. // A seek to the end of the file to be performed during open.
  97.  
  98.         app=0x8,
  99. // All writes are to the end of the file - implies out
  100.  
  101.         trunc=0x10,
  102. // Existing contents of the file to be discarded. Implies out
  103. // unless ate or app specified as well.
  104.  
  105.         nocreate=0x20,
  106. // Open will fail if the file does not already exist
  107.  
  108.         noreplace=0x40,
  109. // Open will fail if the file does already exist
  110.  
  111.         translated = 0x80
  112. // CR/LF pairs to be translated to newline characters on input
  113. // and newline characters to be translated to CR/LF pairs on
  114. // output (the normal behaviour for DOS)
  115.     };
  116.  
  117. // The formatting state is a bit-mask used to control some of the
  118. // inserters and extractors. All of the bits of the format state
  119. // can be manipulated by flags(), setf(), and unsetf(). Some
  120. // specialized parts of the formatting state can be manipulated
  121. // by fill(), width(), and precision() . Here are the meanings
  122. // of the various bits:
  123.  
  124.     enum format_mode {
  125.         skipws = 0x1,
  126. // Skip past leading white space when extracting.
  127. // Since zero-width fields are considered an
  128. // error by the numeric extractors, attempting
  129. // to extract white-space into a number without
  130. // this bit set will set an error flag.
  131.  
  132.         left = 0x2,
  133. // Left-adjust values when inserting (fill on the right).
  134.  
  135.         right = 0x4,
  136. // Right-adjust values when inserting (fill on the left).
  137.  
  138.         internal = 0x8,
  139. // When inserting, fill _between_ the numeric sign or
  140. // base indicator and the value.
  141.  
  142.         dec = 0x10, oct = 0x20, hex = 0x40,
  143. // Default radix for integers. If neither dec, octal,
  144. // or hex is set, integer inserters use base 10, and
  145. // integer extractors interpret numbers according to the
  146. // C++ lexical convention: "0x" precedes a base-16 number,
  147. // and a number with a leading zero is base-8.
  148.  
  149.         showbase = 0x80,
  150. // If this is set, base-16 numbers will be inserted with a
  151. // leading "0x", and base-8 numbers will have a leading zero.
  152.  
  153.         showpoint = 0x100,
  154. // If this is set, the floating-point inserters will print a
  155. // decimal point and trailing zeroes, even when the trailing
  156. // places are not significant.
  157.  
  158.         uppercase = 0x200,
  159. // If this is set, "E" instead of "e" will be used to indicate
  160. // the exponent of a floating point number, and "A" through "F"
  161. // will be used to represent base-16 numerals instead of "a"
  162. // through "f".
  163. //
  164. // If uppercase and showbase are both set, the string "0X"
  165. // instead of "0x" will be used to indicate a base-16 number.
  166.  
  167.         showpos = 0x400,
  168. // If this is set, positive numbers will be inserted with a
  169. // leading "+".
  170.  
  171.         scientific = 0x800,
  172. // If this is set, the floating-point inserters will print a
  173. // number with one digit before the decimal point, and the
  174. // number of digits after the decimal point equal to the
  175. // value of precision(). The character "e" will introduce the
  176. // exponent.
  177.  
  178.         fixed = 0x1000,
  179. // If this is set, the floating-point inserters will use
  180. // precision() to determine the number of digits after the
  181. // decimal point.
  182. //
  183. // If neither scientific or fixed is set, numbers with
  184. // exponents smaller than -4 or greater than precision() will
  185. // be printed as if scientific were set. Other numbers will
  186. // be printed using zeroes to explicitly show the decimal place.
  187.  
  188.         unitbuf = 0x2000,
  189. // When this is set, a flush is performed after each insertion
  190. // (by ostream::osfx()). This is more efficient than using
  191. // unbuffered output, but provides most of the same advantages.
  192.  
  193.         stdio = 0x4000
  194. // When this is set, streams using stdiobufs will flush stdout and
  195. // stderr after each insertion.
  196.  
  197. // Note that it is not possible to use bit 0x8000 in this way,
  198. // as this evaluates to an enumerator with a negative value and
  199. // lots of bits set.
  200.     };   // end enum format_mode
  201.  
  202.     static const long stickywidth;
  203.     static const long spacing;
  204.  
  205.     enum format_mode_mask {
  206.         defaults = right|skipws,
  207.         basefield = dec|oct|hex,
  208.         adjustfield = left|right|internal,
  209.         floatfield = scientific|fixed
  210.     };
  211.  
  212. public: // just a reminder!
  213.  
  214.     ios(streambuf *buffer);
  215. // Construct an ios associated with the argument streambuf.
  216. // "buffer" should not be null.
  217.  
  218.     virtual ~ios();
  219.  
  220. /////////////////////////////////////////////////////////////
  221. //
  222. // Functions to interrogate/set the error state
  223.  
  224.     int good() const { return error_state == 0; };
  225. // If there are no error bits set, this returns non-zero, otherwise
  226. // it returns zero.
  227.  
  228.     int eof() const { return error_state & eofbit; };
  229. // If eofbit is set in the error state, this return non-zero, otherwise
  230. // it returns zero. This indicates that end-of-file has been reached
  231. // while reading the character stream.
  232.  
  233.     int fail() { return error_state & (badbit | failbit); }
  234. // If badbit or failbit is set in the error state, return non-zero.
  235. // Otherwise, return zero. Failbit generally indicates that some
  236. // extraction has failed, but the stream may still be used once
  237. // failbit has been cleared.
  238.  
  239.     int bad() const { return error_state & badbit; }
  240. // Returns non-zero if badbit is set in the error state. This
  241. // indicates some unrecoverable error, generally an I/O error.
  242.  
  243.     int operator!() const
  244.     {
  245.         return error_state & (badbit|failbit);
  246.     }
  247. // Return non-zero if badbit or failbit is set in the error state,
  248. // which allows expressions of the form:
  249. //  if ( !cout )
  250.  
  251.     operator void*()
  252.     {
  253.         return (error_state & (badbit|failbit))?
  254.             0: this;
  255.     }
  256. // Convert an ios to a value that can be compared to zero, but can
  257. // not be easily used accidentally. The return value is a void pointer
  258. // that will be zero if failbit or badbit is set in the error state,
  259. // and non-zero otherwise. This allows an iostream to be used
  260. // in conditional expressions that test its state, as in:
  261. // if ( cin ) and if ( cin >> variable )
  262.  
  263.     int rdstate() const { return error_state; };
  264. // Returns the current error state.
  265.  
  266.     void clear(int new_state = 0) { error_state = new_state; }
  267. // Stores "new_state" as the error state.
  268. //
  269. // To set a bit without clearing others requires something like
  270. // clear(bits_to_set | rdstate()) .
  271.  
  272. //////////////////////////////////////////////////////////////////
  273. //
  274. // Functions to set/interrogate various options
  275.  
  276.     char fill(char new_value)
  277.     {
  278.         char old_value = padding_character;
  279.         padding_character = new_value;
  280.         return old_value;
  281.     }
  282. // Sets the fill character, and returns the old value. This is the
  283. // character used to pad output when the left, right, or internal
  284. // adjustments are in effect.
  285.  
  286.     char fill() const { return padding_character; }
  287. // Returns the fill character.
  288.  
  289.     int precision(int new_value)
  290.     {
  291.         int old_value = decimal_precision;
  292.         decimal_precision = new_value >= 0?
  293.             new_value: _ios_default_decimal_precision;
  294.         return old_value;
  295.     }
  296. // Sets the decimal precision, and returns the old value.
  297. // This controls the number of digits inserted after the decimal
  298. // point by the floating-point inserter.
  299.  
  300.     int precision() const { return decimal_precision; }
  301. // Returns the current decimal precision.
  302.  
  303.     ostream *tie(ostream * new_value)
  304.     {
  305.         ostream *old_value = tied_ostream;
  306.         tied_ostream = new_value;
  307.         return old_value;
  308.     };
  309. // Facilitate automatic flushing of iostreams.
  310. // Associate this ios to an ostream such that the ostream will be
  311. // flushed before this ios makes a request for characters, or flushes
  312. // output characters. These ties exist by default:
  313. //  cin.tie(cout);
  314. //  cout.tie(cerr);
  315. //  cout.tie(clog);
  316. //
  317. // For other instances of ios, the tie is set to zero by default.
  318. //
  319. // This returns the old value.
  320.  
  321.     long flags() const { return format_state; }
  322. // Return the format state flags.
  323.  
  324.     long flags(long new_value)
  325.     {
  326.         long old_value = format_state;
  327.         format_state = new_value;
  328.         return old_value;
  329.     }
  330. // Sets the format state flags, returning the old values.
  331.  
  332.     long setf(long bits_to_set, long mask = 0)
  333.     {
  334.         long old_value = format_state;
  335.         format_state = (format_state & ~mask)
  336.                 | bits_to_set;
  337.         return old_value;
  338.     }
  339. // Clears the bits corresponding to mask in the format state, and
  340. // then sets only those bits from the value of bits_to_set.
  341.  
  342.     long unsetf(long bits_to_clear)
  343.     {
  344.         long old_value = format_state;
  345.         format_state &= ~bits_to_clear;
  346.         return old_value;
  347.     };
  348. // Clears flags in the format state.
  349.  
  350.     ostream *tie() const { return tied_ostream; };
  351. // Return the current "tied" ostream.
  352.  
  353.     int width(int new_value);
  354. // Sets the field width used by inserters.
  355. // When the width is zero, inserters will use only as many characters
  356. // as necessary to represent a value. When the width is non-zero,
  357. // inserters will insert at least that many characters, using the
  358. // fill character to pad out the field. Inserters will never truncate,
  359. // so they may output more characters than the current width.
  360.  
  361.     int width() const { return field_width; };
  362. // Returns the current width.
  363.  
  364. ////////////////////////////////////////////////////////////////
  365. //
  366. // Other utilities
  367.  
  368.     streambuf *rdbuf() { return buf; }
  369. // Returns a pointer to the streambuf associated with this ios.
  370.  
  371.     static void sync_with_stdio();
  372. // Use this when mixing C and C++ in the same program.
  373. // It resets cin, cout, cerr, and clog to use stdiobufs, and
  374. // thus makes I/O to these streams compatible with the C stdio.
  375. // Invoking this degrades performance.
  376.  
  377. ////////////////////////////////////////////////////////////////
  378. //
  379. // Functions to manipulate user defined format flags and control
  380. // words.
  381.  
  382.     static int bitalloc();
  383. // Returns an int with one previously-unallocated bit set.
  384. // This allows users who need an additional format flag to
  385. // get one. Note that this allocation is global for class ios,
  386. // not local to any instance of class ios.
  387.  
  388.     static int xalloc();
  389. // Returns a previously-unused index into an array of words usable
  390. // as format-state variables by derived classes.
  391.  
  392.     long &iword(int index)
  393.     {
  394.         return extended_format_words[index].l;
  395.     }
  396. // Returns a reference to one of the auxillary format state words
  397. // reserved for use by derived classes and user inserters, where
  398. // index is a value returned by xalloc().
  399.  
  400.     void* &pword(int index)
  401.     {
  402.         return extended_format_words[index].vp;
  403.     };
  404.  
  405. // Returns a reference to a pointer to one of the auxillary format
  406. // state words reserved for use by derived classes and user
  407. // inserters, where index is a value returned by xalloc().
  408.  
  409. protected:
  410.     void set_buffer(streambuf *b) { buf = b; }
  411.  
  412. private:
  413.     streambuf   *buf;
  414.     ostream     *tied_ostream;
  415.     long        format_state;
  416.     char        error_state;
  417.     char        padding_character;
  418.     short       decimal_precision;
  419.     short       field_width;
  420.     union {
  421.         void *vp;
  422.         long l;
  423.     } extended_format_words[_ios_n_extended_format_words];
  424.  
  425.     ios(const ios&);
  426.     ios &operator=(const ios&);
  427. // These copying functions are private so that the compiler
  428. // will complain about an attempt to assign an ios. It is not
  429. // actually defined. Instead of copying an ios, assign a pointer
  430. // to it.
  431.  
  432. };
  433.  
  434. ////////////////////////////////////////////////////////////////
  435. //
  436. // Manipulator functions
  437.  
  438. ios &dec(ios&);
  439. // Set the default integer radix to 10.
  440. // Invoke this (and the other manipulators) this way:
  441. // stream >> dec;
  442. // stream << dec;
  443.  
  444. ios &hex(ios&);
  445. ios &oct(ios&);
  446. ios &defaults(ios&);
  447.  
  448. class streambuf {
  449. // Streambuf abstracts the operations used to perform I/O on a character
  450. // stream such as a computer terminal, a file or a string.
  451. //
  452. // The two basic operations are "get", which fetches characters from the
  453. // stream, and "put", which places characters on the stream. Some streambufs
  454. // are unidirectional, in which case they support the "get" operation but
  455. // not "put", or vice-versa.
  456. //
  457. // Some streambufs are bi-directional, with the "get" and "put" offsets
  458. // at different locations in the stream. Some streambufs may implement
  459. // a circular buffer between the "get" and "put" offsets, as in a FIFO.
  460. // Some streambufs lock the "get" and "put" offset together, as in a UNIX file.
  461. // Some streambufs provide buffered I/O on the underlying character streams
  462. // for efficiency.
  463. //
  464. // Streambufs contain a buffer, a get area, and a put area. Usually, the
  465. // get area and put area overlap the buffer, but do not overlap each other.
  466.  
  467. public:
  468.  
  469.     streambuf();
  470. // The default constructor.
  471.  
  472.     streambuf(char *memory, int length);
  473. // Constructs a streambuf, possibly using the argument buffer.
  474.     virtual ~streambuf();
  475.  
  476.     void dbp();
  477. // Write debugging information about the streambuf on file
  478. // descriptor 1. Nothing about the form of that information
  479. // is specified.
  480.  
  481.     int in_avail() const { return _egptr - _gptr; }
  482. // Return the number of characters that can be read immediately
  483. // with a guarantee that no errors will be reported.
  484.  
  485.     int out_waiting() const { return _pptr - _pbase; }
  486. // Return the number of characters that have not
  487. // been flushed away, or EOF if there are no characters.
  488.  
  489.     virtual streambuf *setbuf(char *memory, int length);
  490. // Requests that this streambuf use the argument memory buffer.
  491. // Special case: a null memory argument, or a length argument that is
  492. // zero or negative, is taken as a request that this streambuf be
  493. // unbuffered.
  494. //
  495. // If the request to set the buffer is honored, return "this",
  496. // otherwise return 0.
  497. //
  498. // * Use of this interface, except by a derived class of streambuf,
  499. // is discouraged. It is not protected for compatibility with the
  500. // original stream package in Stroustrup.
  501. //
  502. // The default version of this member will honor the request if no
  503. // buffer has been allocated.
  504.  
  505.     virtual streampos  seekpos(streampos position,
  506.                                     int which=ios::in|ios::out);
  507. // Performs an absolute seek of the get or put pointers, or both, to
  508. // "position". Position is an implementation-dependent value which
  509. // should not have arithmetic performed on it. "which" signifies what
  510. // pointer is to be affected: ios::in signifies the get pointer, and
  511. // ios::out signifies the put pointer. The two "which" values may be
  512. // or-ed together, in which case the operation affects both pointers.
  513. //
  514. // * The default version of this member returns
  515. //       seekoff(position, ios::beg, which)
  516. // thus is is only necessary for a derived class to define
  517. // seekoff(), and use the inherited seekpos().
  518.  
  519.     virtual streampos seekoff(streamoff offset, ios::relative_to pos,
  520.                                     int which=ios::in|ios::out);
  521. // Performs a relative seek of the get or put pointers, or both, by
  522. // "offset". Position is a byte offset, and may be negative.
  523. // "pos" may be ios::beg, which signifies a seek relative to
  524. // the beginning of the stream; ios::cur, which signifies a seek
  525. // relative to the current position in the stream; and ios::end, which
  526. // signifies a seek relative to the end of the stream.
  527. // "which" signifies what pointer is to be affected: ios::in
  528. // signifies the get pointer, and ios::out signifies the put pointer.
  529. // The two "which" values may be or-ed together, in which case the
  530. // operation affects both pointers.
  531. //     * The default version of this member always returns EOF.
  532.  
  533.     int sgetc()
  534.     {
  535.         return _gptr < _egptr?
  536.                     (unsigned char) *_gptr: underflow();
  537.     }
  538. // Returns the character at the get pointer, without moving the get
  539. // pointer.
  540.  
  541.     int sbumpc()
  542.     {
  543.         return _gptr < _egptr?
  544.                     (unsigned char) *_gptr++: sbumpc_special();
  545.     }
  546. // Moves the get pointer forward one character, and return the
  547. // character it moved past.
  548.  
  549.     int sgetn(char *buffer, int count);
  550. // Fetch the "count" characters following the get pointer, and
  551. // stores them in "buffer". If there are less than "count" characters,
  552. // the number remaining are fetched. The get pointer is repositioned
  553. // after the fetched characters, and the number of characters fetched
  554. // is returned.
  555.  
  556.     int snextc()
  557.     {
  558.         return _gptr+1 < _egptr?
  559.                     ((unsigned char) *(++_gptr)): underflow();
  560.     }
  561.  
  562. // Skip past the character at the get pointer, and return the
  563. // character after that, or EOF.
  564.  
  565.     int sputbackc(char c)
  566.     {
  567.         return _gptr > _gbase?
  568.                     (*(--_gptr) = c, 0): pbackfail(c & 0x00ff);
  569.     }
  570. // Move the get pointer back one character. If c is not the last
  571. // character retrieved, the effect is undefined. In this implementation
  572. // it should work
  573.  
  574.     int sputc(int c)
  575.     {
  576.         return _pptr < _epptr?
  577.                     (*_pptr++ = c, 0): overflow(c & 0x00ff);
  578.     }
  579. // Store c on the character stream, advancing the "put" pointer.
  580. // Return EOF when an error occurs.
  581.  
  582.     int sputn(const char *string, int length);
  583. // Store "n" characters on the character stream, advancing the "put"
  584. // pointer past the stored characters. Return the number of characters
  585. // stored, which may be less than "n" if there is an error.
  586.  
  587.     void stossc()
  588.     {
  589.         if (_gptr < _egptr) ++_gptr;
  590.     }
  591. // Skip the "get" pointer forward, wasting one character of input.
  592. // If the get pointer is already at the end of the sequence
  593. // this has no effect.
  594.  
  595.     virtual int sync();
  596. // Make the external character stream and the streambuf consistent
  597. // with each other. This usually means:
  598. // 1. If there are characters in the get area, return them to the
  599. //    stream, or throw them away. Re-position the stream so that
  600. //    it appears that the characters that had been in the get area
  601. //    had never been taken from the stream.
  602. // 2. If there are characters in the put area, store them to the
  603. //    stream. Re-position the stream immediately after the characters
  604. //    just stored.
  605. // 3. Return EOF if there's an error, otherwise return some other
  606. //    value.
  607. //     * The default version of this member will return 0 if the get area
  608. // is empty, otherwise it returns EOF.
  609.  
  610.     virtual int overflow(int c = EOF);
  611. // This is called to store characters in the character stream, usually
  612. // when the put area is full. It generally stores all of the characters
  613. // that are in the put area, stores c if it is not EOF, and calls
  614. // setp() to establish a new put area. It should return EOF if it
  615. // has an error, otherwise return some other value.
  616.  
  617.     virtual int underflow();
  618. // This is called to read characters from the character stream,
  619. // usually when the get area is empty. It should read one or more
  620. // characters, and place them in the get area. It should return the
  621. // first character in the get area, without incrementing the get
  622. // pointer. It should return EOF if there's a problem.
  623.  
  624. protected:
  625.  
  626. // Buffer base and one past the end
  627.     char *base() const { return _base; }
  628.     char *ebuf() const { return _ebuf; }
  629. // Get pointer, pushback limit, and one past end of put area
  630.     char *gptr() const { return _gptr; }
  631.     char *eback() const { return _gbase; }
  632.     char *egptr() const { return _egptr; }
  633. // Put pointer, and one past end of put area
  634.     char *pbase() const { return _pbase; }
  635.     char *pptr() const { return _pptr; }
  636.     char *epptr() const { return _epptr; }
  637.  
  638.     int blen() const { return _ebuf-_base; }
  639. // Return the size in characters of the buffer.
  640.  
  641.     int allocate();
  642. // Try to set up the buffer. If one already exists or unbuffered() is
  643. // non-zero, return 0 without doing anything. If something goes wrong,
  644. // return EOF. Otherwise, return 1. This function is only called by
  645. // virtual members of the base class streambuf, thus you can override
  646. // everything that uses it.
  647.  
  648.     virtual int doallocate();
  649. // This is called when allocate() determines that space is needed.
  650. // This function must call setb() to set up the buffer, and return
  651. // a non-eof value, or return EOF if it can not get space.
  652. // This function is only called if unbuffered() is zero, and base()
  653. // is zero.
  654. //
  655. // * The default version allocates a buffer using operator new.
  656.  
  657.     virtual int pbackfail(int c);
  658. // This is called by sputbackc() when the get pointer is equal to
  659. // the base of the get area, and there is thus no space to put back
  660. // characters. It may try to re-arrange the buffer so that it is
  661. // possible to put back characters, and then put back c. If it
  662. // succeeds it should return c, otherwise it should return EOF.
  663. //     * The default version of this member always returns EOF.
  664.  
  665.     void setg(char *base, char *get, char *end)
  666.     {
  667.         _gbase = base;
  668.         _gptr = get;
  669.         _egptr = end;
  670.     }
  671. // Sets the base of the get area, the get pointer, and the end of
  672. // the get area.
  673.  
  674.     void setp(char *base, char *end)
  675.     {
  676.         _pbase = _pptr = base;
  677.         _epptr = end;
  678.     }
  679. // Sets the base of the put area, the put pointer, and the end of
  680. // the put area.
  681.  
  682.     void setb(char *base, char *end, int own = 0);
  683. // Sets the base and end of the buffer. If own is true, then this
  684. // streambuf will delete base in its destructor or if setb() is called
  685. // again.
  686.  
  687.     void gbump(int n) { _gptr += n; }
  688.     void pbump(int n) { _pptr += n; }
  689.  
  690.     int unbuffered() const { return _unbuffered; }
  691.     void unbuffered(int u) { _unbuffered = u; }
  692.  
  693.  
  694. private:
  695.     char *_base;
  696.     char *_ebuf;
  697.     char *_gbase;
  698.     char *_gptr;
  699.     char *_egptr;
  700.     char *_pbase;
  701.     char *_pptr;
  702.     char *_epptr;
  703.     char _unbuffered;
  704.     char _owned;
  705.  
  706.     char unbuf[2];
  707. // This is big enough to deal with the translated case when unbuffered
  708.  
  709.     int     sbumpc_special();
  710. // Called by sbumpc() when get_pointer == get_area_end.
  711. // Simply calls overflow(), increments the get pointer, and
  712. // returns the value returned by overflow(). It's here so that
  713. // the special-case code does not have to be inlined.
  714. };
  715.  
  716. class ostream : virtual public ios {
  717.  
  718. // This is the class used for formatted character output.
  719. // The various "operator << (...)" members are called "inserters", because
  720. // they insert information into the character stream.
  721. //
  722. // Unless otherwise noted, all of the functions here that do output set
  723. // the error state on failure.
  724. //
  725. // Where the inserters perform conversions of numbers to a readable string, 
  726. // the conversion is affected by the format state flags in ios::flags() .
  727.  
  728. public:
  729.     ostream(streambuf * buffer);
  730. // Construct an ostream associated with the argument streambuf.
  731.  
  732.     virtual ~ostream();
  733.  
  734.     ostream &flush();
  735. // Flush any characters queued for output.
  736.  
  737.     int opfx();
  738. // Perform output-prefix operations.
  739. // If the error state is non-zero, return zero immediately.
  740. // If there is a tied ostream (see ios::tie()), flush it.
  741. // Return non-zero. If you define your own inserter that directly
  742. // manipulates the streambuf instead of calling other inserters,
  743. // it should call this when it starts.
  744.  
  745.     void osfx();
  746. // Perform output-suffix operations.
  747. // If ios::unitbuf is set in the format state, flush output.
  748. // If ios::stdio is set, flush stdio and stderr.
  749. // If you define your own inserter that directly manipulates the
  750. // streambuf, it should call this just before returning.
  751. // All of the inserters in ostream call this.
  752. // The binary put() and write() functions do not call this.
  753.  
  754.     ostream &put(char c);
  755. // Inserts a character
  756.  
  757.     ostream &seekp(streampos position);
  758. // Absolute seeks the output stream to "position".
  759.  
  760.     ostream &seekp(streamoff offset, seek_dir direction);
  761. // Relative seeks the output stream. See streambuf::seekoff()
  762.  
  763.     streampos tellp();
  764. // Returns the current position in the output stream.
  765.  
  766.     ostream &write(const void *data, size_t size);
  767.     size_t pcount() { return write_count; }
  768. // Inserts the block of "size" bytes starting at "data". Does not
  769. // perform any formatting. The number of characters successfully
  770. // output can be determined by a following call to ostream::pcount().
  771.  
  772.     ostream &operator<<(const char *string);
  773.     ostream &operator<<(const signed char *string)
  774.         { return operator<<((const char *) string); }
  775.     ostream &operator<<(const unsigned char *string)
  776.         { return operator<<((const char *) string); }
  777. // Insert a null-terminated string.
  778.  
  779.     ostream &operator<<(char c);
  780.     ostream &operator<<(signed char c)
  781.     { return operator<<((char) c); }
  782.     ostream &operator<<(unsigned char c)
  783.     { return operator<<((char) c); }
  784. // Insert a single character.
  785.  
  786.     ostream &operator<<(short v)
  787.         { return _2Comp::insert(*this,&v,sizeof(short),1); }
  788.     ostream &operator<<(int v)
  789.         { return _2Comp::insert(*this,&v,sizeof(int),1); }
  790.     ostream &operator<<(long v)
  791.         { return _2Comp::insert(*this,&v,sizeof(long),1); }
  792.     ostream &operator<<(unsigned short v)
  793.         { return _2Comp::insert(*this,&v,sizeof(unsigned short),0); }
  794.     ostream &operator<<(unsigned v)
  795.         { return _2Comp::insert(*this,&v,sizeof(unsigned),0); }
  796.     ostream &operator<<(unsigned long v)
  797.         { return _2Comp::insert(*this,&v,sizeof(unsigned long),0); }
  798.  
  799. #if macintosh
  800.     ostream &operator<<(float v) { return operator<<((long double) v); }
  801.     ostream &operator<<(double v) { return operator<<((long double) v); }
  802.     ostream &operator<<(long double);
  803. #else
  804.     ostream &operator<<(float v) { return operator<<((double) v); }
  805.     ostream &operator<<(double);
  806. #endif
  807.  
  808.     ostream &operator<<(void *);
  809. // Convert a pointer to a hex integer, and insert.
  810.  
  811.     ostream &operator<<(streambuf *source);
  812. // Insert all of the characters that can be fetched from the streambuf.
  813.  
  814.     ostream &operator<<(ostream &(*manipulator)(ostream &))
  815.         { return (*manipulator)(*this); }
  816. // Parameterless manipulators are implemented by providing an
  817. // inserter for the type pointer to function taking an ostream reference
  818. // argument and returning an ostream reference.  The specified function
  819. // is simple executed for the current ostream.
  820.  
  821.     ostream &operator<<(ios &(*manipulator)(ios &))
  822.     {
  823.         (*manipulator)(*this);
  824.         return *this;
  825.     }
  826. // Similar facility to insert a pointer to function taking an ios
  827. // reference argument.
  828.  
  829. // This isn't in the draft standard, but it is so useful in the
  830. // implementation of inserters that it is made public here.
  831.     int pad(int where, int wide);
  832.  
  833. protected:
  834.     ostream();  // ios will be initialized by derived class
  835. private:
  836.     size_t write_count;
  837. // Records number of bytes actually put out by a write()
  838.  
  839.     void eof_fail() { clear(ios::badbit | ios::failbit | ios:: eofbit); }
  840. // Internal. Set error flags after an output EOF error.
  841.  
  842. #if macintosh
  843.     int fixed_point(long double, char *);
  844. #else
  845.     int fixed_point(double, char *);
  846. #endif
  847. // Internal. Convert double to fixed-point string.
  848.  
  849. #if macintosh
  850.     int sci_notation(long double, char *, int &);
  851. #else
  852.     int sci_notation(double, char *, int &);
  853. #endif
  854. // Internal. Convert double to scientific-notation string.
  855.  
  856.     int adjust(int bit_to_test, int field_width);
  857. // Manages padding
  858.     void flush_stdio();
  859. // Called by osfx() if ios::stdio is set.  This function is in a
  860. // separate module to prevent the stdio stuff from being pulled
  861. // in when it is not required.
  862. };
  863.  
  864. // The following functions provide parameterless inserters of the
  865. // type noted above. They have to be real functions because we
  866. // need to take their address.
  867.  
  868. ostream &ends(ostream&);
  869. // Ends a string by inserting a null character. Use this on strstreams
  870. // before you call ostrstream::freeze() .
  871. //
  872. // stream << "whatever" << ends;
  873.  
  874. ostream &flush(ostream&);
  875. // Flushes output on an ostream.
  876. //
  877. // stream << "whatever" << flush
  878.  
  879. ostream &endl(ostream &);
  880. // Inserts the appropriate new-line character(s) for the system, and flushes
  881. // output. On Unix the new-line character is inserted. On DOS, the
  882. // carriage-return and new-line characters are inserted. Thus, using endl
  883. // instead of '\n' or "\r\n" is more portable.
  884. //
  885. // stream << "whatever" << endl;
  886.  
  887. ostream &stickywidth(ostream &);
  888. // Sets the state so that width is not reset after each insertion
  889. // unlatch this by a call to width() or use setw manipulator.
  890.  
  891. ostream &spacing(ostream &);
  892. ostream &nospacing(ostream &);
  893. // Sets the state so that a space is output after any item which
  894. // makes a call to osfx() except the endl manipulator.
  895.  
  896. ostream &fixed(ostream &);
  897. ostream &scientific(ostream &);
  898. ostream &showpoint(ostream &);
  899. ostream &floating(ostream &);
  900. ostream &uppercase(ostream &);
  901. // Manipulate the floating point format, floating sets the default
  902. // state.
  903.  
  904. ostream &leftjust(ostream &);
  905. ostream &rightjust(ostream &);
  906. ostream &internal(ostream &);
  907. ostream &showbase(ostream &);
  908. // Set a justify mode
  909.  
  910. // types used by istream dfa extractor function
  911. typedef void (*translate_function_t)(void *, const char *, void *);
  912. typedef int (*helper_function_t)(int, char, void *);
  913.  
  914. class istream : virtual public ios {
  915.  
  916. // This is the class used for character input.
  917. // The various "operator>>(...)" members are called "extractors", because
  918. // they extract information from the character stream.
  919. //  * Where the extractors perform conversions of a string to a number,
  920. // the conversion is affected by the format state flags in ios::flags() .
  921. // Each of the extractors calls ipfx() first, and returns immediately if
  922. // ipfx returns zero. Extractors indicate errors by setting bits of the
  923. // error state. ios::failbit means the input characters weren't a
  924. // representation of the required type. ios::badbit means that the I/O
  925. // system failed to get the required characters.
  926. //  * The unformatted extractors (get(), getline()) will set ios::failbit
  927. // only if they are not able to extract at least one character.
  928. //  * Caveat: The ATT version ignores overflow. This version attempts to set
  929. // the error flags on overflow.
  930.  
  931. public:
  932.     static int is_white_space(char c);
  933. // Internal. Return 1 if the argument character is white-space,
  934. // otherwise return 0.
  935.  
  936.     istream(streambuf *buffer);
  937. // Construct a new istream associated with the argument streambuf.
  938.  
  939.     virtual ~istream();
  940.  
  941.     size_t gcount() { return read_count; }
  942. // Return the number of characters extracted by the last unformatted
  943. // input function. Other input functions may call the unformatted
  944. // input functions, so this counter is only accurate immediately
  945. // after a call to an unformatted input function.
  946.  
  947.     istream &get(char *data, int length, char delimiter = '\n');
  948.     istream &get(signed char *data, int length, char delimiter = '\n');
  949.     istream &get(unsigned char *data, int length, char delimiter = '\n');
  950. // Both of these extract up to "length" characters, storing them in
  951. // "data". If a character equal to "delimiter" is seen, it is pushed
  952. // back on the input stream and extraction stops.
  953.  
  954.     istream &get(char &destination);
  955.     istream &get(signed char &destination);
  956.     istream &get(unsigned char &destination);
  957. // Extract a single character.
  958.  
  959.     istream &get(streambuf &destination, char delimiter = '\n');
  960. // Call ipfx(0), and if the result is non-zero, get characters until
  961. // the delimiter is seen or EOF, and stuff the characters into the
  962. // streambuf. Doesn't extract the delimiter character. Only sets
  963. // ios::badbit if it can't extract at least one character.
  964.  
  965.     int get();
  966. // Get one character, and return it in an int. Return EOF if there's
  967. // an error.
  968.  
  969.     istream &getline(char *data, int length, char delimiter = '\n');
  970.     istream &getline(signed char *data, int length, char delimiter = '\n');
  971.     istream &getline(unsigned char *data, int length, char delimiter = '\n');
  972. // These two are like get(char *data, int length, char delimiter),
  973. // except that they extract the terminating delimiter instead of
  974. // pushing it back.
  975.  
  976.     istream &ignore(int length = 1, int delimiter = EOF);
  977. // Extract and throw away up to "length" characters. Extraction
  978. // stops if "delimiter" is extracted, or at EOF. If "delimiter"
  979. // is EOF, it won't match any character, and thus will not stop
  980. // extraction.
  981.  
  982.     int ipfx(int need = 0);
  983. // Perform input-prefix operations.
  984. // The argument "need" is the number of characters that will be
  985. // required, or 0 if you don't know how many you'll need. The
  986. // formatted input functions call this with 0, and the unformatted
  987. // input functions call it with 1.
  988. //      * If the error state is non-zero, this returns zero immediately.
  989. // Otherwise, it returns non-zero when finished.
  990. // If there is a tied stream (see ios::tie()) and need is 0 or greater
  991. // than the number of immediately available characters, the tied
  992. // iostream is flushed.
  993. //      * If ios::skipws is set in the format state, and "need" is zero,
  994. // leading white-space characters are thrown away and the stream
  995. // is advanced to the first available character.
  996. // This returns zero if there is an error while skipping a
  997. // white-space character, otherwise it returns non-zero.
  998.  
  999.     int peek();
  1000. // Calls ipfx(1). If that returns zero, or if the input character
  1001. // stream is at EOF, return EOF. Otherwise return the next character
  1002. // without extracting it from the input stream.
  1003.  
  1004.     istream &putback(char c);
  1005. // Attempt to push the last character read back onto the input stream.
  1006. // "c" must be the last character read. This does not call ipfx(), but
  1007. // it will return without doing anything if the error state is
  1008. // non-zero.
  1009.  
  1010.     istream &read(void *data, int size);
  1011. // Extracts a block of "size" bytes and stores them at "data".
  1012. // If EOF is reached before "size" bytes are extracted,
  1013. // ios::failbit is set. The number of characters actually
  1014. // extracted is available as the return value of istream::gcount().
  1015.  
  1016.     istream &seekg(streampos position);
  1017. // Absolute-seek the input character stream to "position".
  1018.  
  1019.     istream &seekg(streamoff offset, seek_dir direction);
  1020. // Relative-seek the input character stream by "offset" relative to
  1021. // the current position.
  1022.  
  1023.     int sync();
  1024. // Establishes consistency between the internal data structure
  1025. // and the external character source. This usually means that
  1026. // characters that were buffered for input are thrown away, and
  1027. // the file pointer is decremented so that it appears that the
  1028. // buffered characters were never read.
  1029.  
  1030.     streampos tellg();
  1031. // Return the current file position. The value returned is a magic
  1032. // cookie that is usable only as an argument to seekg(streampos).
  1033. // Don't perform arithmetic on the return value.
  1034.  
  1035.     istream &operator>>(char *);
  1036.     istream &operator>>(signed char *s)
  1037.         { return operator>>((char *) s); }
  1038.     istream &operator>>(unsigned char *s)
  1039.         { return operator>>((char *) s); }
  1040. // For the string extractors, characters are extracted until a
  1041. // white-space is seen. The white-space is pushed back on the
  1042. // incoming character stream. If width() is non-zero, it is taken
  1043. // as the size of the argument character array, and no more than
  1044. // width()-1 characters are extracted. A terminating null character
  1045. // is ALWAYS stored, even when nothing else is done because of
  1046. // the error state.
  1047.  
  1048.     istream &operator>>(char &);
  1049. // Extracts a single character, similar to get()
  1050.     istream &operator>>(signed char &c)
  1051.     { return operator>>((char) c); }
  1052.     istream &operator>>(unsigned char &c)
  1053.     { return operator>>((char) c); }
  1054.  
  1055. // Extract byte wide integers -128 - +127 and 0 - 255 respectively
  1056.  
  1057.     istream &operator>>(short &v)
  1058.         { return _2Comp::extract(*this,&v,sizeof(short)); }
  1059.     istream &operator>>(int &v)
  1060.         { return _2Comp::extract(*this,&v,sizeof(int)); }
  1061.     istream &operator>>(long &v)
  1062.         { return _2Comp::extract(*this,&v,sizeof(long)); }
  1063.     istream &operator>>(unsigned short &v)
  1064.         { return _2Comp::extract(*this,&v,sizeof(unsigned short)); }
  1065.     istream &operator>>(unsigned int &v)
  1066.         { return _2Comp::extract(*this,&v,sizeof(unsigned int)); }
  1067.     istream &operator>>(unsigned long &v)
  1068.         { return _2Comp::extract(*this,&v,sizeof(unsigned long)); }
  1069.  
  1070.     istream &operator>>(float &);
  1071.     istream &operator>>(double &);
  1072. #if macintosh
  1073.     istream &operator>>(long double &);
  1074. #endif
  1075.  
  1076.     istream &operator>>(streambuf*);
  1077. // Calls ipfx(0), and if the result is non-zero, extracts characters
  1078. // and stuffs them into the argument streambuf until EOF is reached.
  1079. // Only sets ios::badbit if it can't get at least one character.
  1080.  
  1081.     istream &operator>>(istream &(*manip)(istream&))
  1082.     {
  1083.         (*manip)(*this);
  1084.         return *this;
  1085.     }
  1086. // Provides an pseudo extractor which allows for parameterless
  1087. // manipulators. The target type is pointer to function returning
  1088. // istream reference and taking istream reference argument.
  1089.  
  1090.     istream &operator>>(ios &(*manip)(ios&))
  1091.     {
  1092.         (*manip)(*this);
  1093.         return *this;
  1094.     }
  1095. // Similar facility to manipulate an ios object directly
  1096.  
  1097. protected:
  1098.     istream();  // derived class will initialize ios directly
  1099.  
  1100. private:
  1101. friend istream &_2Comp::extract(istream&, void *, int);
  1102.  
  1103. // The extract function provides for general type translation using
  1104. // a character set string and a DFA transition table
  1105.     istream &extract(void *type, const char *okchars, int *tt,
  1106.             translate_function_t tf, helper_function_t hf, void *tds);
  1107.     size_t read_count;
  1108. };
  1109.  
  1110. istream &ws(istream&);
  1111. // Manipulator to skip white-space, positioning the input
  1112. // character stream to the first available non-white-space
  1113. // character.
  1114.  
  1115. class iostream : public istream, public ostream {
  1116.  
  1117. // A stream that supports both insertion and extraction.
  1118.  
  1119. public:
  1120.     iostream(streambuf *buf);
  1121. // Construct an iostream associated with the argument streambuf.
  1122.  
  1123.     virtual ~iostream();
  1124. protected:
  1125.     iostream(); // derived class will initialize ios directly
  1126.  
  1127. };
  1128.  
  1129. // Iostream classes that support assignment.
  1130. //
  1131. // They're used for cin, cout, cerr, and clog, but you should not use them for
  1132. // anything else. Instead, use a regular stream and assign a pointer to it.
  1133.  
  1134. class istream_withassign : public istream {
  1135. public:
  1136.     istream_withassign();
  1137.     istream_withassign(streambuf *);
  1138.     ~istream_withassign();
  1139.     istream_withassign &operator=(istream &);
  1140.     istream_withassign &operator=(istream_withassign &s) {return operator=((istream &)s);}
  1141.     istream_withassign &operator=(streambuf *);
  1142. private:
  1143.     short assigned_to;
  1144. };
  1145.  
  1146. class ostream_withassign : public ostream {
  1147. public:
  1148.     ostream_withassign();
  1149.     ostream_withassign(streambuf *);
  1150.     ~ostream_withassign();
  1151.     ostream_withassign &operator=(ostream &);
  1152.     ostream_withassign &operator=(ostream_withassign &s) {return operator=((ostream &)s);}
  1153.     ostream_withassign &operator=(streambuf *);
  1154. private:
  1155.     short assigned_to;
  1156. };
  1157.  
  1158. class iostream_withassign : public iostream {
  1159. public:
  1160.     iostream_withassign();
  1161.     iostream_withassign(streambuf *);
  1162.     ~iostream_withassign();
  1163.     iostream_withassign &operator=(ios &);
  1164.     iostream_withassign &operator=(iostream_withassign &s) {return operator=((ios &)s);}
  1165.     iostream_withassign &operator=(streambuf *);
  1166. private:
  1167.     short assigned_to;
  1168. };
  1169.  
  1170. extern istream_withassign cin;
  1171. extern ostream_withassign cout;
  1172. extern ostream_withassign cerr;
  1173. #if M_UNIX || M_XENIX
  1174. extern ostream_withassign   clog;
  1175. #endif
  1176.  
  1177. class IosTie {
  1178. public:
  1179.     IosTie(ios *a, ostream *b) {a->tie(b);
  1180. #if THINK_CPLUS
  1181.                                 ios::sync_with_stdio();
  1182. #endif
  1183.                                 }
  1184. };
  1185.  
  1186. class IosUnitbuf {
  1187. public:
  1188.     IosUnitbuf(ios *a) { a->setf(ios::unitbuf); }
  1189. };
  1190.  
  1191. #pragma pack()
  1192.  
  1193. #endif  // __IOSTREAM_H
  1194.