Mercurial > hg > nnchat
annotate th_util.h @ 136:4ec36204d34e
Add doubly linked list handling functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Nov 2010 21:31:11 +0200 |
parents | 0313fabd8049 |
children | 900768340e65 |
rev | line source |
---|---|
0 | 1 /* |
2 * Generic utility-functions, macros and defaults | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
136
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
4 * (C) Copyright 2002-2010 Tecnic Software productions (TNSP) |
0 | 5 * |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #ifndef _TH_UTIL_H | |
9 #define _TH_UTIL_H | |
10 | |
11 #ifdef __cplusplus | |
12 extern "C" { | |
13 #endif | |
14 | |
15 #include "th_types.h" | |
16 #include <stdarg.h> | |
17 #include <stdlib.h> | |
18 #ifndef HAVE_NO_ASSERT | |
19 #include <assert.h> | |
20 #endif | |
21 | |
22 #ifdef HAVE_STRING_H | |
23 #include <string.h> | |
24 #define HAVE_MEMSET 1 | |
25 #else | |
26 #ifdef HAVE_STRINGS_H | |
27 #include <strings.h> | |
28 #endif | |
29 #endif | |
30 | |
31 #ifdef HAVE_MEMORY_H | |
32 #include <memory.h> | |
33 #endif | |
34 | |
35 | |
20 | 36 #ifdef TH_NO_DEFAULTS |
37 | 37 #define TH_PROG_AUTHOR NULL |
38 #define TH_PROG_LICENSE NULL | |
20 | 39 #else |
80 | 40 #define TH_PROG_AUTHOR "By Matti 'ccr' Hämäläinen (C) Copyright 2009 TNSP" |
37 | 41 #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2" |
20 | 42 #endif |
43 | |
44 | |
0 | 45 /* Replacement for assert() */ |
46 #ifdef HAVE_NO_ASSERT | |
47 # ifdef NDEBUG | |
48 # define assert(NEXPR) /* stub */ | |
49 # else | |
50 # define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0) | |
51 # endif | |
52 #endif | |
53 | |
54 /* Global variables | |
55 */ | |
81 | 56 extern int th_verbosityLevel; |
37 | 57 extern char *th_prog_name, |
58 *th_prog_fullname, | |
59 *th_prog_version, | |
60 *th_prog_author, | |
61 *th_prog_license; | |
0 | 62 |
63 /* Functions | |
64 */ | |
37 | 65 void th_init(char *progName, char *progFullName, char *progVersion, |
66 char *progAuthor, char *progLicense); | |
67 void THERR(const char *, ...); | |
68 void THMSG(int, const char *, ...); | |
69 void THPRINT(int, const char *, ...); | |
0 | 70 |
99
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
71 void THERR_V(const char *, va_list); |
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
72 void THMSG_V(int, const char *, va_list); |
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
73 void THPRINT_V(int, const char *, va_list); |
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
74 |
37 | 75 void *th_malloc(size_t); |
76 void *th_calloc(size_t, size_t); | |
77 void *th_realloc(void *, size_t); | |
78 void th_free(void *); | |
0 | 79 |
80 #ifdef HAVE_MEMSET | |
37 | 81 #define th_memset memset |
0 | 82 #else |
37 | 83 void *th_memset(void *, int, size_t); |
0 | 84 #endif |
85 | |
136
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
86 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
87 /* Doubly linked list handling |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
88 */ |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
89 typedef struct _qlist_t { |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
90 void *data; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
91 size_t num; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
92 struct _qlist_t *prev, *next; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
93 } qlist_t; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
94 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
95 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
96 qlist_t * th_llist_new(void *data); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
97 void th_llist_free(qlist_t *list); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
98 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data)); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
99 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
100 qlist_t * th_llist_append(qlist_t **list, void *data); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
101 qlist_t * th_llist_prepend(qlist_t **list, void *data); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
102 void th_llist_delete(qlist_t **list, const void *data); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
103 void th_llist_delete_node(qlist_t **list, qlist_t *node); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
104 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
105 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
106 size_t th_llist_length(const qlist_t *list); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
107 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
108 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
109 qlist_t * th_llist_find(qlist_t *list, const void *data); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
110 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *)); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
111 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
112 |
0 | 113 #ifdef __cplusplus |
114 } | |
115 #endif | |
116 #endif /* _TH_UTIL_H */ |