Pickles
Pickles

Reputation: 1390

Artificially limited ostream

I'm writing an object serialization library that couples to std::ostreams. Depending on the underlying streambuf, some ostreams support seek operations and some don't. Additionally, some streams write to a fixed-size sink and some will grow (fstream, stringstream, etc.).

In my case, I'm working on an embedded system, and I want to be very protective of my resources. I would like to impose an arbitrary maximum limit on the number of bytes I am allowed to write, in a stream-independent manner.

I've written custom streambufs before (one wrapping zlib, one wrapping the sqlite blob api). I suppose I could write another custom wrapping streambuf just for this limiting purpose. Does anyone have any other suggestions? If I have to write another streambuf, is there a trivial way to keep count of number of actual bytes written to the underlying stream?

Limitations:
can't use boost (embedded platform)
should work for any standard ostream (fstream, stringstream, cout, etc)

Upvotes: 2

Views: 222

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129454

So, you need to implement your own streambuf::overflow, and return EOF if the limit is reached.

Upvotes: 1

Related Questions