forked from troydhanson/uthash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest22.c
68 lines (57 loc) · 1.88 KB
/
test22.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
#include <stdlib.h> /* malloc */
#include <stddef.h> /* offsetof */
#include <stdio.h> /* printf */
#include <string.h> /* memset */
#include "uthash.h"
#define UTF32 '\x1'
typedef struct {
UT_hash_handle hh;
size_t len;
char encoding; /* these two fields */
int text[]; /* comprise the key */
} msg_t;
typedef struct {
char encoding;
int text[];
} lookup_key_t;
int main()
{
unsigned keylen;
msg_t *msg, *tmp, *msgs = NULL;
lookup_key_t *lookup_key;
int beijing[] = {0x5317, 0x4eac}; /* UTF-32LE for 北京 */
/* allocate and initialize our structure */
msg = (msg_t*)malloc( sizeof(msg_t) + sizeof(beijing) );
if (msg == NULL) {
exit(-1);
}
memset(msg, 0, sizeof(msg_t)+sizeof(beijing)); /* zero fill */
msg->len = sizeof(beijing);
msg->encoding = UTF32;
memcpy(msg->text, beijing, sizeof(beijing));
/* calculate the key length including padding, using formula */
keylen = offsetof(msg_t, text) /* offset of last key field */
+ sizeof(beijing) /* size of last key field */
- offsetof(msg_t, encoding); /* offset of first key field */
/* add our structure to the hash table */
HASH_ADD( hh, msgs, encoding, keylen, msg);
/* look it up to prove that it worked :-) */
msg=NULL;
lookup_key = (lookup_key_t*)malloc(sizeof(*lookup_key) + sizeof(beijing));
if (lookup_key == NULL) {
exit(-1);
}
memset(lookup_key, 0, sizeof(*lookup_key) + sizeof(beijing));
lookup_key->encoding = UTF32;
memcpy(lookup_key->text, beijing, sizeof(beijing));
HASH_FIND( hh, msgs, &lookup_key->encoding, keylen, msg );
if (msg != NULL) {
printf("found \n");
}
free(lookup_key);
HASH_ITER(hh, msgs, msg, tmp) {
HASH_DEL(msgs, msg);
free(msg);
}
return 0;
}