Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-09-14 03:55:53


John Torjo wrote:
>>PS:
>> I noticed that in earlier versions strings within STL containers
>> where surrounded by "quotes"; now, they are no longer, which causes
>> problems if a string contains ",". Why was that changed?
>>
>at my suggestion ;)
>
>I think you'd use this library a lot for pretty printing. Thus, the above
>would only hurt. You can still specify it manually (if you wish to read the
>strings back)

The problem with the system I developed was that it overrided the "natural"
behaviour of the basic format object (read/write the object to/from the
stream using >> and << respectively). As John points out, this can result in
output that looks like:

   [ "Tina", "Dana", "Eric", "Shaun" ]

when you would want:

   [ Tina, Dana, Eric, Shaun ]

I am aware of the problems reading the data back in if the string contains
spaces. Placing quotes around it requires that you escape the quotes (to
allow quotes in the string, e.g. \") and to escape the character that
signals an escaped character (e.g. \\). This can lead to "ugly" output,
e.g.:

   "This is a message \\ to say \"Hallo Welt!\" to my German friends."

when you'd want:

   This is a message \ to say "Hallo Welt!" to my German friends.

Now, consider the case when you want to generate XML or HTML-based output.
In that case, you would want something like:

   <message>This is a message \ to say "Hallo Welt!" to my German
friends.</message>

and do not need to explicitly escape the string. In this case you can do:

   namespace io = boost::io;
   std::cout << io::formatob( str, io::containerfmt()).format( "<message>",
"</message>", "" );

I am looking to add support for format objects that transform elements,
i.e.:

   OutStream & write( OutStream & os, const Elem & e )
   {
      return( os << fn( e )); // fn = transformation function
   }
   bool read( InStream & in, Elem & e )
   {
      return( inv_fn( in, e )); // inv_fn = inverse transformation function
   }

That way you can define character escapers for strings, XML, HTML and other
escaped formats. To format the string as:

   "This is a message \\ to say \"Hallo Welt!\" to my German friends."

you can do:

   std::cout << io::formatob( str,
         io::containerfmt( io::string_escaper())
      ).format( "\"", "\"", "" );

This makes the process more well formed, consistent within the framework and
explicit.

Regards,
Reece

_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today!
http://www.msn.co.uk/messenger


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk