SDL_error.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "./SDL_internal.h"
  19. /* Simple error handling in SDL */
  20. #include "SDL_log.h"
  21. #include "SDL_error.h"
  22. #include "SDL_error_c.h"
  23. /* Routine to get the thread-specific error variable */
  24. #if SDL_THREADS_DISABLED
  25. /* The default (non-thread-safe) global error variable */
  26. static SDL_error SDL_global_error;
  27. #define SDL_GetErrBuf() (&SDL_global_error)
  28. #else
  29. extern SDL_error *SDL_GetErrBuf(void);
  30. #endif /* SDL_THREADS_DISABLED */
  31. #define SDL_ERRBUFIZE 1024
  32. /* Private functions */
  33. static const char *
  34. SDL_LookupString(const char *key)
  35. {
  36. /* FIXME: Add code to lookup key in language string hash-table */
  37. return key;
  38. }
  39. /* Public functions */
  40. int
  41. SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  42. {
  43. va_list ap;
  44. SDL_error *error;
  45. /* Ignore call if invalid format pointer was passed */
  46. if (fmt == NULL) return -1;
  47. /* Copy in the key, mark error as valid */
  48. error = SDL_GetErrBuf();
  49. error->error = 1;
  50. SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
  51. va_start(ap, fmt);
  52. error->argc = 0;
  53. while (*fmt) {
  54. if (*fmt++ == '%') {
  55. while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
  56. ++fmt;
  57. }
  58. switch (*fmt++) {
  59. case 0: /* Malformed format string.. */
  60. --fmt;
  61. break;
  62. case 'c':
  63. case 'i':
  64. case 'd':
  65. case 'u':
  66. case 'o':
  67. case 'x':
  68. case 'X':
  69. error->args[error->argc++].value_i = va_arg(ap, int);
  70. break;
  71. case 'f':
  72. error->args[error->argc++].value_f = va_arg(ap, double);
  73. break;
  74. case 'p':
  75. error->args[error->argc++].value_ptr = va_arg(ap, void *);
  76. break;
  77. case 's':
  78. {
  79. int i = error->argc;
  80. const char *str = va_arg(ap, const char *);
  81. if (str == NULL)
  82. str = "(null)";
  83. SDL_strlcpy((char *) error->args[i].buf, str,
  84. ERR_MAX_STRLEN);
  85. error->argc++;
  86. }
  87. break;
  88. default:
  89. break;
  90. }
  91. if (error->argc >= ERR_MAX_ARGS) {
  92. break;
  93. }
  94. }
  95. }
  96. va_end(ap);
  97. /* If we are in debug mode, print out an error message */
  98. SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());
  99. return -1;
  100. }
  101. #ifdef __GNUC__
  102. #pragma GCC diagnostic push
  103. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  104. #endif
  105. /* This function has a bit more overhead than most error functions
  106. so that it supports internationalization and thread-safe errors.
  107. */
  108. static char *
  109. SDL_GetErrorMsg(char *errstr, int maxlen)
  110. {
  111. SDL_error *error;
  112. /* Clear the error string */
  113. *errstr = '\0';
  114. --maxlen;
  115. /* Get the thread-safe error, and print it out */
  116. error = SDL_GetErrBuf();
  117. if (error->error) {
  118. const char *fmt;
  119. char *msg = errstr;
  120. int len;
  121. int argi;
  122. fmt = SDL_LookupString(error->key);
  123. argi = 0;
  124. while (*fmt && (maxlen > 0)) {
  125. if (*fmt == '%') {
  126. char tmp[32], *spot = tmp;
  127. *spot++ = *fmt++;
  128. while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
  129. && spot < (tmp + SDL_arraysize(tmp) - 2)) {
  130. *spot++ = *fmt++;
  131. }
  132. *spot++ = *fmt++;
  133. *spot++ = '\0';
  134. switch (spot[-2]) {
  135. case '%':
  136. *msg++ = '%';
  137. maxlen -= 1;
  138. break;
  139. case 'c':
  140. case 'i':
  141. case 'd':
  142. case 'u':
  143. case 'o':
  144. case 'x':
  145. case 'X':
  146. len =
  147. SDL_snprintf(msg, maxlen, tmp,
  148. error->args[argi++].value_i);
  149. if (len > 0) {
  150. msg += len;
  151. maxlen -= len;
  152. }
  153. break;
  154. case 'f':
  155. len =
  156. SDL_snprintf(msg, maxlen, tmp,
  157. error->args[argi++].value_f);
  158. if (len > 0) {
  159. msg += len;
  160. maxlen -= len;
  161. }
  162. break;
  163. case 'p':
  164. len =
  165. SDL_snprintf(msg, maxlen, tmp,
  166. error->args[argi++].value_ptr);
  167. if (len > 0) {
  168. msg += len;
  169. maxlen -= len;
  170. }
  171. break;
  172. case 's':
  173. len =
  174. SDL_snprintf(msg, maxlen, tmp,
  175. SDL_LookupString(error->args[argi++].
  176. buf));
  177. if (len > 0) {
  178. msg += len;
  179. maxlen -= len;
  180. }
  181. break;
  182. }
  183. } else {
  184. *msg++ = *fmt++;
  185. maxlen -= 1;
  186. }
  187. }
  188. /* slide back if we've overshot the end of our buffer. */
  189. if (maxlen < 0) {
  190. msg -= (-maxlen) + 1;
  191. }
  192. *msg = 0; /* NULL terminate the string */
  193. }
  194. return (errstr);
  195. }
  196. #ifdef __GNUC__
  197. #pragma GCC diagnostic pop
  198. #endif
  199. /* Available for backwards compatibility */
  200. const char *
  201. SDL_GetError(void)
  202. {
  203. static char errmsg[SDL_ERRBUFIZE];
  204. return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
  205. }
  206. void
  207. SDL_ClearError(void)
  208. {
  209. SDL_error *error;
  210. error = SDL_GetErrBuf();
  211. error->error = 0;
  212. }
  213. /* Very common errors go here */
  214. int
  215. SDL_Error(SDL_errorcode code)
  216. {
  217. switch (code) {
  218. case SDL_ENOMEM:
  219. return SDL_SetError("Out of memory");
  220. case SDL_EFREAD:
  221. return SDL_SetError("Error reading from datastream");
  222. case SDL_EFWRITE:
  223. return SDL_SetError("Error writing to datastream");
  224. case SDL_EFSEEK:
  225. return SDL_SetError("Error seeking in datastream");
  226. case SDL_UNSUPPORTED:
  227. return SDL_SetError("That operation is not supported");
  228. default:
  229. return SDL_SetError("Unknown SDL error");
  230. }
  231. }
  232. #ifdef TEST_ERROR
  233. int
  234. main(int argc, char *argv[])
  235. {
  236. char buffer[BUFSIZ + 1];
  237. SDL_SetError("Hi there!");
  238. printf("Error 1: %s\n", SDL_GetError());
  239. SDL_ClearError();
  240. SDL_memset(buffer, '1', BUFSIZ);
  241. buffer[BUFSIZ] = 0;
  242. SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
  243. printf("Error 2: %s\n", SDL_GetError());
  244. exit(0);
  245. }
  246. #endif
  247. /* vi: set ts=4 sw=4 expandtab: */