-
Notifications
You must be signed in to change notification settings - Fork 1
/
htcli.c
184 lines (169 loc) · 4.35 KB
/
htcli.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
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
/*****************************************************
* Hash Table Command Line Interface *
* Author: Michael P. Nitowski <mpnitowski@gmail.com> *
* License: MIT License *
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "hashtable.h"
#define MIN_TABLE_SIZE 2
#define MAX_INPUT 255
#define MAX_KEY 100
#define MAX_VALUE 154
static void header()
{
puts("----------------------------------------------------------");
puts("| Hash Table Implementation (c) 2016 Michael P. Nitowski |");
puts("----------------------------------------------------------");
}
static bool haspre(char *prefix, char *str)
{
char *test = strstr(str, prefix);
return test == str;
}
static bool command(char *command, char *data)
{
return haspre(command, data);
}
static bool put(char *input, HASHTABLE ht)
{
bool ret = 1;
char *key;
char *value;
key = (char*)malloc(sizeof(char) * MAX_KEY);
value = (char*)malloc(sizeof(char) * MAX_VALUE);
if(sscanf(input, "put %s %[^\n]", key, value) < 2) return 0;
ret = hashtable_put(key, value, ht);
if (ret) puts("Put."); else puts("Cannot add anymore elements.");
free(key);
free(value);
return 1;
}
static bool get(char *input, HASHTABLE ht)
{
char *key;
key = (char*)malloc(sizeof(char) * MAX_KEY);
if(sscanf(input, "get %s", key) < 1) {
return 0;
}
char *value = hashtable_getv(key, ht);
if(strcmp(value, "") == 0) {
puts("Entry does not exist.");
} else {
printf("Got: %s\n", value);
}
free(key);
return 1;
}
static bool _remove(char *input, HASHTABLE ht)
{
char *key;
key = (char*)malloc(sizeof(char) * MAX_KEY);
if(sscanf(input, "remove %s", key) < 1) return 0;
hashtable_remove(key, ht);
printf("Removed: %s\n", key);
free(key);
return 1;
}
static bool print(char *input, HASHTABLE ht)
{
bool b;
char *all = (char*)malloc(sizeof(char) * 4);
if(sscanf(input, "print %s", all) >= 1) {
b = 1;
} else {
b = 0;
}
hashtable_print(ht, b);
free(all);
return 1;
}
static bool resize(char *input, HASHTABLE *ht)
{
size_t size;
char *ssize = (char*)malloc(sizeof(char) * 4);
if(sscanf(input, "resize %s", ssize) > 0) {
size = (size_t) atoi(ssize);
} else {
size = ht->size * 2;
}
if(size < MIN_TABLE_SIZE) {
return 0;
}
hashtable_resize(size, ht);
printf("Hashtable resized to %d.\n", (int) ht->size);
free(ssize);
return 1;
}
static void stats(char *input, HASHTABLE ht)
{
printf("Count: %d\nSize: %d\n", *ht.count, (int) ht.size);
}
static void help()
{
puts("Commands:");
puts("put <key> <value>");
puts("get <key>");
puts("remove <key>");
puts("print");
puts("print all");
puts("resize");
puts("resize <size>");
puts("help");
puts("quit");
}
static void info()
{
puts("Maximum fill size is (size - 1) elements in this implementation.");
puts("Hence, minimum usable table size is 2.");
}
bool process(char *input, HASHTABLE *ht)
{
if(command("quit", input)) {
hashtable_deinit(ht);
exit(0);
return 1;
}
if(command("put", input)) return put(input, *ht);
if(command("get", input)) return get(input, *ht);
if(command("remove", input)) return _remove(input, *ht);
if(command("print", input)) return print(input, *ht);
if(command("resize", input)) return resize(input, ht);
if(command("stat", input)) {
stats(input, *ht);
return 1;
}
if(command("help", input)) {
help();
return 1;
}
if(command("info", input)) {
info();
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
long int size;
if(argc < 2 ) {
puts("Usage: htcli <size>");
return 0;
}
size = strtol(argv[1], NULL, 10);
if(size < MIN_TABLE_SIZE) {
printf("Hashtable size must be greater than %d.\n", MIN_TABLE_SIZE);
return 0;
}
HASHTABLE ht = hashtable_init((size_t) size);
header();
char input[MAX_INPUT];
while(1) {
printf(" hashtable>");
fgets(input, MAX_INPUT, stdin);
if(!(process(input, &ht))) puts("Invalid input. Type help for commands.");
}
return 0;
}