symbol.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include <config.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. //#include <assert.h>
  27. #include <zbar.h>
  28. #include "symbol.h"
  29. const char *zbar_get_symbol_name (zbar_symbol_type_t sym)
  30. {
  31. switch(sym & ZBAR_SYMBOL) {
  32. case ZBAR_EAN8: return("EAN-8");
  33. case ZBAR_UPCE: return("UPC-E");
  34. case ZBAR_ISBN10: return("ISBN-10");
  35. case ZBAR_UPCA: return("UPC-A");
  36. case ZBAR_EAN13: return("EAN-13");
  37. case ZBAR_ISBN13: return("ISBN-13");
  38. case ZBAR_I25: return("I2/5");
  39. case ZBAR_CODE39: return("CODE-39");
  40. case ZBAR_CODE128: return("CODE-128");
  41. case ZBAR_PDF417: return("PDF417");
  42. case ZBAR_QRCODE: return("QR-Code");
  43. default: return("UNKNOWN");
  44. }
  45. }
  46. const char *zbar_get_addon_name (zbar_symbol_type_t sym)
  47. {
  48. switch(sym & ZBAR_ADDON) {
  49. case ZBAR_ADDON2: return("+2");
  50. case ZBAR_ADDON5: return("+5");
  51. default: return("");
  52. }
  53. }
  54. void _zbar_symbol_free (zbar_symbol_t *sym)
  55. {
  56. if(sym->syms) {
  57. zbar_symbol_set_ref(sym->syms, -1);
  58. sym->syms = NULL;
  59. }
  60. if(sym->pts)
  61. free(sym->pts);
  62. if(sym->data_alloc && sym->data)
  63. free(sym->data);
  64. free(sym);
  65. }
  66. void zbar_symbol_ref (const zbar_symbol_t *sym,
  67. int refs)
  68. {
  69. zbar_symbol_t *ncsym = (zbar_symbol_t*)sym;
  70. _zbar_symbol_refcnt(ncsym, refs);
  71. }
  72. zbar_symbol_type_t zbar_symbol_get_type (const zbar_symbol_t *sym)
  73. {
  74. return(sym->type);
  75. }
  76. const char *zbar_symbol_get_data (const zbar_symbol_t *sym)
  77. {
  78. return(sym->data);
  79. }
  80. unsigned int zbar_symbol_get_data_length (const zbar_symbol_t *sym)
  81. {
  82. return(sym->datalen);
  83. }
  84. int zbar_symbol_get_count (const zbar_symbol_t *sym)
  85. {
  86. return(sym->cache_count);
  87. }
  88. int zbar_symbol_get_quality (const zbar_symbol_t *sym)
  89. {
  90. return(sym->quality);
  91. }
  92. unsigned zbar_symbol_get_loc_size (const zbar_symbol_t *sym)
  93. {
  94. return(sym->npts);
  95. }
  96. int zbar_symbol_get_loc_x (const zbar_symbol_t *sym,
  97. unsigned idx)
  98. {
  99. if(idx < sym->npts)
  100. return(sym->pts[idx].x);
  101. else
  102. return(-1);
  103. }
  104. int zbar_symbol_get_loc_y (const zbar_symbol_t *sym,
  105. unsigned idx)
  106. {
  107. if(idx < sym->npts)
  108. return(sym->pts[idx].y);
  109. else
  110. return(-1);
  111. }
  112. const zbar_symbol_t *zbar_symbol_next (const zbar_symbol_t *sym)
  113. {
  114. return((sym) ? sym->next : NULL);
  115. }
  116. const zbar_symbol_set_t*
  117. zbar_symbol_get_components (const zbar_symbol_t *sym)
  118. {
  119. return(sym->syms);
  120. }
  121. const zbar_symbol_t *zbar_symbol_first_component (const zbar_symbol_t *sym)
  122. {
  123. return((sym && sym->syms) ? sym->syms->head : NULL);
  124. }
  125. static const char *xmlfmt[] = {
  126. "<symbol type='%s' quality='%d'",
  127. " count='%d'",
  128. "><data><![CDATA[",
  129. "]]></data></symbol>",
  130. };
  131. /* FIXME suspect... */
  132. #define MAX_INT_DIGITS 10
  133. char *zbar_symbol_xml (const zbar_symbol_t *sym,
  134. char **buf,
  135. unsigned *len)
  136. {
  137. const char *type = zbar_get_symbol_name(sym->type);
  138. /* FIXME binary data */
  139. unsigned datalen = strlen(sym->data);
  140. unsigned maxlen = (strlen(xmlfmt[0]) + strlen(xmlfmt[1]) +
  141. strlen(xmlfmt[2]) + strlen(xmlfmt[3]) +
  142. strlen(type) + datalen + MAX_INT_DIGITS + 1);
  143. if(!*buf || (*len < maxlen)) {
  144. if(*buf)
  145. free(*buf);
  146. *buf = malloc(maxlen);
  147. /* FIXME check OOM */
  148. *len = maxlen;
  149. }
  150. int n = snprintf(*buf, maxlen, xmlfmt[0], type, sym->quality);
  151. assert(n > 0);
  152. assert(n <= maxlen);
  153. if(sym->cache_count) {
  154. int i = snprintf(*buf + n, maxlen - n, xmlfmt[1], sym->cache_count);
  155. assert(i > 0);
  156. n += i;
  157. assert(n <= maxlen);
  158. }
  159. int i = strlen(xmlfmt[2]);
  160. memcpy(*buf + n, xmlfmt[2], i + 1);
  161. n += i;
  162. assert(n <= maxlen);
  163. /* FIXME binary data */
  164. /* FIXME handle "]]>" */
  165. strncpy(*buf + n, sym->data, datalen + 1);
  166. n += datalen;
  167. assert(n <= maxlen);
  168. i = strlen(xmlfmt[3]);
  169. memcpy(*buf + n, xmlfmt[3], i + 1);
  170. n += i;
  171. assert(n <= maxlen);
  172. *len = n;
  173. return(*buf);
  174. }
  175. zbar_symbol_set_t *_zbar_symbol_set_create ()
  176. {
  177. zbar_symbol_set_t *syms = calloc(1, sizeof(*syms));
  178. _zbar_refcnt(&syms->refcnt, 1);
  179. return(syms);
  180. }
  181. inline void _zbar_symbol_set_free (zbar_symbol_set_t *syms)
  182. {
  183. zbar_symbol_t *sym, *next;
  184. for(sym = syms->head; sym; sym = next) {
  185. next = sym->next;
  186. sym->next = NULL;
  187. _zbar_symbol_refcnt(sym, -1);
  188. }
  189. syms->head = NULL;
  190. free(syms);
  191. }
  192. void zbar_symbol_set_ref (const zbar_symbol_set_t *syms,
  193. int delta)
  194. {
  195. zbar_symbol_set_t *ncsyms = (zbar_symbol_set_t*)syms;
  196. if(!_zbar_refcnt(&ncsyms->refcnt, delta) && delta <= 0)
  197. _zbar_symbol_set_free(ncsyms);
  198. }
  199. int zbar_symbol_set_get_size (const zbar_symbol_set_t *syms)
  200. {
  201. return(syms->nsyms);
  202. }
  203. const zbar_symbol_t*
  204. zbar_symbol_set_first_symbol (const zbar_symbol_set_t *syms)
  205. {
  206. zbar_symbol_t *sym = syms->tail;
  207. if(sym)
  208. return(sym->next);
  209. return(syms->head);
  210. }