Repository navigation
Expand file tree
/
Copy pathdata.cpp
More file actions
114 lines (86 loc) · 2.35 KB
/
Copy pathdata.cpp
File metadata and controls
114 lines (86 loc) · 2.35 KB
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
#include "data.h"
namespace datastream {
void Data::clear(){
sets_by_id_.clear();
sets_.clear();
}
void Data::build(const Schema& schema){
load(schema.sets());
map(schema);
}
void Data::write(ostream & os, Formatter& formatter, const Schema& schema ) const {
// no data loaded
//throw error?
if (!sets_by_id_.size()){
return;
}
// no graph loaded
//throw error?
if(!schema.dependencyOrder().size()){
return;
}
int root_id = schema.dependencyOrder().at(0);
auto root_search = sets_by_id_.find(root_id);
if (root_search == sets_by_id_.end()){
throw std::domain_error("sorry, I cannot find the beginning of the data to begin writing");
}
//only support one root for now
root_search->second->write(os, formatter);
}
void Data::load(const Schema::Sets& schema_sets){
clear();
for(const SchemaSet& schema_set : schema_sets){
ifstream file (schema_set.inputFileName());
if (!file.is_open()){
throw std::domain_error("cannot open schema file");
}
sets_.emplace_back(
schema_set
);
string line;
while (std::getline(file, line)){
if(isBlank(line) || isComment(line)){
continue;
}
// current data set is last added,
// load rows
sets_.rbegin()->load(schema_set.childElements(), line);
}
/* Drew Dormann -
Only the EOF state is acceptable here. */
if (!file.eof())
{
throw std::runtime_error("file operation failure while reading " + schema_set.inputFileName());
}
file.close();
}
}
void Data::map(const Schema& schema){
//map sets_
for (DataSet& data_set : sets_){
//map sets_
sets_by_id_.emplace(
data_set.id(),
&data_set
);
}
//map rows
auto& dependency_order = schema.dependencyOrder();
for ( int set_id : dependency_order){
auto set_search = sets_by_id_.find(set_id);
if ( set_search == sets_by_id_.end()){
throw std::domain_error("sorry, the data cannot be understood : a section is missing.");
}
DataSet* parent_set_ptr = nullptr;
if (set_search->second->hasParent()){
auto parent_search = sets_by_id_.find(set_search->second->parent());
if ( parent_search == sets_by_id_.end()){
throw std::domain_error("data structure is invalid. parent unknown");
}
parent_set_ptr = parent_search->second;
}
//map set rows
set_search->second->mapRows(parent_set_ptr);
}
}
}