Mercurial > hg > th-libs
annotate th_util.h @ 622:6d99150a8f89
Some more slight Doxygenisation.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 17 Jan 2020 04:16:32 +0200 |
parents | 4d12924f9544 |
children | 893b2deeb5be |
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 |
553 | 4 * (C) Copyright 2002-2020 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> |
16 | 22 #include <sys/types.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 #ifndef HAVE_NO_ASSERT |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
24 # include <assert.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 #ifdef HAVE_STRING_H |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
28 # include <string.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 #else |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
30 # ifdef HAVE_STRINGS_H |
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
31 # include <strings.h> |
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
32 # endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 #ifdef HAVE_MEMORY_H |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
36 # include <memory.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
92 | 39 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
40 #ifdef __cplusplus |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
41 extern "C" { |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
42 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
44 |
580 | 45 // Kludge, as MinGW64 cross-compiler does not like certain printf() format specifiers |
577
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
46 #if defined(__MINGW64__) |
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
47 # define TH_ATTR_PRINTF_FMT(xstart, xend) |
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
48 #else |
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
49 # define TH_ATTR_PRINTF_FMT(xstart, xend) __attribute__ ((__format__ (__printf__, (xstart), (xend)))) |
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
50 #endif |
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
51 |
70d44ba005ac
Define a helper macro for __attribute__ ((__format__ (__printf__, 2, 3)))
Matti Hamalainen <ccr@tnsp.org>
parents:
573
diff
changeset
|
52 |
95 | 53 // Replacement for assert() |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 #ifdef HAVE_NO_ASSERT |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 # ifdef NDEBUG |
129 | 56 # define assert(NEXPR) // stub |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 # else |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 # 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
|
59 # endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 |
96 | 62 |
63 /* Error codes | |
64 */ | |
97 | 65 enum |
66 { | |
96 | 67 // General error codes |
68 THERR_OK = 0, | |
69 THERR_PROGRESS, // Status OK, but operation in progress | |
70 | |
71 THERR_INTERNAL, | |
72 | |
73 THERR_FOPEN, | |
74 THERR_FREAD, | |
75 THERR_FWRITE, | |
76 THERR_FSEEK, | |
77 THERR_NOT_FOUND, // Resource/data not found | |
78 | |
79 THERR_INVALID_DATA, // Some data was invalid | |
80 THERR_MALLOC, // Memory allocation failure | |
81 THERR_ALREADY_INIT, // Resource has already been initialized | |
82 THERR_INIT_FAIL, // General initialization failure | |
83 THERR_INVALID_ARGS, | |
84 | |
85 THERR_NULLPTR, // NULL pointer specified in critical argument | |
86 THERR_NOT_SUPPORTED,// Operation not supported | |
87 THERR_OUT_OF_DATA, | |
88 THERR_EXTRA_DATA, | |
89 THERR_BOUNDS, | |
163 | 90 |
96 | 91 THERR_TIMED_OUT, |
163 | 92 THERR_BUSY, |
93 THERR_IO_ERROR, | |
96 | 94 |
95 // Network errors | |
96 THERR_AUTH_FAILED, | |
97 }; | |
98 | |
99 #define TH_SYSTEM_ERRORS 100000 | |
100 | |
378 | 101 |
97 | 102 /* Log levels |
103 */ | |
104 enum | |
105 { | |
106 THLOG_NONE = 0, | |
107 THLOG_ERROR, | |
108 THLOG_WARNING, | |
109 THLOG_INFO, | |
110 THLOG_DEBUG, | |
111 }; | |
112 | |
96 | 113 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 /* Global variables |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 */ |
469
fe5b803ae449
Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents:
468
diff
changeset
|
116 extern int th_verbosity; |
19 | 117 extern char *th_prog_name, |
60 | 118 *th_prog_desc, |
19 | 119 *th_prog_version, |
120 *th_prog_author, | |
121 *th_prog_license; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 /* Functions |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 */ |
60 | 125 void th_init(char *name, char *desc, char *version, |
126 char *author, char *license); | |
127 void th_print_banner(FILE *outFile, const char *binName, const char *usage); | |
489 | 128 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
|
129 |
143 | 130 int th_term_width(); |
131 int th_term_height(); | |
132 | |
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
|
133 int th_get_error(); |
99 | 134 int th_errno_to_error(int error); |
96 | 135 const char *th_error_str(int error); |
136 | |
570
262411458cde
Add __attribute__(__format__ ..) specifiers for functions that use printf() style format specifiers.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
144 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
|
145 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
|
146 void THPRINT_V(int level, const char *fmt, va_list ap); |
11 | 147 |
462 | 148 void *th_malloc(size_t len); |
149 void *th_malloc0(size_t len); | |
150 void *th_calloc(size_t n, size_t len); | |
151 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
|
152 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
|
153 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
|
154 |
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
|
155 #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
|
156 |
16 | 157 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 #endif |
129 | 161 #endif // TH_UTIL_H |