-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchema.cc
138 lines (109 loc) · 2.6 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
#include "Schema.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int Schema :: Find (const 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 (const 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 (const char *fName, const char *relName) {
FILE *foo = fopen (fName, "r");
// 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;
}