1
0
mirror of https://git.code.sf.net/p/zint/code synced 2025-12-21 11:57:04 +00:00

updated form master

some changes in batch_process
This commit is contained in:
tgotic
2011-01-17 19:58:20 +01:00
parent b4996f00b4
commit 4baf4bef73

View File

@@ -1,40 +1,41 @@
/* main.c - Command line handling routines for Zint */
/*
libzint - the open source barcode library
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
libzint - the open source barcode library
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
#include <errno.h>
#ifndef WIN32
#include <getopt.h>
#include <zint.h>
#else
#include "getopt.h"
#include "zint.h"
#endif
#define NESET "0123456789"
#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#include <malloc.h>
#endif
#define NESET "0123456789"
void types(void) {
printf( " 1: Code 11 51: Pharma One-Track 90: KIX Code\n"
@@ -88,9 +89,12 @@ void usage(void)
" --fg=COLOUR Specify a foreground colour.\n"
" --bg=COLOUR Specify a background colour.\n"
" --scale=NUMBER Adjust size of output image.\n"
#ifndef NO_PNG
" --directpng Send PNG output to stdout\n"
#endif
" --directeps Send EPS output to stdout\n"
" --directsvg Send SVG output to stdout\n"
" --dump Dump binary data to stdout\n"
" --rotate=NUMBER Rotate symbol (PNG output only).\n"
" --cols=NUMBER (PDF417) Number of columns.\n"
" --vers=NUMBER (QR Code) Version\n"
@@ -101,24 +105,49 @@ void usage(void)
" --binary Treat input as Binary data\n"
" --notext Remove human readable text\n"
" --square Force Data Matrix symbols to be square\n"
" --init Create reader initialisation symbol (Code 128)\n"
" --smalltext Use half-size text in PNG images\n"
" --batch Treat each line of input as a separate data set\n"
, ZINT_VERSION);
}
int validator(char test_string[], char source[])
{ /* Verifies that a string only uses valid characters */
unsigned int i, j, latch;
int validator(char test_string[], char source[]) {
/* Verifies that a string only uses valid characters */
int latch;
size_t i, j, cchSource, cchTest;
cchSource = strlen(source);
cchTest = strlen(test_string);
for(i = 0; i < strlen(source); i++) {
for(i = 0; i < cchSource; i++) {
latch = 0;
for(j = 0; j < strlen(test_string); j++) {
if (source[i] == test_string[j]) { latch = 1; } }
if (!(latch)) {
return ERROR_INVALID_DATA; }
for(j = 0; j < cchTest; j++) {
if (source[i] == test_string[j])
latch = 1;
}
if (!latch)
return ZERROR_INVALID_DATA;
}
return 0;
}
#ifndef WIN32
char* _strrev(char* string) {
if (string) {
char tmp;
char* pos1 = string;
char* pos2 = string + strlen(string) - 1;
while (pos1 < pos2) {
tmp = *pos1;
*pos1++ = *pos2;
*pos2-- = tmp;
}
}
return string;
}
#endif
int escape_char_process(struct zint_symbol *my_symbol, unsigned char input_string[], int length)
{
int error_number;
@@ -164,6 +193,118 @@ int escape_char_process(struct zint_symbol *my_symbol, unsigned char input_strin
return error_number;
}
int batch_process(struct zint_symbol *symbol, char *filename)
{
FILE *file;
#define BUFFER_SIZE 7100
char buffer[BUFFER_SIZE];
int inpos, error_number = 0;
char output_file[FILENAME_MAX];
char format_string[FILENAME_MAX];
char reverse_number[12];
unsigned int line_count = 1;
char format_char;
size_t format_len, posn, i;
char adjusted[2] = { 0 };
if(symbol->outfile[0] == '\0') {
strcpy(format_string, "$$$$$.png");
} else {
if(strlen(symbol->outfile) < FILENAME_MAX) {
strcpy(format_string, symbol->outfile);
} else {
strcpy(symbol->errtxt, "Format string too long");
return ZERROR_INVALID_DATA;
}
}
if(!strcmp(filename, "-")) {
file = stdin;
#ifdef WIN32
if (-1 == _setmode(_fileno(stdin), _O_BINARY)) {
strcpy(symbol->errtxt, "Can't open stdin for binary");
return ZERROR_FILE_ACCESS;
}
#endif
} else {
file = fopen(filename, "rb");
}
if (!file) {
strcpy(symbol->errtxt, "Unable to read input file");
return ZERROR_INVALID_DATA;
}
format_len = strlen(format_string);
while (!feof(file) && (line_count < (unsigned int)(-1))) {
if (!fgets(buffer, BUFFER_SIZE, file) && ferror(file)) {
fprintf(stderr, "On line %d: error %s\n", line_count, strerror(errno));
break;
}
posn = strlen(buffer);
if (buffer[posn - 2] == '\r' && buffer[posn - 1] == '\n') {
buffer[posn - 2] = '\n';
buffer[posn - 1] = '\0';
posn--;
}
else if (buffer[posn - 1] != '\n')
{
fprintf(stderr, "On line %d: Input data too long\n", line_count);
break;
}
inpos = sprintf(reverse_number, "%u", line_count);
_strrev(reverse_number);
output_file[0] = '\0';
for(i = format_len; i > 0; i--) {
format_char = format_string[i - 1];
switch(format_char) {
case '#':
if (inpos > 0) {
adjusted[0] = reverse_number[inpos - 1];
inpos--;
} else {
adjusted[0] = ' ';
}
break;
case '$':
if (inpos > 0) {
adjusted[0] = reverse_number[inpos - 1];
inpos--;
} else {
adjusted[0] = '0';
}
break;
case '*':
if (inpos > 0) {
adjusted[0] = reverse_number[inpos - 1];
inpos--;
} else {
adjusted[0] = '*';
}
break;
default:
adjusted[0] = format_string[i - 1];
break;
}
strcat(output_file, adjusted);
}
_strrev(output_file);
strcpy(symbol->outfile, output_file);
error_number = ZBarcode_Encode_and_Print(symbol, (unsigned char*)buffer, posn, 0);
if(error_number != 0) {
fprintf(stderr, "On line %d: %s\n", line_count, symbol->errtxt);
}
ZBarcode_Clear(symbol);
line_count++;
}
if(strcmp(filename, "-"))
fclose(file);
return error_number;
}
int main(int argc, char **argv)
{
struct zint_symbol *my_symbol;
@@ -171,12 +312,14 @@ int main(int argc, char **argv)
int error_number;
int rotate_angle;
int generated;
int batch_mode;
error_number = 0;
rotate_angle = 0;
generated = 0;
my_symbol = ZBarcode_Create();
my_symbol->input_mode = UNICODE_MODE;
batch_mode = 0;
if(argc == 1) {
usage();
@@ -193,6 +336,7 @@ int main(int argc, char **argv)
{"directeps", 0, 0, 0},
{"directpng", 0, 0, 0},
{"directsvg", 0, 0, 0},
{"dump", 0, 0, 0},
{"barcode", 1, 0, 'b'},
{"height", 1, 0, 0},
{"whitesp", 1, 0, 'w'},
@@ -216,6 +360,9 @@ int main(int argc, char **argv)
{"binary", 0, 0, 0},
{"notext", 0, 0, 0},
{"square", 0, 0, 0},
{"init", 0, 0, 0},
{"smalltext", 0, 0, 0},
{"batch", 0, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "htb:w:d:o:i:rcmp", long_options, &option_index);
@@ -229,18 +376,30 @@ int main(int argc, char **argv)
if(!strcmp(long_options[option_index].name, "box")) {
my_symbol->output_options += BARCODE_BOX;
}
if(!strcmp(long_options[option_index].name, "init")) {
my_symbol->output_options += READER_INIT;
}
if(!strcmp(long_options[option_index].name, "smalltext")) {
my_symbol->output_options += SMALL_TEXT;
}
if(!strcmp(long_options[option_index].name, "directeps")) {
my_symbol->output_options += BARCODE_STDOUT;
strncpy(my_symbol->outfile, "dummy.eps", 10);
}
#ifndef NO_PNG
if(!strcmp(long_options[option_index].name, "directpng")) {
my_symbol->output_options += BARCODE_STDOUT;
strncpy(my_symbol->outfile, "dummy.png", 10);
}
#endif
if(!strcmp(long_options[option_index].name, "directsvg")) {
my_symbol->output_options += BARCODE_STDOUT;
strncpy(my_symbol->outfile, "dummy.svg", 10);
}
if(!strcmp(long_options[option_index].name, "dump")) {
my_symbol->output_options += BARCODE_STDOUT;
strncpy(my_symbol->outfile, "dummy.txt", 10);
}
if(!strcmp(long_options[option_index].name, "gs1")) {
my_symbol->input_mode = GS1_MODE;
}
@@ -275,7 +434,7 @@ int main(int argc, char **argv)
}
if(!strcmp(long_options[option_index].name, "border")) {
error_number = validator(NESET, optarg);
if(error_number == ERROR_INVALID_DATA) {
if(error_number == ZERROR_INVALID_DATA) {
fprintf(stderr, "Invalid border width\n");
exit(1);
}
@@ -287,7 +446,7 @@ int main(int argc, char **argv)
}
if(!strcmp(long_options[option_index].name, "height")) {
error_number = validator(NESET, optarg);
if(error_number == ERROR_INVALID_DATA) {
if(error_number == ZERROR_INVALID_DATA) {
fprintf(stderr, "Invalid symbol height\n");
exit(1);
}
@@ -336,7 +495,7 @@ int main(int argc, char **argv)
if(!strcmp(long_options[option_index].name, "rotate")) {
/* Only certain inputs allowed */
error_number = validator(NESET, optarg);
if(error_number == ERROR_INVALID_DATA) {
if(error_number == ZERROR_INVALID_DATA) {
fprintf(stderr, "Invalid rotation parameter\n");
exit(1);
}
@@ -347,6 +506,10 @@ int main(int argc, char **argv)
default: rotate_angle = 0; break;
}
}
if(!strcmp(long_options[option_index].name, "batch")) {
/* Switch to batch processing mode */
batch_mode = 1;
}
break;
case 'h':
@@ -359,7 +522,7 @@ int main(int argc, char **argv)
case 'b':
error_number = validator(NESET, optarg);
if(error_number == ERROR_INVALID_DATA) {
if(error_number == ZERROR_INVALID_DATA) {
fprintf(stderr, "Invalid barcode type\n");
exit(1);
}
@@ -368,7 +531,7 @@ int main(int argc, char **argv)
case 'w':
error_number = validator(NESET, optarg);
if(error_number == ERROR_INVALID_DATA) {
if(error_number == ZERROR_INVALID_DATA) {
fprintf(stderr, "Invalid whitespace value\n");
exit(1);
}
@@ -380,6 +543,7 @@ int main(int argc, char **argv)
break;
case 'd': /* we have some data! */
if(batch_mode == 0) {
error_number = escape_char_process(my_symbol, (unsigned char*)optarg, strlen(optarg));
if(error_number == 0) {
error_number = ZBarcode_Print(my_symbol, rotate_angle);
@@ -390,9 +554,13 @@ int main(int argc, char **argv)
ZBarcode_Delete(my_symbol);
return 1;
}
} else {
fprintf(stderr, "Cannot define data in batch mode");
}
break;
case 'i': /* Take data from file */
if(batch_mode == 0) {
error_number = ZBarcode_Encode_File(my_symbol, optarg);
if(error_number == 0) {
error_number = ZBarcode_Print(my_symbol, rotate_angle);
@@ -403,10 +571,20 @@ int main(int argc, char **argv)
ZBarcode_Delete(my_symbol);
return 1;
}
} else {
/* Take each line of text as a separate data set */
error_number = batch_process(my_symbol, optarg);
generated = 1;
if(error_number != 0) {
fprintf(stderr, "%s\n", my_symbol->errtxt);
ZBarcode_Delete(my_symbol);
return 1;
}
}
break;
case 'o':
strncpy(my_symbol->outfile, optarg, 250);
strncpy(my_symbol->outfile, optarg, FILENAME_MAX - 1);
break;
case 'r':