Tidy up check digit function common to 2 of 5 codes.

Thanks to Ismael Luceno.
This commit is contained in:
Robin Stuart 2011-03-08 09:58:48 +00:00
parent b30f8e110f
commit 30f5b996bf

View File

@ -36,7 +36,11 @@ static char *C25IndustTable[10] = {"1111313111", "3111111131", "1131111131", "31
static char *C25InterTable[10] = {"11331", "31113", "13113", "33111", "11313", "31311", "13311", "11133", static char *C25InterTable[10] = {"11331", "31113", "13113", "33111", "11313", "31311", "13311", "11133",
"31131", "13131"}; "31131", "13131"};
static inline char check_digit(unsigned int count)
{
return itoc((10 - (count % 10)) % 10);
}
int matrix_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) int matrix_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length)
{ /* Code 2 of 5 Standard (Code 2 of 5 Matrix) */ { /* Code 2 of 5 Standard (Code 2 of 5 Matrix) */
@ -235,7 +239,7 @@ int interleaved_two_of_five(struct zint_symbol *symbol, unsigned char source[],
int itf14(struct zint_symbol *symbol, unsigned char source[], int length) int itf14(struct zint_symbol *symbol, unsigned char source[], int length)
{ {
int i, error_number, zeroes; int i, error_number, zeroes;
unsigned int count, check_digit; unsigned int count;
char localstr[16]; char localstr[16];
error_number = 0; error_number = 0;
@ -269,9 +273,7 @@ int itf14(struct zint_symbol *symbol, unsigned char source[], int length)
count += 2 * ctoi(localstr[i]); count += 2 * ctoi(localstr[i]);
} }
} }
check_digit = 10 - (count%10); localstr[13] = check_digit(count);
if (check_digit == 10) { check_digit = 0; }
localstr[13] = itoc(check_digit);
localstr[14] = '\0'; localstr[14] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr)); error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr));
ustrcpy(symbol->text, (unsigned char*)localstr); ustrcpy(symbol->text, (unsigned char*)localstr);
@ -281,7 +283,7 @@ int itf14(struct zint_symbol *symbol, unsigned char source[], int length)
int dpleit(struct zint_symbol *symbol, unsigned char source[], int length) int dpleit(struct zint_symbol *symbol, unsigned char source[], int length)
{ /* Deutshe Post Leitcode */ { /* Deutshe Post Leitcode */
int i, error_number; int i, error_number;
unsigned int count, check_digit; unsigned int count;
char localstr[16]; char localstr[16];
int zeroes; int zeroes;
@ -310,9 +312,7 @@ int dpleit(struct zint_symbol *symbol, unsigned char source[], int length)
count += 5 * ctoi(localstr[i]); count += 5 * ctoi(localstr[i]);
} }
} }
check_digit = 10 - (count%10); localstr[13] = check_digit(count);
if (check_digit == 10) { check_digit = 0; }
localstr[13] = itoc(check_digit);
localstr[14] = '\0'; localstr[14] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr)); error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr));
ustrcpy(symbol->text, (unsigned char*)localstr); ustrcpy(symbol->text, (unsigned char*)localstr);
@ -322,7 +322,7 @@ int dpleit(struct zint_symbol *symbol, unsigned char source[], int length)
int dpident(struct zint_symbol *symbol, unsigned char source[], int length) int dpident(struct zint_symbol *symbol, unsigned char source[], int length)
{ /* Deutsche Post Identcode */ { /* Deutsche Post Identcode */
int i, error_number, zeroes; int i, error_number, zeroes;
unsigned int count, check_digit; unsigned int count;
char localstr[16]; char localstr[16];
count = 0; count = 0;
@ -349,9 +349,7 @@ int dpident(struct zint_symbol *symbol, unsigned char source[], int length)
count += 5 * ctoi(localstr[i]); count += 5 * ctoi(localstr[i]);
} }
} }
check_digit = 10 - (count%10); localstr[11] = check_digit(count);
if (check_digit == 10) { check_digit = 0; }
localstr[11] = itoc(check_digit);
localstr[12] = '\0'; localstr[12] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr)); error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr));
ustrcpy(symbol->text, (unsigned char*)localstr); ustrcpy(symbol->text, (unsigned char*)localstr);