Mercurial > hg > th-libs
annotate th_util.h @ 481:e4ce60239d16
Bump copyright years.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 10 Jan 2019 20:01:38 +0200 |
parents | fe5b803ae449 |
children | 85039ab7e6ab |
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 |
481 | 4 * (C) Copyright 2002-2019 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 */ |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
8 #ifndef TH_UTIL_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
9 #define TH_UTIL_H |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
11 #ifdef HAVE_CONFIG_H |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
12 #include "config.h" |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
183
8baec5a454c1
Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
15 #if defined(__WIN64) || defined(_WIN64) || defined(__WIN32) || defined(_WIN32) |
8baec5a454c1
Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
16 # define TH_PLAT_WINDOWS 1 |
8baec5a454c1
Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
17 #else |
8baec5a454c1
Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
18 # define TH_PLAT_UNIX 1 |
8baec5a454c1
Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
19 #endif |
8baec5a454c1
Define TH_PLAT_{WINDOWS,UNIX} depending on the platform we are on.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
20 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 #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
|
22 #include <stdio.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 #include <stdarg.h> |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 #include <stdlib.h> |
75 | 25 #include <unistd.h> |
16 | 26 #include <sys/types.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 #ifndef HAVE_NO_ASSERT |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
28 # include <assert.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 #ifdef HAVE_STRING_H |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
32 # include <string.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 #else |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
34 # ifdef HAVE_STRINGS_H |
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
35 # include <strings.h> |
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
36 # endif |
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 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 #ifdef HAVE_MEMORY_H |
466
bdee04d21282
Preprocessor stuff indentation.
Matti Hamalainen <ccr@tnsp.org>
parents:
462
diff
changeset
|
40 # include <memory.h> |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 |
92 | 43 |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
44 #ifdef __cplusplus |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
45 extern "C" { |
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
46 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
4
67f4a4233372
Move defaults to header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 |
95 | 49 // Replacement for assert() |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 #ifdef HAVE_NO_ASSERT |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 # ifdef NDEBUG |
129 | 52 # define assert(NEXPR) // stub |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 # else |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 # 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
|
55 # endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 #endif |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
96 | 58 |
59 /* Error codes | |
60 */ | |
97 | 61 enum |
62 { | |
96 | 63 // General error codes |
64 THERR_OK = 0, | |
65 THERR_PROGRESS, // Status OK, but operation in progress | |
66 | |
67 THERR_INTERNAL, | |
68 | |
69 THERR_FOPEN, | |
70 THERR_FREAD, | |
71 THERR_FWRITE, | |
72 THERR_FSEEK, | |
73 THERR_NOT_FOUND, // Resource/data not found | |
74 | |
75 THERR_INVALID_DATA, // Some data was invalid | |
76 THERR_MALLOC, // Memory allocation failure | |
77 THERR_ALREADY_INIT, // Resource has already been initialized | |
78 THERR_INIT_FAIL, // General initialization failure | |
79 THERR_INVALID_ARGS, | |
80 | |
81 THERR_NULLPTR, // NULL pointer specified in critical argument | |
82 THERR_NOT_SUPPORTED,// Operation not supported | |
83 THERR_OUT_OF_DATA, | |
84 THERR_EXTRA_DATA, | |
85 THERR_BOUNDS, | |
163 | 86 |
96 | 87 THERR_TIMED_OUT, |
163 | 88 THERR_BUSY, |
89 THERR_IO_ERROR, | |
96 | 90 |
91 // Network errors | |
92 THERR_AUTH_FAILED, | |
93 }; | |
94 | |
95 #define TH_SYSTEM_ERRORS 100000 | |
96 | |
378 | 97 |
97 | 98 /* Log levels |
99 */ | |
100 enum | |
101 { | |
102 THLOG_NONE = 0, | |
103 THLOG_ERROR, | |
104 THLOG_WARNING, | |
105 THLOG_INFO, | |
106 THLOG_DEBUG, | |
107 }; | |
108 | |
96 | 109 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 /* Global variables |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 */ |
469
fe5b803ae449
Rename the global variable th_verbosityLevel to th_verbosity.
Matti Hamalainen <ccr@tnsp.org>
parents:
468
diff
changeset
|
112 extern int th_verbosity; |
19 | 113 extern char *th_prog_name, |
60 | 114 *th_prog_desc, |
19 | 115 *th_prog_version, |
116 *th_prog_author, | |
117 *th_prog_license; | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 /* Functions |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 */ |
60 | 121 void th_init(char *name, char *desc, char *version, |
122 char *author, char *license); | |
123 void th_print_banner(FILE *outFile, const char *binName, const char *usage); | |
45
e031a062b731
Separate program "banner" printing from th_args_help() to th_print_banner().
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
124 |
143 | 125 int th_term_width(); |
126 int th_term_height(); | |
127 | |
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
|
128 int th_get_error(); |
99 | 129 int th_errno_to_error(int error); |
96 | 130 const char *th_error_str(int error); |
131 | |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
132 void THERR(const char *fmt, ...); |
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
133 void THMSG(int level, const char *fmt, ...); |
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
134 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
|
135 |
194
87dac812cac4
Define some argument identifiers in function prototypes and rename some arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
192
diff
changeset
|
136 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
|
137 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
|
138 void THPRINT_V(int level, const char *fmt, va_list ap); |
11 | 139 |
462 | 140 void *th_malloc(size_t len); |
141 void *th_malloc0(size_t len); | |
142 void *th_calloc(size_t n, size_t len); | |
143 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
|
144 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
|
145 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
|
146 |
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
|
147 #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
|
148 |
16 | 149 |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 #ifdef __cplusplus |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 #endif |
129 | 153 #endif // TH_UTIL_H |