GS1 Barcode Syntax Tests reference
A reference to the AI component "linter" routines referred to by the GS1 Barcode Syntax Dictionary. Copyright (c) 2022-2024 GS1 AISBL.
lint_pieceoftotal.c File Reference

Purpose

The pieceoftotal linter ensures that the data represents a meaningful piece of total, i.e. a concatenation of non-zero piece number follow by an equal-width, non-zero total number of pieces.

Functional Description

◆ gs1_lint_pieceoftotal()

GS1_SYNTAX_DICTIONARY_API gs1_lint_err_t gs1_lint_pieceoftotal ( const char *const  data,
size_t *const  err_pos,
size_t *const  err_len 
)

Used to ensure that an AI component conforms to a PPTT format, where PP and TT have equal width.

Parameters
[in]dataPointer to the null-terminated data to be linted. Must not be NULL.
[out]err_posTo facilitate error highlighting, the start position of the bad data is written to this pointer, if not NULL.
[out]err_lenThe length of the bad data is written to this pointer, if not NULL.
Returns
GS1_LINTER_OK if okay.
GS1_LINTER_INVALID_LENGTH_FOR_PIECE_OF_TOTAL if the data is not an even length.
GS1_LINTER_NON_DIGIT_CHARACTER if the data contains a non-digit character.
GS1_LINTER_ZERO_PIECE_NUMBER if the data contains a piece number with a zero value.
GS1_LINTER_ZERO_TOTAL_PIECES if the data contains a total piece count with a zero value.
GS1_LINTER_PIECE_NUMBER_EXCEEDS_TOTAL if the data contains a piece number that is larger than the total piece count.
57{
58
59/// \cond
60#define P(i) data[i]
61#define T(i) data[len / 2 + i]
62/// \endcond
63
64 size_t pos, len, i;
65 int pieceiszero, totaliszero, compare;
66
67 assert(data);
68
69 len = strlen(data);
70
71 /*
72 * Data must be a non-zero, even number of characters.
73 *
74 */
75 if (len == 0 || len % 2 != 0)
78 0,
79 len
80 );
81
82 /*
83 * Data must consist of all digits.
84 *
85 */
86 if ((pos = strspn(data, "0123456789")) != len)
89 pos,
90 1
91 );
92
93 /*
94 * Determine whether either the piece number or total piece count is
95 * zero and whether the piece number exceeds the total piece count.
96 *
97 */
98 pieceiszero = totaliszero = 1;
99 compare = 0; /* -1:P<T ; 0:P==T ; 1:P>T */
100 for (i = 0; i < len / 2; i++) {
101 if (pieceiszero && P(i) != '0') pieceiszero = 0;
102 if (totaliszero && T(i) != '0') totaliszero = 0;
103 if (!compare && P(i) != T(i)) compare = P(i) < T(i) ? -1 : 1;
104 }
105
106 /*
107 * Neither piece nor total may be zero.
108 *
109 */
110 if (pieceiszero || totaliszero)
113 pieceiszero ? 0 : len / 2,
114 len / 2
115 );
116
117 /*
118 * The piece number must not exceed the total piece count.
119 *
120 */
121 if (compare == 1)
124 0,
125 len
126 );
127
129
130}
#define GS1_LINTER_RETURN_ERROR(error, position, length)
Return from a linter indicating that a problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:77
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:62
@ GS1_LINTER_PIECE_NUMBER_EXCEEDS_TOTAL
The piece number must not exceed the piece total.
Definition gs1syntaxdictionary.h:121
@ GS1_LINTER_ZERO_PIECE_NUMBER
The piece number must not have a value of zero.
Definition gs1syntaxdictionary.h:119
@ GS1_LINTER_INVALID_LENGTH_FOR_PIECE_OF_TOTAL
The piece with total must have an even length, having equal-length components.
Definition gs1syntaxdictionary.h:118
@ GS1_LINTER_ZERO_TOTAL_PIECES
The piece total must not have a value of zero.
Definition gs1syntaxdictionary.h:120
@ GS1_LINTER_NON_DIGIT_CHARACTER
A non-digit character was found where a digit is expected.
Definition gs1syntaxdictionary.h:78