-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuffend.h
238 lines (185 loc) · 4.88 KB
/
buffend.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#define SIZE 512
#define PAGES 1024
#define TAM 20
typedef struct table_bd{
char nome[TAM];
char tipo;
int tam;
}tp_table;
typedef struct buffer{
unsigned char db; //Dirty bit
unsigned char pc; //Pin counter
unsigned int nrec; //numero de registros armazenados na pagina
char data[SIZE];
}tp_buffer;
union c_double{
double dnum;
char double_cnum[sizeof(double)];
};
union c_int{
int num;
char cnum[sizeof(int)];
};
void cpystr(char *, char *, int, int);
void initbuffer(tp_buffer *);
void cria_campo(int, int, char *);
void drawline(tp_buffer *, tp_table *, int, int *, int);
int cabecalho(tp_table *, int);
int printbufferpoll(tp_buffer *, tp_table *, int, int);
int load_metadata(FILE *, tp_table *, int);
void load_data(FILE *, tp_buffer *, int);
void cpystr(char *tg, char *sc, int st, int len)
{
st = st * len;
int i=st,j=0;
for (;i<len+st;i++)
tg[i]=sc[j++];
}
void initbuffer(tp_buffer *bp){
int i;
for (i = 0;i < PAGES; i++){
bp->db=0;
bp->pc=0;
bp->nrec=0;
bp->data[i] = 0;
bp++;
}
}
void cria_campo(int tam, int header, char *val){
int i;
char aux[40];
if(header){
aux[0] = '|';
for(i = 1; i <= TAM && val[i-1] != '\0'; i++){
aux[i] = val[i-1];
}
for(;i <= TAM; i++)
aux[i] = '=';
aux[i++] = '|';
aux[i] ='\0';
printf("%s", aux);
}
else{
for(i = 0; i < tam; i++){
printf(" ");
}
printf("|");
}
}
void drawline(tp_buffer *buffpoll, tp_table *s, int num_reg, int *pos_ini, int num_page){
int count, pos_aux, bit_pos;
union c_double cd;
union c_int ci;
count = pos_aux = bit_pos = 0;
for(count = 0; count < num_reg; count++){
pos_aux = *(pos_ini);
bit_pos = 0;
//---para imprimir com espacos corretos
printf("|");
switch(s[count].tipo){
case 'S':
while(pos_aux < *(pos_ini) + s[count].tam && buffpoll[num_page].data[pos_aux] != '\0'){
printf("%c", buffpoll[num_page].data[pos_aux++]);
bit_pos++;
}
cria_campo((TAM - (bit_pos)), 0, (char*)' ');
break;
case 'I':
while(pos_aux < *(pos_ini) + s[count].tam){
ci.cnum[bit_pos++] = buffpoll[num_page].data[pos_aux++];
}
if(ci.num < 10)
printf("%d ", ci.num); //Controla o número de casas até a centena
else if(ci.num < 100)
printf("%d ", ci.num);
else
printf("%d", ci.num);
cria_campo((TAM - 3), 0, (char*)' ');
break;
case 'D':
while(pos_aux < *(pos_ini) + s[count].tam){
cd.double_cnum[bit_pos++] = buffpoll[num_page].data[pos_aux++]; // Cópias os bytes do double para área de memória da union
}
printf("%.3lf", cd.dnum);
cria_campo((TAM - 8), 0, (char*)' ');
break;
case 'C':
printf("%c", buffpoll[num_page].data[pos_aux]);
if(s[count].tam < strlen(s[count].nome)){
bit_pos = strlen(s[count].nome);
}
else{
bit_pos = s[count].tam;
}
cria_campo((bit_pos - 1), 0, (char*)' ');
break;
default: printf("Erro de Impressão\n\n\n");
break;
}
*(pos_ini) += s[count].tam;
}
printf("\n");
}
int cabecalho(tp_table *s, int num_reg){
int count, aux;
aux = 0;
for(count = 0; count < num_reg; count++){
cria_campo(s[count].tam, 1, s[count].nome);
aux += s[count].tam;
}
printf("\n");
return aux;
}
int printbufferpoll(tp_buffer *buffpoll, tp_table *s, int num_page, int num_reg){
int aux, i;
if(buffpoll[num_page].nrec == 0){
return 0;
}
i = aux = 0;
aux = cabecalho(s, num_reg);
while(i < buffpoll[num_page].nrec * aux){
drawline(buffpoll, s, num_reg, &i, num_page);
}
return 1;
}
int load_metadata(FILE *arq_meta, tp_table *s, int valor_reg){
int count, pos;
int tam_registro;
pos = tam_registro = 0;
char c;
for(count = 0; count < valor_reg; count++){ //Passa pelo numero de ocorrencias da tabela
pos = 0;
while(fread(&c, sizeof(char), 1, arq_meta) && c != '\0'){ //le o nome do campo
s[count].nome[pos] = c;
pos++;
}
s[count].nome[pos] = c; //acrescenta o \0 no final da string
fread(&c, sizeof(char), 1, arq_meta); //le o tipo (S, I, D, C)
s[count].tipo = c;
fread(&s[count].tam, sizeof(int), 1, arq_meta);
tam_registro += s[count].tam;
//printf("%s %c %d\n", s[count].nome, s[count].tipo, s[count].tam);
//printf("TAMANHO DA TUPLA: %d\n", tam_registro);
}
return tam_registro;
}
void load_data(FILE *arq_data, tp_buffer *bufferpool, int tam_registro){
char *lei_aux;
int page_count, num_page;
page_count = num_page = 0;
lei_aux = (char*)malloc(sizeof(char)*tam_registro);
while(fgetc(arq_data) != EOF && num_page < PAGES){ //le os dados do arquivo data
fseek(arq_data, -1, 1);
fread(lei_aux, sizeof(char)*tam_registro, 1, arq_data);
if(page_count + tam_registro < SIZE){
cpystr(bufferpool[num_page].data, lei_aux, bufferpool[num_page].nrec, tam_registro);
bufferpool[num_page].nrec++;
page_count += tam_registro;
}
else{
page_count = 0;
num_page++;
}
}
free(lei_aux);
}