-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
347 lines (294 loc) · 8.73 KB
/
test.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
#include "database.h"
#include "user.h"
#include <variant>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
using namespace db;
vector<database> dbs;
database cur_db;
bool db_in_use;
int db_name_max_len = 9;
vector<user> users;
user cur_user;
void list_db(){
for (int i = 0; i < db_name_max_len + 6; i++) cout << "=";
cout << endl;
cout << "# DATABASES";
for (int i = 0; i < (int)db_name_max_len - 9; i++) cout << " ";
cout << " #" << endl;
for (int i = 0; i < db_name_max_len + 6; i++) cout << "=";
cout << endl;
for (int i = 0; i < (int)dbs.size(); i++){
if (cur_user.has_view_perm(dbs[i].name())){
cout << "# " << dbs[i].name();
for (int j = 0; j < db_name_max_len - (int)dbs[i].name().size(); j++) cout << " ";
cout << " #" << endl;
}
}
for (int i = 0; i < db_name_max_len + 6; i++) cout << "=";
cout << endl;
}
bool db_search(string name){
for (auto db : dbs){
if (db.name() == name){
return true;
}
}
return false;
}
void create_db(){
cout << "Enter name of the new database: ";
string name;
cin >> name;
while(db_search(name)){
cout << "ERROR! database already exist!" << endl;
cout << "Enter another name: ";
cin >> name;
}
database new_db(name);
cout << name << " successfully created!" << endl;
cout << "Do you want this database to be public? (y/n): ";
string tmp;
cin >> tmp;
if (tmp == "Y" || tmp == "y"){
new_db.make_public();
for (user &usr : users){
usr.add_view_perm(name);
}
cur_user.add_edit_perm(name);
} else {
new_db.make_private();
cur_user.add_view_perm(name);
cur_user.add_edit_perm(name);
}
db_name_max_len = max(db_name_max_len, (int)name.size());
dbs.push_back(new_db);
}
void use_db(){
cout << "Enter name of database: ";
string name;
cin >> name;
while(!db_search(name)){
cout << "ERROR! database not found!" << endl;
cout << "Enter another name: ";
cin >> name;
}
for (auto db : dbs){
if (db.name() == name){
db_in_use = true;
cur_db = db;
break;
}
}
cout << name << " is in use!" << endl;
}
void menu(){
auto display_options = [&](){
cout << "1. CREATE DATABASE" << endl;
cout << "2. USE DATABASE" << endl;
cout << "3. SEE A LIST OF DATABASES" << endl;
cout << "4. EXIT" << endl;
//
};
display_options();
int inp;
cin >> inp;
while(inp != 4){
if (inp == 1){
create_db();
} else if (inp == 2){
use_db();
} else if (inp == 3){
list_db();
}
display_options();
cin >> inp;
}
}
void load(){
cout << "Loading users..." << endl;
ifstream ifs("users.txt");
string buf;
while(ifs >> buf){
cout <<buf << endl;
string username, password, p;
username = buf;
ifs >> password;
ifs.ignore();
user usr(username, password);
getline(ifs, buf);
istringstream iss(buf);
while(iss >> p) usr.add_edit_perm(p);
getline(ifs, buf);
iss.str(buf);
while(iss >> p) usr.add_view_perm(p);
users.push_back(usr);
}
cout << "Loading databases..." << endl;
ifstream list_fs("db_list.txt");
int db_cnt = 0;
list_fs >> db_cnt;
for (int i = 0; i < db_cnt; i++){
string db_name;
list_fs >> db_name;
db_name_max_len = max(db_name_max_len, (int)db_name.size());
for (int j = 0; j < 4; j++) db_name.pop_back();
dbs.push_back(database(db_name));
}
cout << "list loaded" << endl;
for (int i = 0; i < db_cnt; i++){
string f = dbs[i].name();
f += ".txt";
ifstream ifs(f);
int t_cnt = 0;
ifs >> t_cnt;
for (int j = 0; j < t_cnt; j++){
int r, c;
ifs >> r >> c;
vector<string> name(c), type(c);
for (int k = 0; k < c; k++){
ifs >> name[i];
}
for (int k = 0; k < c; k++){
ifs >> type[i];
}
dbs[i].create_table(dbs[i].name(), name, type);
for (int k = 0; k < r; k++){
row rw(c);
for (int k1 = 0; k1 < c; k1++){
if (type[k1] == "int"){
int x;
cin >> x;
rw[k1] = x;
} else if (type[k1] == "string"){
string x;
cin >> x;
rw[k1] = x;
} else if (type[k1] == "bool"){
bool x;
cin >> x;
rw[k1] = x;
} else if (type[k1] == "double"){
double x;
cin >> x;
rw[k1] = x;
}
}
dbs[i][j].insert_row(rw);
}
}
}
cout << "Databases loaded successfully!" << endl;
}
bool check_username(string username){
for (user usr : users){
if (username == usr.get_name()){
cur_user = usr;
return true;
}
}
return false;
}
bool check_password(string password){
return password == cur_user.get_pass();
}
bool valid_password(string password){
if ((int)password.size() < 8) cout << "Password too short! ";
if ((int)password.size() > 20) cout << "Password too long! ";
for (char c : password){
if (!isdigit(c) && !(c <= 'z' && c >= 'a') && !(c <= 'Z' && c >= 'A')){
cout << "Password contains character \'" << c << "\'! ";
return false;
}
}
return true;
}
void sign_up(){
string username;
cout << "Enter the username: ";
cin >> username;
while(check_username(username)){
cout << "User already exist! Please Enter another username: ";
cin >> username;
}
string password;
cout << "Enter the password (Password must contain 8-20 alphanumeric characters): ";
cin >> password;
while(!valid_password(password)){
cout << "Enter a new password: ";
cin >> password;
}
users.push_back(user(username, password));
cur_user = users.back();
}
void login(){
cout << "Enter Username: ";
string username;
cin >> username;
while(true){
while(!check_username(username)){
cout << "User do not exist!" << endl;
cout << "Enter 'c' to create new user: ";
cin >> username;
if (username == "c" || username == "C"){
sign_up();
return;
}
}
cout << "Enter password: ";
string password;
cin >> password;
while(!check_password(password)){
cout << "Username and password mismatch!" << endl;
cout << "Enter 'r' to re-enter username: ";
cin >> password;
if (password == "r" || password == "R") break;
}
if (check_password(password)){
return;
}
}
}
void save(){
cout << "Creating user list..." << endl;
ofstream ofs("users.txt");
for (user usr : users){
ofs << usr.get_name() << endl;
ofs << usr.get_pass() << endl;
for (auto p : usr.get_edit_perm()) ofs << p << ' ';
ofs << endl;
for (auto p : usr.get_view_perm()) ofs << p << ' ';
ofs << endl;
}
cout << "Creating database list..." << endl;
ofs.open("db_list.txt");
ofs << (int)dbs.size() << endl;
for (auto db : dbs){
ofs << db.name() + ".txt" << endl;
}
cout << "Database list created successfully!" << endl;
for (auto db : dbs){
string f = db.name();
cout << "Saving " << f << " ..." << endl;
f += ".txt";
ofs.open(f);
ofs << (int)db.tables().size() << endl;
for (auto t : db.tables()){
ofs << t.table_name() << '\n';
t.print_table(ofs);
}
}
cout << "Databases saved!" << endl;
}
int main(){
system("cls");
load();
login();
cout << "Logged in Successfully!" << endl;
menu();
save();
cout << "Goodbye!" << endl;
}