-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv.cpp
56 lines (42 loc) · 1.62 KB
/
csv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2017 gahag
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <tpc/util/string.hpp>
#include <tpc/parser/combinators/or.hpp>
#include <tpc/parser/combinators/sepby.hpp>
#include <tpc/parser/combinators/sependby.hpp>
#include <tpc/parser/standard/char.hpp>
#include <tpc/parser/standard/identifier.hpp>
#include <tpc/parser/standard/string.hpp>
typedef std::vector<std::string> Line;
typedef std::vector<Line> CSV;
bool isIdent(char c) {
return std::isprint(c)
&& c != tpc::Char::Comma
&& c != tpc::Char::LineFeed
&& c != tpc::Char::Carriage;
}
constexpr tpc::parser<std::string> cell = tpc::orP<std::string, tpc::string
, tpc::identifier<isIdent> >;
constexpr tpc::parser<Line> line = tpc::sepBy1<char, tpc::comma, // No empty lines allowed.
Line, cell >;
constexpr tpc::parser<CSV> csv =
tpc::first< CSV, tpc::sepEndBy< tpc::void_t, tpc::newline,
CSV, line >,
char, tpc::eos >;
auto main() -> int {
std::ifstream stream("Examples/test.csv");
auto r = csv(stream);
if (r)
std::cout << "lines: " << r->size() << std::endl
<< "last field: " << r->back().back() << std::endl;
else
std::cerr << "Failed at " << tpc::to_string(r.pos) << std::endl;
}