forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asc.h
451 lines (409 loc) · 16.5 KB
/
asc.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*!
* \file
* \ingroup misc
* \brief Miscellaneous functions used for file handling and string utilities.
*/
#ifndef __ASC_H__
#define __ASC_H__
#include <SDL_types.h>
#include <libxml/tree.h>
#include "client_serv.h"
#include "font.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* Check if a character is a color character
*/
static __inline__ int is_color (Uint8 c)
{
return c >= 127 + c_lbound && c <= 127 + c_ubound;
}
/*!
* \brief Get the color number from a color character
*
* Get the color number from a character sent by the server. Only valid
* color characters should give a valid color number between \c c_lbound
* and \c c_ubound, but no checks are performed.
* \param c The character to get the color number from
* \retval int the color number
* \sa is_color(), to_color_char()
*/
static __inline__ int from_color_char (Uint8 c)
{
return c-127;
}
/*!
* \brief Convert a color number into a color character
*
* Compute the color character for the color with index \a color.
*
* \param color A valid color number between \c c_lbound and \c c_ubound
* \retval Uint8 The color character
* \sa from_color_char()
*/
static __inline__ Uint8 to_color_char (int color)
{
return (Uint8) (color+127);
}
/*!
* Check if a character is printable. In this context, that means
* printable ascii, or non-ascii if we know what symbol to use
*/
static __inline__ int is_printable (Uint8 c)
{
return get_font_char(c) >= 0;
}
/*!
* A macro for the my_xmlstrncopy function that copies and converts an xml-string. Sets the length to 0, hence it will copy untill \\0 is reached.
*/
#define MY_XMLSTRCPY(d,s) my_xmlStrncopy(d,s,0)
#ifndef FASTER_MAP_LOAD
/*!
* \ingroup misc_utils
* \brief Gets an integer after the given string
*
* The function finds \a needle in \a haystack and returns the integer value after the string given after it.
*
* \param needle The string you wish to find
* \param haystack The pointer to the char array you wish to find the string from
* \param max_len The maximum length it should check
* \retval Sint32 Returns the integer behind the string or -1 on failure.
*/
Sint32 get_integer_after_string (const char* needle, const char* haystack, Uint32 max_len);
/*!
* \ingroup misc_utils
* \brief Gets a float after the given string
*
* The function finds \a needle in \a haystack and returns the floating point value after it.
*
* \param needle The string you wish to find
* \param haystack The pointer to the char array you want to search for the string in.
* \param max_len The maximum length it should check
* \retval float Returns the float after the string or -1.0f on failure.
*/
float get_float_after_string (const char* needle, const char* haystack, Uint32 max_len);
#endif // FASTER_MAP_LOAD
/*!
* \ingroup misc_utils
* \brief Gets the offset of a string in a char array
*
* The function gets the location of source_pointer in the dest_pointer char array, then returns the offset. The functio is not case-sensitive.
*
* \param needle The string you wish to find
* \param haystack The char array you want to search for \a needle
* \param max_len The maximum length of \a haystack
* \param beginning Whether it should return the offset to the beginning of the string or the end of the string
* \retval Sint32 Returns either the offset to the beginning of the string or to the end of the string - if the string was not found in the char array it returns -1 on failure.
*/
Sint32 get_string_occurance (const char *needle, const char *haystack, const Uint32 max_len, const char beginning);
/*!
* \ingroup misc_utils
* \brief The function copies the string from source to dest, making sure it doesn't overflow and remains null terminated. Strncpy doesn't guarantee the null termination.
*
* \param dest The destination char array
* \param source The source char array
* \param len The sizeof the array.
*/
char* safe_strncpy(char *dest, const char * source, const size_t len);
/*!
* \ingroup misc_utils
* \brief The function copies the string from source to dest, making sure it doesn't overflow and remains null terminated, and furthermore that it doesn't copy more than a certain number of chars. Strncpy doesn't guarantee the null termination.
*
* \param dest The destination char array
* \param source The source char array
* \param dest_len The sizeof the destination array.
* \param src_len The desired number of characters from source.
*/
char* safe_strncpy2(char *dest, const char * source, const size_t dest_len, const size_t src_len);
/*!
* \ingroup misc_utils
* \brief Like snprintf, but guarentees nul termination.
*
* \param dest The destination char array
* \param len The sizeof the destination array.
* \param format A printf-style format string
* \param ... arguments to be passed to snprintf
*/
int safe_snprintf(char *dest, const size_t len, const char* format, ...);
/*!
* \ingroup misc_utils
* \brief Append string src to dest, guaranteeing null-termination
*
* Append string \a src to \a dest, making sure that the result
* is null-terminated and contains at most \a len characters
* (including the terminating nullbyte).
* %Note that the "safe" predicate only applies to the
* result, both \a dest and \a src should be null-terminated
* on entry. Also note that this function is \em not the same
* as \c strncat: the third parameter to \c strncat is the
* number of characters to take from \a dest, not the total
* number of characters in the result string.
*
* \param dest The string to append to
* \param src The string to be appended
* \param len The maximum size of the result string
* \retval char* Pointer to the concatenated string dest
*/
char* safe_strcat (char *dest, const char *src, size_t len);
/*!
* \ingroup misc_utils
* \brief Locate a substring in a case-insensitive matter
*
* Find the first occurence of string \a needle of length in
* \a haystack, checking at most the first \a needle_len bytes
* of \a needle and the first \a haystack_len bytes of
* \a haystack, and disregarding case. This function differs
* from (GNU's) memmem in that it is case-insensitive and
* does not compare bytes beyond a null-terminator.
*
* \param haystack The string to be searched
* \param haystack_len The length of \a haystack
* \param needle The string to search for
* \param needle_len The length of \a needle
* \retval char* Pointer to the first occurence of the search string, or
* NULL when \a haystack does not contain \a needle.
*/
char* safe_strcasestr (const char* haystack, size_t haystack_len, const char* needle, size_t needle_len);
/*!
* \ingroup misc_utils
* \brief The function copies the string from source to dest
*
* The function copies the string from source to destination, and put a terminating \\0
*
* \param dest The destination char array
* \param source The source char array
* \todo We should just use strcpy instead...
*/
void my_strcp(char *dest,const char * source);
/*!
* \ingroup misc_utils
* \brief The function copies the string from source to dest, but no more than n characters
*
* The function copies the string from source to destination, but no more than n characters. It also puts an ending \\0
*
* \param dest The destination char array
* \param source The source char array
* \param len The number of bytes you wish to copy
*/
void my_strncp (char *dest, const char *source, size_t len);
/*!
* \ingroup misc_utils
* \brief The function concencates the source string to the dest string
*
* The function concencates the source string to the dest string and sets a terminating \\0
*
* \param dest The destination string
* \param source The source string
* \todo Err, use strcat instead...
*/
void my_strcat(char *dest, const char * source);
/*!
* \ingroup misc_utils
* \brief Compares n bytes of the 2 strings (case insensitive)
*
* The function compares n bytes of the 2 strings. It is not case sensitive
*
* \param dest The first string
* \param src The second string
* \param len The number of bytes to compare
* \retval Sint32 Returns 1 on match, 0 if the strings doesn't match.
*/
Sint32 my_strncompare(const char *dest, const char *src, Sint32 len);
/*!
* \ingroup misc_utils
* \brief Compares the 2 strings
*
* The function compares the 2 strings, calls my_strncompare.
*
* \param dest The first string
* \param src The second string
* \retval Sint32 Returns 1 on match, 0 if the strings doesn't match.
* \sa my_strncompare
*/
Sint32 my_strcompare(const char *dest, const char *src);
/*!
* \ingroup misc_utils
* \brief Checks if len/2 characters of the string is uppercase
*
* Checks if len/2 characters of the string is uppercase
*
* \param src The string to be checked
* \param len The length of characters you wish to check
* \retval Sint32 Returns 1 if enough characters are uppercase, 0 if they are lowercase.
*/
Sint32 my_isupper(const char *src, int len);
/*!
* \ingroup misc_utils
* \brief Converts all characters in the string to lowercase
*
* Converts all characters in the string to lowercase
*
* \param src The string to convert
* \retval char* Returns the src-pointer.
*/
char *my_tolower (char *src);
/*!
* \ingroup misc_utils
* \brief Splits up the char array into multiple character arrays
*
* Splits up the char array into multiple character arrays. The new arrays will have chars_per_line+3 bytes allocated. The char ** array will have a NULL pointer as the end pointer.
*
* \param str The string to split
* \param chars_per_line The number of characters per line
* \retval char** Returns a char ** to the new array. You must free the memory yourself.
*/
char ** get_lines(char * str, int chars_per_line);
/*!
* \ingroup misc_utils
* \brief Goes through the file-name and replaces \\ with /
*
* Goes through the file-name and replaces \\ with /. Leaves the source intact, and copies the string to the destination.
*
* \param dest The destination string
* \param src The source string
* \param max_len The maximum length
* \retval Uint32 Returns the length of the string
*/
Uint32 clean_file_name (char *dest, const char *src, Uint32 max_len);
/*!
* \ingroup xml_utils
* \brief Finds the xml-attribute with the identifier p in the xmlNode and returns it as a floating point value
*
* Finds the xml-attribute with the identifier p in the xmlNode and returns it as a floating point value
*
* \param n The xml-node you wish to search
* \param p The attribute name you wish to search for
* \retval float The floating point value of the string. Returns 0 on failure.
*/
float xmlGetFloat(xmlNode * n, xmlChar * p);