Skip to content

Latest commit

 

History

History
416 lines (343 loc) · 12.9 KB

universal.h

File metadata and controls

416 lines (343 loc) · 12.9 KB
 
May 12, 2007
May 12, 2007
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
9
10
11
#ifndef _INCL_UNIVERSAL_H_
#define _INCL_UNIVERSAL_H_
Mar 25, 2006
Mar 25, 2006
12
// Include this file from everywhere...it provides basic type sanity, etc.
Mar 25, 2006
Mar 25, 2006
14
// Include the Holy Trinity...
Mar 25, 2006
Mar 25, 2006
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Mar 25, 2006
Mar 25, 2006
19
20
// and some others...
#include <assert.h>
May 9, 2007
May 9, 2007
21
#include <time.h> // !!! FIXME: maybe use this in platform layer?
Mar 25, 2006
Mar 25, 2006
22
Sep 25, 2007
Sep 25, 2007
23
24
25
26
27
28
29
30
31
32
33
34
35
// Windows system headers conflict with MojoSetup typedefs, so chop out
// all the massive and unnecessary dependencies that windows.h pulls in...
#if PLATFORM_WINDOWS
#define WIN32_LEAN_AND_MEAN 1
#endif
#if _MSC_VER
#include <malloc.h> // need this to get alloca() in MSVC.
// !!! FIXME: temporary solution.
#define snprintf _snprintf
#define strcasecmp(x,y) _stricmp(x,y)
#endif
Mar 24, 2006
Mar 24, 2006
36
37
38
39
#ifdef __cplusplus
extern "C" {
#endif
Apr 13, 2009
Apr 13, 2009
40
// !!! FIXME: bad.
41
42
43
44
45
46
47
48
49
typedef char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
Dec 9, 2006
Dec 9, 2006
50
51
52
53
54
55
56
57
58
59
60
// These are likely to get stolen by some overzealous library header...
#ifdef boolean
#error Something is defining boolean. Resolve this before including universal.h.
#endif
#ifdef true
#error Something is defining true. Resolve this before including universal.h.
#endif
#ifdef false
#error Something is defining false. Resolve this before including universal.h.
#endif
61
typedef int boolean;
Mar 25, 2006
Mar 25, 2006
62
63
64
#define true 1
#define false 0
Sep 25, 2007
Sep 25, 2007
65
66
67
68
69
// MSVC doesn't support the "inline" keyword for normal C sources, just C++.
#if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline)
#define inline __inline
#endif
Dec 27, 2006
Dec 27, 2006
70
71
72
73
74
75
76
77
78
79
// Compiler-enforced printf() safety helper.
// This is appended to function declarations that use printf-style semantics,
// and will make sure your passed the right params to "..." for the
// format you specified, so gcc can catch programmer bugs at compile time.
#ifdef __GNUC__
#define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
#else
#define ISPRINTF(x,y)
#endif
Jun 1, 2007
Jun 1, 2007
80
81
82
83
84
85
86
87
88
89
// Compiler-enforced sentinel safety helper.
// This is appended to function declarations that use sentinel-style semantics,
// and will make sure your passed the right params to "..." where a NULL
// is needed at the end of the list.
#ifdef __GNUC__
#define ISSENTINEL __attribute__((sentinel))
#else
#define ISSENTINEL
#endif
Mar 25, 2006
Mar 25, 2006
90
// Command line access outside of main().
Mar 25, 2006
Mar 25, 2006
91
extern int GArgc;
Dec 9, 2006
Dec 9, 2006
92
extern const char **GArgv;
Mar 25, 2006
Mar 25, 2006
93
Dec 7, 2006
Dec 7, 2006
94
95
96
// Build-specific details.
extern const char *GBuildVer;
Mar 25, 2006
Mar 25, 2006
97
98
99
// Static, non-stack memory for scratch work...not thread safe!
extern uint8 scratchbuf_128k[128 * 1024];
Mar 2, 2008
Mar 2, 2008
100
101
102
103
#define UNICODE_BOGUS_CHAR_VALUE 0xFFFFFFFF
#define UNICODE_BOGUS_CHAR_CODEPOINT '?'
// !!! FIXME: document me!
Mar 3, 2008
Mar 3, 2008
104
uint32 utf8codepoint(const char **str);
Mar 2, 2008
Mar 2, 2008
105
Mar 3, 2008
Mar 3, 2008
106
107
108
109
110
111
// !!! FIXME: document me!
int utf8len(const char *str);
// !!! FIXME: document me!
char **splitText(const char *text, int scrw, int *_count, int *_w);
Mar 2, 2008
Mar 2, 2008
112
Jan 12, 2008
Jan 12, 2008
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Format a string, sort of (but not exactly!) like sprintf().
// The only formatters accepted are %0 through %9 (and %%), which do not
// have to appear in order in the string, but match the varargs passed to the
// function. Only strings are accepted for varargs. This function allocates
// memory as necessary, so you need to free() the result, but don't need to
// preallocate a buffer and be concerned about overflowing it.
// This does not use scratchbuf_128k.
char *format(const char *fmt, ...);
// Convert an int to a string. This uses incremental pieces of
// scratchbuf_128k for a buffer to store the results, and
// will overwrite itself after some number of calls when the memory
// is all used, but note that other things use scratchbuf_128k too,
// so this is only good for printf() things:
// fmtfunc("mission took %0 seconds, %1 points", numstr(secs), numstr(pts));
const char *numstr(int val);
Dec 2, 2006
Dec 2, 2006
130
131
132
133
134
135
136
137
138
// Call this for fatal errors that require immediate app termination.
// Does not clean up, or translate the error string. Try to avoid this.
// These are for crucial lowlevel issues that preclude any meaningful
// recovery (GUI didn't initialize, etc)
// Doesn't return, but if it did, you can assume it returns zero, so you can
// do: 'return panic("out of memory");' or whatnot.
int panic(const char *err);
// Call this for a fatal problem that attempts an orderly shutdown (system
Dec 20, 2006
Dec 20, 2006
139
140
141
142
143
144
145
// is fully working, but config file is hosed, etc).
// This makes an attempt to clean up any files already created by the
// current installer (successfully run Setup.Install blocks in the config
// file are not cleaned up).
// If there's no GUI or Lua isn't initialized, this calls panic(). That's bad.
// Doesn't return, but if it did, you can assume it returns zero, so you can
// do: 'return fatal("missing config file");' or whatnot.
Jan 12, 2008
Jan 12, 2008
146
147
148
// THIS DOES NOT USE PRINTF-STYLE FORMAT CODES. Please see the comments for
// format() for details.
int fatal(const char *fmt, ...);
Dec 2, 2006
Dec 2, 2006
149
May 7, 2007
May 7, 2007
150
151
152
// The platform layer should set up signal/exception handlers before calling
// MojoSetup_main(), that will call these functions. "crashed" for bug
// signals (SIGSEGV, GPF, etc), and "terminated" for external forces that
Nov 11, 2010
Nov 11, 2010
153
// destroy the process (SIGTERM, SIGINT, etc). These functions do not return.
May 7, 2007
May 7, 2007
154
155
156
void MojoSetup_crashed(void);
void MojoSetup_terminated(void);
Mar 25, 2006
Mar 25, 2006
157
// Malloc replacements that blow up on allocation failure.
Jan 11, 2008
Jan 11, 2008
158
159
// Please note that xmalloc() will zero the newly-allocated memory buffer,
// like calloc() would, but xrealloc() makes no such promise!
Mar 25, 2006
Mar 25, 2006
160
161
void *xmalloc(size_t bytes);
void *xrealloc(void *ptr, size_t bytes);
Mar 25, 2006
Mar 25, 2006
162
char *xstrdup(const char *str);
Mar 25, 2006
Mar 25, 2006
163
Dec 2, 2006
Dec 2, 2006
164
165
166
// strncpy() that promises to null-terminate the string, even on overflow.
char *xstrncpy(char *dst, const char *src, size_t len);
Jun 18, 2007
Jun 18, 2007
167
168
169
170
#ifdef malloc
#undef malloc
#endif
Mar 25, 2006
Mar 25, 2006
171
#define malloc(x) DO_NOT_CALL_MALLOC__USE_XMALLOC_INSTEAD
Jun 18, 2007
Jun 18, 2007
172
173
174
175
#ifdef calloc
#undef calloc
#endif
Mar 25, 2006
Mar 25, 2006
176
#define calloc(x,y) DO_NOT_CALL_CALLOC__USE_XMALLOC_INSTEAD
Jun 18, 2007
Jun 18, 2007
177
178
179
180
#ifdef realloc
#undef realloc
#endif
Mar 25, 2006
Mar 25, 2006
181
#define realloc(x,y) DO_NOT_CALL_REALLOC__USE_XREALLOC_INSTEAD
Jun 18, 2007
Jun 18, 2007
182
183
184
185
#ifdef strdup
#undef strdup
#endif
Mar 25, 2006
Mar 25, 2006
186
#define strdup(x) DO_NOT_CALL_STRDUP__USE_XSTRDUP_INSTEAD
Jun 18, 2007
Jun 18, 2007
187
188
189
190
#ifdef strncpy
#undef strncpy
#endif
Dec 20, 2006
Dec 20, 2006
191
#define strncpy(x,y,z) DO_NOT_CALL_STRNCPY__USE_XSTRNCPY_INSTEAD
Jun 18, 2007
Jun 18, 2007
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#if 0 // !!! FIXME: write me.
#ifdef strcasecmp
#undef strcasecmp
#endif
#define strcasecmp(x,y) DO_NOT_CALL_STRCASECMP__USE_UTF8STRICMP_INSTEAD
#endif
#if 0 // !!! FIXME: write me.
#ifdef stricmp
#undef stricmp
#endif
#define stricmp(x,y) DO_NOT_CALL_STRICMP__USE_UTF8STRICMP_INSTEAD
#endif
#if 0 // !!! FIXME: write me.
#ifdef strcmpi
#undef strcmpi
#endif
#define strcmpi(x,y) DO_NOT_CALL_STRCMPI__USE_UTF8STRICMP_INSTEAD
#endif
Dec 2, 2006
Dec 2, 2006
213
214
215
216
217
// Localization support.
const char *translate(const char *str);
#ifdef _
#undef _
Mar 25, 2006
Mar 25, 2006
218
#endif
Dec 2, 2006
Dec 2, 2006
219
#define _(x) translate(x)
Mar 25, 2006
Mar 25, 2006
220
Dec 6, 2006
Dec 6, 2006
221
222
223
224
225
226
// Call this with what you are profiling and the start time to log it:
// uint32 start = MojoPlatform_ticks();
// ...do something...
// profile("Something I did", start);
uint32 profile(const char *what, uint32 start_time);
Nov 24, 2007
Nov 24, 2007
227
228
229
230
231
232
233
234
235
// This tries to decode a graphic file in memory into an RGBA framebuffer,
// first with platform-specific facilities, if any, and then any built-in
// decoders, if that fails.
// (data) points to the compressed data, (size) is the number of bytes
// of compressed data. (*w) and (*h) will contain the images dimensions on
// return.
// Returns NULL on failure (unsupported, etc) and a pointer to the
// uncompressed data on success. Caller must free() the returned pointer!
uint8 *decodeImage(const uint8 *data, uint32 size, uint32 *w, uint32 *h);
Dec 10, 2006
Dec 10, 2006
236
Apr 25, 2009
Apr 25, 2009
237
238
239
240
// See if a given string is in a valid product key format.
// (fmt) points to a string that would be a Setup.ProductKey.format.
// (key) is the key as it currently stands. It might be partially entered.
// Returns true if the key is valid for the format, false otherwise.
Apr 25, 2009
Apr 25, 2009
241
boolean isValidProductKey(const char *fmt, const char *key);
Apr 25, 2009
Apr 25, 2009
242
Dec 10, 2006
Dec 10, 2006
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// See if a given flag was on the command line
//
// cmdline("nosound") will return true if "-nosound", "--nosound",
// etc was on the command line. The option must start with a '-' on the
// command line to be noticed by this function.
//
// \param arg the command line flag to find
// \return true if arg was on the command line, false otherwise.
boolean cmdline(const char *arg);
// Get robust command line options, with optional default for missing ones.
//
// If the command line was ./myapp --a=b -c=d ----e f
// - cmdlinestr("a") will return "b"
// - cmdlinestr("c") will return "d"
// - cmdlinestr("e") will return "f"
// - cmdlinestr("g") will return the default string.
//
// Like cmdline(), the option must start with a '-'.
//
// \param arg The command line option to find.
// \param envr optional environment var to check if command line wasn't found.
// \param deflt The return value if (arg) isn't on the command line.
// \return The command line option, or (deflt) if the command line wasn't
// found. This return value is READ ONLY. Do not free it, either.
const char *cmdlinestr(const char *arg, const char *envr, const char *deflt);
Apr 28, 2007
Apr 28, 2007
271
272
273
// Returns true if (str) matches (pattern), false otherwise.
// This is NOT a regexp! It only understands '?' and '*', similar to how
// wildcards worked in MS-DOS command lines. '?' matches one char, and
May 1, 2007
May 1, 2007
274
275
// '*' matches until the end of string or the next char in the pattern
// is seen. Case matters!
Apr 28, 2007
Apr 28, 2007
276
boolean wildcardMatch(const char *str, const char *pattern);
Dec 10, 2006
Dec 10, 2006
277
278
279
280
281
282
283
284
285
286
287
288
// Logging functions.
typedef enum
{
MOJOSETUP_LOG_NOTHING,
MOJOSETUP_LOG_ERRORS,
MOJOSETUP_LOG_WARNINGS,
MOJOSETUP_LOG_INFO,
MOJOSETUP_LOG_DEBUG,
MOJOSETUP_LOG_EVERYTHING
} MojoSetupLogLevel;
May 1, 2007
May 1, 2007
289
extern MojoSetupLogLevel MojoLog_logLevel;
Dec 10, 2006
Dec 10, 2006
290
291
void MojoLog_initLogging(void);
void MojoLog_deinitLogging(void);
Jan 12, 2008
Jan 12, 2008
292
293
294
295
296
297
298
299
// Logging facilities.
// THESE DO NOT USE PRINTF-STYLE FORMAT CODES. Please see the comments for
// format() for details.
void logWarning(const char *fmt, ...);
void logError(const char *fmt, ...);
void logInfo(const char *fmt, ...);
void logDebug(const char *fmt, ...);
Dec 10, 2006
Dec 10, 2006
300
May 27, 2007
May 27, 2007
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Checksums.
typedef uint32 MojoCrc32;
void MojoCrc32_init(MojoCrc32 *context);
void MojoCrc32_append(MojoCrc32 *context, const uint8 *buf, uint32 len);
void MojoCrc32_finish(MojoCrc32 *context, uint32 *digest);
typedef struct MojoMd5
{
uint32 count[2];
uint32 abcd[4];
uint8 buf[64];
} MojoMd5;
void MojoMd5_init(MojoMd5 *pms);
void MojoMd5_append(MojoMd5 *pms, const uint8 *data, int nbytes);
void MojoMd5_finish(MojoMd5 *pms, uint8 digest[16]);
typedef struct
{
uint32 state[5];
uint32 count[2];
uint8 buffer[64];
} MojoSha1;
void MojoSha1_init(MojoSha1 *context);
void MojoSha1_append(MojoSha1 *context, const uint8 *data, uint32 len);
void MojoSha1_finish(MojoSha1 *context, uint8 digest[20]);
typedef struct MojoChecksumContext
{
MojoCrc32 crc32;
MojoMd5 md5;
MojoSha1 sha1;
} MojoChecksumContext;
typedef struct MojoChecksums
{
uint32 crc32;
uint8 md5[16];
uint8 sha1[20];
} MojoChecksums;
void MojoChecksum_init(MojoChecksumContext *ctx);
void MojoChecksum_append(MojoChecksumContext *c, const uint8 *data, uint32 ln);
void MojoChecksum_finish(MojoChecksumContext *c, MojoChecksums *sums);
Dec 7, 2006
Dec 7, 2006
354
355
356
357
358
359
360
361
362
// A pointer to this struct is passed to plugins, so they can access
// functionality in the base application. (Add to this as you need to.)
typedef struct MojoSetupEntryPoints
{
void *(*xmalloc)(size_t bytes);
void *(*xrealloc)(void *ptr, size_t bytes);
char *(*xstrdup)(const char *str);
char *(*xstrncpy)(char *dst, const char *src, size_t len);
const char *(*translate)(const char *str);
Jan 12, 2008
Jan 12, 2008
363
364
365
366
367
368
void (*logWarning)(const char *fmt, ...);
void (*logError)(const char *fmt, ...);
void (*logInfo)(const char *fmt, ...);
void (*logDebug)(const char *fmt, ...);
char *(*format)(const char *fmt, ...);
const char *(*numstr)(int val);
May 7, 2007
May 7, 2007
369
uint32 (*ticks)(void);
Mar 2, 2008
Mar 2, 2008
370
uint32 (*utf8codepoint)(const char **_str);
Mar 3, 2008
Mar 3, 2008
371
372
int (*utf8len)(const char *str);
char **(*splitText)(const char *text, int scrw, int *_count, int *_w);
Apr 25, 2009
Apr 25, 2009
373
boolean (*isValidProductKey)(const char *f, const char *k);
Dec 7, 2006
Dec 7, 2006
374
375
376
377
} MojoSetupEntryPoints;
extern MojoSetupEntryPoints GEntryPoints;
Mar 25, 2006
Mar 25, 2006
378
#ifndef DOXYGEN_SHOULD_IGNORE_THIS
Dec 13, 2006
Dec 13, 2006
379
#if ((defined _MSC_VER) || (PLATFORM_BEOS))
Mar 25, 2006
Mar 25, 2006
380
#define __EXPORT__ __declspec(dllexport)
Apr 17, 2009
Apr 17, 2009
381
382
#elif (defined SUNPRO_C)
#define __EXPORT__ __global
Mar 25, 2006
Mar 25, 2006
383
384
385
386
387
#elif (__GNUC__ >= 3)
#define __EXPORT__ __attribute__((visibility("default")))
#else
#define __EXPORT__
#endif
Mar 25, 2006
Mar 25, 2006
388
#endif // DOXYGEN_SHOULD_IGNORE_THIS
Mar 25, 2006
Mar 25, 2006
389
Jan 12, 2008
Jan 12, 2008
390
391
392
#define DEFINE_TO_STR2(x) #x
#define DEFINE_TO_STR(x) DEFINE_TO_STR2(x)
Sep 20, 2007
Sep 20, 2007
393
#ifndef DOXYGEN_SHOULD_IGNORE_THIS
Feb 2, 2008
Feb 2, 2008
394
#define STUBBED2(prelog, x) \
Sep 20, 2007
Sep 20, 2007
395
do { \
Mar 25, 2006
Mar 25, 2006
396
397
398
399
static boolean seen_this = false; \
if (!seen_this) \
{ \
seen_this = true; \
Feb 2, 2008
Feb 2, 2008
400
401
prelog logDebug("STUBBED: %0 at %1 (%2:%3)\n", x, __FUNCTION__, \
__FILE__, DEFINE_TO_STR(__LINE__)); \
Mar 25, 2006
Mar 25, 2006
402
} \
Sep 20, 2007
Sep 20, 2007
403
} while (false)
Feb 2, 2008
Feb 2, 2008
404
#define STUBBED(x) STUBBED2(,x)
Mar 25, 2006
Mar 25, 2006
405
406
#endif
Mar 26, 2006
Mar 26, 2006
407
408
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
Mar 24, 2006
Mar 24, 2006
409
410
411
412
#ifdef __cplusplus
}
#endif
Mar 25, 2006
Mar 25, 2006
415
// end of universal.h ...