BDE 4.14.0 Production release
|
Provide an input basic_streambuf using a client buffer.
This component defines a class, bdlsb::FixedMemInStreamBuf
, that implements the input portion of the bsl::basic_streambuf
protocol using a client-supplied memory buffer. Method names necessarily correspond to the protocol-specified method names. Clients supply the character buffer at stream buffer construction, and can later reinitialize the stream buffer with a different character buffer by calling the pubsetbuf
method.
This component provides none of the output-related functionality of basic_streambuf (see Streaming Architecture, below), nor does it use locales in any way.
Stream buffers are designed to decouple device handling from content formatting, providing the requisite device handling and possible buffering services, and leaving the formatting to the client stream. The standard C++ IOStreams library further partitions streaming into input streaming and output streaming, separating responsibilities for each at both the stream layer and the stream buffer layer. The BDE streaming library for bdex
, including all of bdlsb
, follows this model.
This section illustrates intended use of this component.
bdlsb::FixedMemInStreamBuf
can be used in situations when you already have an array of bytes in memory and you'd like to wrap it in an input stream to extract data in a formatted manner. A bdlsb::FixedMemInStreamBuf
object refers to an externally managed buffer that is supplied either at construction, or using the pubsetbuf
method of the bsl::streambuf
base-class.
First, we create an array of characters to provide data that needs to be parsed, and construct bdlsb::FixedMemInStreamBuf
on that array:
Notice that bdlsb::FixedMemInStreamBuf
can be used with buffers referring to stack memory or to heap memory.
Then, we use buffer
to construct a bsl::istream
:
Finally, we can input the data from the stream in a formatted manner:
This example illustrates scanning of the input stream buffer for particular pattern ( digits, in our case ) and then using stream to read out found number.
First, we create an array of characters to provide data that needs to be parsed, and construct bdlsb::FixedMemInStreamBuf
on that array:
Then, we use buffer
to construct a bsl::istream
that will be used later to read found number:
Next, we scan input buffer one character at a time searching for the first digit:
Now, when the digit character is found, we return the first digit into the input stream buffer for subsequent read:
Finally, we read out the whole number: