error.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 { \
  93. if(_zbar_verbosity >= level) { \
  94. fprintf(stderr, "%s: " format, __func__ , ##args); \
  95. ZFLUSH \
  96. } \
  97. } while(0)
  98. # else
  99. # define zprintf(level, format, ...) do { \
  100. if(_zbar_verbosity >= level) { \
  101. fprintf(stderr, "%s: " format, __func__ , ##__VA_ARGS__); \
  102. ZFLUSH \
  103. } \
  104. } while(0)
  105. # endif
  106. #endif
  107. #endif
  108. static inline int err_copy (void *dst_c,
  109. void *src_c)
  110. {
  111. // errinfo_t *dst = dst_c;
  112. // errinfo_t *src = src_c;
  113. // assert(dst->magic == ERRINFO_MAGIC);
  114. // assert(src->magic == ERRINFO_MAGIC);
  115. //
  116. // dst->errnum = src->errnum;
  117. // dst->sev = src->sev;
  118. // dst->type = src->type;
  119. // dst->func = src->func;
  120. // dst->detail = src->detail;
  121. // dst->arg_str = src->arg_str;
  122. // src->arg_str = NULL; /* unused at src, avoid double free */
  123. // dst->arg_int = src->arg_int;
  124. return(-1);
  125. }
  126. static inline int err_capture (const void *container,
  127. errsev_t sev,
  128. zbar_error_t type,
  129. const char *func,
  130. const char *detail)
  131. {
  132. // errinfo_t *err = (errinfo_t*)container;
  133. // assert(err->magic == ERRINFO_MAGIC);
  134. // if(type == ZBAR_ERR_SYSTEM)
  135. // err->errnum = errno;
  136. //#ifdef _WIN32
  137. // else if(type == ZBAR_ERR_WINAPI)
  138. // err->errnum = GetLastError();
  139. //#endif
  140. // err->sev = sev;
  141. // err->type = type;
  142. // err->func = func;
  143. // err->detail = detail;
  144. // if(_zbar_verbosity >= 1)
  145. // _zbar_error_spew(err, 0);
  146. return(-1);
  147. }
  148. static inline int err_capture_str (const void *container,
  149. errsev_t sev,
  150. zbar_error_t type,
  151. const char *func,
  152. const char *detail,
  153. const char *arg)
  154. {
  155. // errinfo_t *err = (errinfo_t*)container;
  156. // assert(err->magic == ERRINFO_MAGIC);
  157. // if(err->arg_str)
  158. // free(err->arg_str);
  159. // err->arg_str = strdup(arg);
  160. // return(err_capture(container, sev, type, func, detail));
  161. return -1;
  162. }
  163. static inline int err_capture_int (const void *container,
  164. errsev_t sev,
  165. zbar_error_t type,
  166. const char *func,
  167. const char *detail,
  168. int arg)
  169. {
  170. // errinfo_t *err = (errinfo_t*)container;
  171. // assert(err->magic == ERRINFO_MAGIC);
  172. // err->arg_int = arg;
  173. // return(err_capture(container, sev, type, func, detail));
  174. return -1;
  175. }
  176. static inline int err_capture_num (const void *container,
  177. errsev_t sev,
  178. zbar_error_t type,
  179. const char *func,
  180. const char *detail,
  181. int num)
  182. {
  183. // errinfo_t *err = (errinfo_t*)container;
  184. // assert(err->magic == ERRINFO_MAGIC);
  185. // err->errnum = num;
  186. // return(err_capture(container, sev, type, func, detail));
  187. return -1;
  188. }
  189. static inline void err_init (errinfo_t *err,
  190. errmodule_t module)
  191. {
  192. // err->magic = ERRINFO_MAGIC;
  193. // err->module = module;
  194. }
  195. static inline void err_cleanup (errinfo_t *err)
  196. {
  197. // assert(err->magic == ERRINFO_MAGIC);
  198. // if(err->buf) {
  199. // free(err->buf);
  200. // err->buf = NULL;
  201. // }
  202. // if(err->arg_str) {
  203. // free(err->arg_str);
  204. // err->arg_str = NULL;
  205. // }
  206. }
  207. #endif