Mercurial > hg > th-libs
annotate th_util.h @ 60:eef9c6312b88
Change some internals.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 19 Sep 2012 23:55:44 +0300 |
parents | c78757d6e440 |
children | 34d58b0f2d52 |
rev | line source |
---|---|
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Generic utility-functions, macros and defaults |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
16 | 4 * (C) Copyright 2002-2010 Tecnic Software productions (TNSP) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
8 #ifndef TH_UTIL_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
9 #define TH_UTIL_H |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
11 #ifdef HAVE_CONFIG_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
12 #include "config.h" |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 #include "th_types.h" |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
16 #include <stdio.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 #include <stdarg.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 #include <stdlib.h> |
16 | 19 #include <sys/types.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 #ifndef HAVE_NO_ASSERT |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 #include <assert.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 #ifdef HAVE_STRING_H |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 #include <string.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 #define HAVE_MEMSET 1 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 #else |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 #ifdef HAVE_STRINGS_H |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 #include <strings.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 #ifdef HAVE_MEMORY_H |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 #include <memory.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
37 #ifdef __cplusplus |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
38 extern "C" { |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
39 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
41 #ifdef TH_NO_DEFAULTS |
6 | 42 #define TH_PROG_AUTHOR NULL |
43 #define TH_PROG_LICENSE NULL | |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
44 #else |
57 | 45 #define TH_PROG_AUTHOR "By Matti 'ccr' Hämäläinen (C) Copyright 2012 TNSP" |
6 | 46 #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2" |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 #endif |
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 |
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
49 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 /* Replacement for assert() */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 #ifdef HAVE_NO_ASSERT |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 # ifdef NDEBUG |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 # define assert(NEXPR) /* stub */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 # else |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 # define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 # endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 /* Global variables |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 */ |
19 | 61 extern int th_verbosityLevel; |
62 extern char *th_prog_name, | |
60 | 63 *th_prog_desc, |
19 | 64 *th_prog_version, |
65 *th_prog_author, | |
66 *th_prog_license; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 /* Functions |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 */ |
60 | 70 void th_init(char *name, char *desc, char *version, |
71 char *author, char *license); | |
72 void th_print_banner(FILE *outFile, const char *binName, const char *usage); | |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
73 |
6 | 74 void THERR(const char *, ...); |
75 void THMSG(int, const char *, ...); | |
76 void THPRINT(int, const char *, ...); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 |
11 | 78 void THERR_V(const char *, va_list); |
79 void THMSG_V(int, const char *, va_list); | |
80 void THPRINT_V(int, const char *, va_list); | |
81 | |
6 | 82 void *th_malloc(size_t); |
83 void *th_calloc(size_t, size_t); | |
84 void *th_realloc(void *, size_t); | |
85 void th_free(void *); | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 #ifdef HAVE_MEMSET |
6 | 88 #define th_memset memset |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 #else |
6 | 90 void *th_memset(void *, int, size_t); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 |
16 | 93 |
94 /* Doubly linked list handling | |
95 */ | |
96 typedef struct _qlist_t { | |
97 void *data; | |
98 size_t num; | |
99 struct _qlist_t *prev, *next; | |
100 } qlist_t; | |
101 | |
102 | |
103 qlist_t * th_llist_new(void *data); | |
104 void th_llist_free(qlist_t *list); | |
105 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data)); | |
106 | |
53
2c90b33d3617
Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
107 void th_llist_append_node(qlist_t **list, qlist_t *node); |
16 | 108 qlist_t * th_llist_append(qlist_t **list, void *data); |
53
2c90b33d3617
Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents:
49
diff
changeset
|
109 void th_llist_prepend_node(qlist_t **list, qlist_t *node); |
16 | 110 qlist_t * th_llist_prepend(qlist_t **list, void *data); |
111 void th_llist_delete(qlist_t **list, const void *data); | |
112 void th_llist_delete_node(qlist_t **list, qlist_t *node); | |
113 | |
114 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n); | |
115 size_t th_llist_length(const qlist_t *list); | |
116 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node); | |
117 | |
55
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
118 void th_llist_foreach(qlist_t *list, void (*func)(qlist_t *node, void *userdata), void *data); |
300fba04b7ad
Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
119 int th_llist_foreach_cond(qlist_t *list, int (*func)(qlist_t *node, void *userdata), void *data, qlist_t **res); |
54
48926b3ff598
Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
120 |
16 | 121 qlist_t * th_llist_find(qlist_t *list, const void *data); |
122 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *)); | |
123 | |
124 | |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
125 /* Ringbuffer implementation |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
126 */ |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
127 typedef struct { |
33 | 128 char **data; |
23
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
129 int n, size; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
130 void (*deallocator)(void *); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
131 } qringbuf_t; |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
132 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
133 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *)); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
134 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
135 void th_ringbuf_free(qringbuf_t *buf); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
136 void th_ringbuf_add(qringbuf_t *buf, void *ptr); |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
137 |
a4f894105953
Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
138 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 #endif |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
142 #endif /* TH_UTIL_H */ |