libpniio
Modules | Data Structures
Parsers: creating objects from strings
Collaboration diagram for Parsers: creating objects from strings:

Modules

 Internal classes used by the parsers
 

Data Structures

class  pni::io::parser< T, ITERT >
 parser for primitive types More...
 

Detailed Description

These classes are dedicated for generating objects from strings using the boost spirit parser framework. The heart of the parser fraemwork is the parser<T,ITERT> template. To use it include pni/io/parsers.hpp in your source file. The pni::io::parser<T,ITERT> template currently works for

In general the usage of the template is fairly easy. The following code snipped reads a single scalar from a string

#include <pni/core/types.hpp>
using namespace pni::core;
using namespace pni::io;
typedef parser<float64> float64_parser_type;
int main(int,char **)
{
float64 data;
string input;
float64_parser_type p;
std::cin>>input;
data = p(input);
return 0;
}

The ITERT template parameter normaly must not be set by the user. Its default value is pni::core::string::const_iterator which should meet the most common use cases. In the same manner as a scalare one can read a comman separated list of floating point numbers from a string

#include <pni/core/types.hpp>
#include <vector>
using namespace pni::core;
using namespace pni::io;
typedef std::vector<float64> f64_v_type;
typedef parser<f64_v_type> f64_v_parser_type;
int main(int, char**)
{
f64_v_type data;
string input;
f64_v_parser_type p(container_io_config(','));
std::cin << input;
data = p(input);
return 0;
}

When parsing a list of items to a vector all whitespace characters before or after the items are ignored or interpreted es separator symbols when whitespaces are used as separators. This is true for all types except for strings. Strings get some special treatment as described in the next section.

Parsing strings

As always strings are different. When parsing a single string the input string is passed unchanged with all leading and trailing whitspace characters to the output. When parsing a list of strings to a std::vector some special rules apply