-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeowSQL.cpp
515 lines (458 loc) · 14.5 KB
/
MeowSQL.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <stdio.h>
#include <cctype>
using namespace std;
const string USER = "meowmin";
const string PASS = "1w@ntch1cken";
const string COMMAND_PROMPT = ">";
const string COMMAND_DELIMITER = " ";
const string QUIT_CMD = "quit";
const string SHOW_CMD = "show";
const string CREATE_CMD = "create";
const string DELETE_CMD = "delete";
const string TABLE_FILETYPE = ".csv";
const string TABLE_FILE_DIRECTORY = "data/";
const string TABLES_TABLE = "data/tables.csv";
const string SHOW_ARG_1 = "tables";
const string USAGE_MSG = "Usage: ./a.out <username> <password>\n";
const string WELCOME_MSG = "Welcome ";
const string VALID_ARG_MSG = "";
const string QUIT_ARG_CNT_MSG = "QUIT Error: invalid argument count";
const string SHOW_ARG_CNT_MSG = "SHOW Error: invalid argument count";
const string CREATE_ARG_CNT_MSG = "CREATE Error: invalid argument count";
const string DELETE_ARG_CNT_MSG = "DELETE Error: invalid argument count";
const string TABLE_CREATE_SUCCESS_MSG = " table created successfully";
const string TABLE_DELETE_SUCCESS_MSG = " table removed successfully";
const string INVALID_CREDENTIALS_MSG = "Error: invalid credentials\n";
const string INV_CMD_MSG = "Error: invalid command name";
const string SHOW_INV_OPT_MSG = "SHOW Error: invalid option";
const string CREATE_INV_TABLE_NAME_MSG = "CREATE Error: table name should only contain "
" alpha numeric characters, '-', or '_'";
const string CREATE_EXISTS_MSG = "CREATE Error: table already exists";
const string CREATE_INV_HEADERS_MSG = "CREATE Error: column names should be separated "
" by delimiter and should only contain alpha numeric characters, '-', or '_'";
const string DELETE_UNDELETABLE_MSG = "DELETE Error: table cannot be deleted";
const string DELETE_INV_TABLE_NAME_MSG = "DELETE Error: invalid table name";
void header();
string toLower(string);
void getCredentials(int argc, char const *argv[], string &user, string &pass);
bool validateCredentials(string, string);
// creating commandloop
void commandLoop();
// Getting input
vector<string> getInput();
string validateArguments(vector<string> args);
void executeCommand(vector<string> args);
int removeDupWord(string str);
bool is_only_alpha(const string &str);
bool is_file_exist(string fileName);
void DeleteLine(string filename, vector<string> args);
// sections 2/3 add function prototypes
// YOUR CODE HERE
int main(int argc, char const *argv[])
{
if (argc < 3)
{
cout << USAGE_MSG;
return 0;
}
string user = "", pass = "";
getCredentials(argc, argv, user, pass);
if (validateCredentials(user, pass) == true)
{
// 1.2 call get header
// YOUR CODE HERE
header();
// 2.1 call the loop to get commands from the user
// YOUR CODE HERE
commandLoop();
}
return 0;
}
// used for section 1
// prints header
void header()
{
cout << "+-----------------------------------------------------------------"
"----+\n"
<< "| __ __ _______ _______ _ _ _______ _______ ___ "
" |\n"
<< "| | |_| || || || | _ | | | || || | "
" |\n"
<< "| | || ___|| _ || || || | | _____|| _ || | "
" |\n"
<< "| | || |___ | | | || | | |_____ | | | || | "
" |\n"
<< "| | || ___|| |_| || | |_____ || |_| || "
"|___ |\n"
<< "| | ||_|| || |___ | || _ | _____| || | | "
" | |\n"
<< "| |_| |_||_______||_______||__| |__| "
"|_______||____||_||_______| |\n"
<< "| "
"v1.0.0|\n"
<< "+-----------------------------------------------------------------"
"----+\n";
} // Used for section 2
// lowercases string and returns it
string toLower(string s)
{
for (char &x : s)
x = static_cast<char>(tolower(x));
return s;
}
// Used for section 3
// Prints the csv at the passed file path
bool printTable(string file)
{
string current = "", rest = "", delimiter = ",";
int rowCnt = 0;
ifstream toShow(file);
getline(toShow, current);
int colCnt = count(current.begin(), current.end(), delimiter[0]) + 1;
toShow.seekg(0);
vector<unsigned int> widths(colCnt, 0);
while (getline(toShow, rest))
{
for (int i = 0; i < colCnt; i++)
{
current = rest.substr(0, rest.find(delimiter));
rest = rest.erase(0, rest.find(delimiter) + 1);
if (current.size() > widths[i])
widths[i] = current.size();
}
rowCnt++;
}
if (rowCnt > 1)
{
toShow.clear();
toShow.seekg(0);
cout << file.substr(file.find("data/") + 5, file.length() - 9) << " table : \n";
cout
<< setfill('-')
<< right;
for (int i = 0; i < colCnt; i++)
cout << "+" << setw(widths[i] + 3);
cout << "+" << endl;
getline(toShow, current);
vector<string> ret;
string token = "";
while (current.find(delimiter) != string::npos)
{
token = current.substr(0, current.find(delimiter));
current = current.erase(0, current.find(delimiter) +
delimiter.length());
ret.push_back(token);
}
ret.push_back(current);
cout << setfill(' ') << left;
for (unsigned int i = 0; i < widths.size(); i++)
cout << "| " << setw(widths[i]) << ret[i] << " ";
cout << "|" << endl;
cout << setfill('-') << right;
for (int i = 0; i < colCnt; i++)
cout << "+" << setw(widths[i] + 3);
cout << "+" << endl;
while (getline(toShow, current))
{
ret.clear();
while (current.find(delimiter) != string::npos)
{
token = current.substr(0, current.find(delimiter));
current = current.erase(0, current.find(delimiter) +
delimiter.length());
ret.push_back(token);
}
ret.push_back(current);
cout << setfill(' ') << left;
for (unsigned int i = 0; i < widths.size(); i++)
cout << "| " << setw(widths[i]) << ret[i] << " ";
cout << "|" << endl;
}
cout << setfill('-') << right;
for (int i = 0; i < colCnt; i++)
cout << "+" << setw(widths[i] + 3);
cout << "+" << endl;
cout << rowCnt - 1 << " row(s) in set.\n";
return true;
}
return false;
}
// Fill this function out for section 1.1
void getCredentials(int argc, char const *argv[], string &user, string &pass)
{
user = argv[1];
pass = argv[2];
// 1.1 get username and password from cmd args
// YOUR CODE HERE
}
// Fill this function out for section 1.2
bool validateCredentials(string u, string p)
{
// 1.2 make sure proper user/pass
// YOUR CODE HERE
if (u == USER && p == PASS)
{
cout << WELCOME_MSG << " " << u << endl;
return true;
}
else
{
cout << INVALID_CREDENTIALS_MSG;
return false;
}
}
// 2.1 add getInput() function
vector<string> getInput()
{
vector<string> Vec_Getinputs;
Vec_Getinputs.clear();
string input = " ";
string token;
cout << endl;
cout << COMMAND_PROMPT;
getline(cin, input);
istringstream ss(input);
while (getline(ss, token, ' '))
{
Vec_Getinputs.push_back(token);
}
return Vec_Getinputs;
}
// 2.1 add validateArguments(vector<string>) function
string validateArguments(vector<string> args)
{
string input = " ";
//
string filename = " ";
string input2 = " ";
for (unsigned int i = 0; i < args.size(); i++)
{
args[0] = toLower(args[0]);
input += args[i];
input2 += args[i + 1];
}
for (unsigned int i = 0; i < args.size(); i++)
{
input2 += args[i + 1];
// QUIT
// checking to see it quite is typed in
if (toLower(args[i]) == QUIT_CMD)
{
exit(0);
}
// checking to see if Delete is typed n [
if (toLower(args[i]) == DELETE_CMD && (args.size() < 2 || args.size() > 2))
{
cout << DELETE_ARG_CNT_MSG;
return input;
cin.clear();
}
// DELETE
if (toLower(args[i]) == DELETE_CMD && args.size() == 2)
{
if (args[1] == SHOW_ARG_1)
{
cout << DELETE_UNDELETABLE_MSG << endl;
return input;
}
// checking to see if the file already exists
if (is_file_exist(args[1]) == true)
{
return VALID_ARG_MSG;
}
else
{
cout << DELETE_INV_TABLE_NAME_MSG << endl;
return input;
}
}
// SHOW
if (toLower(args[i]) == SHOW_CMD && (args.size() < 2 || args.size() > 2))
{
cout << SHOW_ARG_CNT_MSG << endl;
return input;
}
if (toLower(args[i]) == SHOW_CMD && args.size() == 2)
{
return VALID_ARG_MSG;
}
// checking to see if Show if typed in
// checking if there are 3 argmemtns
if (toLower(args[i]) == CREATE_CMD && (args.size() < 3 || args.size() > 3))
{
cout << CREATE_ARG_CNT_MSG << endl;
cin.clear();
return input;
}
// CREATE
if (toLower(args[i]) == CREATE_CMD && args.size() == 3)
{
if (is_only_alpha(input) == false)
{
cout << CREATE_INV_HEADERS_MSG << endl;
return input;
}
// we know its a alphanumeric character
// check if the file already exist
filename = args[1];
if (is_file_exist(filename) == true)
{
return input;
}
for (unsigned int i = 0; i < input2.size(); ++i)
{
// checking if the file colums starts with comma
if (input2[1] == ',')
{
cout << CREATE_INV_TABLE_NAME_MSG;
break;
}
// checking if the file has 2 commas together
if (input[i] == ',' && input[i + 1] == ',')
{
cout << CREATE_INV_TABLE_NAME_MSG;
break;
}
// every thing is valid
return VALID_ARG_MSG;
}
}
// when the input is something else
if (args[0] == "\n")
{
cout << INV_CMD_MSG;
return input;
}
cout << INV_CMD_MSG;
return input;
}
// cout << input << endl;
return input;
}
// 2.1 add executeCommand(vector<string>) function
void executeCommand(vector<string> args)
{
// CREATE
if (toLower(args[0]) == CREATE_CMD)
{
ofstream oFile; // output file
ifstream iFile; // input file
string filename = toLower(args[1]);
oFile.open(toLower(filename) + TABLE_FILETYPE);
if (!oFile.is_open())
{
cout << "Error opening output file!\n";
}
oFile << args[2];
oFile.close();
oFile.open("tables.csv", ios_base::app);
oFile << filename << endl;
oFile.close();
cout << args[1] << TABLE_CREATE_SUCCESS_MSG << endl;
return;
}
// SHOW
if (toLower(args[0]) == SHOW_CMD)
{
cout << args[1];
printTable("tables.csv");
}
// DELETE
if (toLower(args[0]) == DELETE_CMD)
{
string deleteline = args[1];
string filename = "tables.csv";
DeleteLine("tables.csv", args);
}
}
// 2.1 add commandLoop() function
void commandLoop()
{
vector<string> input;
while (true)
{
input = getInput();
string valid = validateArguments(input);
if (valid == VALID_ARG_MSG)
{
executeCommand(input);
}
}
}
// checking if the file is alph
bool is_only_alpha(const string &str)
{
for (unsigned int i = 1; i < str.size(); ++i)
{
if (!isalnum(str[i]) && str[i] != ',' && str[i] != '_' && str[i] != '-')
{
return false;
}
}
return true;
}
// checking if the file already exits
bool is_file_exist(string fileName)
{
ifstream myfile;
myfile.open(fileName + TABLE_FILETYPE);
if (myfile)
{
return true;
}
else
{
return false;
}
}
void DeleteLine(string filename, vector<string> args)
{
string to_delete = args[1];
// Prompt the user to enter the line number to delete in the file, store it
// into line_number
// fstream object will be used to read all of the existing lines in the file
fstream read_file;
// Open the file with the provided filename
read_file.open(filename);
// If file failed to open, exit with an error message and error exit status
if (read_file.fail())
{
cout << "Error opening file." << endl;
}
vector<string> lines;
string line;
// Read each line of the file and store it as the next element of the vector,
// the loop will stop when there are no more lines to read
while (getline(read_file, line))
lines.push_back(line);
// Close our access to the file since we are done reading with it
read_file.close();
string file_open_removed = to_delete + TABLE_FILETYPE;
remove(filename.c_str());
remove(file_open_removed.c_str());
ofstream write_file;
// Open the file with the provided filename
write_file.open(filename);
// If the file failed to open, exit with an error message and exit status
if (write_file.fail())
{
cout << "Error opening file." << endl;
}
// Loop through the vector elements to write each line back to the file
// EXCEPT the line we want to delete.
for (unsigned int i = 0; i < lines.size(); i++)
if (lines[i] != to_delete)
write_file << lines[i] << endl;
// Close our access to the file since we are done working with it
cout << TABLE_DELETE_SUCCESS_MSG;
write_file.close();
lines.clear();
}