Mercurial > sdl-ios-xcode
comparison src/SDL_error.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | 845bcffc0dd8 |
children | 5f6550e5184f |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
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 *SDL_LookupString(const char *key) | 42 static const char * |
43 { | 43 SDL_LookupString(const char *key) |
44 /* FIXME: Add code to lookup key in language string hash-table */ | 44 { |
45 return key; | 45 /* FIXME: Add code to lookup key in language string hash-table */ |
46 return key; | |
46 } | 47 } |
47 | 48 |
48 /* Public functions */ | 49 /* Public functions */ |
49 | 50 |
50 void SDL_SetError (const char *fmt, ...) | 51 void |
51 { | 52 SDL_SetError(const char *fmt, ...) |
52 va_list ap; | 53 { |
53 SDL_error *error; | 54 va_list ap; |
54 | 55 SDL_error *error; |
55 /* Copy in the key, mark error as valid */ | 56 |
56 error = SDL_GetErrBuf(); | 57 /* Copy in the key, mark error as valid */ |
57 error->error = 1; | 58 error = SDL_GetErrBuf(); |
58 SDL_strlcpy((char *)error->key, fmt, sizeof(error->key)); | 59 error->error = 1; |
59 | 60 SDL_strlcpy((char *) error->key, fmt, sizeof(error->key)); |
60 va_start(ap, fmt); | 61 |
61 error->argc = 0; | 62 va_start(ap, fmt); |
62 while ( *fmt ) { | 63 error->argc = 0; |
63 if ( *fmt++ == '%' ) { | 64 while (*fmt) { |
64 while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) { | 65 if (*fmt++ == '%') { |
65 ++fmt; | 66 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) { |
66 } | 67 ++fmt; |
67 switch (*fmt++) { | 68 } |
68 case 0: /* Malformed format string.. */ | 69 switch (*fmt++) { |
69 --fmt; | 70 case 0: /* Malformed format string.. */ |
70 break; | 71 --fmt; |
71 case 'c': | 72 break; |
72 case 'i': | 73 case 'c': |
73 case 'd': | 74 case 'i': |
74 case 'u': | 75 case 'd': |
75 case 'o': | 76 case 'u': |
76 case 'x': | 77 case 'o': |
77 case 'X': | 78 case 'x': |
78 error->args[error->argc++].value_i = | 79 case 'X': |
79 va_arg(ap, int); | 80 error->args[error->argc++].value_i = va_arg(ap, int); |
80 break; | 81 break; |
81 case 'f': | 82 case 'f': |
82 error->args[error->argc++].value_f = | 83 error->args[error->argc++].value_f = va_arg(ap, double); |
83 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 = | 87 break; |
87 va_arg(ap, void *); | 88 case 's': |
88 break; | 89 { |
89 case 's': | 90 int i = error->argc; |
90 { | 91 const char *str = va_arg(ap, const char *); |
91 int i = error->argc; | 92 if (str == NULL) |
92 const char *str = va_arg(ap, const char *); | 93 str = "(null)"; |
93 if (str == NULL) | 94 SDL_strlcpy((char *) error->args[i].buf, str, |
94 str = "(null)"; | 95 ERR_MAX_STRLEN); |
95 SDL_strlcpy((char *)error->args[i].buf, str, ERR_MAX_STRLEN); | 96 error->argc++; |
96 error->argc++; | 97 } |
97 } | 98 break; |
98 break; | 99 default: |
99 default: | 100 break; |
100 break; | 101 } |
101 } | 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 *SDL_GetErrorMsg(char *errstr, unsigned int maxlen) | 118 char * |
119 { | 119 SDL_GetErrorMsg(char *errstr, unsigned int maxlen) |
120 SDL_error *error; | 120 { |
121 | 121 SDL_error *error; |
122 /* Clear the error string */ | 122 |
123 *errstr = '\0'; --maxlen; | 123 /* Clear the error string */ |
124 | 124 *errstr = '\0'; |
125 /* Get the thread-safe error, and print it out */ | 125 --maxlen; |
126 error = SDL_GetErrBuf(); | 126 |
127 if ( error->error ) { | 127 /* Get the thread-safe error, and print it out */ |
128 const char *fmt; | 128 error = SDL_GetErrBuf(); |
129 char *msg = errstr; | 129 if (error->error) { |
130 int len; | 130 const char *fmt; |
131 int argi; | 131 char *msg = errstr; |
132 | 132 int len; |
133 fmt = SDL_LookupString(error->key); | 133 int argi; |
134 argi = 0; | 134 |
135 while ( *fmt && (maxlen > 0) ) { | 135 fmt = SDL_LookupString(error->key); |
136 if ( *fmt == '%' ) { | 136 argi = 0; |
137 char tmp[32], *spot = tmp; | 137 while (*fmt && (maxlen > 0)) { |
138 *spot++ = *fmt++; | 138 if (*fmt == '%') { |
139 while ( (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) && spot < (tmp+SDL_arraysize(tmp)-2) ) { | 139 char tmp[32], *spot = tmp; |
140 *spot++ = *fmt++; | 140 *spot++ = *fmt++; |
141 } | 141 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) |
142 *spot++ = *fmt++; | 142 && spot < (tmp + SDL_arraysize(tmp) - 2)) { |
143 *spot++ = '\0'; | 143 *spot++ = *fmt++; |
144 switch (spot[-2]) { | 144 } |
145 case '%': | 145 *spot++ = *fmt++; |
146 *msg++ = '%'; | 146 *spot++ = '\0'; |
147 maxlen -= 1; | 147 switch (spot[-2]) { |
148 break; | 148 case '%': |
149 case 'c': | 149 *msg++ = '%'; |
150 case 'i': | 150 maxlen -= 1; |
151 case 'd': | 151 break; |
152 case 'u': | 152 case 'c': |
153 case 'o': | 153 case 'i': |
154 case 'x': | 154 case 'd': |
155 case 'X': | 155 case 'u': |
156 len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i); | 156 case 'o': |
157 msg += len; | 157 case 'x': |
158 maxlen -= len; | 158 case 'X': |
159 break; | 159 len = |
160 case 'f': | 160 SDL_snprintf(msg, maxlen, tmp, |
161 len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f); | 161 error->args[argi++].value_i); |
162 msg += len; | 162 msg += len; |
163 maxlen -= len; | 163 maxlen -= len; |
164 break; | 164 break; |
165 case 'p': | 165 case 'f': |
166 len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr); | 166 len = |
167 msg += len; | 167 SDL_snprintf(msg, maxlen, tmp, |
168 maxlen -= len; | 168 error->args[argi++].value_f); |
169 break; | 169 msg += len; |
170 case 's': | 170 maxlen -= len; |
171 len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf)); | 171 break; |
172 msg += len; | 172 case 'p': |
173 maxlen -= len; | 173 len = |
174 break; | 174 SDL_snprintf(msg, maxlen, tmp, |
175 } | 175 error->args[argi++].value_ptr); |
176 } else { | 176 msg += len; |
177 *msg++ = *fmt++; | 177 maxlen -= len; |
178 maxlen -= 1; | 178 break; |
179 } | 179 case 's': |
180 } | 180 len = |
181 *msg = 0; /* NULL terminate the string */ | 181 SDL_snprintf(msg, maxlen, tmp, |
182 } | 182 SDL_LookupString(error-> |
183 return(errstr); | 183 args[argi++].buf)); |
184 msg += len; | |
185 maxlen -= len; | |
186 break; | |
187 } | |
188 } else { | |
189 *msg++ = *fmt++; | |
190 maxlen -= 1; | |
191 } | |
192 } | |
193 *msg = 0; /* NULL terminate the string */ | |
194 } | |
195 return (errstr); | |
184 } | 196 } |
185 | 197 |
186 /* Available for backwards compatibility */ | 198 /* Available for backwards compatibility */ |
187 char *SDL_GetError (void) | 199 char * |
188 { | 200 SDL_GetError(void) |
189 static char errmsg[SDL_ERRBUFIZE]; | 201 { |
190 | 202 static char errmsg[SDL_ERRBUFIZE]; |
191 return((char *)SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE)); | 203 |
192 } | 204 return ((char *) SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE)); |
193 | 205 } |
194 void SDL_ClearError(void) | 206 |
195 { | 207 void |
196 SDL_error *error; | 208 SDL_ClearError(void) |
197 | 209 { |
198 error = SDL_GetErrBuf(); | 210 SDL_error *error; |
199 error->error = 0; | 211 |
212 error = SDL_GetErrBuf(); | |
213 error->error = 0; | |
200 } | 214 } |
201 | 215 |
202 /* Very common errors go here */ | 216 /* Very common errors go here */ |
203 void SDL_Error(SDL_errorcode code) | 217 void |
204 { | 218 SDL_Error(SDL_errorcode code) |
205 switch (code) { | 219 { |
206 case SDL_ENOMEM: | 220 switch (code) { |
207 SDL_SetError("Out of memory"); | 221 case SDL_ENOMEM: |
208 break; | 222 SDL_SetError("Out of memory"); |
209 case SDL_EFREAD: | 223 break; |
210 SDL_SetError("Error reading from datastream"); | 224 case SDL_EFREAD: |
211 break; | 225 SDL_SetError("Error reading from datastream"); |
212 case SDL_EFWRITE: | 226 break; |
213 SDL_SetError("Error writing to datastream"); | 227 case SDL_EFWRITE: |
214 break; | 228 SDL_SetError("Error writing to datastream"); |
215 case SDL_EFSEEK: | 229 break; |
216 SDL_SetError("Error seeking in datastream"); | 230 case SDL_EFSEEK: |
217 break; | 231 SDL_SetError("Error seeking in datastream"); |
218 default: | 232 break; |
219 SDL_SetError("Unknown SDL error"); | 233 default: |
220 break; | 234 SDL_SetError("Unknown SDL error"); |
221 } | 235 break; |
236 } | |
222 } | 237 } |
223 | 238 |
224 #ifdef TEST_ERROR | 239 #ifdef TEST_ERROR |
225 int main(int argc, char *argv[]) | 240 int |
226 { | 241 main(int argc, char *argv[]) |
227 char buffer[BUFSIZ+1]; | 242 { |
228 | 243 char buffer[BUFSIZ + 1]; |
229 SDL_SetError("Hi there!"); | 244 |
230 printf("Error 1: %s\n", SDL_GetError()); | 245 SDL_SetError("Hi there!"); |
231 SDL_ClearError(); | 246 printf("Error 1: %s\n", SDL_GetError()); |
232 SDL_memset(buffer, '1', BUFSIZ); | 247 SDL_ClearError(); |
233 buffer[BUFSIZ] = 0; | 248 SDL_memset(buffer, '1', BUFSIZ); |
234 SDL_SetError("This is the error: %s (%f)", buffer, 1.0); | 249 buffer[BUFSIZ] = 0; |
235 printf("Error 2: %s\n", SDL_GetError()); | 250 SDL_SetError("This is the error: %s (%f)", buffer, 1.0); |
236 exit(0); | 251 printf("Error 2: %s\n", SDL_GetError()); |
252 exit(0); | |
237 } | 253 } |
238 #endif | 254 #endif |
255 /* vi: set ts=4 sw=4 expandtab: */ |