mirror of
https://github.com/zint/zint
synced 2024-11-16 20:57:25 +13:00
340 lines
10 KiB
C
340 lines
10 KiB
C
/* main.c - Command line handling routines for Zint */
|
|
|
|
/*
|
|
libzint - the open source barcode library
|
|
Copyright (C) 2008 Robin Stuart <zint@hotmail.co.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 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.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <getopt.h>
|
|
#include <zint.h>
|
|
|
|
#define NESET "0123456789"
|
|
|
|
void types(void) {
|
|
printf( " 1: Code 11 40: Postnet 80: Databar-14 Stack Omni\n"
|
|
" 2: Standard 2 of 5 47: MSI Plessey 81: Databar Extended Stack\n"
|
|
" 3: Interleaved 2 of 5 49: FIM 82: Planet\n"
|
|
" 4: IATA 2 of 5 50: Logmars 84: MicroPDF\n"
|
|
" 6: Data Logic 51: Pharma One-Track 85: USPS OneCode\n"
|
|
" 7: Industrial 2 of 5 52: PZN 86: UK Plessey\n"
|
|
" 8: Code 39 53: Pharma Two-Track 87: Telepen Numeric\n"
|
|
" 9: Extended Code 39 55: PDF417 89: ITF-14\n"
|
|
" 13: EAN 56: PDF417 Trunc 90: KIX Code\n"
|
|
" 16: GS1-128 57: Maxicode 92: Aztec Code\n"
|
|
" 18: Codabar 58: QR Code 93: DAFT Code\n"
|
|
" 20: Code 128 60: Code 128-B 129: Code 23\n"
|
|
" 21: Leitcode 63: AP Standard Customer 130: Comp + EAN\n"
|
|
" 22: Identcode 66: AP Reply Paid 131: Comp + GS1-128\n"
|
|
" 23: Code 16k 67: AP Routing 132: Comp + Databar-14\n"
|
|
" 25: Code 93 68: AP Redirection 133: Comp + Databar Ltd\n"
|
|
" 28: Flattermarken 69: ISBN 134: Comp + Databar Ext\n"
|
|
" 29: Databar-14 70: RM4SCC 135: Comp + UPC-A\n"
|
|
" 30: Databar Limited 71: Data Matrix 136: Comp + UPC-E\n"
|
|
" 31: Databar Extended 72: EAN-14 137: Comp + Databar-14 Stack\n"
|
|
" 32: Telepen Alpha 74: Codablock-F 138: Comp + Databar Stack Omni\n"
|
|
" 34: UPC-A 75: NVE-18 139: Comp + Databar Ext Stack\n"
|
|
" 37: UPC-E 79: Databar-14 Stack\n"
|
|
);
|
|
}
|
|
|
|
void usage(void)
|
|
{
|
|
printf(
|
|
"Zint version %s\n"
|
|
"Encode input data in a barcode and save as a PNG or EPS file.\n\n"
|
|
" -h, --help Display this message.\n"
|
|
" -t, --types Display table of barcode types\n"
|
|
" -o, --output=FILE Write image to FILE. (default is out.png)\n"
|
|
" -d, --data=DATA Barcode content.\n"
|
|
" -b, --barcode=NUMBER Number of barcode type (default is 20 (=Code128)).\n"
|
|
" --height=HEIGHT Height of my_symbol in pixels.\n"
|
|
" -w, --whitesp=NUMBER Width of whitespace in pixels.\n"
|
|
" --border=NUMBER Width of border in pixels.\n"
|
|
" --box Add a box.\n"
|
|
" --bind Add boundary bars.\n"
|
|
" -r, --reverse Reverse colours (white on black).\n"
|
|
" --fg=COLOUR Specify a foreground colour.\n"
|
|
" --bg=COLOUR Specify a background colour.\n"
|
|
" --rotate=NUMBER Rotate symbol (PNG output only).\n"
|
|
" --cols=NUMBER (PDF417) Number of columns.\n"
|
|
" --vers=NUMBER (QR Code) Version\n"
|
|
" --secure=NUMBER (PDF417 and QR Code) Error correction level.\n"
|
|
" --primary=STRING (Maxicode and Composite) Structured primary message.\n"
|
|
" --mode=NUMBER (Maxicode and Composite) Set encoding mode.\n"
|
|
, ZINT_VERSION);
|
|
}
|
|
|
|
int data_process(struct zint_symbol *symbol, unsigned char source[], int rotate_angle)
|
|
{
|
|
/* Supports UTF-8 input by converting it to Latin-1 Extended ASCII */
|
|
int input_length;
|
|
int error_number, i, j, next;
|
|
|
|
error_number = 0;
|
|
input_length = strlen(source);
|
|
unsigned char latin1[input_length];
|
|
|
|
j = 0;
|
|
i = 0;
|
|
do {
|
|
next = -1;
|
|
if(source[i] < 128) {
|
|
latin1[j] = source[i];
|
|
j++;
|
|
next = i + 1;
|
|
} else {
|
|
if(source[i] == 0xC2) {
|
|
latin1[j] = source[i + 1];
|
|
j++;
|
|
next = i + 2;
|
|
}
|
|
if(source[i] == 0xC3) {
|
|
latin1[j] = source[i + 1] + 64;
|
|
j++;
|
|
next = i + 2;
|
|
}
|
|
}
|
|
if(next == -1) {
|
|
strcpy(symbol->errtxt, "Invalid character in input string (only Latin-1 characters supported)");
|
|
error_number = WARN_INVALID_OPTION;
|
|
return error_number;
|
|
}
|
|
i = next;
|
|
} while(i < input_length);
|
|
latin1[j] = '\0';
|
|
|
|
if(rotate_angle == 0) {
|
|
error_number = ZBarcode_Encode_and_Print(symbol, latin1);
|
|
} else {
|
|
error_number = ZBarcode_Encode_and_Print_Rotated(symbol, latin1, rotate_angle);
|
|
}
|
|
|
|
return error_number;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct zint_symbol *my_symbol;
|
|
int i, mode, stack_row;
|
|
int c;
|
|
int error_number;
|
|
int rotate_angle;
|
|
|
|
error_number = 0;
|
|
rotate_angle = 0;
|
|
my_symbol = ZBarcode_Create();
|
|
|
|
if(argc == 1) {
|
|
usage();
|
|
exit(1);
|
|
}
|
|
|
|
while(1) {
|
|
int option_index = 0;
|
|
static struct option long_options[] = {
|
|
{"help", 0, 0, 'h'},
|
|
{"types", 0, 0, 't'},
|
|
{"bind", 0, 0, 0},
|
|
{"box", 0, 0, 0},
|
|
{"barcode=", 1, 0, 'b'},
|
|
{"height=", 1, 0, 0},
|
|
{"whitesp=", 1, 0, 'w'},
|
|
{"border=", 1, 0, 0},
|
|
{"data=", 1, 0, 'd'},
|
|
{"output=", 1, 0, 'o'},
|
|
{"fg=", 1, 0, 0},
|
|
{"bg=", 1, 0, 0},
|
|
{"cols=", 1, 0, 0},
|
|
{"vers=", 1, 0, 0},
|
|
{"rotate=", 1, 0, 0},
|
|
{"secure=", 1, 0, 0},
|
|
{"reverse", 1, 0, 'r'},
|
|
{"case", 0, 0, 'c'},
|
|
{"mode=", 1, 0, 0},
|
|
{"primary=", 1, 0, 0},
|
|
{0, 0, 0, 0}
|
|
};
|
|
c = getopt_long(argc, argv, "htb:w:d:o:i:rcmp", long_options, &option_index);
|
|
if(c == -1) break;
|
|
|
|
switch(c) {
|
|
case 0:
|
|
if(!strcmp(long_options[option_index].name, "bind")) {
|
|
my_symbol->output_options = BARCODE_BIND;
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "box")) {
|
|
my_symbol->output_options = BARCODE_BOX;
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "fg=")) {
|
|
strncpy(my_symbol->fgcolour, optarg, 7);
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "bg=")) {
|
|
strncpy(my_symbol->bgcolour, optarg, 7);
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "border=")) {
|
|
error_number = is_sane(NESET, optarg);
|
|
if(error_number == ERROR_INVALID_DATA) {
|
|
fprintf(stderr, "Invalid border width\n");
|
|
exit(1);
|
|
}
|
|
if((atoi(optarg) >= 0) && (atoi(optarg) <= 1000)) {
|
|
my_symbol->border_width = atoi(optarg);
|
|
} else {
|
|
fprintf(stderr, "Border width out of range\n");
|
|
}
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "height=")) {
|
|
error_number = is_sane(NESET, optarg);
|
|
if(error_number == ERROR_INVALID_DATA) {
|
|
fprintf(stderr, "Invalid symbol height\n");
|
|
exit(1);
|
|
}
|
|
if((atoi(optarg) >= 1) && (atoi(optarg) <= 1000)) {
|
|
my_symbol->height = atoi(optarg);
|
|
} else {
|
|
fprintf(stderr, "Symbol height out of range\n");
|
|
}
|
|
}
|
|
|
|
if(!strcmp(long_options[option_index].name, "cols=")) {
|
|
if((atoi(optarg) >= 1) && (atoi(optarg) <= 30)) {
|
|
my_symbol->option_2 = atoi(optarg);
|
|
} else {
|
|
fprintf(stderr, "Number of columns out of range\n");
|
|
}
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "vers=")) {
|
|
if((atoi(optarg) >= 1) && (atoi(optarg) <= 40)) {
|
|
my_symbol->option_2 = atoi(optarg);
|
|
} else {
|
|
fprintf(stderr, "Invalid QR Code version\n");
|
|
}
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "secure=")) {
|
|
if((atoi(optarg) >= 1) && (atoi(optarg) <= 8)) {
|
|
my_symbol->option_1 = atoi(optarg);
|
|
} else {
|
|
fprintf(stderr, "ECC level out of range\n");
|
|
}
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "primary=")) {
|
|
if(strlen(optarg) <= 90) {
|
|
strcpy(my_symbol->primary, optarg);
|
|
} else {
|
|
fprintf(stderr, "Primary data string too long");
|
|
}
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "mode=")) {
|
|
/* Don't allow specification of modes 2 and 3 - do it
|
|
automagically instead */
|
|
if((optarg[0] >= '0') && (optarg[0] <= '6')) {
|
|
my_symbol->option_1 = optarg[0] - '0';
|
|
} else {
|
|
fprintf(stderr, "Invalid mode\n");
|
|
}
|
|
}
|
|
if(!strcmp(long_options[option_index].name, "rotate=")) {
|
|
/* Only certain inputs allowed */
|
|
error_number = is_sane(NESET, optarg);
|
|
if(error_number == ERROR_INVALID_DATA) {
|
|
fprintf(stderr, "Invalid rotation parameter\n");
|
|
exit(1);
|
|
}
|
|
switch(atoi(optarg)) {
|
|
case 90: rotate_angle = 90; break;
|
|
case 180: rotate_angle = 180; break;
|
|
case 270: rotate_angle = 270; break;
|
|
default: rotate_angle = 0; break;
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'h':
|
|
usage();
|
|
break;
|
|
|
|
case 't':
|
|
types();
|
|
break;
|
|
|
|
case 'b':
|
|
error_number = is_sane(NESET, optarg);
|
|
if(error_number == ERROR_INVALID_DATA) {
|
|
printf("Invalid barcode type\n");
|
|
exit(1);
|
|
}
|
|
my_symbol->symbology = atoi(optarg);
|
|
break;
|
|
|
|
case 'w':
|
|
error_number = is_sane(NESET, optarg);
|
|
if(error_number == ERROR_INVALID_DATA) {
|
|
printf("Invalid whitespace value\n");
|
|
exit(1);
|
|
}
|
|
if((atoi(optarg) >= 0) && (atoi(optarg) <= 1000)) {
|
|
my_symbol->whitespace_width = atoi(optarg);
|
|
} else {
|
|
fprintf(stderr, "Whitespace value out of range");
|
|
}
|
|
break;
|
|
|
|
case 'd': /* we have some data! */
|
|
error_number = data_process(my_symbol, optarg, rotate_angle);
|
|
if(error_number != 0) {
|
|
printf("%s\n", my_symbol->errtxt);
|
|
ZBarcode_Delete(my_symbol);
|
|
return 1;
|
|
}
|
|
break;
|
|
|
|
case 'o':
|
|
strncpy(my_symbol->outfile, optarg, 250);
|
|
break;
|
|
|
|
case 'r':
|
|
strcpy(my_symbol->fgcolour, "ffffff");
|
|
strcpy(my_symbol->bgcolour, "000000");
|
|
break;
|
|
|
|
case '?':
|
|
break;
|
|
|
|
default:
|
|
printf("?? getopt error 0%o\n", c);
|
|
}
|
|
}
|
|
|
|
if (optind < argc) {
|
|
printf("Invalid option ");
|
|
while (optind < argc)
|
|
printf("%s", argv[optind++]);
|
|
printf("\n");
|
|
}
|
|
|
|
if(strcmp(my_symbol->errtxt, "")) {
|
|
printf(my_symbol->errtxt);
|
|
printf("\n");
|
|
}
|
|
ZBarcode_Delete(my_symbol);
|
|
|
|
return error_number;
|
|
}
|