forked from troydhanson/uthash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest56.c
98 lines (81 loc) · 2.09 KB
/
test56.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
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
#include <string.h>
#include "uthash.h"
#include "utlist.h"
#include "utstring.h"
typedef struct example_user_t {
int id;
int cookie;
UT_hash_handle hh;
} example_user_t;
#define BUFLEN 20
typedef struct el {
char bname[BUFLEN];
struct el *next, *prev;
} el;
static int namecmp(void *_a, void *_b)
{
el *a = (el*)_a;
el *b = (el*)_b;
return strcmp(a->bname,b->bname);
}
int main()
{
el *name, *elt, *tmp, etmp;
int i;
example_user_t *user, *users=NULL;
el *head = NULL; /* important- initialize to NULL! */
char linebuf[BUFLEN];
FILE *file;
UT_string *s;
char binary[] = "\xff\xff";
file = fopen( "test11.dat", "r" );
if (file == NULL) {
perror("can't open: ");
exit(-1);
}
while (fgets(linebuf,BUFLEN,file) != NULL) {
name = (el*)malloc(sizeof(el));
if (name == NULL) {
exit(-1);
}
strcpy(name->bname, linebuf);
DL_APPEND(head, name);
}
DL_SORT(head, namecmp);
DL_FOREACH(head,elt) {
printf("%s", elt->bname);
}
memcpy(etmp.bname, "WES\n", 5UL);
DL_SEARCH(head,elt,&etmp,namecmp);
if (elt != NULL) {
printf("found %s\n", elt->bname);
}
/* now delete each element, use the safe iterator */
DL_FOREACH_SAFE(head,elt,tmp) {
DL_DELETE(head,elt);
}
fclose(file);
/* create elements */
for(i=0; i<10; i++) {
user = (example_user_t*)malloc(sizeof(example_user_t));
if (user == NULL) {
exit(-1);
}
user->id = i;
user->cookie = i*i;
HASH_ADD_INT(users,id,user);
}
for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
printf("user %d, cookie %d\n", user->id, user->cookie);
}
utstring_new(s);
utstring_bincpy(s, binary, sizeof(binary));
printf("length is %u\n", (unsigned)utstring_len(s));
utstring_clear(s);
utstring_printf(s,"number %d", 10);
printf("%s\n", utstring_body(s));
utstring_free(s);
return 0;
}