author | Medo <smaxein@googlemail.com> |
Wed, 27 Jun 2012 18:02:45 +0200 | |
changeset 7275 | 15f722e0b96f |
parent 7269 | 5b0aeef8ba2a |
child 7314 | 6171f0bad318 |
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" |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
2 |
#include "logging.h" |
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
#include <stddef.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
#include <stdarg.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
#include <stdlib.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
#include <string.h> |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
#include <stdio.h> |
7227
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
9 |
#include <ctype.h> |
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
char *flib_asprintf(const char *fmt, ...) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
va_list argp; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
va_start(argp, fmt); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
char *result = flib_vasprintf(fmt, argp); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
va_end(argp); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
return result; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
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
|
20 |
char *result = NULL; |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
21 |
if(!fmt) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
22 |
flib_log_e("null parameter in flib_vasprintf"); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
23 |
} else { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
24 |
int requiredSize = vsnprintf(NULL, 0, fmt, args)+1; // Figure out how much memory we need, |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
25 |
if(requiredSize<0) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
26 |
flib_log_e("Error formatting string with template \"%s\" in flib_vasprintf", fmt); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
27 |
} else { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
28 |
char *tmpbuf = flib_malloc(requiredSize); // allocate it |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
29 |
if(tmpbuf && vsnprintf(tmpbuf, requiredSize, fmt, args)>=0) { // and then do the actual formatting. |
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
result = tmpbuf; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
tmpbuf = NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
} |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
33 |
free(tmpbuf); |
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
return result; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
|
7269
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
39 |
char *flib_join(char **parts, int partCount, const char *delimiter) { |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
40 |
size_t totalSize = 1; |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
41 |
size_t delimLen = strlen(delimiter); |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
42 |
for(int i=0; i<partCount; i++) { |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
43 |
totalSize += strlen(parts[i]) + delimLen; |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
44 |
} |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
45 |
|
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
46 |
char *result = flib_malloc(totalSize); |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
47 |
if(result) { |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
48 |
size_t outpos = 0; |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
49 |
for(int i=0; i<partCount; i++) { |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
50 |
if(i>0) { |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
51 |
strcpy(result+outpos, delimiter); |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
52 |
outpos += delimLen; |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
53 |
} |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
54 |
strcpy(result+outpos, parts[i]); |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
55 |
outpos += strlen(parts[i]); |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
56 |
} |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
57 |
} |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
58 |
return result; |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
59 |
} |
5b0aeef8ba2a
More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
7230
diff
changeset
|
60 |
|
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
61 |
char *flib_strdupnull(const char *str) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
62 |
if(!str) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
63 |
return NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
64 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
65 |
return flib_asprintf("%s", str); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
66 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
67 |
|
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
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
|
69 |
if(!buf || size==0) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
70 |
return NULL; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
71 |
} |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
72 |
void *result = flib_malloc(size); |
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
73 |
if(result) { |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
74 |
memcpy(result, buf, size); |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
75 |
} |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
76 |
return result; |
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
77 |
} |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
78 |
|
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
79 |
void *flib_malloc(size_t size) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
80 |
void *result = malloc(size); |
7230
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
81 |
if(!result && size>0) { |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
82 |
flib_log_e("Out of memory trying to malloc %zu bytes.", size); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
83 |
} |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
84 |
return result; |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
85 |
} |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
86 |
|
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
87 |
void *flib_calloc(size_t count, size_t elementsize) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
88 |
void *result = calloc(count, elementsize); |
7230
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
89 |
if(!result && count>0 && elementsize>0) { |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
90 |
flib_log_e("Out of memory trying to calloc %zu objects of %zu bytes each.", count, elementsize); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
91 |
} |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
92 |
return result; |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
93 |
} |
7227
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
94 |
|
7230
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
95 |
void *flib_realloc(void *ptr, size_t size) { |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
96 |
void *result = realloc(ptr, size); |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
97 |
if(!result && size>0) { |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
98 |
flib_log_e("Out of memory trying to realloc %zu bytes.", size); |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
99 |
} |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
100 |
return result; |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
101 |
} |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
102 |
|
7227
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
103 |
static bool isAsciiAlnum(char c) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
104 |
return (c>='0' && c<='9') || (c>='a' && c <='z') || (c>='A' && c <='Z'); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
105 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
106 |
|
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
107 |
char *flib_urlencode(const char *inbuf) { |
7230
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
108 |
return flib_urlencode_pred(inbuf, isAsciiAlnum); |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
109 |
} |
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
110 |
|
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
111 |
char *flib_urlencode_pred(const char *inbuf, bool (*needsEscaping)(char c)) { |
7227
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
112 |
if(!inbuf) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
113 |
return NULL; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
114 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
115 |
size_t insize = strlen(inbuf); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
116 |
if(insize > SIZE_MAX/4) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
117 |
flib_log_e("String too long in flib_urlencode: %zu bytes.", insize); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
118 |
return NULL; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
119 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
120 |
|
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
121 |
char *outbuf = flib_malloc(insize*3+1); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
122 |
if(!outbuf) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
123 |
return NULL; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
124 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
125 |
|
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
126 |
size_t inpos = 0, outpos = 0; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
127 |
while(inbuf[inpos]) { |
7230
240620f46dd7
Changed frontlib to use the existing ini file formats of the QtFrontend
Medo <smaxein@googlemail.com>
parents:
7227
diff
changeset
|
128 |
if(!needsEscaping(inbuf[inpos])) { |
7227
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
129 |
outbuf[outpos++] = inbuf[inpos++]; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
130 |
} else { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
131 |
if(snprintf(outbuf+outpos, 4, "%%%02X", (unsigned)((uint8_t*)inbuf)[inpos])<0) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
132 |
flib_log_e("printf error in flib_urlencode"); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
133 |
free(outbuf); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
134 |
return NULL; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
135 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
136 |
inpos++; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
137 |
outpos += 3; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
138 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
139 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
140 |
outbuf[outpos] = 0; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
141 |
char *shrunk = realloc(outbuf, outpos+1); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
142 |
return shrunk ? shrunk : outbuf; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
143 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
144 |
|
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
145 |
char *flib_urldecode(const char *inbuf) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
146 |
char *outbuf = flib_malloc(strlen(inbuf)+1); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
147 |
if(!outbuf) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
148 |
return NULL; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
149 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
150 |
|
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
151 |
size_t inpos = 0, outpos = 0; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
152 |
while(inbuf[inpos]) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
153 |
if(inbuf[inpos] == '%' && isxdigit(inbuf[inpos+1]) && isxdigit(inbuf[inpos+2])) { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
154 |
char temp[3] = {inbuf[inpos+1],inbuf[inpos+2],0}; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
155 |
outbuf[outpos++] = strtol(temp, NULL, 16); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
156 |
inpos += 3; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
157 |
} else { |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
158 |
outbuf[outpos++] = inbuf[inpos++]; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
159 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
160 |
} |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
161 |
outbuf[outpos] = 0; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
162 |
char *shrunk = realloc(outbuf, outpos+1); |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
163 |
return shrunk ? shrunk : outbuf; |
1c859f572d72
frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents:
7224
diff
changeset
|
164 |
} |
7275
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
165 |
|
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
166 |
bool flib_contains_dir_separator(const char *str) { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
167 |
if(!log_badparams_if(!str)) { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
168 |
for(;*str;str++) { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
169 |
if(*str=='\\' || *str=='/') { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
170 |
return true; |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
171 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
172 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
173 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
174 |
return false; |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
175 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
176 |
|
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
177 |
int flib_gets(char *str, size_t strlen) { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
178 |
if(fgets(str, strlen, stdin)) { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
179 |
for(char *s=str; *s; s++) { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
180 |
if(*s=='\r' || *s=='\n') { |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
181 |
*s = 0; |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
182 |
break; |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
183 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
184 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
185 |
return 0; |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
186 |
} |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
187 |
return -1; |
15f722e0b96f
frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
7269
diff
changeset
|
188 |
} |