-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgs1-parse.cpp
More file actions
253 lines (205 loc) · 5.66 KB
/
gs1-parse.cpp
File metadata and controls
253 lines (205 loc) · 5.66 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
MIT License
Copyright (c) 2021 Serge Reinov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gs1-parse.h"
#ifdef WIN32
#pragma warning(disable:4996) //about unsafe sprintf
#endif
namespace GS1
{
bool isstrdigit(const char *ptr, int digits_count)
{
while(digits_count > 0)
{
if (!*ptr || !isdigit(*ptr))
return false;
ptr++;
digits_count--;
}
return true;
}
bool FieldAI::format_body(std::string &result, bool ISO_date) const
{
if (!ai || text_body.empty())
return false;
switch(ai->data_format)
{
default:
case GS1_STRING:
result = text_body;
break;
case GS1_DATE:
{
//YYMMDD
if (!isstrdigit(text_body.c_str(), 6))
return false; //error format
int yy = 2000 + atoi(text_body.substr(0, 2).c_str());
int mm = atoi(text_body.substr(2, 2).c_str());
int dd = atoi(text_body.substr(4, 2).c_str());
if ((mm < 1) || (mm > 12) ||
(dd < 1) || (dd > 31))
return false; //err fmt
char buf[32];
if (ISO_date)
sprintf(buf, "%4.4u-%2.2u-%2.2u", yy, mm, dd); //YYYY-MM-DD
else
sprintf(buf, "%2.2u-%2.2u-%4.4u", dd, mm, yy); //DD-MM-YYYY
result = buf;
}
break;
case GS1_DECIMAL:
{
if (!isstrdigit(text_body.c_str(), text_body.length()))
return false; //error format
int ai_len = strlen(ai->ai);
if (ai->ai[ai_len-1] != 'n')
return false;
int decimal_point = (int)(*text_ai.rbegin() - '0');
int body_len = text_body.length();
int pos = body_len - decimal_point;
if (pos <= 0)
{
result = "0.";
while(pos++ < 0)
result.push_back('0');
result += text_body;
break; //ok
}
result = text_body;
if (decimal_point > 0)
result.insert(pos, ".");
while((result[0] == '0') && (result[1] != '.'))
result.erase(0, 1);
}
break;
}
return true;
}
bool FieldsGS1::ParseGS1(const char *gs1_stream)
{
//clear
ai_map.clear();
fields.clear();
bool ok = parse_gs1(fields, gs1_stream);
if (ok)
{
for(int i=0; i<(int)fields.size(); i++)
ai_map[fields[i].ai->ai] = i;
}
return ok;
}
int FieldsGS1::GetCount()
{
return (int)fields.size();
}
const FieldAI* FieldsGS1::Get(int index)
{
if (index < (int)fields.size())
return &fields[index];
return 0;
}
const FieldAI* FieldsGS1::GetByAI(const std::string &ai)
{
std::map<std::string, int>::const_iterator it = ai_map.find(ai);
if (it != ai_map.end())
return &fields[it->second];
return 0;
}
const char * copy_gs1_body(std::string &body, int min_len, const char *ptr, const char *end)
{
body.clear();
int len = end - ptr;
if (len < min_len)
return 0;
body.reserve(len+1);
while(*ptr && (ptr < end) && (*ptr != GS1::FNC1))
{
body.push_back(*ptr);
ptr++;
}
while(*ptr && (ptr < end) && (*ptr == GS1::FNC1))
ptr++; //skip FNC1
if ((int)body.length() < min_len)
return 0;
return ptr;
}
bool parse_gs1(std::vector<FieldAI> &result, const char *gs1_stream, ErrorAI &error)
{
result.clear();
const char *ptr = gs1_stream;
int len = strlen(gs1_stream);
const char *end = ptr + len;
while(ptr < end)
{
error.reset(ptr - gs1_stream);
const AI* ai = get_ai(ptr);
if (!ai)
{
error.code = ErrorAI::UnknownAI;
break; //unknown ai
}
const char *start = ptr;
FieldAI f;
f.ai = ai;
f.reference_pos = start - gs1_stream;
int ai_len = strlen(f.ai->ai);
f.text_ai = std::string(ptr, ai_len);
ptr += ai_len;
error.ai = f.ai;
error.text_ai = f.text_ai;
int body_len = f.ai->field_len - ai_len;
if (body_len <= 0)
{
error.code = ErrorAI::InvalidSpecLen;
return false; //invalid format
}
const char *next;
if (f.ai->max_field_len_optional > 0)
{
//vari len
int body_len_max = f.ai->max_field_len_optional - ai_len;
if (body_len_max <= body_len)
{
error.code = ErrorAI::InvalidSpecMaxLen;
return false; //invalid format
}
next = ptr + body_len_max;
}
else
{
//const len
next = ptr + body_len;
}
if (next > end)
next = end;
next = copy_gs1_body(f.text_body, body_len, ptr, next);
if (!next)
{
error.code = f.text_body.empty() ? ErrorAI::EmptyBody : ErrorAI::BodyTooShort;
return false; //can't get body (wrong min_len-param or body too short)
}
while(*next && (next < end) && (*next == GS1::FNC1))
next++; //skip FNC1
ptr = next;
f.reference_len = ptr - start;
result.push_back(f);
}
return (ptr == end);
}
}