I have an iterative algorithm, written in C++. I am using yaml-cpp. On each iteration I send send some data to a YAML::Emitter object. When the algorithm terminates I use YAML::Emitter::c_str() to write the underlying buffer to an ofstream.
However, I would prefer to write the buffer to the file incrementally every few hundred iterations and on each increment clear the written data from the YAML::Emitter object. There are two reasons for this:
1) In case the program terminates unexpectedly, I want to have access to (as much as possible of) the output on disk.
2) The YAML::Emitter object self-expands and I don't want to waste memory.
What's the best way to go about this?
It sounds like you'd like a pluggable "writer" for the YAML::Emitter
- if so, please file a feature request at http://code.google.com/p/yaml-cpp/issues/list.
(I can't guarantee how quickly I'll get to it, but I'd be happy to accept patches as well.)
In the meantime, you can tag-team the emitter's c_str()
and size()
methods to almost do what you want (everything except clearing the emitter's memory):
YAML::Emitter emitter;
std::size_t bytesWritten = 0;
while(1) {
fetch_more_data(emitter);
file.append(emitter.c_str() + bytesWritten);
bytesWritten = emitter.size();
}