Abdelrazak Younes wrote:

> Georg Baum wrote:
>> A char is not a "character in the stream sense" if you want to output it
>> to a 4 byte wide stream.
>> Look e.g. at this code from insetnote.C:
>> 
>>         ostringstream ss;
>>         ss << "[";
>>         InsetText::plaintext(buf, ss, runparams);
>>         ss << "]";
>> 
>> Here you need to widen '[' and ']' if ss becomes an ucs4 stringstream.
>> Here "[" is used instead of '[', but the principle is the same.
> 
> Ah but you are talking about ostringstream here not ostream. This is a
> different problem: if ss was an ostream, ss << "]" would really output a
> single one-bite char and that's what we want.

I do not make any difference between ofstream and ostringstream. No
operator<< knows the exact type of stream. It is simply a stream derived
from ostream, and it does not matter whether it is connected to a file or a
string buffer.

> So, IIUC, you want to use the same method (plaintext()) for
> ostringstream or ostream output types... if so, now I see the problem.

Not exactly. I want to convert the plaintext() method to use an ucs4
ostream. This is needed because plaintext() is used in some places to
produce a string, and we do not want to convert to utf8 in plaintext(),
output to a 1byte wide ostream and then convert back to ucs4.

> But I you look a bit more at this code, you don't really need the
> ostringstream here, this code below:
> 
> ostringstream ss;
> ss << "[";
> InsetText::plaintext(buf, ss, runparams);
> ss << "]";
> 
> string const str = ss.str();
> os << str;
> 
> could be reduced to that:
> 
> os << "[";
> InsetText::plaintext(buf, os, runparams);
> os << "]";

No it could not. I snipped something:

        string const str = ss.str();
        os << str;
        // Return how many newlines we issued.
        return int(lyx::count(str.begin(), str.end(),'\n'));

> And the nice thing about it is that you don't need to do any widening on
> the ] and [ characters if os stays an std::ostream.

But it will not. Yes, this example is for plaintext output that finally goes
to a file, but I have seen others (don't find them at the moment) where a
string is constructed using an ostringstream that will become a ucs4
ostringstream.

> Thanks for the explanation. I hope I am not wasting your time.

I hope you will be able to help with the conversion. If yes, then that time
is well invested ;-)


Georg

Reply via email to