-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.h
More file actions
186 lines (156 loc) · 4.54 KB
/
Copy pathheader.h
File metadata and controls
186 lines (156 loc) · 4.54 KB
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
#ifndef HEADER_H
#define HEADER_H
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cctype>
using namespace std;
//determine data types
string determineDataType(const string& input) {
if (input.empty()) {
return "EMPTY";
}
//check int
bool isInteger = true;
for (char c : input) {
if (!isdigit(c)) {
isInteger = false;
break;
}
}
//output
if (isInteger) {
return "INT";
} else {
//check date
istringstream dateSS(input);
int month, day, year;
char delimiter;
//takes in whole input of date format month_int/date_int/year
dateSS >> month >> delimiter >> day >> delimiter >> year;
if (delimiter == '/') {
return "DATE";
} else {
return "VARCHAR(255)";
}
}
}
//table struct so we can store each table
struct Table {
string name;
vector<string> attributes, types, keys, fundamentalDep;
unordered_map<string, vector<string>> data;
static int tableCount;
//constructor
Table(const vector<string>& tableAttributes, const vector<string> fd, const vector<string>& tableKeys, unordered_map<string, vector<string>> tableData, const vector<string> type)
: name("Table" + to_string(++tableCount)), attributes(tableAttributes), fundamentalDep(fd), keys(tableKeys), data(tableData), types(type) {}
//columns remove function
static void remove_columns(Table& table, const vector<string>& attributesToRemove) {
for (const string& attribute : attributesToRemove) {
auto it = find(table.attributes.begin(), table.attributes.end(), attribute);
if (it != table.attributes.end()) {
size_t index = distance(table.attributes.begin(), it);
table.attributes.erase(table.attributes.begin() + index);
table.types.erase(table.types.begin() + index);
table.data.erase(attribute);
}
}
}
//parser
static Table parseCSV(const string& filename, const string& fileFD) {
//FD parser
ifstream parseFD(fileFD);
string line2;
vector<string> FDinput;
if (parseFD.is_open())
{
while( getline(parseFD,line2) )
FDinput.push_back(line2);
parseFD.close();
}
//key parser
string keysIn;
cout << "\nEnter keys seperated by , " << endl;
cin >> keysIn;
stringstream ks(keysIn);
string keyHere;
vector<string> parsedKeys;
while (getline(ks, keyHere, ',')){
parsedKeys.push_back(keyHere);
}
ifstream file(filename);
string line;
getline(file, line);
stringstream ss(line);
string attribute;
vector<string> attributes;
//attribute parser
while (getline(ss, attribute, ',')) {
attributes.push_back(attribute);
}
//type parser
ifstream typeFile(filename);
string csvLine, field;
//ignores first line so we need 2 getlines
getline(typeFile, csvLine);
getline(typeFile, csvLine);
istringstream ss2(csvLine);
vector<string> dataTypes;
while (getline(ss2, field, ',')) {
string dataType = determineDataType(field);
dataTypes.push_back(dataType);
}
typeFile.close();
//creates a table with attributes
Table table(attributes, FDinput, parsedKeys, {}, dataTypes);
//actual parsing of data
while (getline(file, line)) {
stringstream ss(line);
string value;
vector<string> dataRow;
bool inQuotes = false;
string tempValue = "";
while (getline(ss, value, ',')) {
if (!inQuotes) {
//multivalued
if (value.front() == '\"' && value.back() == '\"')
dataRow.push_back(value.substr(1, value.size() - 2));
else if (value.front() == '\"') {
//multi start
inQuotes = true;
tempValue = value;
}
else
dataRow.push_back(value);
} else {
//concatinating values
tempValue += "," + value;
if (value.back() == '\"') {
//multi end
inQuotes = false;
dataRow.push_back(tempValue);
}
}
}
if (dataRow.size() != attributes.size()) {
cerr << "Error: Number of columns in a row does not match the number of attributes." << endl;
return table;
}
//store data into table
for (size_t i = 0; i < attributes.size(); ++i) {
table.data[attributes[i]].push_back(dataRow[i]);
}
}
file.close();
return table;
}
//end of parser
};
#endif