annotate th_util.c @ 99:5819454a28a4

Add th_errno_to_error().
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 25 May 2014 01:01:31 +0300
parents 1760e7f53359
children a8f732601fdc
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
66
34d58b0f2d52 Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
4 * (C) Copyright 2002-2012 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 {
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
49 fprintf(outFile,
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
50 "\n%s v%s (%s)\n"
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
51 "%s\n"
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
52 "%s\n"
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
53 "Usage: %s %s\n",
60
eef9c6312b88 Change some internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
54 th_prog_name, th_prog_version, th_prog_desc,
64
40b6e8dca885 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
55 th_prog_author, th_prog_license, 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
56 }
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
57
e031a062b731 Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
58
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 /* 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
60 * 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
61 */
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
62 void THERR_V(const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
63 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
64 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
65
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
66 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
67 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
68 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
69
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
70
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
71 void THMSG_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
72 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
73 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
74
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
75 if (th_verbosityLevel >= level)
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
76 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
77 fprintf(stderr, "%s: ", th_prog_name);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
78 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
79 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
80 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
81
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
82
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
83 void THPRINT_V(int level, const char *fmt, va_list ap)
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
84 {
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
85 assert(th_initialized == TRUE);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
86
48
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
87 if (th_verbosityLevel >= level)
55e36ec05881 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
88 {
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
89 vfprintf(stderr, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
90 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
91 }
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
92
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
93
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
94 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
95 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
96 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
97 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
99 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
100 THERR_V(fmt, ap);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
101 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
105 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
106 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
107 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
108 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
110 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
111 THMSG_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
112 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
116 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
117 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
118 va_list ap;
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
119 assert(th_initialized == TRUE);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
11
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
121 va_start(ap, fmt);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
122 THPRINT_V(level, fmt, ap);
e467b3586e4d Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
123 va_end(ap);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
127 /* Error handling
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
128 */
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
129 int th_get_errno()
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
130 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
131 return TH_SYSTEM_ERRORS + errno;
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
132 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
133
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
134
99
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
135 int th_errno_to_error(int error)
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
136 {
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
137 return TH_SYSTEM_ERRORS + error;
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
138 }
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
139
5819454a28a4 Add th_errno_to_error().
Matti Hamalainen <ccr@tnsp.org>
parents: 98
diff changeset
140
96
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
141 const char *th_error_str(int error)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
142 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
143 if (error >= TH_SYSTEM_ERRORS)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
144 return strerror(error - TH_SYSTEM_ERRORS);
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
145
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
146 switch (error)
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
147 {
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
148 case THERR_OK: return "No error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
149 case THERR_FOPEN: return "File open error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
150 case THERR_FREAD: return "Read error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
151 case THERR_FWRITE: return "Write error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
152 case THERR_FSEEK: return "Seek error";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
153 case THERR_NOT_FOUND: return "Resource not found";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
154
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
155 case THERR_INVALID_DATA: return "Invalid data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
156 case THERR_MALLOC: return "Memory allocation failure";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
157 case THERR_ALREADY_INIT: return "Already initialized";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
158 case THERR_INIT_FAIL: return "Initialization failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
159 case THERR_INVALID_ARGS: return "Invalid arguments";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
160
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
161 case THERR_NULLPTR: return "NULL pointer";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
162 case THERR_NOT_SUPPORTED: return "Operation not supported";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
163 case THERR_OUT_OF_DATA: return "Out of data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
164 case THERR_EXTRA_DATA: return "Extra data";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
165 case THERR_BOUNDS: return "Bounds check failed";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
166
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
167 case THERR_TIMED_OUT: return "Operation timed out";
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
168
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
169 case THERR_AUTH_FAILED: return "Authentication failed";
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 default: return "Unknown 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 }
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
174
3df6bec3c6b1 Add error handling stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 93
diff changeset
175
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 /* Memory handling routines
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 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
179 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
180 return malloc(l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183
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
184 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
185 {
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
186 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
187 }
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
188
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
189
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 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
191 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
192 return calloc(n, l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 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
197 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
198 return realloc(p, l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 void th_free(void *p)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
204 /* Check for NULL pointers for portability due to some libc
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
205 * implementations not handling free(NULL) too well.
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
206 */
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
207 if (p) free(p);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
211 /* Doubly linked list handling
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
212 *
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
213 * In this implementation first node's prev points to last node of the list,
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
214 * and last node's next is NULL. This way we can semi-efficiently traverse to
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
215 * beginning and end of the list, assuming user does not do weird things.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
216 */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
217 qlist_t * th_llist_new(void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
218 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
219 qlist_t *res = th_calloc(sizeof(qlist_t), 1);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
220 res->data = data;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
221 return res;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
222 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
223
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
224 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data))
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
225 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
226 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
227
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
228 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
229 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
230 qlist_t *next = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
231 if (freefunc != NULL && curr->data != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
232 freefunc(curr->data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
233 th_free(curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
234 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
235 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
236 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
237
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
238
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
239 void th_llist_free(qlist_t *list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
240 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
241 th_llist_free_func(list, NULL);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
242 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
243
53
2c90b33d3617 Add th_llist_append_node() and th_llist_prepend_node() to public header.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
244
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
245 void th_llist_append_node(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
246 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
247 if (*list != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
248 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
249 node->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
250 (*list)->prev->next = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
251 (*list)->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
252 (*list)->num++;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
253 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
254 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
255 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
256 *list = node;
59
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
257 node->prev = node;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
258 (*list)->num = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
259 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
260
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
261 node->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
262 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
263
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
264
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
265 qlist_t *th_llist_append(qlist_t **list, void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
266 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
267 qlist_t *node = th_llist_new(data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
268
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
269 th_llist_append_node(list, node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
270
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
271 return node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
272 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
273
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
274
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
275 void th_llist_prepend_node(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
276 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
277 if (*list != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
278 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
279 node->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
280 node->next = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
281 (*list)->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
282 node->num = (*list)->num + 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
283 *list = node;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
284 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
285 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
286 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
287 *list = node->prev = node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
288 node->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
289 (*list)->num = 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
290 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
291
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
292 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
293
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
294
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
295 qlist_t *th_llist_prepend(qlist_t **list, void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
296 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
297 qlist_t *node = th_llist_new(data);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
298
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
299 th_llist_prepend_node(list, node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
300
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
301 return node;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
302 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
303
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
304 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
305 1) Remove a middle node
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
306
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
307 node0->prev->next = node->next (node1)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
308 node0->next->prev = node->prev (list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
309
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
310 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
311 node2 <- list <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
312
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
313 2) Remove first node when many items
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
314
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
315
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
316 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
317 node2 <- node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
318
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
319 *list = node0
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
320
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
321 3) Remove last node in list
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
322
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
323 if (node->next == NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
324 list->prev = node->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
325 node->prev->next = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
326 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
327
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
328 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
329 node1 <- list <=> node0 <=> node1 -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
330
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
331 4) Remove last
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
332
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
333 list <- list -> NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
334
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
335
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
336 */
93
029b3dea31ff Make th_llist_delete_node_fast() public.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
337 void th_llist_delete_node_fast(qlist_t **list, qlist_t *node)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
338 {
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
339 if (node == *list)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
340 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
341 /* First node in list */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
342 qlist_t *tmp = (*list)->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
343 if (tmp != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
344 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
345 tmp->num = (*list)->num - 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
346 tmp->prev = (*list)->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
347 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
348 *list = tmp;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
349 }
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
350 else
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
351 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
352 /* Somewhere in middle or end */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
353 if (node->prev != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
354 node->prev->next = node->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
355
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
356 if (node->next != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
357 node->next->prev = node->prev;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
358 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
359 (*list)->prev = node; /* Last node */
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
360
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
361 (*list)->num--;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
362 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
363
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
364 node->next = node->prev = NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
365 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
366
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
367
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
368 void th_llist_delete_node(qlist_t **list, qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
369 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
370 qlist_t *curr = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
371
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
372 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
373 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
374 qlist_t *next = curr->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
375 if (curr == node)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
376 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
377 th_llist_delete_node_fast(list, curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
378 th_free(node);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
379 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
380 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
381 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
382 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
383 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
384
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
385
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
386 void th_llist_delete(qlist_t **list, const void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
387 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
388 qlist_t *curr = *list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
389
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
390 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
391 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
392 qlist_t *next = curr->next;
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
393 if (curr->data == data)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
394 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
395 th_llist_delete_node_fast(list, curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
396 th_free(curr);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
397 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
398 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
399 curr = next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
400 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
401 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
402
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
403
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
404 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
405 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
406 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
407 size_t i;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
408
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
409 for (i = 0; curr != NULL && i < n; curr = curr->next, i++);
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
410
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
411 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
412 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
413
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
414
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
415 size_t th_llist_length(const qlist_t *list)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
416 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
417 if (list == NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
418 return 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
419 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
420 return list->num;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
421 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
422
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
423
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
424 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
425 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
426 const qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
427 ssize_t i = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
428
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
429 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
430 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
431 if (curr == node)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
432 return i;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
433 else
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
434 i++;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
435
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
436 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
437 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
438
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
439 return -1;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
440 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
441
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
442
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
443 void th_llist_foreach(qlist_t *list, void (*func)(qlist_t *node, void *userdata), void *data)
54
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
444 {
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
445 qlist_t *curr = list;
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
446
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
447 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
448 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
449 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
450 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
451 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
452 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
453
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
454
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
455 int th_llist_foreach_cond(qlist_t *list, int (*func)(qlist_t *node, void *userdata), void *data, qlist_t **ret)
54
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
456 {
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
457 qlist_t *curr = list;
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
458
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
459 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
460 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
461 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
462 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
463 {
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
464 *ret = curr;
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
465 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
466 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
467 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
468 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
469
55
300fba04b7ad Adjust the foreach linked list functions a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
470 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
471 }
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
472
48926b3ff598 Add new linked list handling functions: th_llist_foreach() and th_llist_foreach_cond()
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
473
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
474 qlist_t * th_llist_find(qlist_t *list, const void *data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
475 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
476 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
477
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
478 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
479 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
480 if (curr->data == data)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
481 return curr;
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 NULL;
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
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
489 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *))
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
490 {
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
491 qlist_t *curr = list;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
492
40
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
493 while (curr != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
494 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
495 if (compare(curr->data, userdata) == 0)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
496 return curr;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
497 curr = curr->next;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
498 }
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
499
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
500 return NULL;
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
501 }
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
502
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
503
26
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
504 /*
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
505 * Ringbuffers
04187ae0b3b6 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
506 */
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
507 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
508 {
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
509 qringbuf_t *res = th_calloc(1, sizeof(qringbuf_t));
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
510
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
511 res->data = (char **) th_calloc(size, sizeof(char *));
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
512 res->size = size;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
513 res->n = 0;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
514 res->deallocator = mdeallocator;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
515
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
516 return res;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
517 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
518
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
519
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
520 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n)
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
521 {
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
522 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
523 if (buf->data != NULL)
5b1e38a41bac Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
524 {
98
1760e7f53359 Remove th_memset().
Matti Hamalainen <ccr@tnsp.org>
parents: 96
diff changeset
525 memset(buf->data + buf->size, 0, sizeof(char *) * n);
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
526 buf->size += n;
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
527 return TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
528 } else
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
529 return FALSE;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
530 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
531
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
532
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
533 void th_ringbuf_free(qringbuf_t *buf)
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
534 {
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
535 int i;
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
536
33
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
537 for (i = 0; i < buf->size; i++)
58
c12b66ec0362 Add braces, mostly cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
538 {
25
2f7270a9d593 Make ringbuffer deallocator function call only happen if data pointer is != NULL
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
539 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
540 buf->deallocator(buf->data[i]);
58
c12b66ec0362 Add braces, mostly cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
541 }
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
542
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
543 th_free(buf->data);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
544 th_free(buf);
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
545 }
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
546
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
547
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
548 void th_ringbuf_add(qringbuf_t *buf, void *ptr)
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
549 {
28
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
550 if (buf->n < buf->size)
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
551 buf->n++;
28
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
552
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
553 th_free(buf->data[0]);
c47dcb8a4ffe Change how ringbuffer actually works to be more sensible.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
554 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
555 buf->data[buf->size - 1] = ptr;
23
a4f894105953 Add ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
556 }