-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
42 lines (35 loc) · 994 Bytes
/
string.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
#include "string.h"
#include "hash_table.h"
#include "error.h"
#include "hash.h"
#include <string.h>
#include <stdlib.h>
struct hash_table string_hash;
static void const *get_key_string(void const *obj);
static unsigned int hash_string(void const *obj, unsigned int size);
int
compare_string(void const *left, void const *right) {
return strcmp(left, right);
}
char const *
make_string(char const *key) {
char *obj, *new_obj;
if (!key)
return NULL;
obj = (char *)lookup_hash(key, get_key_string, compare_string, hash_string, &string_hash);
if (!obj && (new_obj = (char *)malloc(strlen(key) + 1)) != NULL) {
strcpy(new_obj, key);
insert_hash(new_obj, hash_string, &string_hash);
obj = new_obj;
}
MemoryCheck(obj);
return obj;
}
static void const *
get_key_string(void const *obj) {
return obj;
}
static unsigned int
hash_string(void const *obj, unsigned int size) {
return strhash((char const *)obj) & (size - 1);
}