Boost logo

Boost Users :

Subject: [Boost-users] Iostreams: how can I control my (ostream) filter?
From: Akim Demaille (akim_at_[hidden])
Date: 2014-11-04 07:56:31


Hi!

I'm trying to use Boost.Iostream to create a filter that
will escape some characters (typically backslash must be
backslashed). This is to conform with the syntax of an
output format that requires some portions to be escaped
this way. But other place must be left untouched.

My problem is I don't find a means to "talk" to my filter.

My first attempt used two streams: the "real" ostream
and the filtered one that wraps it. Of course it failed:
both streams are not synchronized, and buffers are
flushed at different moments resulting in scrambled
output. I don't want to flush too often (I got to
keep my throughput high), so that's ruled out.

My second attempt was to introduce an xalloc-based custom
iomanip to enable/disable the processing in my filter.
But my problem is that in my filter, I don't know how to
reach the final ostream and fetch it pword, so I failed
to have it work. From the syntax point of view (from
the caller viewpoint) that was my favorite approach.

In my third attempt, I have to boolean variable living
in the caller frame, taken as a bool& by my filter's
actor, and checked in its "write" function to decide
whether to escape or not. This looks worrying (bool&
members don't look nice), and anyway does not work nicely,
again, with buffers: the asynchrony between setting/unsetting
the boolean and using it breaks everything.

So, what are the good options? I think I really needed
something like an iomanipulator which ensures that "sequentiality"
is preserved, but how should I do that?

Thanks in advance!

PS/ One not-good-option would be to have my filter try
to "parse" what it is outputting to decide what's the
current state, and whether it should escape or not.
First, things become uselessly complex, and second, anyway
that's not right; for instance when I'm in a string,
double-quotes should be escaped, or mark the end of the string.
The filter cannot know which kind of double-quote it just
saw, but I know, so _I_ should change the state at that time,
not it.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net