Mercurial > sdl-ios-xcode
annotate src/SDL_error.c @ 1548:a77c022733fe
EXTRA_LDFLAGS are the linker flags and libraries needed to build SDL.
SDL_LIBS are the linker flags and libraries needed to build SDL applications.
SDL_STATIC_LIBS is set to SDL_LIBS by default.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 18 Mar 2006 18:41:59 +0000 |
parents | d910939febfa |
children | 97d0966f4bf7 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1172
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
1
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1379
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* Simple error handling in SDL */ | |
25 | |
26 #include "SDL_error.h" | |
27 #include "SDL_error_c.h" | |
28 | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
29 /* Routine to get the thread-specific error variable */ |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
30 #if SDL_THREADS_DISABLED |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
31 /* The SDL_arraysize(The ),default (non-thread-safe) global error variable */ |
0 | 32 static SDL_error SDL_global_error; |
33 #define SDL_GetErrBuf() (&SDL_global_error) | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
34 #else |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
35 extern SDL_error *SDL_GetErrBuf(void); |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
36 #endif /* SDL_THREADS_DISABLED */ |
0 | 37 |
38 #define SDL_ERRBUFIZE 1024 | |
39 | |
40 /* Private functions */ | |
41 | |
42 static void SDL_LookupString(const Uint8 *key, Uint16 *buf, int buflen) | |
43 { | |
44 /* FIXME: Add code to lookup key in language string hash-table */ | |
45 | |
46 /* Key not found in language string hash-table */ | |
47 while ( *key && (--buflen > 0) ) { | |
48 *buf++ = *key++; | |
49 } | |
50 *buf = 0; /* NULL terminate string */ | |
51 } | |
52 | |
53 /* Public functions */ | |
54 | |
55 void SDL_SetError (const char *fmt, ...) | |
56 { | |
57 va_list ap; | |
58 SDL_error *error; | |
59 | |
60 /* Copy in the key, mark error as valid */ | |
61 error = SDL_GetErrBuf(); | |
62 error->error = 1; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
63 SDL_strlcpy((char *)error->key, fmt, sizeof(error->key)); |
0 | 64 |
65 va_start(ap, fmt); | |
66 error->argc = 0; | |
67 while ( *fmt ) { | |
68 if ( *fmt++ == '%' ) { | |
69 switch (*fmt++) { | |
70 case 0: /* Malformed format string.. */ | |
71 --fmt; | |
72 break; | |
73 #if 0 /* What is a character anyway? (UNICODE issues) */ | |
74 case 'c': | |
75 error->args[error->argc++].value_c = | |
76 va_arg(ap, unsigned char); | |
77 break; | |
78 #endif | |
79 case 'd': | |
80 error->args[error->argc++].value_i = | |
81 va_arg(ap, int); | |
82 break; | |
83 case 'f': | |
84 error->args[error->argc++].value_f = | |
85 va_arg(ap, double); | |
86 break; | |
87 case 'p': | |
88 error->args[error->argc++].value_ptr = | |
89 va_arg(ap, void *); | |
90 break; | |
91 case 's': | |
92 { | |
93 int index = error->argc; | |
1172
f69f4d25fb20
Don't crash if a NULL is passed for a "%s" parameter to SDL_SetError(),
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
94 char *str = va_arg(ap, char *); |
f69f4d25fb20
Don't crash if a NULL is passed for a "%s" parameter to SDL_SetError(),
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
95 if (str == NULL) |
f69f4d25fb20
Don't crash if a NULL is passed for a "%s" parameter to SDL_SetError(),
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
96 str = "(null)"; |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
97 SDL_strlcpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN); |
0 | 98 error->argc++; |
99 } | |
100 break; | |
101 default: | |
102 break; | |
103 } | |
104 if ( error->argc >= ERR_MAX_ARGS ) { | |
105 break; | |
106 } | |
107 } | |
108 } | |
109 va_end(ap); | |
110 | |
111 /* If we are in debug mode, print out an error message */ | |
112 #ifdef DEBUG_ERROR | |
113 fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError()); | |
114 #endif | |
115 } | |
116 | |
117 /* Print out an integer value to a UNICODE buffer */ | |
118 static int PrintInt(Uint16 *str, unsigned int maxlen, int value) | |
119 { | |
120 char tmp[128]; | |
121 int len, i; | |
122 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
123 SDL_snprintf(tmp, SDL_arraysize(tmp), "%d", value); |
0 | 124 len = 0; |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
125 if ( SDL_strlen(tmp) < maxlen ) { |
0 | 126 for ( i=0; tmp[i]; ++i ) { |
127 *str++ = tmp[i]; | |
128 ++len; | |
129 } | |
130 } | |
131 return(len); | |
132 } | |
133 /* Print out a double value to a UNICODE buffer */ | |
134 static int PrintDouble(Uint16 *str, unsigned int maxlen, double value) | |
135 { | |
136 char tmp[128]; | |
137 int len, i; | |
138 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
139 SDL_snprintf(tmp, SDL_arraysize(tmp), "%f", value); |
0 | 140 len = 0; |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
141 if ( SDL_strlen(tmp) < maxlen ) { |
0 | 142 for ( i=0; tmp[i]; ++i ) { |
143 *str++ = tmp[i]; | |
144 ++len; | |
145 } | |
146 } | |
147 return(len); | |
148 } | |
149 /* Print out a pointer value to a UNICODE buffer */ | |
150 static int PrintPointer(Uint16 *str, unsigned int maxlen, void *value) | |
151 { | |
152 char tmp[128]; | |
153 int len, i; | |
154 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
155 SDL_snprintf(tmp, SDL_arraysize(tmp), "%p", value); |
0 | 156 len = 0; |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
157 if ( SDL_strlen(tmp) < maxlen ) { |
0 | 158 for ( i=0; tmp[i]; ++i ) { |
159 *str++ = tmp[i]; | |
160 ++len; | |
161 } | |
162 } | |
163 return(len); | |
164 } | |
165 | |
166 /* This function has a bit more overhead than most error functions | |
167 so that it supports internationalization and thread-safe errors. | |
168 */ | |
169 Uint16 *SDL_GetErrorMsgUNICODE(Uint16 *errstr, unsigned int maxlen) | |
170 { | |
171 SDL_error *error; | |
172 | |
173 /* Clear the error string */ | |
174 *errstr = 0; --maxlen; | |
175 | |
176 /* Get the thread-safe error, and print it out */ | |
177 error = SDL_GetErrBuf(); | |
178 if ( error->error ) { | |
179 Uint16 translated[ERR_MAX_STRLEN], *fmt, *msg; | |
180 int len; | |
181 int argi; | |
182 | |
183 /* Print out the UNICODE error message */ | |
184 SDL_LookupString(error->key, translated, sizeof(translated)); | |
185 msg = errstr; | |
186 argi = 0; | |
187 for ( fmt=translated; *fmt && (maxlen > 0); ) { | |
188 if ( *fmt == '%' ) { | |
189 switch (fmt[1]) { | |
190 case 'S': /* Special SKIP operand */ | |
191 argi += (fmt[2] - '0'); | |
192 ++fmt; | |
193 break; | |
194 case '%': | |
195 *msg++ = '%'; | |
196 maxlen -= 1; | |
197 break; | |
198 #if 0 /* What is a character anyway? (UNICODE issues) */ | |
199 case 'c': | |
200 *msg++ = (unsigned char) | |
201 error->args[argi++].value_c; | |
202 maxlen -= 1; | |
203 break; | |
204 #endif | |
205 case 'd': | |
206 len = PrintInt(msg, maxlen, | |
207 error->args[argi++].value_i); | |
208 msg += len; | |
209 maxlen -= len; | |
210 break; | |
211 case 'f': | |
212 len = PrintDouble(msg, maxlen, | |
213 error->args[argi++].value_f); | |
214 msg += len; | |
215 maxlen -= len; | |
216 break; | |
217 case 'p': | |
218 len = PrintPointer(msg, maxlen, | |
219 error->args[argi++].value_ptr); | |
220 msg += len; | |
221 maxlen -= len; | |
222 break; | |
223 case 's': /* UNICODE string */ | |
224 { Uint16 buf[ERR_MAX_STRLEN], *str; | |
225 SDL_LookupString(error->args[argi++].buf, buf, sizeof(buf)); | |
226 str = buf; | |
227 while ( *str && (maxlen > 0) ) { | |
228 *msg++ = *str++; | |
229 maxlen -= 1; | |
230 } | |
231 } | |
232 break; | |
233 } | |
234 fmt += 2; | |
235 } else { | |
236 *msg++ = *fmt++; | |
237 maxlen -= 1; | |
238 } | |
239 } | |
240 *msg = 0; /* NULL terminate the string */ | |
241 } | |
242 return(errstr); | |
243 } | |
244 | |
245 Uint8 *SDL_GetErrorMsg(Uint8 *errstr, unsigned int maxlen) | |
246 { | |
247 Uint16 *errstr16; | |
248 unsigned int i; | |
249 | |
250 /* Allocate the UNICODE buffer */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
251 errstr16 = (Uint16 *)SDL_malloc(maxlen * (sizeof *errstr16)); |
0 | 252 if ( ! errstr16 ) { |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
253 SDL_strlcpy((char *)errstr, "Out of memory", maxlen); |
0 | 254 return(errstr); |
255 } | |
256 | |
257 /* Get the error message */ | |
258 SDL_GetErrorMsgUNICODE(errstr16, maxlen); | |
259 | |
260 /* Convert from UNICODE to Latin1 encoding */ | |
261 for ( i=0; i<maxlen; ++i ) { | |
262 errstr[i] = (Uint8)errstr16[i]; | |
263 } | |
264 | |
265 /* Free UNICODE buffer (if necessary) */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
266 SDL_free(errstr16); |
0 | 267 |
268 return(errstr); | |
269 } | |
270 | |
271 /* Available for backwards compatibility */ | |
272 char *SDL_GetError (void) | |
273 { | |
274 static char errmsg[SDL_ERRBUFIZE]; | |
275 | |
276 return((char *)SDL_GetErrorMsg((unsigned char *)errmsg, SDL_ERRBUFIZE)); | |
277 } | |
278 | |
279 void SDL_ClearError(void) | |
280 { | |
281 SDL_error *error; | |
282 | |
283 error = SDL_GetErrBuf(); | |
284 error->error = 0; | |
285 } | |
286 | |
287 /* Very common errors go here */ | |
288 void SDL_Error(SDL_errorcode code) | |
289 { | |
290 switch (code) { | |
291 case SDL_ENOMEM: | |
292 SDL_SetError("Out of memory"); | |
293 break; | |
294 case SDL_EFREAD: | |
295 SDL_SetError("Error reading from datastream"); | |
296 break; | |
297 case SDL_EFWRITE: | |
298 SDL_SetError("Error writing to datastream"); | |
299 break; | |
300 case SDL_EFSEEK: | |
301 SDL_SetError("Error seeking in datastream"); | |
302 break; | |
303 default: | |
304 SDL_SetError("Unknown SDL error"); | |
305 break; | |
306 } | |
307 } | |
308 | |
309 #ifdef TEST_ERROR | |
310 int main(int argc, char *argv[]) | |
311 { | |
312 char buffer[BUFSIZ+1]; | |
313 | |
314 SDL_SetError("Hi there!"); | |
315 printf("Error 1: %s\n", SDL_GetError()); | |
316 SDL_ClearError(); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
317 SDL_memset(buffer, '1', BUFSIZ); |
0 | 318 buffer[BUFSIZ] = 0; |
319 SDL_SetError("This is the error: %s (%f)", buffer, 1.0); | |
320 printf("Error 2: %s\n", SDL_GetError()); | |
321 exit(0); | |
322 } | |
323 #endif |