barcode_utils.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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* unknown = malloc(sizeof(BarcodeTypeObj));
  40. unknown->name = "Unknown";
  41. unknown->type = UNKNOWN;
  42. unknown->min_digits = 0;
  43. unknown->max_digits = 0;
  44. unknown->start_pos = 0;
  45. barcode_type_objs[UNKNOWN] = unknown;
  46. }
  47. void free_types() {
  48. for(int i = 0; i < NUMBER_OF_BARCODE_TYPES; i++) {
  49. free(barcode_type_objs[i]);
  50. }
  51. }
  52. BarcodeTypeObj* get_type(FuriString* type_string) {
  53. if(furi_string_cmp_str(type_string, "UPC-A") == 0) {
  54. return barcode_type_objs[UPCA];
  55. }
  56. if(furi_string_cmp_str(type_string, "EAN-8") == 0) {
  57. return barcode_type_objs[EAN8];
  58. }
  59. if(furi_string_cmp_str(type_string, "EAN-13") == 0) {
  60. return barcode_type_objs[EAN13];
  61. }
  62. if(furi_string_cmp_str(type_string, "CODE-39") == 0) {
  63. return barcode_type_objs[CODE39];
  64. }
  65. if(furi_string_cmp_str(type_string, "CODE-128") == 0) {
  66. return barcode_type_objs[CODE128];
  67. }
  68. return barcode_type_objs[UNKNOWN];
  69. }
  70. const char* get_error_code_name(ErrorCode error_code) {
  71. switch(error_code) {
  72. case WrongNumberOfDigits:
  73. return "Wrong Number Of Digits";
  74. case InvalidCharacters:
  75. return "Invalid Characters";
  76. case UnsupportedType:
  77. return "Unsupported Type";
  78. case FileOpening:
  79. return "File Opening Error";
  80. case InvalidFileData:
  81. return "Invalid File Data";
  82. case MissingEncodingTable:
  83. return "Missing Encoding Table";
  84. case EncodingTableError:
  85. return "Encoding Table Error";
  86. case OKCode:
  87. return "OK";
  88. default:
  89. return "Unknown Code";
  90. };
  91. }
  92. const char* get_error_code_message(ErrorCode error_code) {
  93. switch(error_code) {
  94. case WrongNumberOfDigits:
  95. return "Wrong # of characters";
  96. case InvalidCharacters:
  97. return "Invalid characters";
  98. case UnsupportedType:
  99. return "Unsupported barcode type";
  100. case FileOpening:
  101. return "Could not open file";
  102. case InvalidFileData:
  103. return "Invalid file data";
  104. case MissingEncodingTable:
  105. return "Missing encoding table";
  106. case EncodingTableError:
  107. return "Encoding table error";
  108. case OKCode:
  109. return "OK";
  110. default:
  111. return "Could not read barcode data";
  112. };
  113. }