annotate th_util.c @ 48:55e36ec05881

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