Mercurial > hg > nnchat
annotate th_util.h @ 357:2aef8606539e dev-0_9_7
Bump version.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 23 Jun 2011 09:00:08 +0300 |
parents | 21528fe535fb |
children |
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> | |
152
900768340e65
Including sys/types.h is required here.
Matti Hamalainen <ccr@tnsp.org>
parents:
136
diff
changeset
|
18 #include <sys/types.h> |
0 | 19 #ifndef HAVE_NO_ASSERT |
20 #include <assert.h> | |
21 #endif | |
22 | |
23 #ifdef HAVE_STRING_H | |
24 #include <string.h> | |
25 #define HAVE_MEMSET 1 | |
26 #else | |
27 #ifdef HAVE_STRINGS_H | |
28 #include <strings.h> | |
29 #endif | |
30 #endif | |
31 | |
32 #ifdef HAVE_MEMORY_H | |
33 #include <memory.h> | |
34 #endif | |
35 | |
36 | |
20 | 37 #ifdef TH_NO_DEFAULTS |
37 | 38 #define TH_PROG_AUTHOR NULL |
39 #define TH_PROG_LICENSE NULL | |
20 | 40 #else |
237 | 41 #define TH_PROG_AUTHOR "By Matti 'ccr' Hämäläinen (C) Copyright 2011 TNSP" |
37 | 42 #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2" |
20 | 43 #endif |
44 | |
45 | |
0 | 46 /* Replacement for assert() */ |
47 #ifdef HAVE_NO_ASSERT | |
48 # ifdef NDEBUG | |
49 # define assert(NEXPR) /* stub */ | |
50 # else | |
51 # define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0) | |
52 # endif | |
53 #endif | |
54 | |
55 /* Global variables | |
56 */ | |
238 | 57 extern int th_verbosityLevel; |
58 extern char *th_prog_name, | |
59 *th_prog_fullname, | |
60 *th_prog_version, | |
61 *th_prog_author, | |
62 *th_prog_license; | |
0 | 63 |
64 /* Functions | |
65 */ | |
37 | 66 void th_init(char *progName, char *progFullName, char *progVersion, |
238 | 67 char *progAuthor, char *progLicense); |
37 | 68 void THERR(const char *, ...); |
69 void THMSG(int, const char *, ...); | |
70 void THPRINT(int, const char *, ...); | |
0 | 71 |
99
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
72 void THERR_V(const char *, va_list); |
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
73 void THMSG_V(int, const char *, va_list); |
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
74 void THPRINT_V(int, const char *, va_list); |
0313fabd8049
Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
81
diff
changeset
|
75 |
37 | 76 void *th_malloc(size_t); |
77 void *th_calloc(size_t, size_t); | |
78 void *th_realloc(void *, size_t); | |
79 void th_free(void *); | |
0 | 80 |
81 #ifdef HAVE_MEMSET | |
37 | 82 #define th_memset memset |
0 | 83 #else |
37 | 84 void *th_memset(void *, int, size_t); |
0 | 85 #endif |
86 | |
136
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
87 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
88 /* Doubly linked list handling |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
89 */ |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
90 typedef struct _qlist_t { |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
91 void *data; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
92 size_t num; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
93 struct _qlist_t *prev, *next; |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
94 } qlist_t; |
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 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
97 qlist_t * th_llist_new(void *data); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
98 void th_llist_free(qlist_t *list); |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
99 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
|
100 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 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
|
105 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
110 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
|
111 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
|
112 |
4ec36204d34e
Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
99
diff
changeset
|
113 |
276
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
114 /* Ringbuffer implementation |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
115 */ |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
116 typedef struct { |
306
21528fe535fb
Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
117 char **data; |
276
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
118 int n, size; |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
119 void (*deallocator)(void *); |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
120 } qringbuf_t; |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
121 |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
122 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *)); |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
123 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n); |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
124 void th_ringbuf_free(qringbuf_t *buf); |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
125 void th_ringbuf_add(qringbuf_t *buf, void *ptr); |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
126 |
1807059fb8f2
Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
238
diff
changeset
|
127 |
0 | 128 #ifdef __cplusplus |
129 } | |
130 #endif | |
131 #endif /* _TH_UTIL_H */ |