-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv.cpp
308 lines (223 loc) · 6.73 KB
/
csv.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <map>
#include <iterator>
using namespace std;
class CSV{
private:
const char *filename;
public:
CSV(const char *filename);
map<string, vector<string>> selectAll();
vector<string> selectById(int id);
int updateById(int id,string data);
int updateById(int id,vector<string>* data);
int deleteById(int id);
string add(string data);
vector<string> add(vector<string>* data);
};
CSV :: CSV(const char *filename){
this->filename = filename;
}
/**
* @brief select everything from file
*
* @return map<string, vector<string>>
*/
map<string, vector<string>> CSV::selectAll(){
fstream file(this->filename);
map<string, vector<string>> dataSet;
vector<string> row;
string word,line;
while(getline(file,line)){
row.clear();
stringstream s(line);
while (getline(s, word, ',')) {
row.push_back(word);
}
dataSet.insert({row[0], row});
cout << endl;
}
//close file after reading
file.close();
return dataSet;
}
/**
* @brief select specific record in file
*
* @param id
* @return vector<string>
*/
vector<string> CSV::selectById(int id){
fstream file(this->filename);
vector<string> foundRow;
string line,word;
int foundId;
while(getline(file,line)){
foundRow.clear();
stringstream s(line);
while(getline(s,word,',')){
foundRow.push_back(word);
}
// assuming that every first element will be interger
foundId = stoi(foundRow[0]);
if( foundId == id)
return foundRow;
}
file.close();
throw 404;
}
/**
* @brief Update certain line in csv file by id
*
* @param id id of the line , this is the first number that appears in csv
* @param data new data to replace old one as string
* @return int : number of updated rows
*/
int CSV::updateById(int id, string data){
// NOTE: i know this function has stupid logic that could never be done in large enterprise application
// rewriting whole file 😒, i understands how overwhelming it is and how poor perfomant it is
// but you need to understand that i was rushing and this was fast for me, or if you have time open PR
// dealing with file pointers is kinda pain
fstream file(this->filename);
ofstream temp;
int updatedRows =0;
string line,identity, tempFileName = "temp.csv";
temp.open(tempFileName);
while(getline(file,line)){
stringstream s(line);
getline(s,identity,',');
if(id == stoi(identity)){
line.replace(line.find(line),line.length(),data);
updatedRows++;
}
temp << line << "\n";
}
temp.close();
file.close();
remove(this->filename);
rename(const_cast<char*>(tempFileName.c_str()),this->filename);
return updatedRows;
}
/**
* @brief Update certain line in csv file by id
*
* @param id id of the line , this is the first number that appears in csv
* @param data new data to replace old one as string
* @return int : number of updated rows
*/
int CSV::updateById(int id, vector<string> *data){
string dataString = "";
for(auto & d : *data){
if(dataString.length() > 0)
dataString +=",";
dataString +=d;
}
return this->updateById(id,dataString);
}
/**
* @brief delete specific line in file
*
* @param id
* @return int : return 0 if nothing deleted , otherwise 1
*/
int CSV::deleteById(int id){
// NOTE: i know this function has stupid logic that could never be done in large enterprise application
// rewriting whole file 😒, i understands how overwhelming it is and how poor perfomant it is
// but you need to understand that i was rushing and this was fast for me, or if you have time open PR
// dealing with file pointers is kinda pain
fstream file(this->filename);
ofstream temp;
int deleted = 0;
string line,identity, tempFileName = "temp.csv";
temp.open(tempFileName);
while(getline(file,line)){
stringstream s(line);
getline(s,identity,',');
if(id != stoi(identity)){
temp << line << "\n" ;
deleted = 1;
}
}
temp.close();
file.close();
remove(this->filename);
rename(const_cast<char*>(tempFileName.c_str()),this->filename);
return deleted;
}
string CSV::add(string data){
fstream file(this->filename);
map<int,int> ids;
string line,id,newLine;
int intId;
while(getline(file,line)){
stringstream lineStream(line);
getline(lineStream,id,',');
ids.insert({stoi(id), stoi(id)});
}
intId= stoi(id)+1;
//know wheter id exist in database
while(ids.find(intId) != ids.end()){
intId++;
}
file.close();
ofstream fileAppend(this->filename,ios::app);
newLine = to_string(intId) + "," + data;
fileAppend << newLine << endl;
fileAppend.close();
return newLine;
}
vector<string> CSV::add(vector<string>* data){
string dataString = "";
for(auto & d : *data){
if(dataString.length() > 0)
dataString +=",";
dataString += d;
}
string newData = this->add(dataString),singleCol;
vector<string> newDataVector;
stringstream newDataStream(newData);
while(getline(newDataStream,singleCol,',')){
newDataVector.push_back(singleCol);
}
return newDataVector;
}
void displayVector(vector<string> _vect){
copy(_vect.begin(), _vect.end(),ostream_iterator<string>(cout, " "));
}
void displayMap(map<string,vector<string>>* list){
map<string,vector<string> >::iterator it;
for (it = list->begin(); it != list->end(); it++)
{
cout << it->first << "\t\t";
displayVector(it->second);
cout << endl;
}
}
int main(){
CSV csv("data.csv");
// 1. TEST SELECT ALL FUNCTIONALITY
// map<string, vector<string>> list = csv.selectAll();
// displayMap(&list);
//2. TEST GET FUNCTIONALITY
// try{
// displayVector(csv.selectById(20));
// }catch(int e){
// if(e == 404)
// cout << "\nNot found\n" ;
// }
//3. TEST UPDATE FUNCTIONALITY
// displayVector(csv.selectById(1));
// vector<string> data= {"1","umwali","11"};
// cout << "\n\n" << csv.updateById(1,data) << "\n\n";
// // cout << "\n\n" << csv.updateById(1,"1,ntwari,900") << "\n\n"; // or using just string string;
// displayVector(csv.selectById(1));
//4. TEST DELETE
// csv.deleteById(2);
//5. ADD DATA TO FILE
// cout << csv.add("tracy,303");
vector<string> data = {"tracy","430"};
displayVector(csv.add(&data));
}