annotate th_util.c @ 177:2b07e452fd78

Adjust program banner printing.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Mar 2015 01:46:31 +0200
parents 578d9298cc1e
children 7d25d43a3ce2
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
144
51eec969b07a Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
4 * (C) Copyright 2002-2015 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>
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
10 #include <errno.h>
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
11
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
4
67f4a4233372 Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
13 /* Default settings
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 */
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
15 static BOOL th_initialized = FALSE;
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
16 int th_verbosityLevel = 2;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
17 char *th_prog_name = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
18 *th_prog_desc = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
19 *th_prog_version = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
20 *th_prog_author = NULL,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
21 *th_prog_license = NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
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 /* 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
25 */
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
26 void th_init(char *name, char *desc, char *version,
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
27 char *author, char *license)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 {
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
29 th_prog_name = name;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
30 th_prog_desc = desc;
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
31 th_prog_version = version;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
33 if (author)
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
34 th_prog_author = author;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
35 else
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
36 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
37
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
38 if (license)
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
39 th_prog_license = license;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
40 else
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
41 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
42
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
43 th_initialized = TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 }
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
64
40b6e8dca885 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
47 void th_print_banner(FILE *outFile, const char *name, 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
48 {
177
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
49 fprintf(outFile, "\n%s", th_prog_name);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
50 if (th_prog_version != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
51 fprintf(outFile, " v%s", th_prog_version);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
52 if (th_prog_desc != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
53 fprintf(outFile, " (%s)", th_prog_desc);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
54 fprintf(outFile, "\n");
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
55
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
56 if (th_prog_author != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
57 fprintf(outFile, "%s\n", th_prog_author);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
58
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
59 if (th_prog_license != NULL)
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
60 fprintf(outFile, "%s\n", th_prog_license);
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
61
2b07e452fd78 Adjust program banner printing.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
62 fprintf(outFile, "Usage: %s %s\n", name, usage);
45
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
63 }
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
64
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
65
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
66 int th_term_width()
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
67 {
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
68 char *var = getenv("COLUMNS");
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
69 int res = (var != NULL) ? atoi(var) : 80;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
70 if (res < 5) res = 80;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
71 return res;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
72 }
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
73
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
74
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
75 int th_term_height()
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
76 {
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
77 char *var = getenv("LINESS");
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
78 int res = (var != NULL) ? atoi(var) : 25;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
79 if (res < 1) res = 1;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
80 return res;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
81 }
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
82
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
83
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 /* 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
85 * 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
86 */
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
87 void THERR_V(const char *fmt, va_list 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 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
90
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
91 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
92 vfprintf(stderr, fmt, ap);
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
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
96 void THMSG_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
97 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
98 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
99
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
100 if (th_verbosityLevel >= level)
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
101 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
102 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
103 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
104 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
105 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
106
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
107
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
108 void THPRINT_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
109 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
110 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
111
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
112 if (th_verbosityLevel >= level)
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
113 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
114 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
115 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
116 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
117
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
118
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
119 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
120 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
121 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
122 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
124 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
125 THERR_V(fmt, ap);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
126 va_end(ap);
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
130 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
131 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
132 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
133 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
135 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
136 THMSG_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
137 va_end(ap);
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
141 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
142 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
143 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
144 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
146 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
147 THPRINT_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
148 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
152 /* Error handling
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
153 */
101
a8f732601fdc Rename th_get_errno() to th_get_error(), it's more appropriate since it does
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
154 int th_get_error()
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
155 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
156 return TH_SYSTEM_ERRORS + errno;
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
157 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
158
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
159
99
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
160 int th_errno_to_error(int error)
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
161 {
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
162 return TH_SYSTEM_ERRORS + error;
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
163 }
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
164
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
165
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
166 const char *th_error_str(int error)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
167 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
168 if (error >= TH_SYSTEM_ERRORS)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
169 return strerror(error - TH_SYSTEM_ERRORS);
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
170
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
171 switch (error)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
172 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
173 case THERR_OK: return "No error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
174 case THERR_FOPEN: return "File open error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
175 case THERR_FREAD: return "Read error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
176 case THERR_FWRITE: return "Write error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
177 case THERR_FSEEK: return "Seek error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
178 case THERR_NOT_FOUND: return "Resource not found";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
179
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
180 case THERR_INVALID_DATA: return "Invalid data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
181 case THERR_MALLOC: return "Memory allocation failure";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
182 case THERR_ALREADY_INIT: return "Already initialized";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
183 case THERR_INIT_FAIL: return "Initialization failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
184 case THERR_INVALID_ARGS: return "Invalid arguments";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
185
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
186 case THERR_NULLPTR: return "NULL pointer";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
187 case THERR_NOT_SUPPORTED: return "Operation not supported";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
188 case THERR_OUT_OF_DATA: return "Out of data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
189 case THERR_EXTRA_DATA: return "Extra data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
190 case THERR_BOUNDS: return "Bounds check failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
191
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
192 case THERR_TIMED_OUT: return "Operation timed out";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
193
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
194 case THERR_AUTH_FAILED: return "Authentication failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
195
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
196 default: return "Unknown error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
197 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
198 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
199
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
200
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 /* Memory handling routines
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 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
204 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
205 return malloc(l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208
68
29f9651465c6 Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
209 void *th_malloc0(size_t l)
29f9651465c6 Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
210 {
29f9651465c6 Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
211 return calloc(1, l);
29f9651465c6 Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
212 }
29f9651465c6 Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
213
29f9651465c6 Added a new memory allocator function, th_malloc0(size), which is the same as calloc(1, size).
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
214
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 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
216 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
217 return calloc(n, l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 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
222 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
223 return realloc(p, l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 void th_free(void *p)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
229 /* Check for NULL pointers for portability due to some libc
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
230 * implementations not handling free(NULL) too well.
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
231 */
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
232 if (p) free(p);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
236 /* Doubly linked list handling
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
237 *
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
238 * In this implementation first node's prev points to last node of the list,
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
239 * and last node's next is NULL. This way we can semi-efficiently traverse to
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
240 * beginning and end of the list, assuming user does not do weird things.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
241 */
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
242 th_llist_t * th_llist_new(void *data)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
243 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
244 th_llist_t *res = th_malloc0(sizeof(th_llist_t));
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
245 res->data = data;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
246 return res;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
247 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
248
154
a6f175e301cc Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
249
156
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
250 void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *))
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
251 {
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
252 th_llist_t *curr = list;
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
253
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
254 while (curr != NULL)
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
255 {
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
256 th_llist_t *next = curr->next;
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
257 freefunc(curr);
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
258 curr = next;
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
259 }
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
260 }
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
261
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
262
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
263 void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data))
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
264 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
265 th_llist_t *curr = list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
266
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
267 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
268 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
269 th_llist_t *next = curr->next;
156
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
270 if (curr->data != NULL)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
271 freefunc(curr->data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
272 th_free(curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
273 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
274 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
275 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
276
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
277
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
278 void th_llist_free(th_llist_t *list)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
279 {
156
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
280 th_llist_t *curr = list;
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
281
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
282 while (curr != NULL)
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
283 {
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
284 th_llist_t *next = curr->next;
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
285 th_free(curr);
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
286 curr = next;
f9254c34ad05 Add new utility function for linked lists, th_llist_free_func_node().
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
287 }
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
288 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
289
53
2c90b33d3617 Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
290
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
291 void th_llist_append_node(th_llist_t **list, th_llist_t *node)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
292 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
293 if (*list != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
294 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
295 node->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
296 (*list)->prev->next = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
297 (*list)->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
298 (*list)->num++;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
299 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
300 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
301 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
302 *list = node;
59
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
303 node->prev = node;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
304 (*list)->num = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
305 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
306
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
307 node->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
308 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
309
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
310
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
311 th_llist_t *th_llist_append(th_llist_t **list, void *data)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
312 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
313 th_llist_t *node = th_llist_new(data);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
314
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
315 th_llist_append_node(list, node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
316
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
317 return node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
318 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
319
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
320
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
321 void th_llist_prepend_node(th_llist_t **list, th_llist_t *node)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
322 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
323 if (*list != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
324 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
325 node->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
326 node->next = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
327 (*list)->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
328 node->num = (*list)->num + 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
329 *list = node;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
330 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
331 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
332 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
333 *list = node->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
334 node->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
335 (*list)->num = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
336 }
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
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
341 th_llist_t *th_llist_prepend(th_llist_t **list, void *data)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
342 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
343 th_llist_t *node = th_llist_new(data);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
344
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
345 th_llist_prepend_node(list, node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
346
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
347 return node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
348 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
349
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
350 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
351 1) Remove a middle node
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
352
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
353 node0->prev->next = node->next (node1)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
354 node0->next->prev = node->prev (list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
355
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
356 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
357 node2 <- list <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
358
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
359 2) Remove first node when many items
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
360
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
361
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
362 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
363 node2 <- node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
364
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
365 *list = node0
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
366
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
367 3) Remove last node in list
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
368
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
369 if (node->next == NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
370 list->prev = node->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
371 node->prev->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
372 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
373
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
374 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
375 node1 <- list <=> node0 <=> node1 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
376
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
377 4) Remove last
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
378
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
379 list <- list -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
380
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
381
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
382 */
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
383 void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
384 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
385 if (node == *list)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
386 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
387 // First node in list
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
388 th_llist_t *tmp = (*list)->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
389 if (tmp != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
390 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
391 tmp->num = (*list)->num - 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
392 tmp->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
393 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
394 *list = tmp;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
395 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
396 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
397 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
398 // Somewhere in middle or end
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
399 if (node->prev != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
400 node->prev->next = node->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
401
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
402 if (node->next != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
403 node->next->prev = node->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
404 else
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
405 (*list)->prev = node; // Last node
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
406
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
407 (*list)->num--;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
408 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
409
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
410 node->next = node->prev = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
411 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
412
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
413
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
414 void th_llist_delete_node(th_llist_t **list, th_llist_t *node)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
415 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
416 th_llist_t *curr = *list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
417
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
418 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
419 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
420 th_llist_t *next = curr->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
421 if (curr == node)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
422 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
423 th_llist_delete_node_fast(list, curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
424 th_free(node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
425 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
426 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
427 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
428 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
429 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
430
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
431
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
432 void th_llist_delete(th_llist_t **list, const void *data)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
433 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
434 th_llist_t *curr = *list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
435
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
436 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
437 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
438 th_llist_t *next = curr->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
439 if (curr->data == data)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
440 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
441 th_llist_delete_node_fast(list, curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
442 th_free(curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
443 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
444 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
445 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
446 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
447 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
448
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
449
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
450 th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
451 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
452 th_llist_t *curr = list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
453 size_t i;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
454
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
455 for (i = 0; curr != NULL && i < n; curr = curr->next, i++);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
456
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
457 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
458 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
459
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
460
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
461 size_t th_llist_length(const th_llist_t *list)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
462 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
463 if (list == NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
464 return 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
465 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
466 return list->num;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
467 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
468
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
469
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
470 ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
471 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
472 const th_llist_t *curr = list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
473 ssize_t i = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
474
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
475 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
476 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
477 if (curr == node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
478 return i;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
479 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
480 i++;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
481
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
482 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
483 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
484
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
485 return -1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
486 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
487
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
488
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
489 void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_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
490 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
491 th_llist_t *curr = list;
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
492
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
493 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
494 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
495 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
496 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
497 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
498 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
499
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
500
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
501 int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_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
502 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
503 th_llist_t *curr = list;
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
504
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
505 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
506 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
507 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
508 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
509 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
510 *ret = curr;
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
511 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
512 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
513 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
514 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
515
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
516 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
517 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
518
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
519
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
520 th_llist_t * th_llist_find(th_llist_t *list, const void *data)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
521 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
522 th_llist_t *curr = list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
523
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
524 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
525 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
526 if (curr->data == data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
527 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
528 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
529 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
530
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
531 return NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
532 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
533
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
534
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
535 th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *))
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
536 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 145
diff changeset
537 th_llist_t *curr = list;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
538
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
539 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
540 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
541 if (compare(curr->data, userdata) == 0)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
542 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
543 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
544 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
545
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
546 return NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
547 }
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
548
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
549
26
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
550 /*
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
551 * Ringbuffers
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
552 */
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
553 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
554 {
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
555 th_ringbuf_t *res = th_malloc0(sizeof(th_ringbuf_t));
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
556
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
557 res->data = (char **) th_calloc(size, sizeof(char *));
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
558 res->size = size;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
559 res->n = 0;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
560 res->deallocator = mdeallocator;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
561
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
562 return res;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
563 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
564
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
565
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
566 BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n)
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
567 {
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
568 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
569 if (buf->data != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
570 {
98
1760e7f53359 Remove th_memset().
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
571 memset(buf->data + buf->size, 0, sizeof(char *) * n);
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
572 buf->size += n;
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
573 return TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
574 } else
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
575 return FALSE;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
576 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
577
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
578
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
579 void th_ringbuf_free(th_ringbuf_t *buf)
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
580 {
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
581 int i;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
582
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
583 for (i = 0; i < buf->size; i++)
58
c12b66ec0362 Add braces, mostly cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
584 {
25
2f7270a9d593 Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
585 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
586 buf->deallocator(buf->data[i]);
58
c12b66ec0362 Add braces, mostly cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
587 }
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
588
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
589 th_free(buf->data);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
590 th_free(buf);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
591 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
592
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
593
157
e96c900ca6f1 Rename qringbuf_t to th_ringbuf_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 156
diff changeset
594 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr)
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
595 {
28
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
596 if (buf->n < buf->size)
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
597 buf->n++;
28
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
598
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
599 th_free(buf->data[0]);
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
600 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
601 buf->data[buf->size - 1] = ptr;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
602 }
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
603
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
604
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
605 /*
115
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
606 * Simple legacy string growing buffer
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
607 */
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
608 BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
609 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
610 if (*buf == NULL)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
611 *bufsize = *len = 0;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
612
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
613 if (*buf == NULL || *len + grow >= *bufsize)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
614 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
615 *bufsize += grow + TH_BUFGROW;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
616 *buf = th_realloc(*buf, *bufsize);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
617 if (*buf == NULL)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
618 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
619 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
620 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
621 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
622
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
623
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
624 BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
625 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
626 if (!th_strbuf_grow(buf, bufsize, len, 1))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
627 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
628
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
629 (*buf)[*len] = ch;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
630 (*len)++;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
631
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
632 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
633 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
634
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
635
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
636 BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
637 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
638 size_t slen;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
639 if (str == NULL)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
640 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
641
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
642 slen = strlen(str);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
643 if (!th_strbuf_grow(buf, bufsize, len, slen + 1))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
644 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
645
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
646 memcpy(*buf + *len, str, slen + 1);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
647 (*len) += slen;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
648
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
649 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
650 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
651
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
652
115
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
653 /*
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
654 * Growing buffer
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
655 */
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
656 void th_growbuf_clear(th_growbuf_t *buf)
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
657 {
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
658 // Simply reset the current "length"
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
659 buf->len = 0;
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
660 }
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
661
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
662
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
663 void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow)
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
664 {
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
665 // Initialize the buffer structure
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
666 memset(buf, 0, sizeof(th_growbuf_t));
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
667 buf->mingrow = mingrow;
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
668 }
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
669
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
670
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
671 th_growbuf_t *th_growbuf_new(const size_t mingrow)
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
672 {
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
673 th_growbuf_t *buf;
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
674
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
675 if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL)
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
676 return NULL;
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
677
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
678 th_growbuf_init(buf, mingrow);
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
679 buf->allocated = TRUE;
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
680
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
681 return buf;
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
682 }
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
683
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
684
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
685 void th_growbuf_free(th_growbuf_t *buf)
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
686 {
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
687 th_free(buf->data);
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
688
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
689 if (buf->allocated)
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
690 th_free(buf);
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
691 }
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
692
13f001f1ad6e More work on growbuf stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 111
diff changeset
693
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
694 BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
695 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
696 if (buf == NULL)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
697 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
698
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
699 if (buf->data == NULL || buf->len + grow >= buf->size)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
700 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
701 buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
702 buf->data = (uint8_t *) th_realloc(buf->data, buf->size);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
703 if (buf->data == NULL)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
704 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
705 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
706 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
707 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
708
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
709
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
710 BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
711 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
712 size_t slen;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
713 if (str == NULL)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
714 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
715
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
716 slen = strlen(str);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
717 if (!th_growbuf_grow(buf, slen + 1))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
718 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
719
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
720 memcpy(buf->data + buf->len, str, slen + 1);
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
721 buf->len += eos ? (slen + 1) : slen;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
722
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
723 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
724 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
725
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
726
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
727 BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
728 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
729 if (!th_growbuf_grow(buf, sizeof(char)))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
730 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
731
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
732 buf->data[buf->len++] = (uint8_t) ch;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
733
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
734 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
735 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
736
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
737
120
41c2638ee537 Use void* instead of uint8_t* in th_growbuf_put_str()
Matti Hamalainen <ccr@tnsp.org>
parents: 115
diff changeset
738 BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len)
111
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
739 {
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
740 if (s == NULL)
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
741 return FALSE;
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
742
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
743 if (!th_growbuf_grow(buf, len + 1))
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
744 return FALSE;
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
745
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
746 memcpy(buf->data + buf->len, s, len + 1);
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
747 buf->len += len;
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
748
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
749 return TRUE;
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
750 }
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
751
cddfc43a4e45 More functions for growbuf.
Matti Hamalainen <ccr@tnsp.org>
parents: 109
diff changeset
752
109
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
753 BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
754 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
755 if (!th_growbuf_grow(buf, sizeof(uint8_t)))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
756 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
757
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
758 buf->data[buf->len++] = val;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
759
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
760 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
761 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
762
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
763
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
764 BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
765 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
766 if (!th_growbuf_grow(buf, sizeof(uint16_t)))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
767 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
768
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
769 buf->data[buf->len++] = (val >> 8) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
770 buf->data[buf->len++] = val & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
771
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
772 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
773 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
774
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
775
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
776 BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
777 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
778 if (!th_growbuf_grow(buf, sizeof(uint16_t)))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
779 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
780
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
781 buf->data[buf->len++] = val & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
782 buf->data[buf->len++] = (val >> 8) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
783
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
784 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
785 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
786
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
787
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
788 BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
789 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
790 if (!th_growbuf_grow(buf, sizeof(uint32_t)))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
791 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
792
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
793 buf->data[buf->len++] = (val >> 24) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
794 buf->data[buf->len++] = (val >> 16) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
795 buf->data[buf->len++] = (val >> 8) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
796 buf->data[buf->len++] = val & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
797
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
798 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
799 }
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
800
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
801
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
802 BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val)
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
803 {
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
804 if (!th_growbuf_grow(buf, sizeof(uint32_t)))
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
805 return FALSE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
806
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
807 buf->data[buf->len++] = val & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
808 buf->data[buf->len++] = (val >> 8) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
809 buf->data[buf->len++] = (val >> 16) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
810 buf->data[buf->len++] = (val >> 24) & 0xff;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
811
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
812 return TRUE;
fedad0ed894f Remove th_growbuf, move to th_util.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
813 }