SDL  2.0
SDL_error.c File Reference
#include "./SDL_internal.h"
#include "SDL_log.h"
#include "SDL_error.h"
#include "SDL_error_c.h"
+ Include dependency graph for SDL_error.c:

Go to the source code of this file.

Macros

#define SDL_ERRBUFIZE   1024
 

Functions

static const char * SDL_LookupString (const char *key)
 
static char * SDL_GetErrorMsg (char *errstr, int maxlen)
 
int SDL_SetError (SDL_PRINTF_FORMAT_STRING const char *fmt,...)
 
const char * SDL_GetError (void)
 
void SDL_ClearError (void)
 
int SDL_Error (SDL_errorcode code)
 

Macro Definition Documentation

◆ SDL_ERRBUFIZE

#define SDL_ERRBUFIZE   1024

Definition at line 29 of file SDL_error.c.

Function Documentation

◆ SDL_ClearError()

void SDL_ClearError ( void  )

Definition at line 138 of file SDL_error.c.

139 {
140  SDL_error *error;
141 
142  error = SDL_GetErrBuf();
143  error->error = 0;
144 }

References SDL_error::error, and SDL_GetErrBuf().

◆ SDL_Error()

int SDL_Error ( SDL_errorcode  code)

Definition at line 148 of file SDL_error.c.

149 {
150  switch (code) {
151  case SDL_ENOMEM:
152  return SDL_SetError("Out of memory");
153  case SDL_EFREAD:
154  return SDL_SetError("Error reading from datastream");
155  case SDL_EFWRITE:
156  return SDL_SetError("Error writing to datastream");
157  case SDL_EFSEEK:
158  return SDL_SetError("Error seeking in datastream");
159  case SDL_UNSUPPORTED:
160  return SDL_SetError("That operation is not supported");
161  default:
162  return SDL_SetError("Unknown SDL error");
163  }
164 }

References SDL_EFREAD, SDL_EFSEEK, SDL_EFWRITE, SDL_ENOMEM, SDL_SetError(), and SDL_UNSUPPORTED.

◆ SDL_GetError()

const char* SDL_GetError ( void  )

Definition at line 130 of file SDL_error.c.

131 {
132  static char errmsg[SDL_ERRBUFIZE];
133 
134  return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
135 }

References SDL_ERRBUFIZE, and SDL_GetErrorMsg().

◆ SDL_GetErrorMsg()

static char * SDL_GetErrorMsg ( char *  errstr,
int  maxlen 
)
static

Definition at line 196 of file SDL_error.c.

197 {
198  SDL_error *error;
199 
200  /* Clear the error string */
201  *errstr = '\0';
202  --maxlen;
203 
204  /* Get the thread-safe error, and print it out */
205  error = SDL_GetErrBuf();
206  if (error->error) {
207  const char *fmt;
208  char *msg = errstr;
209  int len;
210  int argi;
211 
212  fmt = SDL_LookupString(error->key);
213  argi = 0;
214  while (*fmt && (maxlen > 0)) {
215  if (*fmt == '%') {
216  char tmp[32], *spot = tmp;
217  *spot++ = *fmt++;
218  while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
219  && spot < (tmp + SDL_arraysize(tmp) - 2)) {
220  *spot++ = *fmt++;
221  }
222  if (*fmt == 'l') {
223  *spot++ = *fmt++;
224  *spot++ = *fmt++;
225  *spot++ = '\0';
226  switch (spot[-2]) {
227  case 'i': case 'd': case 'u': case 'x': case 'X':
228  len = SDL_snprintf(msg, maxlen, tmp,
229  error->args[argi++].value_l);
230  if (len > 0) {
231  msg += len;
232  maxlen -= len;
233  }
234  break;
235  }
236  continue;
237  }
238  *spot++ = *fmt++;
239  *spot++ = '\0';
240  switch (spot[-2]) {
241  case '%':
242  *msg++ = '%';
243  maxlen -= 1;
244  break;
245  case 'c':
246  case 'i':
247  case 'd':
248  case 'u':
249  case 'o':
250  case 'x':
251  case 'X':
252  len =
253  SDL_snprintf(msg, maxlen, tmp,
254  error->args[argi++].value_i);
255  if (len > 0) {
256  msg += len;
257  maxlen -= len;
258  }
259  break;
260 
261  case 'f':
262  len =
263  SDL_snprintf(msg, maxlen, tmp,
264  error->args[argi++].value_f);
265  if (len > 0) {
266  msg += len;
267  maxlen -= len;
268  }
269  break;
270 
271  case 'p':
272  len =
273  SDL_snprintf(msg, maxlen, tmp,
274  error->args[argi++].value_ptr);
275  if (len > 0) {
276  msg += len;
277  maxlen -= len;
278  }
279  break;
280 
281  case 's':
282  len =
283  SDL_snprintf(msg, maxlen, tmp,
284  SDL_LookupString(error->args[argi++].
285  buf));
286  if (len > 0) {
287  msg += len;
288  maxlen -= len;
289  }
290  break;
291 
292  }
293  } else {
294  *msg++ = *fmt++;
295  maxlen -= 1;
296  }
297  }
298 
299  /* slide back if we've overshot the end of our buffer. */
300  if (maxlen < 0) {
301  msg -= (-maxlen) + 1;
302  }
303 
304  *msg = 0; /* NULL terminate the string */
305  }
306  return (errstr);
307 }

References SDL_error::args, SDL_error::error, SDL_error::key, SDL_arraysize, SDL_GetErrBuf(), SDL_LookupString(), SDL_snprintf, SDL_error::value_f, SDL_error::value_i, SDL_error::value_l, and SDL_error::value_ptr.

Referenced by SDL_GetError(), and SDL_SetError().

◆ SDL_LookupString()

static const char* SDL_LookupString ( const char *  key)
static

Definition at line 34 of file SDL_error.c.

35 {
36  /* FIXME: Add code to lookup key in language string hash-table */
37  return key;
38 }

Referenced by SDL_GetErrorMsg().

◆ SDL_SetError()

int SDL_SetError ( SDL_PRINTF_FORMAT_STRING const char *  fmt,
  ... 
)

Definition at line 45 of file SDL_error.c.

46 {
47  va_list ap;
48  SDL_error *error;
49 
50  /* Ignore call if invalid format pointer was passed */
51  if (fmt == NULL) return -1;
52 
53  /* Copy in the key, mark error as valid */
54  error = SDL_GetErrBuf();
55  error->error = 1;
56  SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
57 
58  va_start(ap, fmt);
59  error->argc = 0;
60  while (*fmt) {
61  if (*fmt++ == '%') {
62  while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
63  ++fmt;
64  }
65  switch (*fmt++) {
66  case 0: /* Malformed format string.. */
67  --fmt;
68  break;
69  case 'l':
70  switch (*fmt++) {
71  case 0: /* Malformed format string.. */
72  --fmt;
73  break;
74  case 'i': case 'd': case 'u': case 'x': case 'X':
75  error->args[error->argc++].value_l = va_arg(ap, long);
76  break;
77  }
78  break;
79  case 'c':
80  case 'i':
81  case 'd':
82  case 'u':
83  case 'o':
84  case 'x':
85  case 'X':
86  error->args[error->argc++].value_i = va_arg(ap, int);
87  break;
88  case 'f':
89  error->args[error->argc++].value_f = va_arg(ap, double);
90  break;
91  case 'p':
92  error->args[error->argc++].value_ptr = va_arg(ap, void *);
93  break;
94  case 's':
95  {
96  int i = error->argc;
97  const char *str = va_arg(ap, const char *);
98  if (str == NULL)
99  str = "(null)";
100  SDL_strlcpy((char *) error->args[i].buf, str,
102  error->argc++;
103  }
104  break;
105  default:
106  break;
107  }
108  if (error->argc >= ERR_MAX_ARGS) {
109  break;
110  }
111  }
112  }
113  va_end(ap);
114 
116  /* If we are in debug mode, print out an error message
117  * Avoid stomping on the static buffer in GetError, just
118  * in case this is called while processing a ShowMessageBox to
119  * show an error already in that static buffer.
120  */
121  char errmsg[SDL_ERRBUFIZE];
122  SDL_GetErrorMsg(errmsg, sizeof(errmsg));
123  SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg);
124  }
125  return -1;
126 }

References SDL_error::argc, SDL_error::args, SDL_error::buf, ERR_MAX_ARGS, ERR_MAX_STRLEN, SDL_error::error, i, SDL_error::key, NULL, SDL_ERRBUFIZE, SDL_GetErrBuf(), SDL_GetErrorMsg(), SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_DEBUG, SDL_LogDebug, SDL_LogGetPriority, SDL_strlcpy, SDL_error::value_f, SDL_error::value_i, SDL_error::value_l, and SDL_error::value_ptr.

Referenced by SDL_Error().

SDL_ERRBUFIZE
#define SDL_ERRBUFIZE
Definition: SDL_error.c:29
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
NULL
#define NULL
Definition: begin_code.h:167
SDL_error::value_ptr
void * value_ptr
Definition: SDL_error_c.h:49
SDL_error::value_l
long value_l
Definition: SDL_error_c.h:54
SDL_error::key
char key[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:43
SDL_ENOMEM
@ SDL_ENOMEM
Definition: SDL_error.h:57
SDL_error
Definition: SDL_error_c.h:33
SDL_LogGetPriority
#define SDL_LogGetPriority
Definition: SDL_dynapi_overrides.h:237
SDL_LOG_PRIORITY_DEBUG
@ SDL_LOG_PRIORITY_DEBUG
Definition: SDL_log.h:105
SDL_LookupString
static const char * SDL_LookupString(const char *key)
Definition: SDL_error.c:34
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2929
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2483
ERR_MAX_ARGS
#define ERR_MAX_ARGS
Definition: SDL_error_c.h:31
SDL_LOG_CATEGORY_ERROR
@ SDL_LOG_CATEGORY_ERROR
Definition: SDL_log.h:67
SDL_error::value_f
double value_f
Definition: SDL_error_c.h:55
SDL_LogDebug
#define SDL_LogDebug
Definition: SDL_dynapi_overrides.h:33
SDL_error::argc
int argc
Definition: SDL_error_c.h:46
key
GLuint64 key
Definition: gl2ext.h:2192
SDL_error::error
int error
Definition: SDL_error_c.h:36
SDL_error::value_i
int value_i
Definition: SDL_error_c.h:53
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
ERR_MAX_STRLEN
#define ERR_MAX_STRLEN
Definition: SDL_error_c.h:30
SDL_SetError
int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt,...)
Definition: SDL_error.c:45
SDL_error::args
union SDL_error::@28 args[ERR_MAX_ARGS]
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_error::buf
char buf[ERR_MAX_STRLEN]
Definition: SDL_error_c.h:56
SDL_GetErrBuf
SDL_error * SDL_GetErrBuf(void)
Definition: SDL_thread.c:206
SDL_UNSUPPORTED
@ SDL_UNSUPPORTED
Definition: SDL_error.h:61
SDL_EFSEEK
@ SDL_EFSEEK
Definition: SDL_error.h:60
SDL_GetErrorMsg
static char * SDL_GetErrorMsg(char *errstr, int maxlen)
Definition: SDL_error.c:196
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_EFWRITE
@ SDL_EFWRITE
Definition: SDL_error.h:59
SDL_EFREAD
@ SDL_EFREAD
Definition: SDL_error.h:58