The basic stream library (iostream) is designed to resolve most of the general problems you may run into during your programming endeavors. However, sometimes it simply isn't enough, and you need to extend it to suit your application—your special situation. This article will show you how to do that.
_Elem **_IGfirst; // pointer to beginning of read buffer
_Elem **_IPfirst; // pointer to beginning of write buffer
_Elem *_Gnext; // current position in read buffer
_Elem *_Pnext; // current position in write buffer
_Elem **_IGnext; // pointer to current position in read
_Elem **_IPnext; // pointer to current position in write
int _Gcount; // length of read buffer
int _Pcount; // length of write buffer
int *_IGcount; // pointer to length of read buffer
int *_IPcount; // pointer to length of write buffer
This is, of course, a simpler version; I have omitted a lot of functions as these alone are the ones on which we want to focus. The figure at the end of the previous page already gave a hint as to how this is built up, and the code should make that crystal clear.
Any I/O stream will have two buffers: a get area and a put area. An ifstream will have a get, an ofstream will have a put and of course, the iostream will have both of them. The buffers may be overlapped also, as is the case with files(filebuffer).
The base() and ebuf() functions will return the pointers within which the stream buffer holds its data. Inside these, you will find two areas: one for the input data and one for the output streams (get/put).
These on their own are delimited into an area and have a pointer for the current position inside them. On the get area the eback () and egptr () functions delimit the boarders, while the gptr () handles the current positions pointer.
The same rules apply for the put area, delimited by the pbase () and epptr () functions. Returned pointers are separate; pointers returned by the function pptr () will show the current position.
There exist two _Init() functions to handle the initialization of this pointer. The default will just assign each pointer to NULL and will make the stream unbuffered.
Unbuffered streams will not save any of the input directly; rather, they will just push on further for each character. The second function has four pointers to delimit the borders for the put and get area, and in addition, two int* pointers that correspond to the size of the areas.
The two areas are different from each other, and while the one of them is buffered, the other can be unbuffered -- or any other combination you can imagine. When choosing between buffered and unbuffered versions, keep in mind that the buffered version can have many advantages of performance improvement, as it will minimize the number of system calls, as I explained in the Introduction to Streams article.
The determination of the stream you use, whether they are buffered or not, is reduced to checking if the corresponding pointers are or are not null. Of course, putting/reading data from either of the areas is allowed only if the pointers are valid, and they are in the corresponding sequence. For instance, if gptr() < egptr() you are free to read the data, and with it, move after each char and read the current position pointer.
The gbump(), _Gndec(), _Gninc(), _Gnpreinc(), _Pninc(), pbump() functions are for handling these pointers by increasing/decreasing them or more exactly moving the current position pointer in the areas. Here you should notice the lack of the decrease function for the put area.
This is because the developer team thought that this can lead to unexpected errors and did not want to encourage this. You see, this can lead to data being overwritten, and with it, corrupted data for a stream introduced earlier. Reading old data will cause no harm to anyone, but producing data corruption is certainly going to do so.