C++
  Home arrow C++ arrow Page 3 - Extending the Basic Streams in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Extending the Basic Streams in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-05-19

    Table of Contents:
  • Extending the Basic Streams in C++
  • The Source of it All
  • The streambuff Class
  • Epilogue

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Extending the Basic Streams in C++ - The streambuff Class


    (Page 3 of 4 )

    The streambuffer has the following definition once you open up the streambuf header:


    class basic_streambuf

    { // control read/write buffers

    /...

    protected:

    basic_streambuf()

    : _Plocale(_NEW_CRT(locale))

    { // construct with no buffers

    _Init();

    }

    /...

    protected:

    _Elem *eback() const;

    _Elem *gptr() const;

    _Elem *pbase() const;

    _Elem *pptr() const;

    _Elem *egptr() const;

    void gbump(int _Off);

    void setg(_Elem *_First, _Elem *_Next, _Elem *_Last)

     

    _Elem *epptr() const;

    _Elem *_Gndec();

    _Elem *_Gninc();

    _Elem *_Gnpreinc();

    void pbump(int _Off);

    void setp(_Elem *_First, _Elem *_Last);

    void setp(_Elem *_First, _Elem *_Next, _Elem *_Last);

    _Elem *_Pninc();

     

    void _Init()

    { // initialize buffer parameters for no buffers

    _IGfirst = &_Gfirst, _IPfirst = &_Pfirst;

    _IGnext = &_Gnext, _IPnext = &_Pnext;

    _IGcount = &_Gcount, _IPcount = &_Pcount;

    setp(0, 0), setg(0, 0, 0);

    }


    void _Init(_Elem **_Gf, _Elem **_Gn, int *_Gc,

    _Elem **_Pf, _Elem **_Pn, int *_Pc)

    { // initialize buffer parameters as specified

    _IGfirst = _Gf, _IPfirst = _Pf;

    _IGnext = _Gn, _IPnext = _Pn;

    _IGcount = _Gc, _IPcount = _Pc;

    }


    private:

    _Elem *_Gfirst; // beginning of read buffer

    _Elem *_Pfirst; // beginning of write buffer

    _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.

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek