Mercurial > hg > th-libs
annotate th_util.h @ 789:d61d3eb29053 default tip
Bump copyright.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 08 Mar 2024 15:26:24 +0200 |
parents | db1a132c7754 |
children |
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 |
726 | 4 * (C) Copyright 2002-2022 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 */ |
622
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
580
diff
changeset
|
8 /// @file |
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
580
diff
changeset
|
9 /// @brief Miscellaneous utility functions, memory allocation, error codes etc. |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
10 #ifndef TH_UTIL_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
11 #define TH_UTIL_H |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
13 #ifdef HAVE_CONFIG_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
14 #include "config.h" |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 #include "th_types.h" |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
18 #include <stdio.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 #include <stdarg.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 #include <stdlib.h> |
75 | 21 #include <unistd.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 #ifndef HAVE_NO_ASSERT |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
23 # include <assert.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
623
893b2deeb5be
Get rid of some useless includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
622
diff
changeset
|
26 #if defined(HAVE_STRING_H) |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
27 # include <string.h> |
623
893b2deeb5be
Get rid of some useless includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
622
diff
changeset
|
28 #elif defined(HAVE_STRINGS_H) |
893b2deeb5be
Get rid of some useless includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
622
diff
changeset
|
29 # include <strings.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 #ifdef HAVE_MEMORY_H |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
33 # include <memory.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 |
92 | 36 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
37 #ifdef __cplusplus |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
38 extern "C" { |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
39 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
41 |
95 | 42 // Replacement for assert() |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 #ifdef HAVE_NO_ASSERT |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 # ifdef NDEBUG |
129 | 45 # define assert(NEXPR) // stub |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 # else |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 # define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0) |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 # endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 |
96 | 51 |
52 /* Error codes | |
53 */ | |
97 | 54 enum |
55 { | |
96 | 56 // General error codes |
57 THERR_OK = 0, | |
58 THERR_PROGRESS, // Status OK, but operation in progress | |
59 | |
60 THERR_INTERNAL, | |
61 | |
62 THERR_FOPEN, | |
63 THERR_FREAD, | |
64 THERR_FWRITE, | |
65 THERR_FSEEK, | |
66 THERR_NOT_FOUND, // Resource/data not found | |
67 | |
68 THERR_INVALID_DATA, // Some data was invalid | |
69 THERR_MALLOC, // Memory allocation failure | |
70 THERR_ALREADY_INIT, // Resource has already been initialized | |
71 THERR_INIT_FAIL, // General initialization failure | |
72 THERR_INVALID_ARGS, | |
73 | |
74 THERR_NULLPTR, // NULL pointer specified in critical argument | |
75 THERR_NOT_SUPPORTED,// Operation not supported | |
76 THERR_OUT_OF_DATA, | |
77 THERR_EXTRA_DATA, | |
78 THERR_BOUNDS, | |
163 | 79 |
96 | 80 THERR_TIMED_OUT, |
163 | 81 THERR_BUSY, |
82 THERR_IO_ERROR, | |
96 | 83 |
84 // Network errors | |
85 THERR_AUTH_FAILED, | |
86 }; | |
87 | |
88 #define TH_SYSTEM_ERRORS 100000 | |
89 | |
378 | 90 |
97 | 91 /* Log levels |
92 */ | |
93 enum | |
94 { | |
95 THLOG_NONE = 0, | |
96 THLOG_ERROR, | |
97 THLOG_WARNING, | |
98 THLOG_INFO, | |
99 THLOG_DEBUG, | |
100 }; | |
101 | |
96 | 102 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 /* Global variables |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 */ |
469
fe5b803ae449
Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents:
468
diff
changeset
|
105 extern int th_verbosity; |
19 | 106 extern char *th_prog_name, |
60 | 107 *th_prog_desc, |
19 | 108 *th_prog_version, |
109 *th_prog_author, | |
110 *th_prog_license; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 /* Functions |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 */ |
60 | 114 void th_init(char *name, char *desc, char *version, |
115 char *author, char *license); | |
116 void th_print_banner(FILE *outFile, const char *binName, const char *usage); | |
489 | 117 void th_print_version(FILE *outFile); |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
118 |
783
db1a132c7754
Add missing void to mark empty function argument lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
746
diff
changeset
|
119 int th_term_width(void); |
db1a132c7754
Add missing void to mark empty function argument lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
746
diff
changeset
|
120 int th_term_height(void); |
143 | 121 |
783
db1a132c7754
Add missing void to mark empty function argument lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
746
diff
changeset
|
122 int th_get_error(void); |
99 | 123 int th_errno_to_error(int error); |
96 | 124 const char *th_error_str(int error); |
125 | |
570
262411458cde
Add __attribute__(__format__ ..) specifiers for functions that use printf() style format specifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
126 void THERR(const char *fmt, ...) |
577
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
127 TH_ATTR_PRINTF_FMT(1, 2); |
570
262411458cde
Add __attribute__(__format__ ..) specifiers for functions that use printf() style format specifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
128 void THMSG(int level, const char *fmt, ...) |
577
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
129 TH_ATTR_PRINTF_FMT(2, 3); |
570
262411458cde
Add __attribute__(__format__ ..) specifiers for functions that use printf() style format specifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
130 void THPRINT(int level, const char *fmt, ...) |
577
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
131 TH_ATTR_PRINTF_FMT(2, 3); |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
133 void THERR_V(const char *fmt, va_list ap); |
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
134 void THMSG_V(int level, const char *fmt, va_list ap); |
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
135 void THPRINT_V(int level, const char *fmt, va_list ap); |
11 | 136 |
462 | 137 void *th_malloc(size_t len); |
138 void *th_malloc0(size_t len); | |
139 void *th_calloc(size_t n, size_t len); | |
140 void *th_realloc(void *ptr, size_t len); | |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
141 void th_free(void *ptr); |
197
88986d0b56ef
Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents:
194
diff
changeset
|
142 void th_free_r_real(void **ptr); |
88986d0b56ef
Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents:
194
diff
changeset
|
143 |
88986d0b56ef
Rename th_free_r() to th_free_r_real() and #define a macro wrapper for
Matti Hamalainen <ccr@tnsp.org>
parents:
194
diff
changeset
|
144 #define th_free_r(ptr) th_free_r_real((void **) ptr) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 |
16 | 146 |
727
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
147 #define th_test_bit(pdata, nbit) \ |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
148 (pdata[nbit >> 3] & (1 << (nbit & 7))) |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
149 |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
150 static inline void th_set_bit(uint8_t *pdata, const size_t nbit, const int value) |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
151 { |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
152 if (value) |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
153 pdata[nbit >> 3] |= (1 << (nbit & 7)); |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
154 else |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
155 pdata[nbit >> 3] &= ~(1 << (nbit & 7)); |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
156 } |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
157 |
b415b70254fa
Add set_bit/test_bit helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
158 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 #endif |
129 | 162 #endif // TH_UTIL_H |