Boost logo

Boost Users :

Subject: Re: [Boost-users] Reading key-value pairs from a string
From: Eric Niebler (eric_at_[hidden])
Date: 2009-04-13 13:35:13


Zdravko Monov wrote:
> Dear boost-users,
>
> I am currently working on a C++ project that includes reading and
> parsing some files.
> Given a string with certain file contents S, I would like to attempt to
> compare it against a certain pattern. In particular, the contents have
> the form key : value. Certain keys are obligatory, while others may or
> may not exist (my program then supplies the default values).
> Can you recommend me a boost facility that can help me reading this type
> of data? My problem is that between key, : and value there could be
> blank spaces, tabs etc. The type of value could be a string, an int, or
> double.

It sounds like Boost.Xpressive would be a good fit for you. It's a regex
engine that lets you nest actions with the regex. Here is an example
that does pretty much exactly what you're looking for: parses a string
containing key/value pairs into a std::map.

http://www.boost.org/doc/libs/1_38_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.semantic_actions_and_user_defined_assertions

HTH,

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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