annotate th_util.c @ 60:eef9c6312b88

Change some internals.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 19 Sep 2012 23:55:44 +0300
parents 82cfbebaa8b9
children c0eefd6b2d11
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
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 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <stdio.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
11 /* Default settings
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 */
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
13 static BOOL th_initialized = FALSE;
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
14 int th_verbosityLevel = 2;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
15 char *th_prog_name = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
16 *th_prog_desc = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
17 *th_prog_version = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
18 *th_prog_author = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
19 *th_prog_license = NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 /* Initialize th_util-library and global variables
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 */
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
24 void th_init(char *name, char *desc, char *version,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
25 char *author, char *license)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 {
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
27 th_prog_name = name;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
28 th_prog_desc = desc;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
29 th_prog_version = version;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
31 if (author)
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
32 th_prog_author = author;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
33 else
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
34 th_prog_author = TH_PROG_AUTHOR;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
36 if (license)
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
37 th_prog_license = license;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
38 else
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
39 th_prog_license = TH_PROG_LICENSE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
41 th_initialized = TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
45 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: 40
diff changeset
46 {
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
47 fprintf(outFile,
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
48 "\n%s v%s (%s)\n"
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
49 "%s\n"
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
50 "%s\n"
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
51 "Usage: %s %s\n",
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
52 th_prog_name, th_prog_version, th_prog_desc,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
53 th_prog_author, th_prog_license, binName, usage);
45
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
54 }
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
55
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
56
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 /* Print formatted error, warning and information messages
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 * TODO: Implement th_vfprintf() and friends?
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 */
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
60 void THERR_V(const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
61 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
62 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
63
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
64 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
65 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
66 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
67
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
68
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
69 void THMSG_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
70 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
71 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
72
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
73 if (th_verbosityLevel >= level)
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
74 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
75 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
76 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
77 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
78 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
79
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
80
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
81 void THPRINT_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
82 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
83 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
84
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
85 if (th_verbosityLevel >= level)
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
86 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
87 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
88 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
89 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
90
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
91
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
92 void THERR(const char *fmt, ...)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
94 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
95 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
97 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
98 THERR_V(fmt, ap);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
99 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
103 void THMSG(int level, const char *fmt, ...)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
105 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
106 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
108 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
109 THMSG_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
110 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
114 void THPRINT(int level, const char *fmt, ...)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
116 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
117 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
119 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
120 THPRINT_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
121 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 /* Memory handling routines
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 void *th_malloc(size_t l)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
129 return malloc(l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 void *th_calloc(size_t n, size_t l)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
135 return calloc(n, l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 void *th_realloc(void *p, size_t l)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
141 return realloc(p, l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 void th_free(void *p)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
147 /* Check for NULL pointers for portability due to some libc
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
148 * implementations not handling free(NULL) too well.
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
149 */
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
150 if (p) free(p);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 #ifndef HAVE_MEMSET
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 void *th_memset(void *p, int c, size_t n)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
157 unsigned char *dp = (unsigned char *) p;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
158
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
159 while (n--)
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
160 *(dp++) = c;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
162 return p;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 #endif
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
165
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
166 /* Doubly linked list handling
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
167 *
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
168 * In this implementation first node's prev points to last node of the list,
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
169 * and last node's next is NULL. This way we can semi-efficiently traverse to
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
170 * beginning and end of the list, assuming user does not do weird things.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
171 */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
172 qlist_t * th_llist_new(void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
173 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
174 qlist_t *res = th_calloc(sizeof(qlist_t), 1);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
175 res->data = data;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
176 return res;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
177 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
178
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
179 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data))
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
180 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
181 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
182
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
183 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
184 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
185 qlist_t *next = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
186 if (freefunc != NULL && curr->data != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
187 freefunc(curr->data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
188 th_free(curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
189 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
190 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
191 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
192
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
193
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
194 void th_llist_free(qlist_t *list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
195 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
196 th_llist_free_func(list, NULL);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
197 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
198
53
2c90b33d3617 Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
199
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
200 void th_llist_append_node(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
201 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
202 if (*list != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
203 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
204 node->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
205 (*list)->prev->next = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
206 (*list)->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
207 (*list)->num++;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
208 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
209 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
210 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
211 *list = node;
59
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
212 node->prev = node;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
213 (*list)->num = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
214 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
215
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
216 node->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
217 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
218
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
219
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
220 qlist_t *th_llist_append(qlist_t **list, void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
221 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
222 qlist_t *node = th_llist_new(data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
223
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
224 th_llist_append_node(list, node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
225
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
226 return node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
227 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
228
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
229
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
230 void th_llist_prepend_node(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
231 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
232 if (*list != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
233 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
234 node->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
235 node->next = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
236 (*list)->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
237 node->num = (*list)->num + 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
238 *list = node;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
239 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
240 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
241 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
242 *list = node->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
243 node->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
244 (*list)->num = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
245 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
246
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
247 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
248
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
249
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
250 qlist_t *th_llist_prepend(qlist_t **list, void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
251 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
252 qlist_t *node = th_llist_new(data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
253
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
254 th_llist_prepend_node(list, node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
255
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
256 return node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
257 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
258
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
259 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
260 1) Remove a middle node
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
261
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
262 node0->prev->next = node->next (node1)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
263 node0->next->prev = node->prev (list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
264
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
265 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
266 node2 <- list <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
267
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
268 2) Remove first node when many items
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
269
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
270
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
271 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
272 node2 <- node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
273
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
274 *list = node0
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
275
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
276 3) Remove last node in list
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
277
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
278 if (node->next == NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
279 list->prev = node->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
280 node->prev->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
281 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
282
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
283 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
284 node1 <- list <=> node0 <=> node1 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
285
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
286 4) Remove last
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
287
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
288 list <- list -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
289
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
290
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
291 */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
292 static void th_llist_delete_node_fast(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
293 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
294 if (node == *list)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
295 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
296 /* First node in list */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
297 qlist_t *tmp = (*list)->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
298 if (tmp != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
299 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
300 tmp->num = (*list)->num - 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
301 tmp->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
302 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
303 *list = tmp;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
304 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
305 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
306 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
307 /* Somewhere in middle or end */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
308 if (node->prev != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
309 node->prev->next = node->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
310
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
311 if (node->next != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
312 node->next->prev = node->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
313 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
314 (*list)->prev = node; /* Last node */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
315
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
316 (*list)->num--;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
317 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
318
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
319 node->next = node->prev = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
320 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
321
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
322
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
323 void th_llist_delete_node(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
324 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
325 qlist_t *curr = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
326
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
327 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
328 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
329 qlist_t *next = curr->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
330 if (curr == node)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
331 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
332 th_llist_delete_node_fast(list, curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
333 th_free(node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
334 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
335 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
336 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
337 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
338 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
339
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
340
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
341 void th_llist_delete(qlist_t **list, const void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
342 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
343 qlist_t *curr = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
344
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
345 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
346 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
347 qlist_t *next = curr->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
348 if (curr->data == data)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
349 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
350 th_llist_delete_node_fast(list, curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
351 th_free(curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
352 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
353 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
354 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
355 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
356 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
357
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
358
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
359 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
360 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
361 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
362 size_t i;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
363
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
364 for (i = 0; curr != NULL && i < n; curr = curr->next, i++);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
365
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
366 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
367 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
368
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
369
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
370 size_t th_llist_length(const qlist_t *list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
371 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
372 if (list == NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
373 return 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
374 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
375 return list->num;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
376 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
377
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
378
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
379 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
380 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
381 const qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
382 ssize_t i = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
383
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
384 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
385 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
386 if (curr == node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
387 return i;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
388 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
389 i++;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
390
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
391 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
392 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
393
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
394 return -1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
395 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
396
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
397
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
398 void th_llist_foreach(qlist_t *list, void (*func)(qlist_t *node, void *userdata), void *data)
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
399 {
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
400 qlist_t *curr = list;
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
401
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
402 while (curr != NULL)
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
403 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
404 func(curr, data);
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
405 curr = curr->next;
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
406 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
407 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
408
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
409
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
410 int th_llist_foreach_cond(qlist_t *list, int (*func)(qlist_t *node, void *userdata), void *data, qlist_t **ret)
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
411 {
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
412 qlist_t *curr = list;
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
413
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
414 while (curr != NULL)
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
415 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
416 int res = func(curr, data);
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
417 if (res != 0)
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
418 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
419 *ret = curr;
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
420 return 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
421 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
422 curr = curr->next;
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
423 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
424
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
425 return 0;
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
426 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
427
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
428
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
429 qlist_t * th_llist_find(qlist_t *list, const void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
430 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
431 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
432
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
433 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
434 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
435 if (curr->data == data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
436 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
437 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
438 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
439
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
440 return NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
441 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
442
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
443
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
444 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *))
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
445 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
446 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
447
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
448 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
449 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
450 if (compare(curr->data, userdata) == 0)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
451 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
452 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
453 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
454
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
455 return NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
456 }
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
457
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
458
26
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
459 /*
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
460 * Ringbuffers
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
461 */
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
462 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
463 {
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
464 qringbuf_t *res = th_calloc(1, sizeof(qringbuf_t));
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
465
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
466 res->data = (char **) th_calloc(size, sizeof(char *));
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
467 res->size = size;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
468 res->n = 0;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
469 res->deallocator = mdeallocator;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
470
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
471 return res;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
472 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
473
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
474
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
475 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n)
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
476 {
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
477 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *));
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
478 if (buf->data != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
479 {
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
480 th_memset(buf->data + buf->size, 0, sizeof(char *) * n);
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
481 buf->size += n;
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
482 return TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
483 } else
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
484 return FALSE;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
485 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
486
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
487
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
488 void th_ringbuf_free(qringbuf_t *buf)
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
489 {
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
490 int i;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
491
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
492 for (i = 0; i < buf->size; i++)
25
2f7270a9d593 Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
493 if (buf->data[i] != NULL)
2f7270a9d593 Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
494 buf->deallocator(buf->data[i]);
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
495
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
496 th_free(buf->data);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
497 th_free(buf);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
498 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
499
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
500
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
501 void th_ringbuf_add(qringbuf_t *buf, void *ptr)
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
502 {
28
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
503 if (buf->n < buf->size)
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
504 buf->n++;
28
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
505
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
506 th_free(buf->data[0]);
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
507 memmove(&(buf->data[0]), &(buf->data[1]), (buf->size - 1) * sizeof(void *));
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
508 buf->data[buf->size - 1] = ptr;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
509 }