-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schema.cc
157 lines (132 loc) · 3.79 KB
/
Schema.cc
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
#include "Schema.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
int Schema ::Find(char *attName) {
for (int i = 0; i < numAtts; i++) {
if (!strcmp(attName, myAtts[i].name)) {
return i;
}
}
// if we made it here, the attribute was not found
return -1;
}
Type Schema ::FindType(char *attName) {
for (int i = 0; i < numAtts; i++) {
if (!strcmp(attName, myAtts[i].name)) {
return myAtts[i].myType;
}
}
// if we made it here, the attribute was not found
return Int;
}
int Schema ::GetNumAtts() {
return numAtts;
}
Attribute *Schema ::GetAtts() {
return myAtts;
}
Schema ::Schema(char *fpath, int num_atts, Attribute *atts) {
fileName = strdup(fpath);
numAtts = num_atts;
myAtts = new Attribute[num_atts];
for (int i = 0; i < num_atts; i++) {
if (atts[i].myType == Int) {
myAtts[i].myType = Int;
} else if (atts[i].myType == Double) {
myAtts[i].myType = Double;
} else if (atts[i].myType == String) {
myAtts[i].myType = String;
} else {
cout << "Bad attribute type for " << atts[i].myType << "\n";
delete[] myAtts;
exit(1);
}
myAtts[i].name = strdup(atts[i].name);
}
}
Schema ::Schema(const char *fName, const char *relName) {
FILE *foo = fopen(fName, "r");
schemaName.assign(relName);
// this is enough space to hold any tokens
char space[200];
fscanf(foo, "%s", space);
int totscans = 1;
// see if the file starts with the correct keyword
if (strcmp(space, "BEGIN")) {
cout << "Unfortunately, this does not seem to be a schema file.\n";
exit(1);
}
while (1) {
// check to see if this is the one we want
fscanf(foo, "%s", space);
totscans++;
if (strcmp(space, relName)) {
// it is not, so suck up everything to past the BEGIN
while (1) {
// suck up another token
if (fscanf(foo, "%s", space) == EOF) {
cerr << "Could not find the schema for the specified relation.\n";
exit(1);
}
totscans++;
if (!strcmp(space, "BEGIN")) {
break;
}
}
// otherwise, got the correct file!!
} else {
break;
}
}
// suck in the file name
fscanf(foo, "%s", space);
totscans++;
fileName = strdup(space);
// count the number of attributes specified
numAtts = 0;
while (1) {
fscanf(foo, "%s", space);
if (!strcmp(space, "END")) {
break;
} else {
fscanf(foo, "%s", space);
numAtts++;
}
}
// now actually load up the schema
fclose(foo);
foo = fopen(fName, "r");
// go past any un-needed info
for (int i = 0; i < totscans; i++) {
fscanf(foo, "%s", space);
}
// and load up the schema
myAtts = new Attribute[numAtts];
for (int i = 0; i < numAtts; i++) {
// read in the attribute name
fscanf(foo, "%s", space);
myAtts[i].name = strdup(space);
// read in the attribute type
fscanf(foo, "%s", space);
if (!strcmp(space, "Int")) {
myAtts[i].myType = Int;
} else if (!strcmp(space, "Double")) {
myAtts[i].myType = Double;
} else if (!strcmp(space, "String")) {
myAtts[i].myType = String;
} else {
cout << "Bad attribute type for " << myAtts[i].name << "\n";
exit(1);
}
}
fclose(foo);
}
Schema ::~Schema() {
delete[] myAtts;
myAtts = 0;
}
string Schema :: getfileName(){
return string(fileName);
}