aboutsummaryrefslogtreecommitdiff
path: root/example_use.c
blob: bbd2433285959ccc10bdd34068ad3bf9eb5dbd7b (plain)
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
// I didn't take the effort to clean up this file as I did with
// hashtable.c and hashtable.h. It's not really important, tho.
// Instead of this "console hashtable" I should make some test cases
// (which I probably won't be motivated enough to do anyway).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hashtable.h"

void print_pair(const char *key, const char *val, void *dummy){
  printf("key: %s   val: %s\n", key, val);
}

void free_pair(void *key, void *val, void *dummy){
  free(key);
  free(val);
}

int main(int argc, char **argv){
  hashtable_t ht;
  if (ht_string_init(&ht)) // HT_NO_MEM
    {
      fprintf(stderr, "ERROR, couldn't allocate memory!\n");
      exit(-1);
    }

  char buf[100];

  while (scanf("%99s", buf) != EOF) {
    if (!strcmp(buf, "add")) {
      if (scanf("%99s", buf) == EOF) break;
      
      size_t len = strlen(buf) + 1;
      char *key = malloc(len);
      if (key == NULL) {
	fprintf(stderr, "ERROR, couldn't allocate memory!\n");
	exit(1);
      }
      
      memcpy(key, buf, len);
      
      if (scanf("%99s", buf) == EOF) {
	free(key);
	break;
      }
      
      len = strlen(buf) + 1;
      char *val = malloc(len);
      if (val == NULL) {
	free(key);
	fprintf(stderr, "ERROR, couldn't allocate memory!\n");
	exit(1);
      }
      
      memcpy(val, buf, len);

      switch (ht_add(&ht, key, val))
	{
	case HT_OK:
	  break;
	case HT_NO_MEM:
	  fprintf(stderr, "ERROR, couldn't allocate memory!\n");
	  free(key); free(val);
	  exit(1);
	default: // case HT_KEY_PRESENT
	  printf("Key already present!\n");
	}
    } 
    else if (!strcmp(buf, "set")) {
      if (scanf("%99s", buf) == EOF) break;

      size_t len = strlen(buf) + 1;
      char *key = malloc(len);
      if (key == NULL) {
	fprintf(stderr, "ERROR, couldn't allocate memory!\n");
	exit(1);
      }

      memcpy(key, buf, len);

      if (scanf("%99s", buf) == EOF) break;

      len = strlen(buf) + 1;
      char *val = malloc(len);
      if (val == NULL) {
	free(key);
	fprintf(stderr, "ERROR, couldn't allocate memory!\n");
	exit(1);
      }

      memcpy(val, buf, len);

      void *oldkey = NULL, *oldval = NULL;
      if (ht_set(&ht, key, val, &oldkey, &oldval) != 0) {
	free(key); free(val);
	
	fprintf(stderr, "ERROR, couldn't allocate memory!\n");
	exit(1);
      }
      
      if (oldval != NULL) {
	printf("Replaced: %s\n", (char*) oldval);
	free(oldval); free(oldkey);
      }
    }
    else if (!strcmp(buf, "get")) {
      if (scanf("%99s", buf) == EOF) break;
      
      void *val;
      if (ht_get(&ht, buf, NULL, &val))
	printf("No such entry!\n");
      else printf("%s\n", (char*) val);
    }
    else if (!strcmp(buf, "rem")) {
      if (scanf("%99s", buf) == EOF) break;

      void *storedkey, *val;
      if (ht_rem(&ht, buf, &storedkey, &val))
	printf("No such entry!\n");
      else {
	printf("%s\n", (char*) val);
	free(val); free(storedkey);
      }
    }
    else if(!strcmp(buf, "entries")) printf("%zu\n", ht.entries);
    else if(!strcmp(buf, "print"))
      ht_map(&ht, NULL, (void (*)(void*, void*, void*)) &print_pair);
    else printf("I have no idea what you're talking about...\n");
  }

  ht_map_destroy(&ht, NULL,
		 (void (*)(void*, void*, void*)) &free_pair);
}