-
Notifications
You must be signed in to change notification settings - Fork 1
/
support_fn.cpp
91 lines (83 loc) · 2.25 KB
/
support_fn.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
#include "support_fn.hh"
#define uint unsigned int
vector < table > list_table;
vector < string > broken_query;
// call appropriate functions to perform relational
// algebra operations
int ra(string query) {
vector < string > table_eval;
// char tmp = query[0];
// if (tmp == '\n' || t)
// tmp = query[1];
// if (!(tmp == '@' || tmp == '#' || tmp == '`'
// || tmp == '^' || tmp == ':' || tmp == '%'
// || tmp == '{'))
// return -1;
table_eval = shunting_yard_nest (query);
// for (int i = 0; i < table_eval.size(); i++)
// cout<<table_eval[i]<<endl;
eval_nest(table_eval);
string temp = table_eval[0].substr(1, table_eval[0].length()-1);
int index = ::atof(temp.c_str());
print_table(dollar[index]);
dollar.clear();
return 0;
}
// parse the query, decide whether it's simple exit, create, insert
// or a relational algebra expression
// then take action accordingly
int parser (string query) {
split(query);
if (broken_query[0] == "EXIT") {
cout<<"\n> PROGRAM EXITING NOW...\n";
// print_table(list_table[0]);
exit(0);
}
else if (broken_query[0] == "CREATE") {
if (create() == 0)
return 1; // indicate query was of CREATE and worked correctly
}
else if (broken_query[0] == "INSERT") {
if (insert() == 0)
return 2; // indicate query was of INSERT and worked correctly
}
else if (broken_query[0] == "SHOW_TABLES")
show_tables(list_table);
else
ra(query);
return 0;
}
// split a string into a vector of strings
// split occurs at every space
// eg. "A B C" converts to "A"->"B"->"C"
int split (string query) {
broken_query.clear();
istringstream iss(query);
do {
string sub;
iss >> sub;
broken_query.push_back(sub);
} while (iss);
broken_query.pop_back();
return 0;
}
// check if a string is entirely composed of numbers
// eg. "2.141" -> True
// "35346" -> True
// "sgs" -> False
// "fgd45" -> False
bool is_num (string str) {
if (str.length() == 0)
return false;
uint i = 0;
if (str[i] == '-')
i++;
for (; i < str.length() && str[i] != '.'; i++)
if (!(str[i] >= '0' && str[i] <= '9'))
return false;
i++;
for (; i < str.length(); i++)
if (!(str[i] >= '0' && str[i] <= '9'))
return false;
return true;
}