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