Apache C++ Standard Library Reference Guide

## basic_stringbuf

Library: Input/output

    basic_stringbuf  basic_streambuf

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Member Types
- Nonmember Types
- Constructors
- Member Functions
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

basic_stringbuf() char_type ios_type off_type | overflow() pbackfail() pos_type seekoff() | seekpos() setbuf() str() traits_type | underflow() xsputn()

#### Non-Members

stringbuf | wstringbuf |

### Summary

Class that associates the input or output sequence with a sequence of arbitrary characters

### Synopsis

    #include <sstream>

    namespace std {
      template<class charT, class traits = char_traits<charT>,
               class Allocator = allocator<charT> >
      class basic_stringbuf;
    }

### Description

The class basic_stringbuf is derived from basic_streambuf. Its purpose is to associate the input or output sequence with a sequence of arbitrary characters. The sequence can be initialized from, or made available as, an object of class basic_string. Each object of type basic_stringbuf controls two character sequences:

- a character input sequence

- a character output sequence

See basic_streambuf for more information.

The two sequences are related to each other, but are manipulated separately. This allows you to read and write characters at different positions in objects of type basic_stringbuf without any conflict (as opposed to the basic_filebuf objects, in which a joint file position is maintained).

### Interface

    namespace std {

      template<class charT, class traits = char_traits<charT>,
               class allocator<charT> >
      class basic_stringbuf
      : public basic_streambuf<charT, traits>
      {
        public:

         typedef charT                        char_type;
         typedef typename traits::int_type    int_type;
         typedef typename traits::pos_type    pos_type;
         typedef typename traits::off_type    off_type;

         explicit basic_stringbuf(ios_base::openmode =
                                    (ios_base::in | ios_base::out));

       explicit basic_stringbuf(const basic_string<char_type,
                                traits_type, Allocator>&,
                                ios_base::openmode =
                                ios_base::in | ios_base::out);
       basic_string<char_type, traits_type, Allocator> str() const;
       void str(const basic_string<char_type, traits_type, Allocator>&);

       protected:

       virtual int_type underflow();
       virtual int_type pbackfail(int_type = traits::eof());
       virtual int_type overflow(int_type = traits::eof());
       virtual basic_streambuf<char_type, traits_type>*
       setbuf(char_type*,streamsize);
       virtual pos_type seekoff(off_type, ios_base::seekdir,
                 ios_base::openmode = ios_base::in | ios_base::out);
       virtual pos_type seekpos(pos_type,
                 ios_base::openmode = ios_base::in | ios_base::out);
       virtual streamsize xsputn(const char_type*, streamsize);
      };
    }

### Member Types

    char_type

The type `char_type` is a synonym for the template parameter `charT`.

    ios_type

The type `ios_type` is an instantiation of class basic_ios on type `charT`.

    off_type

The type `off_type` is a synonym of type `traits::off_type`.

    pos_type

The type `pos_type` is a synonym of type `traits::pos_type`.

    traits_type

The type `traits_type` is a synonym for the template parameter `traits`.

### Nonmember Types

    stringbuf

The type `stringbuf` is a specialization of class template `basic_stringbuf` on type `char`:

`typedef basic_stringbuf<char> stringbuf;`

    wstringbuf

The type `wstringbuf` is a specialization of class template `basic_stringbuf` on type `wchar_t`:

`typedef basic_stringbuf<wchar_t> wstringbuf;`

### Constructors

    explicit basic_stringbuf(ios_base::openmode which =
                    ios_base::in | ios_base::out);

Constructs an object of class basic_stringbuf, initializing the base class with `basic_streambuf()`, and initializing the open mode with `which`.

    explicit basic_stringbuf(const string_type& str,
                    ios_base::openmode which =
                    ios_base::in | ios_base::out);

Constructs an object of class basic_stringbuf, initializing the base class with `basic_streambuf()`, and initializing the open mode with `which`. The string object `str` is copied to the underlying buffer.

- If the opening mode is `in`, initializes the input sequence to point to the first character of the buffer.

- If the opening mode is `out`, initializes the output sequence to point to the first character of the buffer.

- If the opening mode is `out | app`, initializes the output sequence to point to the last character of the buffer.

### Member Functions

    int_type
    overflow(int_type c = traits_type::eof());

If the output sequence has a `put` position available, and `c` is not `traits_type::eof()`, this function writes `c` into it. If there is no position available, the function increases the size of the buffer by allocating more memory and then writes `c` at the new current `put` position. If the operation fails, the function returns `traits_type::eof()`. Otherwise, it returns `traits_type::not_eof(c)`.

    int_type
    pbackfail(int_type c = traits_type::eof());

Puts back the character designated by `c` into the input sequence. If `traits_type::eq_int_type(c,traits_type::eof())` returns `true`, the function moves the input sequence one position backward. If the operation fails, the function returns `traits_type::eof()`. Otherwise, it returns `traits_type::not_eof(c)`.

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

If the open mode is `in | out`, this function alters the stream position of both the input and the output sequences. If the open mode is `in`, it alters the stream position of only the input sequence, and if it is `out`, it alters the stream position of only the output sequence. The new position is calculated by combining the two parameters, `off` (displacement) and `way` (reference point). If the current position of the sequence is invalid before repositioning, the operation fails and the return value is `pos_type(off_type(-1))`. Otherwise, the function returns the current new position.

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

If the open mode is `in | out`, the function alters the stream position of both the input and the output sequences. If the open mode is `in`, it alters the stream position of the input sequence only, and if the open mode is `out`, it alters the stream position of the output sequence only. If the current position of the sequence is invalid before repositioning, the operation fails and the return value is `pos_type(off_type(-1))`. Otherwise, the function returns the current new position.

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

A stringbuf maintains an underlying character array for storing buffered characters. This function gives you a way to resize or replace that buffer, with certain restrictions.

First of all, `n` must be greater than the number of characters currently in the buffer. If `n` is too small, then `setbuf()` has no effect and returns a null pointer to indicate failure.

If `n` is large enough, this function has one of the following effects:

- If `s` is not a null pointer, `setbuf()` copies the buffered contents of the stringbuf into the `n` character array starting at `s,` and installs `s` as the underlying character array used by the stringbuf. `s` replaces the old underlying array. In this case, the function returns `s` on success or a null pointer on failure. The caller is responsible for deleting the buffer.

- If `s` is a null pointer and `n` is `0`, the function has no effect.

- If `s` is a null pointer, `setbuf()` resizes the underlying character array to `n` characters. The function returns a pointer to the beginning of the resized array if the operation is successful, or a null pointer if not. The object is responsible for deleting the buffer.

    basic_string<char_type, traits_type, Allocator>
    str() const;

Returns a string object of type `basic_string<char_type, traits_type, Allocator>`, whose content is a copy of the underlying buffer contents.

    void
    str(const basic_string<char_type, traits_type, Allocator>& str);

Clears the underlying buffer and copies the string object `str` into it. If the opening mode is `in`, initializes the input sequence to point to the first character of the buffer. If the opening mode is `out`, the function initializes the output sequence to point to the first character of the buffer. If the opening mode is `out | app`, it initializes the output sequence to point to the last character of the buffer.

    int_type
    underflow();

If the input sequence has a read position available, returns the contents of this position. Otherwise, it tries to expand the input sequence to match the output sequence, and if possible returns the content of the new current position, `traits_type::to_int_type(*gptr())`. The function returns `traits_type::eof()` to indicate failure.

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

Writes up to `n` characters to the output sequence. The characters written are obtained from successive elements of the array whose first element is designated by `s`. The function returns the number of characters written.

### Example

    //
    //  stringbuf.cpp
    //

    #include <iostream>   // for cout, endl
    #include <sstream>    // for istringstream, ostringstream
    #include <string>     // for string

    int main ()
    {
        // create a read/write string-stream object on tiny char
        // and attach it to an ostringstream object
        std::ostringstream out_1 (std::ios::in | std::ios::out);

        // tie the istream object to the ostringstream object
        std::istream in_1 (out_1.rdbuf ());

        // output to out_1
        out_1 << "Here is the first ouput";

        // create a string object on tiny char
        std::string string_ex = "L'heure est grave!";

        // open a read only stringstream object and initialize it
        std::istringstream in_2 (string_ex);

        // output in_1 to the standard output
        std::cout << in_1.rdbuf () << std::endl;

        // reposition in_1 at the beginning
        in_1.seekg (0);

        // output in_2 to the standard output
        std::cout << in_2.rdbuf () << std::endl;

        // reposition in_2 at the beginning
        in_2.seekg (0);

        // get the current put position (equivalent to
        // out_1.tellp())
        std::stringbuf::pos_type pos =
            out_1.rdbuf ()->pubseekoff (0, std::ios::cur,
                                        std::ios::out);

        // append the content of the stringbuf pointed to by in_2
        // to the one  pointed at by out_1
        out_1 << ' ' << in_2.rdbuf ();

        // output in_1 to the standard output
        std::cout << in_1.rdbuf () << std::endl;

        // position the get sequence (equivalent to
        // in_1.seekg (pos))
        in_1.rdbuf ()->pubseekpos (pos, std::ios::in);

        // output "L'heure est grave!"
        std::cout << in_1.rdbuf () << std::endl;

        return 0;
    }

    Program Output:

    Here is the first ouput
    L'heure est grave!
    Here is the first ouput L'heure est grave!
     L'heure est grave!

### See Also

char_traits, ios_base, basic_ios, basic_streambuf, basic_string, basic_istringstream, basic_ostringstream, basic_stringstream

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 27.7.1