-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvreader.cpp
136 lines (111 loc) · 2.99 KB
/
csvreader.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "infra/csvreader.h"
namespace inf {
CsvReader::CsvReader(const fs::path& filename)
: _charset(detect_character_set(filename))
, _dataset(gdal::VectorDataSet::open(filename, gdal::VectorType::Csv))
, _layer(_dataset.layer(0))
{
}
int32_t CsvReader::column_count() const
{
return _layer.layer_definition().field_count();
}
std::string_view CsvReader::column_name(int32_t index) const
{
return _layer.layer_definition().field_definition(index).name();
}
std::optional<int32_t> CsvReader::column_index(const std::string& name) const noexcept
{
if (auto index = _layer.layer_definition().field_index(name); index >= 0) {
return index;
}
return {};
}
int32_t CsvReader::required_column_index(const std::string& name) const
{
if (auto index = _layer.layer_definition().field_index(name); index >= 0) {
return index;
}
throw RuntimeError("No column with name '{}' present in csv file", name);
}
CsvRowIterator CsvReader::begin() const
{
return CsvRowIterator(_layer, _charset);
}
CsvRowIterator CsvReader::end() const
{
return CsvRowIterator();
}
CsvRow::CsvRow(const gdal::Feature& feat, inf::CharacterSet charSet)
: _feature(&feat)
, _charSet(charSet)
{
}
bool CsvRow::column_is_empty(int32_t index) const noexcept
{
return _feature->field_as<std::string_view>(index).empty();
}
std::string CsvRow::get_string(int32_t index) const noexcept
{
if (_charSet == CharacterSet::Utf8 || _charSet == CharacterSet::Unknown) {
return std::string(_feature->field_as<std::string_view>(index));
} else {
return convert_to_utf8(_feature->field_as<std::string_view>(index));
}
}
std::optional<int32_t> CsvRow::get_int32(int32_t index) const noexcept
{
return _feature->opt_field_as<int32_t>(index);
}
std::optional<int64_t> CsvRow::get_int64(int32_t index) const noexcept
{
return _feature->opt_field_as<int64_t>(index);
}
std::optional<double> CsvRow::get_double(int32_t index) const noexcept
{
auto val = _feature->field_as<std::string>(index);
if (val.empty()) {
return {};
}
return CPLAtofM(val.c_str());
}
bool CsvRow::operator==(const CsvRow& other) const
{
return _feature == other._feature;
}
CsvRowIterator::CsvRowIterator(gdal::Layer layer, inf::CharacterSet charSet)
: _iterator(std::move(layer))
, _charset(charSet)
, _currentRow(*_iterator, _charset)
{
}
const CsvRow& CsvRowIterator::operator*()
{
return _currentRow;
}
const CsvRow* CsvRowIterator::operator->()
{
return &_currentRow;
}
CsvRowIterator& CsvRowIterator::operator++()
{
++_iterator;
_currentRow = CsvRow(*_iterator, _charset);
return *this;
}
CsvRowIterator& CsvRowIterator::operator=(CsvRowIterator&& other)
{
if (this != &other) {
_iterator = std::move(other._iterator);
}
return *this;
}
bool CsvRowIterator::operator==(const CsvRowIterator& other) const
{
return _iterator == other._iterator;
}
bool CsvRowIterator::operator!=(const CsvRowIterator& other) const
{
return !(*this == other);
}
}