config.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*------------------------------------------------------------------------
  2. * Copyright 2008-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 <stdlib.h> /* strtol */
  25. #include <string.h> /* strchr, strncmp, strlen */
  26. #include <errno.h>
  27. //#include <assert.h>
  28. #include <zbar.h>
  29. int zbar_parse_config (const char *cfgstr,
  30. zbar_symbol_type_t *sym,
  31. zbar_config_t *cfg,
  32. int *val)
  33. {
  34. if(!cfgstr)
  35. return(1);
  36. const char *dot = strchr(cfgstr, '.');
  37. if(dot) {
  38. int len = dot - cfgstr;
  39. if(!len || (len == 1 && !strncmp(cfgstr, "*", len)))
  40. *sym = 0;
  41. else if(len < 2)
  42. return(1);
  43. else if(!strncmp(cfgstr, "qrcode", len))
  44. *sym = ZBAR_QRCODE;
  45. else if(len < 3)
  46. return(1);
  47. else if(!strncmp(cfgstr, "upca", len))
  48. *sym = ZBAR_UPCA;
  49. else if(!strncmp(cfgstr, "upce", len))
  50. *sym = ZBAR_UPCE;
  51. else if(!strncmp(cfgstr, "ean13", len))
  52. *sym = ZBAR_EAN13;
  53. else if(!strncmp(cfgstr, "ean8", len))
  54. *sym = ZBAR_EAN8;
  55. else if(!strncmp(cfgstr, "i25", len))
  56. *sym = ZBAR_I25;
  57. else if(len < 4)
  58. return(1);
  59. else if(!strncmp(cfgstr, "scanner", len))
  60. *sym = ZBAR_PARTIAL; /* FIXME lame */
  61. else if(!strncmp(cfgstr, "isbn13", len))
  62. *sym = ZBAR_ISBN13;
  63. else if(!strncmp(cfgstr, "isbn10", len))
  64. *sym = ZBAR_ISBN10;
  65. #if 0
  66. /* FIXME addons are configured per-main symbol type */
  67. else if(!strncmp(cfgstr, "addon2", len))
  68. *sym = ZBAR_ADDON2;
  69. else if(!strncmp(cfgstr, "addon5", len))
  70. *sym = ZBAR_ADDON5;
  71. #endif
  72. else if(len < 6)
  73. return(1);
  74. else if(!strncmp(cfgstr, "code39", len))
  75. *sym = ZBAR_CODE39;
  76. else if(!strncmp(cfgstr, "pdf417", len))
  77. *sym = ZBAR_PDF417;
  78. else if(len < 7)
  79. return(1);
  80. else if(!strncmp(cfgstr, "code128", len))
  81. *sym = ZBAR_CODE128;
  82. else
  83. return(1);
  84. cfgstr = dot + 1;
  85. }
  86. else
  87. *sym = 0;
  88. int len = strlen(cfgstr);
  89. const char *eq = strchr(cfgstr, '=');
  90. if(eq)
  91. len = eq - cfgstr;
  92. else
  93. *val = 1; /* handle this here so we can override later */
  94. char negate = 0;
  95. if(len > 3 && !strncmp(cfgstr, "no-", 3)) {
  96. negate = 1;
  97. cfgstr += 3;
  98. len -= 3;
  99. }
  100. if(len < 1)
  101. return(1);
  102. else if(!strncmp(cfgstr, "y-density", len))
  103. *cfg = ZBAR_CFG_Y_DENSITY;
  104. else if(!strncmp(cfgstr, "x-density", len))
  105. *cfg = ZBAR_CFG_X_DENSITY;
  106. else if(len < 2)
  107. return(1);
  108. else if(!strncmp(cfgstr, "enable", len))
  109. *cfg = ZBAR_CFG_ENABLE;
  110. else if(len < 3)
  111. return(1);
  112. else if(!strncmp(cfgstr, "disable", len)) {
  113. *cfg = ZBAR_CFG_ENABLE;
  114. negate = !negate; /* no-disable ?!? */
  115. }
  116. else if(!strncmp(cfgstr, "min-length", len))
  117. *cfg = ZBAR_CFG_MIN_LEN;
  118. else if(!strncmp(cfgstr, "max-length", len))
  119. *cfg = ZBAR_CFG_MAX_LEN;
  120. else if(!strncmp(cfgstr, "ascii", len))
  121. *cfg = ZBAR_CFG_ASCII;
  122. else if(!strncmp(cfgstr, "add-check", len))
  123. *cfg = ZBAR_CFG_ADD_CHECK;
  124. else if(!strncmp(cfgstr, "emit-check", len))
  125. *cfg = ZBAR_CFG_EMIT_CHECK;
  126. else if(!strncmp(cfgstr, "position", len))
  127. *cfg = ZBAR_CFG_POSITION;
  128. else
  129. return(1);
  130. if(eq) {
  131. errno = 0;
  132. *val = strtol(eq + 1, NULL, 0);
  133. if(errno)
  134. return(1);
  135. }
  136. if(negate)
  137. *val = !*val;
  138. return(0);
  139. }