The STL+ C++ library
Andy Rushton
Sometimes it is useful to be able to redirect output or source input from an in-memory string. That is the purpose of this package. It provides costomisations for the TextIO input and output devices that work on a single string.
Note that for large I/O requirements, the string_vectorio package might be more appropriate.
The most common and simplest way of using string output devices is to used the derived class ostext. This is a derivation of otext and can be used wherever otext would be used. For example:
ostext my_output;
Once an output string device has been opened, it can be used as the base class otext device, giving access to all of the output chevron operators (<<). See the TextIO Documentation for details.
For example:
my_output << "Hello World" << endl;
This fills in the string with the text "Hello World\n".
The internal string which receives the output text can be accessed by the get_string function.
string& result = my_output.get_string();
The string output device has the same newline conversion functions as any TextIO output device, so the '\n' character can be mapped onto any OS-specific convention. For example:
ostext my_output; my_output.set_newline_mode(textio_output_unix);
This example forces Unix conventions on the output string.
Input files are used in a very similar way to output files. The main difference is that, with input strings, the input device needs to be initialised with the string contents by passing a string to the constructor.
For example:
istext my_input("Hello World");
This device can now be used as an input to a chevron expression to extract the elements of the string. For example, the >> operator for a string gets the next non-whitespace sequence from a device. Thus the words can be split very easily:
string first_word; my_input >> first_word; string second_word; my_input >> second_word;
The input string device has the concept of end-of-file in that the device eof() function returns true if all of the string has been read.