Mercurial > sdl-ios-xcode
comparison src/SDL_error.c @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
30 #if SDL_THREADS_DISABLED | 30 #if SDL_THREADS_DISABLED |
31 /* The SDL_arraysize(The ),default (non-thread-safe) global error variable */ | 31 /* The SDL_arraysize(The ),default (non-thread-safe) global error variable */ |
32 static SDL_error SDL_global_error; | 32 static SDL_error SDL_global_error; |
33 #define SDL_GetErrBuf() (&SDL_global_error) | 33 #define SDL_GetErrBuf() (&SDL_global_error) |
34 #else | 34 #else |
35 extern SDL_error *SDL_GetErrBuf (void); | 35 extern SDL_error *SDL_GetErrBuf(void); |
36 #endif /* SDL_THREADS_DISABLED */ | 36 #endif /* SDL_THREADS_DISABLED */ |
37 | 37 |
38 #define SDL_ERRBUFIZE 1024 | 38 #define SDL_ERRBUFIZE 1024 |
39 | 39 |
40 /* Private functions */ | 40 /* Private functions */ |
41 | 41 |
42 static const char * | 42 static const char * |
43 SDL_LookupString (const char *key) | 43 SDL_LookupString(const char *key) |
44 { | 44 { |
45 /* FIXME: Add code to lookup key in language string hash-table */ | 45 /* FIXME: Add code to lookup key in language string hash-table */ |
46 return key; | 46 return key; |
47 } | 47 } |
48 | 48 |
49 /* Public functions */ | 49 /* Public functions */ |
50 | 50 |
51 void | 51 void |
52 SDL_SetError (const char *fmt, ...) | 52 SDL_SetError(const char *fmt, ...) |
53 { | 53 { |
54 va_list ap; | 54 va_list ap; |
55 SDL_error *error; | 55 SDL_error *error; |
56 | 56 |
57 /* Copy in the key, mark error as valid */ | 57 /* Copy in the key, mark error as valid */ |
58 error = SDL_GetErrBuf (); | 58 error = SDL_GetErrBuf(); |
59 error->error = 1; | 59 error->error = 1; |
60 SDL_strlcpy ((char *) error->key, fmt, sizeof (error->key)); | 60 SDL_strlcpy((char *) error->key, fmt, sizeof(error->key)); |
61 | 61 |
62 va_start (ap, fmt); | 62 va_start(ap, fmt); |
63 error->argc = 0; | 63 error->argc = 0; |
64 while (*fmt) { | 64 while (*fmt) { |
65 if (*fmt++ == '%') { | 65 if (*fmt++ == '%') { |
66 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) { | 66 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) { |
67 ++fmt; | 67 ++fmt; |
75 case 'd': | 75 case 'd': |
76 case 'u': | 76 case 'u': |
77 case 'o': | 77 case 'o': |
78 case 'x': | 78 case 'x': |
79 case 'X': | 79 case 'X': |
80 error->args[error->argc++].value_i = va_arg (ap, int); | 80 error->args[error->argc++].value_i = va_arg(ap, int); |
81 break; | 81 break; |
82 case 'f': | 82 case 'f': |
83 error->args[error->argc++].value_f = va_arg (ap, double); | 83 error->args[error->argc++].value_f = va_arg(ap, double); |
84 break; | 84 break; |
85 case 'p': | 85 case 'p': |
86 error->args[error->argc++].value_ptr = va_arg (ap, void *); | 86 error->args[error->argc++].value_ptr = va_arg(ap, void *); |
87 break; | 87 break; |
88 case 's': | 88 case 's': |
89 { | 89 { |
90 int i = error->argc; | 90 int i = error->argc; |
91 const char *str = va_arg (ap, const char *); | 91 const char *str = va_arg(ap, const char *); |
92 if (str == NULL) | 92 if (str == NULL) |
93 str = "(null)"; | 93 str = "(null)"; |
94 SDL_strlcpy ((char *) error->args[i].buf, str, | 94 SDL_strlcpy((char *) error->args[i].buf, str, |
95 ERR_MAX_STRLEN); | 95 ERR_MAX_STRLEN); |
96 error->argc++; | 96 error->argc++; |
97 } | 97 } |
98 break; | 98 break; |
99 default: | 99 default: |
100 break; | 100 break; |
102 if (error->argc >= ERR_MAX_ARGS) { | 102 if (error->argc >= ERR_MAX_ARGS) { |
103 break; | 103 break; |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 va_end (ap); | 107 va_end(ap); |
108 | 108 |
109 /* If we are in debug mode, print out an error message */ | 109 /* If we are in debug mode, print out an error message */ |
110 #ifdef DEBUG_ERROR | 110 #ifdef DEBUG_ERROR |
111 fprintf (stderr, "SDL_SetError: %s\n", SDL_GetError ()); | 111 fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError()); |
112 #endif | 112 #endif |
113 } | 113 } |
114 | 114 |
115 /* This function has a bit more overhead than most error functions | 115 /* This function has a bit more overhead than most error functions |
116 so that it supports internationalization and thread-safe errors. | 116 so that it supports internationalization and thread-safe errors. |
117 */ | 117 */ |
118 char * | 118 char * |
119 SDL_GetErrorMsg (char *errstr, unsigned int maxlen) | 119 SDL_GetErrorMsg(char *errstr, unsigned int maxlen) |
120 { | 120 { |
121 SDL_error *error; | 121 SDL_error *error; |
122 | 122 |
123 /* Clear the error string */ | 123 /* Clear the error string */ |
124 *errstr = '\0'; | 124 *errstr = '\0'; |
125 --maxlen; | 125 --maxlen; |
126 | 126 |
127 /* Get the thread-safe error, and print it out */ | 127 /* Get the thread-safe error, and print it out */ |
128 error = SDL_GetErrBuf (); | 128 error = SDL_GetErrBuf(); |
129 if (error->error) { | 129 if (error->error) { |
130 const char *fmt; | 130 const char *fmt; |
131 char *msg = errstr; | 131 char *msg = errstr; |
132 int len; | 132 int len; |
133 int argi; | 133 int argi; |
134 | 134 |
135 fmt = SDL_LookupString (error->key); | 135 fmt = SDL_LookupString(error->key); |
136 argi = 0; | 136 argi = 0; |
137 while (*fmt && (maxlen > 0)) { | 137 while (*fmt && (maxlen > 0)) { |
138 if (*fmt == '%') { | 138 if (*fmt == '%') { |
139 char tmp[32], *spot = tmp; | 139 char tmp[32], *spot = tmp; |
140 *spot++ = *fmt++; | 140 *spot++ = *fmt++; |
141 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) | 141 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) |
142 && spot < (tmp + SDL_arraysize (tmp) - 2)) { | 142 && spot < (tmp + SDL_arraysize(tmp) - 2)) { |
143 *spot++ = *fmt++; | 143 *spot++ = *fmt++; |
144 } | 144 } |
145 *spot++ = *fmt++; | 145 *spot++ = *fmt++; |
146 *spot++ = '\0'; | 146 *spot++ = '\0'; |
147 switch (spot[-2]) { | 147 switch (spot[-2]) { |
155 case 'u': | 155 case 'u': |
156 case 'o': | 156 case 'o': |
157 case 'x': | 157 case 'x': |
158 case 'X': | 158 case 'X': |
159 len = | 159 len = |
160 SDL_snprintf (msg, maxlen, tmp, | 160 SDL_snprintf(msg, maxlen, tmp, |
161 error->args[argi++].value_i); | 161 error->args[argi++].value_i); |
162 msg += len; | 162 msg += len; |
163 maxlen -= len; | 163 maxlen -= len; |
164 break; | 164 break; |
165 case 'f': | 165 case 'f': |
166 len = | 166 len = |
167 SDL_snprintf (msg, maxlen, tmp, | 167 SDL_snprintf(msg, maxlen, tmp, |
168 error->args[argi++].value_f); | 168 error->args[argi++].value_f); |
169 msg += len; | 169 msg += len; |
170 maxlen -= len; | 170 maxlen -= len; |
171 break; | 171 break; |
172 case 'p': | 172 case 'p': |
173 len = | 173 len = |
174 SDL_snprintf (msg, maxlen, tmp, | 174 SDL_snprintf(msg, maxlen, tmp, |
175 error->args[argi++].value_ptr); | 175 error->args[argi++].value_ptr); |
176 msg += len; | 176 msg += len; |
177 maxlen -= len; | 177 maxlen -= len; |
178 break; | 178 break; |
179 case 's': | 179 case 's': |
180 len = | 180 len = |
181 SDL_snprintf (msg, maxlen, tmp, | 181 SDL_snprintf(msg, maxlen, tmp, |
182 SDL_LookupString (error-> | 182 SDL_LookupString(error-> |
183 args[argi++].buf)); | 183 args[argi++].buf)); |
184 msg += len; | 184 msg += len; |
185 maxlen -= len; | 185 maxlen -= len; |
186 break; | 186 break; |
187 } | 187 } |
188 } else { | 188 } else { |
195 return (errstr); | 195 return (errstr); |
196 } | 196 } |
197 | 197 |
198 /* Available for backwards compatibility */ | 198 /* Available for backwards compatibility */ |
199 char * | 199 char * |
200 SDL_GetError (void) | 200 SDL_GetError(void) |
201 { | 201 { |
202 static char errmsg[SDL_ERRBUFIZE]; | 202 static char errmsg[SDL_ERRBUFIZE]; |
203 | 203 |
204 return ((char *) SDL_GetErrorMsg (errmsg, SDL_ERRBUFIZE)); | 204 return ((char *) SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE)); |
205 } | 205 } |
206 | 206 |
207 void | 207 void |
208 SDL_ClearError (void) | 208 SDL_ClearError(void) |
209 { | 209 { |
210 SDL_error *error; | 210 SDL_error *error; |
211 | 211 |
212 error = SDL_GetErrBuf (); | 212 error = SDL_GetErrBuf(); |
213 error->error = 0; | 213 error->error = 0; |
214 } | 214 } |
215 | 215 |
216 /* Very common errors go here */ | 216 /* Very common errors go here */ |
217 void | 217 void |
218 SDL_Error (SDL_errorcode code) | 218 SDL_Error(SDL_errorcode code) |
219 { | 219 { |
220 switch (code) { | 220 switch (code) { |
221 case SDL_ENOMEM: | 221 case SDL_ENOMEM: |
222 SDL_SetError ("Out of memory"); | 222 SDL_SetError("Out of memory"); |
223 break; | 223 break; |
224 case SDL_EFREAD: | 224 case SDL_EFREAD: |
225 SDL_SetError ("Error reading from datastream"); | 225 SDL_SetError("Error reading from datastream"); |
226 break; | 226 break; |
227 case SDL_EFWRITE: | 227 case SDL_EFWRITE: |
228 SDL_SetError ("Error writing to datastream"); | 228 SDL_SetError("Error writing to datastream"); |
229 break; | 229 break; |
230 case SDL_EFSEEK: | 230 case SDL_EFSEEK: |
231 SDL_SetError ("Error seeking in datastream"); | 231 SDL_SetError("Error seeking in datastream"); |
232 break; | 232 break; |
233 default: | 233 default: |
234 SDL_SetError ("Unknown SDL error"); | 234 SDL_SetError("Unknown SDL error"); |
235 break; | 235 break; |
236 } | 236 } |
237 } | 237 } |
238 | 238 |
239 #ifdef TEST_ERROR | 239 #ifdef TEST_ERROR |
240 int | 240 int |
241 main (int argc, char *argv[]) | 241 main(int argc, char *argv[]) |
242 { | 242 { |
243 char buffer[BUFSIZ + 1]; | 243 char buffer[BUFSIZ + 1]; |
244 | 244 |
245 SDL_SetError ("Hi there!"); | 245 SDL_SetError("Hi there!"); |
246 printf ("Error 1: %s\n", SDL_GetError ()); | 246 printf("Error 1: %s\n", SDL_GetError()); |
247 SDL_ClearError (); | 247 SDL_ClearError(); |
248 SDL_memset (buffer, '1', BUFSIZ); | 248 SDL_memset(buffer, '1', BUFSIZ); |
249 buffer[BUFSIZ] = 0; | 249 buffer[BUFSIZ] = 0; |
250 SDL_SetError ("This is the error: %s (%f)", buffer, 1.0); | 250 SDL_SetError("This is the error: %s (%f)", buffer, 1.0); |
251 printf ("Error 2: %s\n", SDL_GetError ()); | 251 printf("Error 2: %s\n", SDL_GetError()); |
252 exit (0); | 252 exit(0); |
253 } | 253 } |
254 #endif | 254 #endif |
255 /* vi: set ts=4 sw=4 expandtab: */ | 255 /* vi: set ts=4 sw=4 expandtab: */ |