分析具有非常量格式(值的数目)的文件
Parse a file with non-constant format (number of values)
我目前正在尝试解析一个格式如下的文本文件:
[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...
该文件是传感器测量的结果:每个括号thingy表示传感器的值的间隔,每个不同的行表示一个测量。
问题是,我们有时会关闭某些传感器,所以文件的格式不会相同,所以我真的不知道如何做一个"通用"解析器,它不应该考虑打开的传感器的数量。
当然,我不知道是否清楚,不同文件的值数量不同。我的意思是,在同一个文件中,值的数量显然是恒定的。因此,如果我关闭除一个传感器外的每个传感器,我会得到这样的东西:
[value1(double),value2(double]n
[value1(double),value2(double]n
etc...
输出格式为:
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
如果能提供一些帮助,我们将不胜感激。
祝你今天愉快,非常感谢。
PS:很抱歉我的英语不好
读取一行:
[value1,value2];[value1,value2];[value1,value2];.........
处理生产线:
Till the end of line is met do:
For all chars from '[' to ']', read the 2 values.
Store val1 and val2
重复此操作,直到文件结束。
BoostSpirit提供了一个成熟的解析工具:这里有一些代码可以使用并适应您的特定问题:
#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;
}
程序输出:
record
a:1
b:2
c:3
record
u:31.5
v:32
z:-23
ok
我必须说,它不容易使用,但实际上是非常非常强大。HTH
您可以读取第一行,计算开括号"["的数量,然后从此设置一个固定长度的解析器。
相关文章:
- 文件格式的编写解释器(C++ Arduino)
- 如何使用 lldb 在曼扎罗中调试 lz4 文件格式的核心转储?
- Assimp - 如何使用任何文件格式导入带有纹理的网格?
- boost::p rogram_options 配置文件格式
- 如何启用libMagick++以保存.png文件格式
- 生成库失败:无法识别文件格式;作为链接器脚本处理
- 有没有更好的方法来使用比特流解码文件格式?
- MySQL C++连接器:添加符号时出错:文件格式无法识别
- Qt解析自定义文件格式
- ADTF 录制文件格式
- /usr/bin/ld找不到标头:未识别的文件格式
- C 库从3D文件格式(例如STL)获得常规网格
- 随机访问文件格式,用于分层组织的二进制文件和文本文件
- C OPENCV 3.4 / FFMPEG 3.4.1 VIDEOWRITER和MP4输出文件格式
- 具有挑战性的数据文件格式,需要将其读取为包含类对象的数组的VAR
- GDB错误不以可执行格式:未识别的文件格式
- 如何获取 C++/Qt 格式的文件格式
- GetText PO文件格式
- 如何在 c++ 中从".in"格式的文件中获取输入?此代码进入无限循环,但适用于".txt"文件格式
- 用于将三维模型导入OpenGL/C++项目的推荐文件格式和图形库