-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.cpp
171 lines (136 loc) · 3.87 KB
/
print.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
#include <stdio.h>
#include <string.h>
#include "catalog.h"
#include "utility.h"
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
// Compute widths of columns in attribute array.
//
const Status UT_computeWidth(const int attrCnt,
const AttrDesc attrs[],
int *&attrWidth)
{
attrWidth = new int [attrCnt];
if (!attrWidth)
return INSUFMEM;
for(int i = 0; i < attrCnt; i++) {
int namelen = strlen(attrs[i].attrName);
switch(attrs[i].attrType) {
case INTEGER:
case DOUBLE:
// For ints and doubles print at most 7 characters.
attrWidth[i] = MIN(MAX(namelen, 5), 7);
// To print numbers using a longer format, comment out the previous line
// and uncomment the next line.
// attrWidth[i] = MAX(namelen, 5);
break;
case STRING:
// For strings print at most 15 characters.
// BUG: The MAX implies that if the length of the attribute name is greater
// than the type of the attribute, the subsequent print may print out garbage
// values
attrWidth[i] = MIN(MAX(namelen, attrs[i].attrLen), 15);
// To print the entire string, comment out the previous line
// and uncomment the next line.
// attrWidth[i] = MAX(namelen, attrs[i].attrLen);
break;
}
}
return OK;
}
//
// Prints values of attributes stored in buffer pointed to
// by recPtr. The desired width of columns is in attrWidth.
//
void UT_printRec(const int attrCnt, const AttrDesc attrs[], int *attrWidth,
const Record & rec)
{
for(int i = 0; i < attrCnt; i++) {
char *attr = (char *)rec.data + attrs[i].attrOffset;
switch(attrs[i].attrType) {
case INTEGER:
int tempi;
memcpy(&tempi, attr, sizeof(int));
printf("%-*d ", attrWidth[i], tempi);
break;
case DOUBLE:
double tempf;
memcpy(&tempf, attr, sizeof(double));
printf("%-*.2f ", attrWidth[i], tempf);
break;
default:
printf("%-*.*s ", attrWidth[i], attrWidth[i], attr);
break;
}
}
printf("\n");
}
//
// Prints the contents of the specified relation.
//
// Returns:
// OK on success
// an error code otherwise
//
Status Utilities::Print(string relation)
{
Status status;
RelDesc rd;
AttrDesc *attrs;
int attrCnt;
if (relation.empty())
relation = RELCATNAME;
// Solution Starts
// get relation data
if ((status = relCat->getInfo(relation, rd)) != OK)
return status;
// get attribute data
if ((status = attrCat->getRelInfo(rd.relName, attrCnt, attrs)) != OK)
return status;
// compute width of output columns
int *attrWidth;
if ((status = UT_computeWidth(attrCnt, attrs, attrWidth)) != OK)
return status;
// open data file
HeapFileScan *hfile = new HeapFileScan(rd.relName, status);
if (!hfile)
return INSUFMEM;
if (status != OK)
return status;
// cout << "Relation name: " << rd.relName << endl << endl;
int i;
for(i = 0; i < attrCnt; i++) {
printf("%-*.*s%c ", attrWidth[i], attrWidth[i],
attrs[i].attrName, (attrs[i].indexed ? '*' : ' '));
}
printf("\n");
for(i = 0; i < attrCnt; i++) {
for(int j = 0; j < attrWidth[i]; j++)
putchar('-');
printf(" ");
}
printf("\n");
if ((status = hfile->startScan(0, 0, INTEGER, NULL, EQ)) != OK)
return status;
Record rec;
RID rid;
int records = 0;
while((status = hfile->scanNext(rid)) == OK) {
if ((status = hfile->getRecord(rid, rec)) != OK)
return status;
if (records < 100) UT_printRec(attrCnt, attrs, attrWidth, rec);
//UT_printRec(attrCnt, attrs, attrWidth, rec);
records++;
}
if (status != FILEEOF)
return status;
cout << endl << "Number of records: " << records << endl;
delete [] attrWidth;
delete [] attrs;
// close scan and data file
if ((status = hfile->endScan()) != OK)
return status;
delete hfile;
return OK;
// Solution Ends
}