-
Notifications
You must be signed in to change notification settings - Fork 0
/
Datafield.h
279 lines (244 loc) · 6.42 KB
/
Datafield.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
#define BasePath "DataFiles/NCHS_-_Leading_Causes_of_Death__United_States_";
void GotoLine(fstream& file, const int lineno)
{
string str;
file._Seekbeg;
for (int i = 0; i < lineno; i++)
getline(file, str);
}
bool IsEmpty(const string& str)
{
int len = str.length();
for (int i = 0; i < len-1 && i < 5; i++)
if (str[i] != ' ')
return false;
return true;
}
void AppendClearStr(string& str, const int times)
{
for (int i = 0; i < times; i++)
str += " ";
}
template <class data >
class Field {
public:
data Data;
string fileNo;
int lineNo;
Field() : lineNo(-1), fileNo(""), Data(data()){}
Field(const data val, const int Ln = -1, const string fn = "") : lineNo(Ln), fileNo(fn), Data(val){}
Field(const Field<data>& Other) : lineNo(Other.lineNo), fileNo(Other.fileNo), Data(Other.Data) {}
void operator=(const Field& Other)
{
Data = Other.Data;
fileNo = Other.fileNo;
lineNo = Other.lineNo;
}
bool operator>(const Field& Other) const
{
return this->Data > Other.Data;
}
bool operator<(const Field& Other) const
{
return this->Data < Other.Data;
}
bool operator==(const Field& Other) const
{
return this->Data == Other.Data;
}
bool operator<=(const Field& Other) const
{
return this->Data <= Other.Data;
}
bool operator>=(const Field& Other) const
{
return this->Data >= Other.Data;
}
bool MatchAll(const Field<data>& Other) const
{
return (this->Data == Other->Data &&
this->fileNo == Other->fileNo &&
this->LineNo == Other->LineNo);
}
string ClearString(const int index)
{
string res;
for (int i = 0; i < index; i++)
res += ' ';
return res;
}
void Overwrite(string NewValue = "")
{
int LineSize, LineStart;
string Dataline, filename = BasePath;
filename += fileNo + ".csv";
fstream file(filename, ios::in | ios::out);
if (file.fail()) { cout << "Unable to Open File for Overwriting\n"; return; }
GotoLine(file, lineNo);
LineStart = file.tellp();
getline(file, Dataline);
LineSize = Dataline.size();
if (NewValue == "") NewValue = ClearString(LineSize);
if (NewValue.size() < LineSize) AppendClearStr(NewValue, LineSize - NewValue.size());
file.seekp(LineStart);
file.write(NewValue.c_str(), NewValue.size());
file.close();
}
string ReadTuple()
{
string Dataline, filename = BasePath;
filename += fileNo + ".csv";
fstream file(filename, ios::in);
if (file.fail())
{
cout << "Unable to Open File for Tuple Reading : " << filename << endl;
return "";
}
GotoLine(file, lineNo);
getline(file, Dataline);
file.close();
return Dataline;
}
};
template <class data>
void ReadData(const Field<data>& Res, string& str)
{
string Dataline,
filename = "DataFiles/NCHS_-_Leading_Causes_of_Death__United_States_" + Res.fileNo + ".csv";
fstream file(filename, ios::in);
if (file.fail())
{
cout << "Unable to Open File : " << filename << endl;
return;
}
GotoLine(file, Res.lineNo);
getline(file, str);
file.close();
}
string GetData(string line, int& index)
{
string res;
bool Quotation = false;
if (index >= line.length())
return "";
while (line[index] != '\0' && line[index] != '\n' && index < line.length())
{
if (line[index] == ',' && !Quotation) break;
else if (line[index] == '\"' && !Quotation) Quotation = true;
else if (line[index] == '\"' && Quotation) Quotation = false;
else res += line[index];
index++;
}
index++;
return res;
}
template <class data>
ostream& operator<<(ostream& output, const Field<data>& Res)
{
//cout << Res.Data;
//return output;
string res;
ReadData(Res, res);
if (IsEmpty(res))
{ cout << "Record Not Found\n"; return output;}
int index = 0;
string str = GetData(res, index);
output << "ID : " << str << "\n";
str = GetData(res, index);
output << "Year : " << str << "\n";
str = GetData(res, index);
output << "113 Cause-Name : " << str << "\n";
str = GetData(res, index);
output << "Cause Name : " << str << "\n";
str = GetData(res, index);
output << "State : " << str << "\n";
str = GetData(res, index);
output << "Deaths : " << str << "\n";
str = GetData(res, index);
output << "Age-adjusted Death Rate : " << str << "\n";
return output;
}
class FieldTable {
public:
int Total;
string* Table;
FieldTable() { Total = 0; Table = nullptr; }
FieldTable(const string& line, const int count) : Total(count)
{
Table = new string[Total];
for (int i = 0, index = 0; i < count; i++)
Table[i] = GetData(line, index);
}
FieldTable(const FieldTable& Other)
{
this->Total = Other.Total;
Table = new string[Total];
for (int i = 0; i < Total; i++)
Table[i] = Other.Table[i];
}
const string operator=(const int i)
{ return Table[i];}
void DisplayTable()
{
for (int i = 0; i < Total; i++)
cout << Table[i] << endl;
}
};
const int TotalFields(const string& str)
{
int count = 1;
for (int i = 0; str[i] != '\0'; i++)
if (str[i] == ',') count++;
return count++;
}
FieldTable* GenerateFieldMap()
{
string filename = BasePath;
fstream file(filename + "1.csv", ios::in);
getline(file, filename);
FieldTable* Table = new FieldTable(filename, TotalFields(filename));
file.close();
return Table;
}
string GetWhereData(string line, int& index)
{
string res;
bool Quotation = false;
if (index >= line.length())
return "";
while (line[index] != '\0' && line[index] != '\n' && index < line.length())
{
if (line[index] == ',' && !Quotation) break;
else if (line[index] == ' ') break;
else if (line[index] == '=') break;
else if (line[index] == '\"' && !Quotation) Quotation = true;
else if (line[index] == '\"' && Quotation) Quotation = false;
else res += line[index];
index++;
}
index++;
return res;
}
string CreateUpdateData(string line, int& index)
{
string res;
bool Quotation = false;
if (index >= line.length())
return "";
while (line[index] != '\0' && line[index] != '\n' && index < line.length())
{
if (line[index] == ',' && !Quotation) break;
else if (line[index] == '\"' && !Quotation) { Quotation = true; res += line[index]; }
else if (line[index] == '\"' && Quotation) { Quotation = false; res += line[index]; }
else res += line[index];
index++;
}
index++;
return res;
}