简体   繁体   中英

Parse a file with non-constant format (number of values)

I am currently trying to parse a text file formated like this :

 [value1(double),value2(double];[value1(double),value2(double];...;[value1(double),value2(double]\n
 [value1(double),value2(double];[value1(double),value2(double];...;[value1(double),value2(double]\n
 etc...

This file is the result of measures made by sensors : each bracket-thingy represents the interval of values of a sensor, and each different line represents a measure.

The problem is, we sometimes switch-off certain sensors, so the file won't have the same format, so I really don't know how to do a "general" parser, which shouldn't consider the number of sensors that was switched-on.

Of course, I don't know if it's clear, this number of values is different from a file to another. I mean, in a same file, the number of values is obviously constant. So if I switch-off each sensor but one, I would have something like this :

 [value1(double),value2(double]\n
 [value1(double),value2(double]\n
 etc...

The output format would be :

LINE 1:
    x1min: ... (first value of the first bracket-couple)
    x1max: ... (second value of the second bracket-couple)
    x2min: ...
    x2max: ...
etc...

LINE 2:
    same here

ETC
enter code here

Some help would be much appreciated.

Have a nice day, and thank you very much.

PS : Very sorry for my poor English

Read a line:

 [value1,value2];[value1,value2];[value1,value2];.........

Process the line:

Till the end of line is met do:
    For all chars from '[' to ']', read the 2 values.
    Store val1 and val2

Repeat this till the file ends.

您可以阅读第一行,计算左括号'['的数量,然后从中设置一个固定长度的解析器。

Boost.Spirit offers a full fledged parsing tool: here a some code you could use and adapt to your specific problem:

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>

#include <vector>
#include <string>
#include <iostream>

namespace client
{
    using namespace std;

    typedef pair<string, double> t_named_num;
    typedef vector<t_named_num> t_named_numbers;
    typedef vector<t_named_numbers> t_records;

    namespace qi = boost::spirit::qi;

    template <typename Iterator>
    struct parse_records : qi::grammar<Iterator, t_records()>
    {
        parse_records() : parse_records::base_type(records)
        {
            name = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
            named_num = name >> '(' >> qi::double_ >> ')'    ;
            named_numbers = '[' >> (named_num  % ',') >> ']' ;
            records = named_numbers % ';' ;
        }
        qi::rule<Iterator, string()> name;
        qi::rule<Iterator, t_named_num()> named_num;
        qi::rule<Iterator, t_named_numbers()> named_numbers;
        qi::rule<Iterator, t_records()> records;
    };
}
int main(int argc, char *argv[])
{
    using namespace std;
    using namespace client;

    string s("[a(1),b(2),c(3)];[u(31.5),v(32),z(-23)]");
    string::iterator i = s.begin(), e = s.end();
    parse_records<string::iterator> p;
    t_records c;
    if (boost::spirit::qi::parse(i, e, p, c))
    {
        for (t_records::iterator r = c.begin(); r != c.end(); ++r)
        {
            cout << "record" << endl;
            for (t_named_numbers::iterator n = r->begin(); n != r->end(); ++n)
                cout << n->first << ':' << n->second << endl;
        }
        if (i == e)
            cout << "ok" << endl;
        else
            cout << "ko" << endl;
    }
    else
        cout << "??" << endl;
}

Output of the program:

record
a:1
b:2
c:3
record
u:31.5
v:32
z:-23
ok

I must say that it's not easy to use, but actually is very very powerful. HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM