Class basic_streambuf

The template class basic_streambuf is an abstract class for deriving various stream buffers whose objects control input and output sequences. The type streambuf is an instantiation of char type. the type wstreambuf is an instantiation of wchar_t type.

The prototype is listed below. Additional topics in this section are:

basic_streambuf Constructor

The default constructor constructs an object of type basic_streambuf.

  protected:
     basic_streambuf();
  
Remarks

The constructor sets all pointer member objects to null pointers and calls getloc() to copy the global locale at the time of construction.

Destructor

  virtual ~basic_streambuf();
  
Remarks

Removes the object from memory.

basic_streambuf Public Member Functions

The public member functions allow access to member functions from derived classes.

Locales

Locales are used for encapsulation and manipulation of information to a particular locale.

basic_streambuf::pubimbue

To set the locale.

  locale pubimbue(const locale &loc);
  
Remarks

The function pubimbue calls imbue(loc).

Returns the previous value of getloc().

basic_streambuf::getloc

To get the locale.

  locale getloc() const;
  
Remarks

If pubimbue has already been called, it returns the last value of loc supplied; otherwise the current one. If pubimbue has been called but has not returned a value from imbue, it then returns the previous value.

Buffer Management and Positioning

Functions used to manipulate the buffer and the input and output positioning pointers.

basic_streambuf::pubsetbuf

To set an allocation after construction.

  basic_streambuf<char_type, traits> *pubsetbuf
     (char_type* s, streamsize n);
  
Remarks

The first argument is used in an another function by a filebuf derived class. See setbuf(). The second argument is used to set the size of a dynamic allocated buffer.

Returns a pointer to basic_streambuf<char_type, traits> via setbuf(s, n).

Listing: Example of basic_streambuf::pubsetbuf() usage:
#include <iostream>
#include <sstream>

const int size = 100; 

char temp[size] = "\0";

int main()

{

using namespace std;

   

   stringbuf strbuf; 

   strbuf.pubsetbuf('\0', size);

   strbuf.sputn("CodeWarrior",50);

   strbuf.sgetn(temp, 50);

   cout << temp;

   return 0;

}

Result:

  CodeWarrior
  

basic_streambuf::pubseekoff

Determines the position of the get pointer.

  pos_type pubseekoff

     (off_type off,

     ios_base::seekdir way, ios_base::openmode 

     which = ios_base::in | ios_base::out);  
Remarks

The member function pubseekoff() is used to find the difference in bytes of the get pointer from a known position (such as the beginning or end of a stream). The function pubseekoff() returns a type pos_type which holds all the necessary information.

Returns a pos_type via seekoff(off, way, which)

See Also

pubseekpos()

Listing: Example of basic_streambuf::pubseekoff() usage:
// The ewl-test file contains originally
// CodeWarrior "Software at Work"

#include <iostream>
#include <fstream> 
#include <stdlib.h>
char inFile[] = "ewl-test";

int main()
{
using namespace std;
   ifstream inOut(inFile, ios::in | ios::out);
   if(!inOut.is_open()) 
      {cout << "Could not open file"; exit(1);}

   ostream Out(inOut.rdbuf());
   char str[] = "\nRegistered Trademark";
   inOut.rdbuf()->pubseekoff(0, ios::end);
   Out << str;
   inOut.close();
   return 0;
}

Result:

  The File now reads:
  CodeWarrior "Software at Work"
  Registered Trademark

basic_streambuf::pubseekpos

Determine and move to a desired offset.

  pos_type pubseekpos

     (pos_type sp,

     ios_base::openmode which = ios::in |ios::out);  
Remarks

The function pubseekpos() is use to move to a desired offset using a type pos_type, which holds all necessary information.

Returns a pos_type via seekpos(sb, which)

See Also

pubseekoff(), seekoff()

Listing: Example of streambuf::pubseekpos() usage:
// The file ewl-test contains:
// ABCDEFGHIJKLMNOPQRSTUVWXYZ

#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{
using namespace std;
   ifstream in("ewl-test");
   if(!in.is_open())
      {cout << "could not open file"; exit(1);} 
   streampos spEnd(0), spStart(0), aCheck(0);
   spEnd = spStart = 5;
   aCheck = in.rdbuf()->pubseekpos(spStart,ios::in);

   cout << "The offset at the start of the reading"
         << " in bytes is " 
         << static_cast<streamoff>(aCheck) << endl;

   char ch;
   while(spEnd != spStart+10) 
   {
      in.get(ch);
      cout << ch;
      spEnd = in.rdbuf()->pubseekoff(0, ios::cur);
   }

   aCheck = in.rdbuf()->pubseekoff(0,ios::cur);
   cout << "\nThe final position's offset"
         << " in bytes now is "
         << static_cast<streamoff>(aCheck) << endl;

   in.close();
   return 0;
}

Result:

  The offfset for the start of the reading in bytes is 5
  FGHIJKLMNO
  The final position's offset in bytes now is 15  

basic_streambuf::pubsync

To synchronize the streambuf object with its input/output.

  int pubsync();
  
Remarks

The function pubsync() will attempt to synchronize the streambuf input and output.

Returns zero if successful or EOF if not via sync().

Listing: Example of streambuf::pubsync() usage:
#include <iostream>
struct address {

   int number;

   char street[40];

}addbook;

int main()

{ 

using namespace std;

   cout << "Enter your street number: ";

   cin >> addbook.number;

   cin.rdbuf()->pubsync(); // buffer flush

   cout << "Enter your street name: "; 

   cin.get(addbook.street, 40);

   cout << "Your address is: "

         << addbook.number << " " << addbook.street;

   return 0;

}

Result:

  Enter your street number: 2201
  Enter your street name: Donley Drive
  Your address is: 2201 Donley Drive
  

Get Area

Public functions for retrieving input from a buffer.

basic_streambuf::in_avail

To test for availability of input stream.

  streamsize in_avail();
  
Remarks

If a read is permitted returns size of stream as a type streamsize.

basic_streambuf::snextc

To retrieve the next character in a stream.

  int_type snextc();
  
Remarks

The function snextc() calls sbumpc() to extract the next character in a stream. After the operation, the get pointer references the character following the last character extracted.

If sbumpc returns traits::eof, otherwise returns sgetc().

Listing: Example of streambuf::snextc() usage:
#include <iostream>
#include <sstream>

const int size = 100; 

int main()

{

using namespace std;

   stringbuf strbuf; 

   strbuf.pubsetbuf('\0', size);

   strbuf.sputn("ABCDE",50);

   char ch;

               // look ahead at the next character

   ch =strbuf.snextc(); 

   cout << ch;

      // get pointer was not returned after peeking

   ch = strbuf.snextc();

   cout << ch;

   return 0;

}

Result:

  BC
  

basic_streambuf::sbumpc

To move the get pointer.

int_type sbumpc();

Remarks

The function sbumpc() moves the get pointer one element when called.

Return

The value of the character at the get pointer. It returns uflow() if it fails to move the pointer.

See Also

sgetc()

Listing: Example of streambuf::sbumpc() usage:
#include <iostream>
#include <sstream> 

const int size = 100;
std::string buf = "CodeWarrior --Software at Work--"; 

int main()
{    
using namespace std;
   stringbuf strbuf(buf);
   int ch;
   for (int i = 0; i < 23; i++)
   {
      ch = strbuf.sgetc();
      strbuf.sbumpc();
      cout.put(ch);
   }

   cout << endl;
   cout << strbuf.str() << endl;
   return 0;
}

Result:

  CodeWarrior 
  CodeWarrior --Software at Work-- 

basic_streambuf::sgetc

To extract a character from the stream.

  int_type sgetc();
  
Remarks

The function sgetc() extracts a single character, without moving the get pointer.

A int_type type at the get pointer if available, otherwise returns underflow().

For an example of streambuf::sgetc() usage refer to streambuf::sbumpc()

basic_streambuf::sgetn

To extract a series of characters from the stream.

  streamsize sgetn(char_type *s, streamsize n);
  
Remarks

The public member function sgetn() is used to extract a series of characters from the stream buffer. After the operation, the get pointer references the character following the last character extracted.

Returns a streamsize type as returned from the function xsgetn(s,n).

For an example of streambuf::sgetn() usage refer to pubsetbuf()

Putback

Public functions to return a value to a stream.

basic_streambuf::sputbackc

To put a character back into the stream.

  int_type sputbackc(char_type c);

  
Remarks

The function sputbackc() will replace a character extracted from the stream with another character. The results are not assured if the putback is not immediately done or a different character is used.

If successful, returns a pointer to the get pointer as an int_type otherwise returns pbackfail(c).

Listing: Example of streambuf::sputbackc() usage:
#include <iostream>
#include <sstream>

std::string buffer = "ABCDEF";

int main()

{ 

using namespace std;

   stringbuf strbuf(buffer);    

   char ch;

   

   ch = strbuf.sgetc(); // extract first character

   cout << ch;       // show it

      //get the next character 

   ch = strbuf.snextc(); 

   // if second char is B replace first char with x 

   if(ch =='B') strbuf.sputbackc('x'); 

      // read the first character now x

   cout << (char)strbuf.sgetc();    

   strbuf.sbumpc();       // increment get pointer

       // read second character

   cout << (char)strbuf.sgetc();

   strbuf.sbumpc();       // increment get pointer

      // read third character

   cout << (char)strbuf.sgetc(); 

      

      // show the new stream after alteration

   strbuf.pubseekoff(0, ios::beg);

   cout << endl;

   

   cout << (char)strbuf.sgetc();

   

   while( (ch = strbuf.snextc()) != EOF) 

      cout << ch;

   

   return 0;

}

Result:

  AxBC

  
  xBCDEF

  

basic_streambuf::sungetc

To restore a character extracted.

  int_type sungetc();

  
Remarks

The function sungetc() restores the previously extracted character. After the operation, the get pointer references the last character extracted.

If successful, returns a pointer to the get pointer as an int_type otherwise returns pbackfail(c).

For an example of streambuf::sungetc() usage refer to streambuf::sputbackc()

Put Area

Public functions for inputting characters into a buffer.

basic_streambuf::sputc

To insert a character in the stream.

  int_type sputc(char_type c);
  
Remarks

The function sputc() inserts a character into the stream. After the operation, the get pointer references the character following the last character inserted.

If successful, returns c as an int_type otherwise returns overflow(c).

Listing: Example of streambuf::sputc() usage:
#include <iostream>
#include <sstream>

int main()

{ 

using namespace std;

   stringbuf strbuf; 

   strbuf.sputc('A');

   char ch;

   ch = strbuf.sgetc();

   cout << ch;

   return 0;

}

Result:

  A
  

basic_streambuf::sputn

To insert a series of characters into a stream.

  int_type sputn(char_type *s, streamsize n);
  
Remarks

The function sputn() inserts a series of characters into a stream. After the operation, the get pointer references the character following the last character inserted.

Returns a streamsize type returned from a call to xputn(s,n).

basic_streambuf Protected Member Functions

Protected member functions that are used for stream buffer manipulations by the basic_streambuf class and derived classes from it.

Get Area Access

Member functions for extracting information from a stream.

basic_streambuf::eback

Retrieve the beginning pointer for stream input.

  char_type* eback() const;
  
Remarks

Returns the beginning pointer.

basic_streambuf::gptr

Retrieve the next pointer for stream input.

  char_type* gptr() const;
  
Remarks

Returns the next pointer.

basic_streambuf::egptr

Retrieve the end pointer for stream input.

  char_type* egptr() const;
  
Remarks

Returns the end pointer.

basic_streambuf::gbump

Advances the next pointer for stream input.

  void gbump(int n);
  
Remarks

The function gbump() advances the input pointer by the value of the int n argument.

basic_streambuf::setg

To set the beginning, next and end pointers.

  void setg
     (char_type *gbeg,
     char_type *gnext, 
     char_type *gend);
  
Remarks

After the call to setg() the gbeg pointer equals eback(), the gnext pointer equals gptr(), and the gend pointer equals egptr().

Put Area Access

Protected member functions for stream output sequences.

basic_streambuf::pbase

To retrieve the beginning pointer for stream output.

  char_type* pbase() const;
  
Remarks

Returns the beginning pointer.

basic_streambuf::pptr

To retrieve the next pointer for stream output.

  char_type* pptr() const;
  
Remarks

Returns the next pointer.

basic_streambuf::epptr

To retrieve the end pointer for stream output.

  char_type* epptr() const;
  
Remarks

Returns the end pointer.

basic_streambuf::pbump

To advance the next pointer for stream output.

  void pbump(int n);
  
Remarks

The function pbump() advances the next pointer by the value of the int argument n.

basic_streambuf::setp

To set the values for the beginning, next and end pointers.

  void setp
     (char_type* pbeg, 
     char_type* pend);
  
Remarks

After the call to setp(), pbeg equals pbase(), pbeg equals pptr() and pend equals epptr().

basic_streambuf Virtual Functions

The virtual functions in basic_streambuf class are to be overloaded in any derived class.

Locales

To get and set the stream locale. These functions should be overridden in derived classes.

basic_streambuf::imbue

To change any translations base on locale.

  virtual void imbue(const locale &loc);
  
Remarks

The imbue() function allows the derived class to be informed in changes of locale and to cache results of calls to locale functions.

Buffer Management and Positioning

Virtual functions for positioning and manipulating the stream buffer. These functions should be overridden in derived classes.

basic_streambuf::setbuf

To set a buffer for stream input and output sequences.

  virtual basic_streambuf<char_type, traits> *setbuf
     (char_type* s, streamsize n);
  
Remarks

The function setbuf() is overridden in basic_stringbuf and basic_filebuf classes.

Returns the this pointer.

basic_streambuf::seekoff

To return an offset of the current pointer in an input or output stream.

  virtual pos_type seekoff
     (off_type off,
     ios_base::seekdir way, 
     ios_base::openmode which = ios::in |ios::out);
  
Remarks

The function seekoff() is overridden in basic_stringbuf and basic_filebuf classes.

Returns a pos_type value, which is an invalid stream position.

basic_streambuf::seekpos

To alter an input or output stream position.

  virtual pos_type seekpos
     (pos_type sp,
     ios_base::openmode which = ios::in |ios::out);
  
Remarks

The function seekpos() is overridden in basic_stringbuf and basic_filebuf classes.

Returns a pos_type value, which is an invalid stream position.

basic_streambuf::sync

To synchronize the controlled sequences in arrays.

  virtual int sync();
  
Remarks

If pbase() is non null the characters between pbase() and pptr() are written to the control sequence. The function setbuf() overrides the basic_filebuf class.

Returns zero if successful and -1 if failure occurs.

Get Area

Virtual functions for extracting information from an input stream buffer. These functions should be overridden in derived classes.

basic_streambuf::showmanyc

Shows how many characters in an input stream

  virtual int showmanyc();
  
Remarks

The function returns zero for the default behavior. Derived classes may return a negative one or a non-negative value. A positive value estimates the number of characters available in the sequence. If a positive value is returned, then successive calls to underflow() will not return traits::eof() until at least that number of characters have been extracted from the stream. If showmanyc() returns -1, then calls to underflow() or uflow() will fail.

Note that underflow or uflow might fail by throwing an exception prematurely. The intention is that the calls will not return eof() and will return immediately.

basic_streambuf::xsgetn

To read a number of characters from an input stream buffer.

  virtual streamsize xsgetn
     (char_type *s, streamsize n);
  
Remarks

The characters are read by repeated calls to sbumpc() until either n characters have been assigned or EOF is encountered.

Returns the number of characters read.

basic_streambuf::underflow

To show an underflow condition and not increment the get pointer.

  virtual int_type underflow();
  
Remarks

The function underflow() is called when a character is not available for sgetc().

There are many constraints for underflow().

The pending sequence of characters is a concatenation of end pointer minus the get pointer plus some sequence of characters to be read from input.

The result character if the sequence is not empty, the first character in the sequence or the next character in the sequence.

The backup sequence if the beginning pointer is null, the sequence is empty, otherwise the sequence is the get pointer minus the beginning pointer.

Returns the first character of the pending sequence and does not increment the get pointer. If the position is null returns traits::eof() to indicate failure.

basic_streambuf::uflow

To show a underflow condition for a single character and increment the get pointer.

  virtual int_type uflow();
  
Remarks

The function uflow() is called when a character is not available for sbumpc().

The constraints are the same as underflow(), with the exceptions that the resultant character is transferred from the pending sequence to the back up sequence and the pending sequence may not be empty.

Calls underflow() and if traits::eof is not returned returns the integer value of the get pointer and increments the next pointer for input.

Putback

Virtual functions for replacing data to a stream. These functions should be overridden in derived classes.

basic_streambuf::pbackfail

To show a failure in a put back operation.

  virtual int_type pbackfail
     (int_type c = traits::eof());
  
Remarks

The resulting conditions are the same as the function underflow().

The function pbackfail() is only called when a put back operation really has failed and returns traits::eof. If success occurs the return is undefined.

Put Area

Virtual function for inserting data into an output stream buffer. These functions should be overridden in derived classes.

basic_streambuf::xsputn

Write a number of characters to an output buffer.

  virtual streamsize xsputn
     (const char_type *s,streamsize n);
  
Remarks

The function xsputn() writes to the output character by using repeated calls to sputc(c). Write stops when n characters have been written or EOF is encountered.

Returns the number of characters written in a type streamsize.

basic_streambuf::overflow

Consumes the pending characters of an output sequence.

  virtual int_type overflow
  (int_type c = traits::eof());
  
Remarks

The pending sequence is defined as the concatenation of the put pointer minus the beginning pointer plus either the sequence of characters or an empty sequence, unless the beginning pointer is null in which case the pending sequence is an empty sequence.

This function is called by sputc() and sputn() when the buffer is not large enough to hold the output sequence.

Overriding this function requires that:

When overridden by a derived class how characters are consumed must be specified.

After the overflow either the beginning pointer must be null or the beginning and put pointer must both be set to the same non-null value.

The function may fail if appending characters to an output stream fails or failure to set the previous requirement occurs.

The function returns traits::eof() for failure or some unspecified result to indicate success.