-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbLib.cpp
184 lines (176 loc) · 4.24 KB
/
dbLib.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
/* Created by Nguyen Duc Dung on 2019-09-03.
* =========================================================================================
* Name : dbLib.cpp
* Author : Duc Dung Nguyen
* Email : [email protected]
* Copyright : Faculty of Computer Science and Engineering - HCMUT
* Description : The data structure library for Assignment 1
* Course : Data Structure and Algorithms - Fall 2019
* =========================================================================================
*/
#include "dbLib.h"
/* TODO: You can implement methods, functions that support your data structures here.
* */
void LoadData(void* &pData)
{
pData =new TDataset;
}
void ReleaseData(void* &pData)
{
delete static_cast<TDataset*>(pData);
}
string getCell(string &str)
{
char keyCheck = ',';
int i=0;
if (str[0]=='"')
{
keyCheck = '"';
i=1;
}
string s="";
while (str[i]!=keyCheck && str[i]!='\0')
s.push_back(str[i++]);
if (keyCheck=='"')
i++;
if (str[i]=='\0')
i--;
str.erase(0,i+1);
return s;
}
void ignoreCell(string &str,int numberCell)
{
for (int i=0;i<numberCell;i++)
getCell(str);
}
void TDataset::loadData_tCity()
{
ifstream inFile;
string line;
inFile.open("cities.csv");
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TCity tm;
tm.id=stoi(getCell(line));
tm.name=getCell(line);
_tCity.push_back(tm);
}
inFile.close();
}
void TDataset::loadData_tLine()
{
ifstream inFile;
string line;
inFile.open("lines.csv");
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TLine tm;
tm.id=stoi(getCell(line));
tm.city_id=stoi(getCell(line));
_tLine.push_back(tm);
}
inFile.close();
}
void TDataset::loadData_tTrack()
{
ifstream inFile;
inFile.open("tracks.csv");
string line;
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TTrack tm;
tm.id=stoi(getCell(line));
int tmp1=line.find('(');
int tmp2=line.rfind(')');
if (line.find('(')>=0)
{
tm.geometry=line.substr(tmp1+1,tmp2-tmp1-1);
line.erase(0,tmp2+2);
}
else
{
ignoreCell(line,1);
}
ignoreCell(line,4);
tm.city_id=stoi(getCell(line));
_tTrack.push_back(tm);
}
inFile.close();
}
void TDataset::loadData_tStation()
{
ifstream inFile;
inFile.open("stations.csv");
string line;
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TStation tm;
tm.id=stoi(getCell(line));
tm.name=getCell(line);
string tmp=getCell(line);
tmp.pop_back();
tmp.erase(0,6);
tm.geometry=tmp;
ignoreCell(line,3);
tm.city_id=stoi(getCell(line));
_tStation.push_back(tm);
}
inFile.close();
}
void TDataset::loadData_tSystem()
{
ifstream inFile;
inFile.open("systems.csv");
string line;
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TSystem tm;
tm.id=stoi(getCell(line));
tm.city_id=stoi(getCell(line));
_tSystem.push_back(tm);
}
inFile.close();
}
void TDataset::loadData_tStation_line()
{
ifstream inFile;
inFile.open("station_lines.csv");
string line;
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TStation_line tm;
tm.id=stoi(getCell(line));
tm.station_id=stoi(getCell(line));
tm.line_id=stoi(getCell(line));
// printf("%d %d %d\n",tm.id,tm.station_id,tm.line_id);
_tStation_line.push_back(tm);
}
inFile.close();
}
void TDataset::loadData_tTrack_line()
{
ifstream inFile;
inFile.open("station_lines.csv");
string line;
getline(inFile,line,'\n');
while (getline(inFile,line,'\n'))
{
TTrack_line tm;
tm.id=stoi(getCell(line));
_tTrack_line.push_back(tm);
}
inFile.close();
}
int TDataset::getLines()
{
return _tCity.getSize()+_tLine.getSize()
+ _tTrack.getSize()+_tStation.getSize()
+ _tSystem.getSize()+_tStation_line.getSize()
+ _tTrack_line.getSize();
}