-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
111 lines (102 loc) · 2.75 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "hashtable.h"
#define FLDSIZ 256
char printmenu();
void stripnl(char *);
int main()
{
char ans;
char key[FLDSIZ], value[FLDSIZ];
char *target;
unsigned alt = 0;
hashtable table;
init_table(&table);
puts("reading records");
read_records(&table, "table.tab");
while ((ans = printmenu()) != 'E') {
memset(key, 0, sizeof(key));
memset(value, 0, sizeof(value));
printf(" key: ");
fgets(key, sizeof(key), stdin);
stripnl(key);
if (ans == 'C') {
while (strlen(key)) {
printf(" value: ");
fgets(value, sizeof(value), stdin);
stripnl(value);
add_record(&table, key, value);
alt = 1;
printf(" key: ");
fgets(key, sizeof(key), stdin);
stripnl(key);
}
}
else if (ans == 'R') {
while (strlen(key)) {
target = find_value(&table, key);
printf(" value: %s\n", target);
printf(" key: ");
fgets(key, sizeof(key), stdin);
stripnl(key);
}
}
else if (ans == 'U') {
while (strlen(key)) {
printf(" value: ");
fgets(value, sizeof(value), stdin);
stripnl(value);
add_record(&table, key, value);
alt = 1;
printf(" key: ");
fgets(key, sizeof(key), stdin);
stripnl(key);
}
}
else if (ans == 'D') {
while (strlen(key)) {
puts("not implemented");
break;
/* delete_key(&table, key); */
alt = 1;
printf(" key: ");
fgets(key, sizeof(key), stdin);
stripnl(key);
}
}
}
/* print_table(&table); */
if (alt)
write_table(&table, "table.tab");
delete_table(&table);
return(0);
}
char printmenu()
{
char ch;
char ans[25];
do {
printf(" (c)reate records\n");
printf(" (r)etrieve records\n");
printf(" (u)pdate records\n");
printf(" (d)elete records\n");
printf(" (e)xit\n");
printf(" -> ");
memset(ans, 0, sizeof(ans));
fgets(ans, sizeof(ans), stdin);
ch = toupper(ans[0]);
} while (ch != 'C' && ch != 'R' && ch != 'U' && ch != 'D' && ch != 'E');
return(ch);
}
void stripnl(char *str)
{
int n = strlen(str);
if (str[n - 2] == '\r' && str[n - 1] == '\n') {
str[n - 2] = '\0';
return;
}
if (str[n - 1] == '\n')
str[n - 1] = '\0';
return;
}