error.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #ifndef _ERROR_H_
  24. #define _ERROR_H_
  25. #include <config.h>
  26. #ifdef HAVE_INTTYPES_H
  27. # include <inttypes.h>
  28. #endif
  29. //#include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. //#include <assert.h>
  34. #include "debug.h"
  35. #include <zbar.h>
  36. #ifdef _WIN32
  37. # include <windows.h>
  38. #endif
  39. #if __STDC_VERSION__ < 199901L
  40. # if __GNUC__ >= 2
  41. # define __func__ __FUNCTION__
  42. # else
  43. # define __func__ "<unknown>"
  44. # endif
  45. #endif
  46. #define ERRINFO_MAGIC (0x5252457a) /* "zERR" (LE) */
  47. typedef enum errsev_e {
  48. SEV_FATAL = -2, /* application must terminate */
  49. SEV_ERROR = -1, /* might be able to recover and continue */
  50. SEV_OK = 0,
  51. SEV_WARNING = 1, /* unexpected condition */
  52. SEV_NOTE = 2, /* fyi */
  53. } errsev_t;
  54. typedef enum errmodule_e {
  55. ZBAR_MOD_PROCESSOR,
  56. ZBAR_MOD_VIDEO,
  57. ZBAR_MOD_WINDOW,
  58. ZBAR_MOD_IMAGE_SCANNER,
  59. ZBAR_MOD_UNKNOWN,
  60. } errmodule_t;
  61. typedef struct errinfo_s {
  62. uint32_t magic; /* just in case */
  63. errmodule_t module; /* reporting module */
  64. char *buf; /* formatted and passed to application */
  65. int errnum; /* errno for system errors */
  66. errsev_t sev;
  67. zbar_error_t type;
  68. const char *func; /* reporting function */
  69. const char *detail; /* description */
  70. char *arg_str; /* single string argument */
  71. int arg_int; /* single integer argument */
  72. } errinfo_t;
  73. extern int _zbar_verbosity;
  74. /* FIXME don't we need varargs hacks here? */
  75. #ifdef _WIN32
  76. # define ZFLUSH fflush(stderr);
  77. #else
  78. # define ZFLUSH
  79. #endif
  80. #define ZNO_MESSAGES
  81. #ifdef ZNO_MESSAGES
  82. #if 0
  83. # ifdef __GNUC__
  84. /* older versions of gcc (< 2.95) require a named varargs parameter */
  85. # define zprintf(args...)
  86. # else
  87. /* unfortunately named vararg parameter is a gcc-specific extension */
  88. # define zprintf(...)
  89. # endif
  90. #else
  91. # ifdef __GNUC__
  92. # define zprintf(level, format, args...) do { ;} while(0)
  93. # else
  94. # define zprintf(level, format, ...) do { \
  95. if(_zbar_verbosity >= level) { \
  96. fprintf(stderr, "%s: " format, __func__ , ##__VA_ARGS__); \
  97. ZFLUSH \
  98. } \
  99. } while(0)
  100. # endif
  101. #endif
  102. #endif
  103. static inline int err_copy (void *dst_c,
  104. void *src_c)
  105. {
  106. // errinfo_t *dst = dst_c;
  107. // errinfo_t *src = src_c;
  108. // assert(dst->magic == ERRINFO_MAGIC);
  109. // assert(src->magic == ERRINFO_MAGIC);
  110. //
  111. // dst->errnum = src->errnum;
  112. // dst->sev = src->sev;
  113. // dst->type = src->type;
  114. // dst->func = src->func;
  115. // dst->detail = src->detail;
  116. // dst->arg_str = src->arg_str;
  117. // src->arg_str = NULL; /* unused at src, avoid double free */
  118. // dst->arg_int = src->arg_int;
  119. return(-1);
  120. }
  121. static inline int err_capture (const void *container,
  122. errsev_t sev,
  123. zbar_error_t type,
  124. const char *func,
  125. const char *detail)
  126. {
  127. // errinfo_t *err = (errinfo_t*)container;
  128. // assert(err->magic == ERRINFO_MAGIC);
  129. // if(type == ZBAR_ERR_SYSTEM)
  130. // err->errnum = errno;
  131. //#ifdef _WIN32
  132. // else if(type == ZBAR_ERR_WINAPI)
  133. // err->errnum = GetLastError();
  134. //#endif
  135. // err->sev = sev;
  136. // err->type = type;
  137. // err->func = func;
  138. // err->detail = detail;
  139. // if(_zbar_verbosity >= 1)
  140. // _zbar_error_spew(err, 0);
  141. return(-1);
  142. }
  143. static inline int err_capture_str (const void *container,
  144. errsev_t sev,
  145. zbar_error_t type,
  146. const char *func,
  147. const char *detail,
  148. const char *arg)
  149. {
  150. // errinfo_t *err = (errinfo_t*)container;
  151. // assert(err->magic == ERRINFO_MAGIC);
  152. // if(err->arg_str)
  153. // free(err->arg_str);
  154. // err->arg_str = strdup(arg);
  155. // return(err_capture(container, sev, type, func, detail));
  156. return -1;
  157. }
  158. static inline int err_capture_int (const void *container,
  159. errsev_t sev,
  160. zbar_error_t type,
  161. const char *func,
  162. const char *detail,
  163. int arg)
  164. {
  165. // errinfo_t *err = (errinfo_t*)container;
  166. // assert(err->magic == ERRINFO_MAGIC);
  167. // err->arg_int = arg;
  168. // return(err_capture(container, sev, type, func, detail));
  169. return -1;
  170. }
  171. static inline int err_capture_num (const void *container,
  172. errsev_t sev,
  173. zbar_error_t type,
  174. const char *func,
  175. const char *detail,
  176. int num)
  177. {
  178. // errinfo_t *err = (errinfo_t*)container;
  179. // assert(err->magic == ERRINFO_MAGIC);
  180. // err->errnum = num;
  181. // return(err_capture(container, sev, type, func, detail));
  182. return -1;
  183. }
  184. static inline void err_init (errinfo_t *err,
  185. errmodule_t module)
  186. {
  187. // err->magic = ERRINFO_MAGIC;
  188. // err->module = module;
  189. }
  190. static inline void err_cleanup (errinfo_t *err)
  191. {
  192. // assert(err->magic == ERRINFO_MAGIC);
  193. // if(err->buf) {
  194. // free(err->buf);
  195. // err->buf = NULL;
  196. // }
  197. // if(err->arg_str) {
  198. // free(err->arg_str);
  199. // err->arg_str = NULL;
  200. // }
  201. }
  202. #endif