64 lines
1.6 KiB
C
64 lines
1.6 KiB
C
#include "hashmap.h"
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define INPUT_BUF_SIZE 1024
|
|
|
|
int main(int argc, char *argv[]) {
|
|
printf("hashmapp. the map to app all hashes. wait-\n"
|
|
"\":q\" to exit\n\n");
|
|
|
|
HashMap map;
|
|
init_map(&map);
|
|
|
|
char running = 1;
|
|
while (running) {
|
|
printf("> ");
|
|
|
|
char buf[INPUT_BUF_SIZE];
|
|
if (fgets(buf, INPUT_BUF_SIZE, stdin) == NULL) {
|
|
fprintf(stderr, "failed to read stdin: %u\n", errno);
|
|
return EXIT_FAILURE;
|
|
}
|
|
size_t buf_len = strnlen(buf, INPUT_BUF_SIZE);
|
|
buf[buf_len - 1] = 0;
|
|
|
|
if (!strcmp(buf, ":q"))
|
|
break;
|
|
|
|
char *eq = strchr(buf, '=');
|
|
if (eq == NULL) {
|
|
void *data = get(&map, buf);
|
|
if (data == NULL)
|
|
printf("\n");
|
|
else
|
|
printf("%s\n", (char*)data);
|
|
|
|
continue;
|
|
}
|
|
|
|
size_t key_len = eq - buf + 1;
|
|
char key[key_len];
|
|
strncpy(key, buf, key_len);
|
|
key[key_len - 1] = 0;
|
|
|
|
size_t value_len = strlen(buf + key_len);
|
|
char *value = malloc(sizeof(char) * value_len);
|
|
strncpy(value, buf + key_len, value_len);
|
|
value[value_len] = 0;
|
|
|
|
set(&map, key, value, value_len);
|
|
}
|
|
|
|
for (size_t bucket_i = 0; bucket_i < HASHMAP_BUCKETS; bucket_i++) {
|
|
Bucket *bucket = &map.buckets[bucket_i];
|
|
for (size_t i = 0; i < bucket->n_entries; i++) {
|
|
free(bucket->entries[i].key);
|
|
free(bucket->entries[i].data);
|
|
}
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|