P
penguish
Hello,
I wanted an iomanipulator to do something like this:
....
string myxml("<root><text format=\"plain\">hello world</text></root>";
cout<<xmlescape<<myxml;
....
with output:
<root><text format="plain">hello world</
text></root>
(i.e. <, >, &, ' and " become their equivalent entities)
I found a kind-of solution which I don't like so much and I wonder if
someone has a better idea how this can be achieved. It seems to me
that the iomanipulators can't look ahead in the ostream and see what's
coming and then replace it; and although I know how to install facets
for numbers (num_put) by 'imbuing' the locale, I can't see a way to
reformat the character output.
On of my kind-of solutions is to wrap the string in a simple class
holding the string as a reference in a reference data member; then I
define an inserter for the new class which transforms the string using
for_each with string iterators.
XmlWrapper w(myxml);
cout<<w;
for some reason I can't simply use a temporary, so this doesnt work:
cout<<XmlWrapper(myxml);
My other (and currently used) solution is to define a new ostream
with a new unbuffered stream class which overrides the 'overflow'
method so that xml-escape characters become entities; so my final code
looks like
MyXmlStreamBuffer xbufstream;
std:
stream xstr(&xbufstream);
xstr<<myxml;
Is there a solution closer to my original intention? e.g. is it
possible to use the iword/pword information in combination with some
method I am unaware of which can change the characters on output?
I wanted an iomanipulator to do something like this:
....
string myxml("<root><text format=\"plain\">hello world</text></root>";
cout<<xmlescape<<myxml;
....
with output:
<root><text format="plain">hello world</
text></root>
(i.e. <, >, &, ' and " become their equivalent entities)
I found a kind-of solution which I don't like so much and I wonder if
someone has a better idea how this can be achieved. It seems to me
that the iomanipulators can't look ahead in the ostream and see what's
coming and then replace it; and although I know how to install facets
for numbers (num_put) by 'imbuing' the locale, I can't see a way to
reformat the character output.
On of my kind-of solutions is to wrap the string in a simple class
holding the string as a reference in a reference data member; then I
define an inserter for the new class which transforms the string using
for_each with string iterators.
XmlWrapper w(myxml);
cout<<w;
for some reason I can't simply use a temporary, so this doesnt work:
cout<<XmlWrapper(myxml);
My other (and currently used) solution is to define a new ostream
with a new unbuffered stream class which overrides the 'overflow'
method so that xml-escape characters become entities; so my final code
looks like
MyXmlStreamBuffer xbufstream;
std:
xstr<<myxml;
Is there a solution closer to my original intention? e.g. is it
possible to use the iword/pword information in combination with some
method I am unaware of which can change the characters on output?