-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
222 lines (189 loc) · 7.6 KB
/
Copy pathmain.c
File metadata and controls
222 lines (189 loc) · 7.6 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
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
#include "minidb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define COMMAND_MAX_STRLEN 256
#define prompt_string(text, outbuff) \
do { \
printf((text)); \
fflush(stdout); \
fgets((outbuff), sizeof((outbuff)), stdin); \
(outbuff)[strlen((outbuff)) - 1] = '\0'; \
} while (0)
#define prompt_int(text, intval) \
do { \
char buff[20]; \
prompt_string(text, buff); \
(intval) = (int) strtol(buff, NULL, 10); \
} while (0)
#define prompt_float(text, floatval) \
do { \
char buff[20]; \
prompt_string(text, buff); \
(floatval) = strtof(buff, NULL); \
} while (0)
typedef struct Alumno
{
char nombre[52]; // 51 + \0
int ncontrol;
float promedio;
} Alumno;
static void print_pretty_table(const Alumno *alumno, bool print_header)
{
if (print_header) {
puts("+-----------------------------------------------------+------------+----------+");
puts("| Nombre | N. Control | Promedio |");
puts("+-----------------------------------------------------+------------+----------+");
}
if (alumno != NULL) {
printf("| %-51s | % 10d | % 8.1f |\n", alumno->nombre, alumno->ncontrol, alumno->promedio);
puts("+-----------------------------------------------------+------------+----------+");
}
}
static void select_print_callback(int64_t key, void *data)
{
print_pretty_table((Alumno *) data, false);
}
static void print_help_text()
{
puts(
"Ayuda de MiniDB:\n\n"
" Comando Descripción \n"
"-------------- -------------------------------------------------\n"
" exit, salir Cierra la base de datos y termina el programa. \n"
" new, nueva Crea una nueva base de datos vacía. \n"
" open, abrir Abre una base de datos existente. \n"
" dbinfo Muestra información sobre la base de datos. \n"
" select Buscar un registro y mostrarlo en pantalla. \n"
" select * Mostrar todos los registros en pantalla. \n"
" insert Insertar un registro. \n"
" update Actualizar un registro existente. \n"
" delete Borrar un registro. \n"
);
}
int main(void)
{
MiniDb *db;
MiniDbState error;
char command[COMMAND_MAX_STRLEN];
char filepath[COMMAND_MAX_STRLEN];
// Preamble
printf("MiniDb x%zu bits.\n\n", sizeof(int *) * 8);
while (1) {
prompt_string("MiniDb> ", command);
if (strcmp(command, "exit") == 0) {
return 0;
} else if (strcmp(command, "help") == 0) {
print_help_text();
} else if (strcmp(command, "new") == 0 || strcmp(command, "nueva") == 0) {
prompt_string("Path: ", filepath);
printf("Creando base de datos... ");
fflush(stdout);
error = minidb_create(&db, filepath, sizeof(Alumno));
if (error != MINIDB_OK) {
printf("Fatal error: %s\n", minidb_error_get_str(error));
exit(1);
}
puts("Ok!\n");
fflush(stdout);
} else if (strcmp(command, "open") == 0 || strcmp(command, "abrir") == 0) {
prompt_string("Path: ", filepath);
error = minidb_open(&db, filepath);
if (error != MINIDB_OK) {
printf("Fatal error: %s\n", minidb_error_get_str(error));
exit(1);
}
puts("La base de datos se ha abierto correctamente.\n");
} else {
printf(
"Error: commando \"%s\" no es valido.\n"
"Debe crear una nueva base de datos utilizando el comando 'nueva'\n"
"o abrir una base de datos existente con el comando 'abrir'.\n"
"Para terminar el programa, escriba el comando 'salir'.\n\n",
command
);
continue;
}
break;
}
const char *db_name = filepath;
for (int64_t i = (int64_t) strlen(filepath) - 1; i >= 0; i--) {
if (filepath[i] == '\\' || filepath[i] == '/') {
db_name = filepath + i + 1;
break;
}
}
Alumno alumno;
while (1) {
printf("MiniDb\\%s> ", db_name);
fflush(stdout);
fgets(command, sizeof(command), stdin);
command[strlen(command) - 1] = '\0';
if (strcmp(command, "salir") == 0 || strcmp(command, "exit") == 0) {
break;
} else if (strcmp(command, "help") == 0) {
print_help_text();
} else if (strcmp(command, "dbinfo") == 0) {
MiniDbInfo info;
minidb_get_info(db, &info);
puts("== DATABASE INFO ==");
printf("Physical name : %s\n", db_name);
printf("Data Size : %zu\n", info.data_size);
printf("Row Count : %zu\n", info.row_count);
printf("Free Count : %zu\n", info.free_count);
printf("Db Data Size : %zu\n", info.data_size * info.row_count);
puts("");
} else if (strcmp(command, "select") == 0) {
int ncontrol;
prompt_int("N. control: ", ncontrol);
puts("");
error = minidb_select(db, ncontrol, &alumno);
if (error != MINIDB_OK) {
printf("Error: %s\n\n", minidb_error_get_str(error));
continue;
}
print_pretty_table(&alumno, true);
printf("\n");
} else if (strcmp(command, "select *") == 0) {
print_pretty_table(NULL, true);
minidb_select_all(db, select_print_callback);
} else if (strcmp(command, "insert") == 0) {
prompt_string("Nombre[51] : ", alumno.nombre);
prompt_int("N. control : ", alumno.ncontrol);
prompt_float("Promedio : ", alumno.promedio);
error = minidb_insert(db, alumno.ncontrol, &alumno);
if (error != MINIDB_OK) {
printf("Error: %s\n\n", minidb_error_get_str(error));
continue;
}
puts("Tupla insertada correctamente\n");
} else if (strcmp(command, "update") == 0) {
prompt_string("Nombre[51] : ", alumno.nombre);
prompt_int("N. control : ", alumno.ncontrol);
prompt_float("Promedio : ", alumno.promedio);
error = minidb_update(db, alumno.ncontrol, &alumno);
if (error != MINIDB_OK) {
printf("Error: %s\n\n", minidb_error_get_str(error));
continue;
}
puts("Tupla actualizada correctamente\n");
} else if (strcmp(command, "delete") == 0) {
int ncontrol;
prompt_int("N. control: ", ncontrol);
error = minidb_delete(db, ncontrol);
if (error != MINIDB_OK) {
printf("Error: %s\n\n", minidb_error_get_str(error));
continue;
}
puts("Tupla eliminada correctamente\n");
} else {
if (command[0] != '\0') {
puts("Error: comando no reconocido.\n");
}
}
}
minidb_close(&db);
printf("Programa finalizado\n");
return 0;
}