Hhow to read fixed width data?
Daniel Lidström <daniel.lidstrom <at> sbg.se>
2007-06-04 14:38:35 GMT
Hello!
I'm sorry if this is a FAQ. If it is I could not find it in the User's
Guide. Anyway, I want to parse
the following data:
66 1 24000001 -20000 289070 -15000 289210 -10000 289310 -5000
289300
66 1 24000002 -4822 289299 0 289260 1707 289246 5000
289220
66 1 24000003 10000 289190 15000 289190 20000 289130
66 4 24000001 -4822 289299 -3750 290013 -3000 290058 -3000
290058
66 4 24000002 0 289968 10 289968 10 289968 760
289878
66 4 24000003 1707 289246
This is fixed width and sometimes the data is not whitespace separated. The
problem lies in parsing
the third and fourth values. In this case they are together (24000001 is
actually the values 240000 01).
How can I be explicit about which columns contains a specific item? Here is
my grammar, which works
fine as long as the values are separated by white space:
namespace
{
//!
//! Grammar for REB format 66
//!
class REB66_grammar : public grammar<REB66_grammar>
{
vector<double>& out;
int& layer, §ion, &number;
string& type;
public:
REB66_grammar(string& t, int& l, int& s, int& n, std::vector<double>&
v)
: type(t), layer(l), section(s), number(n), out(v)
{ }
template<class Scan>
struct definition
{
definition(const REB66_grammar& self)
{
type
= str_p("66")[assign_a(self.type)]
;
layer
= int_p[assign_a(self.layer)]
;
section
= int_p[assign_a(self.section)]
;
number
= int_p[assign_a(self.number)]
;
pair_item
= int_p[push_back_a(self.out)]
;
pair
= pair_item && pair_item
;
required
= type && layer && section && number
;
optional
= repeat_p(1, 4)[pair]
;
line
= required && optional
;
}
rule<Scan> type, layer, section, number, pair_item, pair, required,
optional, line;
const rule<Scan>& start() const
{
return line;
}
};
};
}
The parsing is done like this:
while( getline(infile, line) )
{
int lyr, section, number;
string type;
std::vector<double> items;
REB66_grammar grammar(type, lyr, section, number, items);
if( parse(line.begin(), line.end(), grammar, blank_p).full )
{ ... }
}
I would be very grateful for any help! Thanks in advance!
Hälsningar,
Daniel
SBG AB
Phone: +4687112090
Fax : +4687112098
Location:
59 14'10" N
18 00'09" E
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/