streambuf

how do i create an ostream/streambuf?

For educational purposes i want to create a ostream and stream buffer to do A) fix endians when doing << myVar; B) store in a deque container instead of using std:cout or writing to a file C) log extra data, such as how many times i did <<, how many times i did .write, the amount of bytes i written and how many times i flush(). But i do ...

redirect std::cout to a custom writer

I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog <<...

Copy a streambuf's contents to a string

Apparently boost::asio::async_read doesn't like strings, as the only overload of boost::asio::buffer allows me to create const_buffers, so I'm stuck with reading everything into a streambuf. Now I want to copy the contents of the streambuf into a string, but it apparently only supports writing to char* (sgetn()), creating an istream with...

Binary version of iostream

I've been writing a binary version of iostreams. It essentially allows you to write binary files, but gives you much control over the format of the file. Example usage: my_file << binary::u32le << my_int << binary::u16le << my_string; Would write my_int as a unsigned 32-bit integer, and my_string as a length-prefixed string (where the...

initializing a C++ std::istringstream from an in memory buffer ?

I have a memory block (opaque), that I want to store in a Blob in mySQL through their C++ adapter. The adapter expects a istream: virtual void setBlob(unsigned int parameterIndex, std::istream * blob) = 0; So my question is: how can I create a std::istream from this memory block (typed as char*). It's not a string as it is not null-te...

Threadsafe logging

Hi All! I want to implement a simple class for logging from multiple threads. The idea there is, that each object that wants to log stuff, receives an ostream-object that it can write messages to using the usual operators. The desired behaviour is, that the messages are added to the log when the stream is flushed. This way, messages wil...

C++: Best text accumulator

Text gets accumulates piecemeal before being sent to client. Now we use own class that allocates memory for each piece as char massive. (Anyway, works like char[][] + std::list<char*>). Then we build the whole string, convert it into std::sting and then create boost::asio::streambuf using it. That's slow enough, I assume. Correct me if...

Deriving streambuf or basic_ostringstream?

Hi, I want to derive a stringstream so that I can use the operator<< to construct a message which will then be thrown. The API would look like: error("some text") << " more text " << 42 << std::endl; This should do a throw "some text more text 42" So what I did is make an errorbuf (inheriting from streambuf) which overloads the 'ov...

How do I build a filtered_streambuf based on basic_streambuf?

I have a project that requires me to insert a filter into a stream so that outgoing data will be modified according to the filter. After some research, it seems that what I want to do is create a filtered_streambuf like this: template <class StreamBuf> class filtered_streambuf: public StreamBuf { ... } And then insert a filtered_strea...

Reading from serial port with Boost Asio?

Hi! I'm going to check for incoming messages (data packages) on the serial port, using Boost Asio. Each message will start with a header that is one byte long, and will specify which type of the message has been sent. Each different type of message has an own length. The function I'm about to write should check for new incoming messages ...

std::ostream interface to an OLE IStream

I have a Visual Studio 2008 C++ application using IStreams. I would like to use the IStream connection in a std::ostream. Something like this: IStream* stream = /*create valid IStream instance...*/; IStreamBuf< WIN32_FIND_DATA > sb( stream ); std::ostream os( &sb ); WIN32_FIND_DATA d = { 0 }; // send the structure along the IStream os...

copying from a std::istreambuf_iterator<> to a std::vector<>

I have a Visual Studio 2008 C++ application where I would like to treat a stream as a set of iterators. For example, if I were to receive an array of WIN32_FIND_DATA structures over the stream, I would like to be able to do something like this: IStreamBuf< WIN32_FIND_DATA > sb( stream ); std::vector< WIN32_FIND_DATA > buffer; std::copy...

Deriving from streambuf without rewriting a corresponding stream

Hello, Some days ago, I decided that it would be fun to write a streambuf subclass that would use mmap and read-ahead. I looked at how my STL (SGI) implemented filebuf and realized that basic_filebuf contains a FILE*. So inheriting from basic_filebuf is out of the question. So I inherited from basic_streambuf. Then i wanted to bind my ...

How do I implement seekg() for a custom istream/streambuf?

I used to be a C++ expert a decade ago, but for the past 10 years I've been programming Java. I just started a C++ project that uses a small third-party XML parser. The XML parser accepts an STL istream. My XML data is coming from a Windows COM IStream. I thought I'd do the Right Thing and create an adapter to take the IStream data and p...

Is it safe to manipulated streambuf after doing boost::asio::async_read?

I know it's not safe to manipulated streambuf while async_write working as stated by asio author on boost mailing list. What I want to know is, is it safe to manipulated streambuf after async_read? Example: async_read(socket_, recv_streambuf_, ...); // manipulated while async_read is working // for example, after I call async_read, rec...

Query regarding overflow function of streambuf

hi Going thorugh overflow function documentation. I found overflow has following as return values. Return Value: A value different than EOF (or traits::eof() for other traits) signals success. If the function fails, either EOF (or traits::eof() for other traits) is returned or an exception is thrown. source :"http://www.cplusplus.com...

boost::asio::async_read and boost::asio::streambuf

I am using async_read with streambuf. However, I would like to limit the amount of data read to 4, so I can properly handle header before going to body. How can I do that using async_read? ...