-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.c
157 lines (146 loc) · 3.76 KB
/
db.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "db_listmodule.h"
//flychecker
// what? ^?
char buffer[128]; // Globala eller inte?
char buffer2[128];
void print_welcome(){
puts("Welcome to");
puts(" ____ ____ ");
puts("/\\ _`\\ /\\ _`\\ ");
puts("\\ \\ \\/\\ \\ \\ \\L\\ \\ ");
puts(" \\ \\ \\ \\ \\ \\ _ <\\ ");
puts(" \\ \\ \\_\\ \\ \\ \\L\\ \\ ");
puts(" \\ \\____/\\ \\____/ ");
puts(" \\/___/ \\/___/ ");
puts("");
}
void read_line(char *dest, int n, FILE *source){
fgets(dest, n, source);
int len = strlen(dest);
if(dest[len-1] == '\n')
dest[len-1] = '\0';
}
void pull_database(char *filename){
printf("Loading database \"%s\"...\n\n", filename);
FILE *database = fopen(filename, "r");
while(!(feof(database))){
read_line(buffer, 128, database);
read_line(buffer2, 128, database);
add_node(buffer, buffer2);
}
fclose(database);
}
void query_entry(){
printf("Enter key: ");
read_line(buffer, 128, stdin);
puts("Searching database...\n");
char *found = search_entry(buffer);
if (found){
printf("Found entry:\nkey: %s\nvalue: %s\n", buffer, found);
}
else{
printf("Could not find an entry matching key \"%s\"!\n", buffer);
}
}
void update_entry(){
printf("Enter key: ");
read_line(buffer, 128, stdin);
puts("Searching database...\n");
char *found = search_entry(buffer);
if(found){
printf("Matching entry found:\nkey: %s\nvalue: %s\n\n", buffer, found);
puts("Enter new value:");
read_line(buffer, 128, stdin);
update_value(found, buffer); // (old_value, new_value)
printf("Value updated to %s\n", found);
}
else{
printf("Could not find an entry matching key \"%s\"!\n", buffer);
}
}
void insert_entry(){
char *found = NULL;
int first = 1;
while(found || first){ //separera till en hjälpfunktion
first = 0;
puts("Enter key");
read_line(buffer, 128, stdin);
puts("Searching database for duplicate keys...");
found = search_entry(buffer);
if(found){
puts("key already exists, choose another one!\n");
}
}
puts("Key is unique!\nEnter value: ");
read_line(buffer2, 128, stdin);
add_node(buffer, buffer2);
}
void delete_entry(){
char *found = NULL;
while(!found){ //separera till en hjälpfunktion
puts("Enter key");
read_line(buffer, 128, stdin);
puts("Searching database for the key...");
found = search_entry(buffer);
if(!found){
puts("Key does not exist, choose another one!\n");
}
}
remove_node(buffer, buffer2);
printf("Deleted the following entry:\nkey: %s\nvalue: %s\n", buffer, buffer2);
}
void main_loop(){
int choice = -1;
while(choice != 0){
puts("Please choose an operation");
puts("1. Query a key");
puts("2. Update an entry");
puts("3. New entry");
puts("4. Remove entry");
puts("5. Print database");
puts("0. Exit database");
printf("? ");
scanf("%d", &choice);
while(getchar() != '\n'); // Clear stdin
switch(choice){
case 1: //query an entry
query_entry();
break;
case 2: //update an existing entry
update_entry();
break;
case 3: //insert a new entry
insert_entry();
break;
case 4: //deletes an entry
delete_entry();
break;
case 5: //prints the whole database
print_database();
break;
case 0: // Exit
puts("Good bye!");
break;
default:
// Please try again
puts("Could not parse choice! Please try again");
}
puts("");
}
}
int main(int argc, char *argv[]){
if (argc < 2){
puts("Usage: db [FILE]");
return -1;
}
print_welcome();
// Read the input file
//pull_database(argv[1]); //arraynotation
pull_database (*(argv+1)); //pekarnotation
// Main loop
main_loop();
return 0;
}