barcode_utils.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "barcode_utils.h"
  2. BarcodeTypeObj* barcode_type_objs[NUMBER_OF_BARCODE_TYPES] = {NULL};
  3. void init_types() {
  4. BarcodeTypeObj* upc_a = malloc(sizeof(BarcodeTypeObj));
  5. upc_a->name = "UPC-A";
  6. upc_a->type = UPCA;
  7. upc_a->min_digits = 11;
  8. upc_a->max_digits = 12;
  9. upc_a->start_pos = 16;
  10. barcode_type_objs[UPCA] = upc_a;
  11. BarcodeTypeObj* ean_8 = malloc(sizeof(BarcodeTypeObj));
  12. ean_8->name = "EAN-8";
  13. ean_8->type = EAN8;
  14. ean_8->min_digits = 7;
  15. ean_8->max_digits = 8;
  16. ean_8->start_pos = 32;
  17. barcode_type_objs[EAN8] = ean_8;
  18. BarcodeTypeObj* ean_13 = malloc(sizeof(BarcodeTypeObj));
  19. ean_13->name = "EAN-13";
  20. ean_13->type = EAN13;
  21. ean_13->min_digits = 12;
  22. ean_13->max_digits = 13;
  23. ean_13->start_pos = 16;
  24. barcode_type_objs[EAN13] = ean_13;
  25. BarcodeTypeObj* code_39 = malloc(sizeof(BarcodeTypeObj));
  26. code_39->name = "CODE-39";
  27. code_39->type = CODE39;
  28. code_39->min_digits = 1;
  29. code_39->max_digits = -1;
  30. code_39->start_pos = 0;
  31. barcode_type_objs[CODE39] = code_39;
  32. BarcodeTypeObj* code_128 = malloc(sizeof(BarcodeTypeObj));
  33. code_128->name = "CODE-128";
  34. code_128->type = CODE128;
  35. code_128->min_digits = 1;
  36. code_128->max_digits = -1;
  37. code_128->start_pos = 0;
  38. barcode_type_objs[CODE128] = code_128;
  39. BarcodeTypeObj* code_128c = malloc(sizeof(BarcodeTypeObj));
  40. code_128c->name = "CODE-128C";
  41. code_128c->type = CODE128C;
  42. code_128c->min_digits = 2;
  43. code_128c->max_digits = -1;
  44. code_128c->start_pos = 0;
  45. barcode_type_objs[CODE128C] = code_128c;
  46. BarcodeTypeObj* codabar = malloc(sizeof(BarcodeTypeObj));
  47. codabar->name = "Codabar";
  48. codabar->type = CODABAR;
  49. codabar->min_digits = 1;
  50. codabar->max_digits = -1;
  51. codabar->start_pos = 0;
  52. barcode_type_objs[CODABAR] = codabar;
  53. BarcodeTypeObj* unknown = malloc(sizeof(BarcodeTypeObj));
  54. unknown->name = "Unknown";
  55. unknown->type = UNKNOWN;
  56. unknown->min_digits = 0;
  57. unknown->max_digits = 0;
  58. unknown->start_pos = 0;
  59. barcode_type_objs[UNKNOWN] = unknown;
  60. }
  61. void free_types() {
  62. for(int i = 0; i < NUMBER_OF_BARCODE_TYPES; i++) {
  63. free(barcode_type_objs[i]);
  64. }
  65. }
  66. BarcodeTypeObj* get_type(FuriString* type_string) {
  67. if(furi_string_cmp_str(type_string, "UPC-A") == 0) {
  68. return barcode_type_objs[UPCA];
  69. }
  70. if(furi_string_cmp_str(type_string, "EAN-8") == 0) {
  71. return barcode_type_objs[EAN8];
  72. }
  73. if(furi_string_cmp_str(type_string, "EAN-13") == 0) {
  74. return barcode_type_objs[EAN13];
  75. }
  76. if(furi_string_cmp_str(type_string, "CODE-39") == 0) {
  77. return barcode_type_objs[CODE39];
  78. }
  79. if(furi_string_cmp_str(type_string, "CODE-128") == 0) {
  80. return barcode_type_objs[CODE128];
  81. }
  82. if(furi_string_cmp_str(type_string, "CODE-128C") == 0) {
  83. return barcode_type_objs[CODE128C];
  84. }
  85. if(furi_string_cmp_str(type_string, "Codabar") == 0) {
  86. return barcode_type_objs[CODABAR];
  87. }
  88. return barcode_type_objs[UNKNOWN];
  89. }
  90. const char* get_error_code_name(ErrorCode error_code) {
  91. switch(error_code) {
  92. case WrongNumberOfDigits:
  93. return "Wrong # Of Characters";
  94. case InvalidCharacters:
  95. return "Invalid Characters";
  96. case UnsupportedType:
  97. return "Unsupported Type";
  98. case FileOpening:
  99. return "File Opening Error";
  100. case InvalidFileData:
  101. return "Invalid File Data";
  102. case MissingEncodingTable:
  103. return "Missing Encoding Table";
  104. case EncodingTableError:
  105. return "Encoding Table Error";
  106. case OKCode:
  107. return "OK";
  108. default:
  109. return "Unknown Code";
  110. };
  111. }
  112. const char* get_error_code_message(ErrorCode error_code) {
  113. switch(error_code) {
  114. case WrongNumberOfDigits:
  115. return "The barcode has too many or\ntoo few characters.";
  116. case InvalidCharacters:
  117. return "The barcode data has invalid\ncharacters";
  118. case UnsupportedType:
  119. return "This barcode type is not\nsupported by this application";
  120. case FileOpening:
  121. return "The barcode file could not\nbe opened";
  122. case InvalidFileData:
  123. return "File data contains incorrect\ninformation";
  124. case MissingEncodingTable:
  125. return "The encoding table files are\nmissing. Please redownload \nthis app, or consult the \ngithub readme";
  126. case EncodingTableError:
  127. return "Either the characters you\nentered are incorrect or there\nis a problem with the\nencoding table";
  128. case OKCode:
  129. return "OK";
  130. default:
  131. return "Could not read barcode data";
  132. };
  133. }