Reading key-value pairs from a string

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading key-value pairs from a string

Zdravko Monov
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.

Thanks in advance!
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Reading key-value pairs from a string

Igor R.
> 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.

On the "current release" page you can filter libraries by category.
Eg., there's "String and text processing" category, and these are the
libraries that belong to it:
http://www.boost.org/doc/libs/1_38_0?view=category_String
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Reading key-value pairs from a string

Eric Niebler
In reply to this post by Zdravko Monov
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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users