author | Medo <smaxein@googlemail.com> |
Sat, 09 Jun 2012 03:28:38 +0200 | |
changeset 7179 | f84805e6df03 |
parent 7177 | project_files/frontlib/util.c@bf6cf4dd847a |
child 7224 | 5143861c83bd |
permissions | -rw-r--r-- |
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
#include "util.h" |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
#include <stddef.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
#include <stdarg.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
#include <string.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
#include <stdio.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
char *flib_asprintf(const char *fmt, ...) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
va_list argp; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
va_start(argp, fmt); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
char *result = flib_vasprintf(fmt, argp); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
va_end(argp); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
return result; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
char *flib_vasprintf(const char *fmt, va_list args) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
char *result = NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
int requiredSize = vsnprintf(NULL, 0, fmt, args)+1; // Figure out how much memory we need, |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
if(requiredSize>=0) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
char *tmpbuf = malloc(requiredSize); // allocate it |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
if(tmpbuf) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
if(vsnprintf(tmpbuf, requiredSize, fmt, args)>=0) { // and then do the actual formatting. |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
result = tmpbuf; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
tmpbuf = NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
free(tmpbuf); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
return result; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
char *flib_strdupnull(const char *str) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
if(!str) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
return NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
return flib_asprintf("%s", str); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
void *flib_bufdupnull(const void *buf, size_t size) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
if(!buf || size==0) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
return NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
void *result = malloc(size); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
if(result) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
memcpy(result, buf, size); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
return result; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
49 |
} |