-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReader.cpp
217 lines (207 loc) · 4.55 KB
/
Reader.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <cassert>
#include "Reader.h"
#define MAX_HEADER_LEN 255 // TODO: Count the real maximum (255 is not the real value)
const std::string WHITESPACE = " \n\r\t\f\v";
std::string ltrim(const std::string &s)
{
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string &s)
{
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string &s)
{
return rtrim(ltrim(s));
}
void Reader::split(std::string strings[], const std::string &str)
{
int currIndex = 0, i = 0;
int startIndex = 0, endIndex;
while (i <= str.length())
{
if (str[i] == '\t' || i == str.length())
{
endIndex = i;
std::string subStr;
subStr.append(str, startIndex, endIndex - startIndex);
strings[currIndex] = subStr;
currIndex++;
startIndex = endIndex + 1;
}
i++;
}
}
int Reader::parse(const std::string &filename)
{
std::ifstream fin(filename);
if (fin.is_open())
{
std::string line;
std::string current_table;
std::string current_header[MAX_HEADER_LEN];
std::string current_record[MAX_HEADER_LEN];
while (getline(fin, line))
{
size_t i = line.find('\r');
if(i < line.length()){ line.replace(i, 1, ""); }
// Split line into tab-separated parts
std::string parts[MAX_HEADER_LEN] = {""};
split(parts, line);
// check if line starts with %T then it's a table
if (parts[0] == "%T")
{
current_table = parts[1];
}
// check if line starts with %F then it's a header
else if (parts[0] == "%F")
{
for (uint i = 0; i < MAX_HEADER_LEN; i++)
{
if (parts[i + 1].empty())
break;
current_header[i] = parts[i + 1];
}
}
// check if line starts with %R then it is a record that could be parsed to the class
// matching the table name
else if (parts[0] == "%R")
{
for (uint i = 0; i < MAX_HEADER_LEN; i++)
{
if (current_header[i].empty())
break;
current_record[i] = parts[i + 1];
}
add(current_table, current_header, current_record);
}
}
fin.close();
}
else
{
std::cout << "Unable to open" << std::endl;
}
return 0;
}
void Reader::add(const std::string &table, const std::string *header, const std::string *record)
{
assert(not(table.empty() || header == nullptr || record == nullptr));
if (table == "ACCOUNT")
{
accounts.add(Account(header, record));
}
else if (table == "ACTVCODE")
{
actvcodes.add(Actvcode(header, record));
}
else if (table == "ACTVTYPE")
{
actvtypes.add(Actvtype(header, record));
}
else if (table == "CALENDAR")
{
calendars.add(Calendar(header, record));
}
else if (table == "PROJWBS")
{
wbss.add(Wbs(header, record));
}
else if (table == "UDFVALUE")
{
udfs.add(Udf(header, record));
}
else if (table == "UDFTYPE")
{
udftypes.add(UDFType(header, record));
}
else if (table == "ROLE")
{
roles.add(Role(header, record, this));
}
else if (table == "CURRTYPE")
{
currencies.add(Currtype(header, record));
}
else if (table == "RCATTYPE")
{
rCatTypes.add(RCatType(header, record));
}
else if (table == "RCATVAL")
{
rCatValues.add(RCatValue(header, record));
}
else if (table == "ROLERATE")
{
roleRates.add(RoleRate(header, record));
}
else if (table == "TASKRSRC")
{
taskRsrcs.add(TaskRsrc(header, record));
}
else if (table == "OBS")
{
obss.add(Obs(header, record));
}
else if (table == "NONWORK")
{
nonWorks.add(NonWork(header, record));
}
else if (table == "FINTMPL")
{
fintmpls.add(Fintmpl(header, record));
}
else if (table == "PCATTYPE")
{
pcattypes.add(Pcattype(header, record));
}
else if (table == "PCATVAL")
{
pcatvals.add(Pcatval(header, record));
}
else if (table == "TASKPRED")
{
taskpreds.add(Taskpred(header, record));
}
else if (table == "PROJPCAT")
{
projpcats.add(Projpcat(header, record));
}
else if (table == "PROJECT")
{
projects.add(Project(header, record));
}
else if (table == "RSRC")
{
rsrcs.add(Rsrc(header, record, this));
}
else if (table == "RSRCRCAT")
{
rsrcrcats.add(Rsrcrcat(header, record));
}
else if (table == "RSRCCURVDATA")
{
rsrccurvdatas.add(Rsrccurvdata(header, record));
}
else if (table == "RSRCRATE")
{
rsrcrates.add(Rsrcrate(header, record));
}
else if (table == "SCHEDOPTION")
{
schedoptions.add(Schedoption(header, record));
}
else if (table == "TASKACTV")
{
taskactvs.add(Taskactv(header, record));
}
else if (table == "TASKPROC")
{
taskprocs.add(Taskproc(header, record));
}else if (table == "TASK")
{
tasks.add(Task(header, record));
}
}