forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
362 lines (274 loc) · 6.16 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
354
355
356
357
358
359
360
361
362
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
// Copyright (c) 2011-2017, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef UTIL_H
#define UTIL_H
#include "common.h"
#include "palcommon.h"
PAL_C_LINKAGE_BEGIN
void
UTIL_MsgBox(
char *string
);
long
flength(
FILE *fp
);
void
trim(
char *str
);
char *
UTIL_GlobalBuffer(
int index
);
#define PAL_BUFFER_SIZE_ARGS(i) UTIL_GlobalBuffer(i), PAL_GLOBAL_BUFFER_SIZE
/*++
Purpose:
Does a varargs printf into the user-supplied buffer,
so we don't need to have varargs versions of all text functions.
Parameters:
buffer - user-supplied buffer.
buflen - size of the buffer, including null-terminator.
format - the format string.
Return value:
The value of buffer if buffer is non-NULL and buflen > 0, otherwise NULL.
--*/
char *
UTIL_va(
char *buffer,
int buflen,
const char *format,
...
);
#define PAL_va(i, fmt, ...) UTIL_va(UTIL_GlobalBuffer(i), PAL_GLOBAL_BUFFER_SIZE, fmt, __VA_ARGS__)
int
RandomLong(
int from,
int to
);
float
RandomFloat(
float from,
float to
);
void
UTIL_Delay(
unsigned int ms
);
void
TerminateOnError(
const char *fmt,
...
);
void *
UTIL_malloc(
size_t buffer_size
);
void *
UTIL_calloc(
size_t n,
size_t size
);
FILE *
UTIL_OpenRequiredFile(
LPCSTR lpszFileName
);
FILE *
UTIL_OpenRequiredFileForMode(
LPCSTR lpszFileName,
LPCSTR szMode
);
FILE *
UTIL_OpenFile(
LPCSTR lpszFileName
);
FILE *
UTIL_OpenFileForMode(
LPCSTR lpszFileName,
LPCSTR szMode
);
FILE *
UTIL_OpenFileAtPath(
LPCSTR lpszPath,
LPCSTR lpszFileName
);
/*++
Purpose:
Open a file in desired mode at the specific path.
If fails, return NULL.
Parameters:
[IN] lpszPath - path to locate the file.
[IN] lpszFileName - file name to open.
[IN] szMode - file open mode.
Return value:
Pointer to the file.
--*/
FILE *
UTIL_OpenFileAtPathForMode(
LPCSTR lpszPath,
LPCSTR lpszFileName,
LPCSTR szMode
);
VOID
UTIL_CloseFile(
FILE *fp
);
/*++
Purpose:
Combine the 'dir' and 'file' part into a single path string.
If 'dir' is non-NULL, then it ensures that the output string contains
'/' between 'dir' and 'file' (no matter whether 'file' is NULL or not).
Parameters:
buffer - user-supplied buffer.
buflen - size of the buffer, including null-terminator.
dir - the directory path.
file - the file path.
Return value:
The value of buffer if buffer is non-NULL and buflen > 0, otherwise NULL.
--*/
const char *
UTIL_CombinePath(
char *buffer,
size_t buflen,
int numentry,
...
);
#define PAL_CombinePath(i, d, f) UTIL_CombinePath(UTIL_GlobalBuffer(i), PAL_GLOBAL_BUFFER_SIZE, 2, (d), (f))
const char *
UTIL_GetFullPathName(
char *buffer,
size_t buflen,
const char *basepath,
const char *subpath
);
PALFILE
UTIL_CheckResourceFiles(
const char *path,
const char *msgfile
);
/*
* Platform-specific utilities
*/
BOOL
UTIL_GetScreenSize(
DWORD *pdwScreenWidth,
DWORD *pdwScreenHeight
);
BOOL
UTIL_IsAbsolutePath(
const char *lpszFileName
);
int
UTIL_Platform_Startup(
int argc,
char *argv[]
);
int
UTIL_Platform_Init(
int argc,
char *argv[]
);
void
UTIL_Platform_Quit(
void
);
/*
* Logging utilities
*/
/*++
Purpose:
The pointer to callback function that produces actual log output.
Parameters:
[IN] level - The log level of this output call.
[IN] full_log - The full log string produced by UTIL_LogOutput.
[IN] user_log - The log string produced by user-provided format.
Return value:
None.
--*/
typedef void(*LOGCALLBACK)(LOGLEVEL level, const char *full_log, const char *user_log);
/*++
Purpose:
Adds a log output callback.
Parameters:
[IN] callback - The callback function to be added. Once added,
it will be called by UTIL_LogOutput.
[IN] loglevel - The minimal log level that the callback should
be called. Any log whose level below this will
be ignored by the callback.
Return value:
The slot id (>= 0), -1 if all slots are used or callback is NULL.
--*/
int
UTIL_LogAddOutputCallback(
LOGCALLBACK callback,
LOGLEVEL loglevel
);
/*++
Purpose:
Removes a log output callback.
Parameters:
[IN] id - The id of callback function to be removed.
Return value:
None
--*/
void
UTIL_LogRemoveOutputCallback(
int id
);
/*++
Purpose:
Set the minimal log level that could be output.
Any level below this level will produce no output.
Parameters:
[IN] minlevel - The minimal log level, must be within the
range [LOGLEVEL_MIN, LOGLEVEL_MAX].
Return value:
None.
--*/
void
UTIL_LogOutput(
LOGLEVEL level,
const char *fmt,
...
);
/*++
Purpose:
Set the minimal log level that could be output.
Any level below this level will produce no output.
Parameters:
[IN] minlevel - The minimal log level, must be within the
range [LOGLEVEL_MIN, LOGLEVEL_MAX].
Return value:
None.
--*/
void
UTIL_LogSetLevel(
LOGLEVEL minlevel
);
void
UTIL_LogToFile(
LOGLEVEL _,
const char *string,
const char *__
);
PAL_C_LINKAGE_END
#endif