Imported cueprint fixes and improvements from Branden Robinson's Debian patch.

This commit is contained in:
Svend Sorensen
2006-03-01 01:18:24 +00:00
parent 68f2e31068
commit 06448a1021

View File

@@ -5,11 +5,11 @@
* For license terms, see the file COPYING in this distribution. * For license terms, see the file COPYING in this distribution.
*/ */
#include <stdio.h> #include <ctype.h> /* isdigit() */
#include <stdlib.h> /* exit() */ #include <getopt.h> /* getopt_long() */
#include <string.h> /* strcmp() */ #include <stdio.h> /* fprintf(), printf(), snprintf(), stderr */
#include <getopt.h> #include <stdlib.h> /* exit() */
#include <ctype.h> /* isdigit() */ #include <string.h> /* strcasecmp() */
#include "cuefile.h" #include "cuefile.h"
#if HAVE_CONFIG_H #if HAVE_CONFIG_H
@@ -51,7 +51,7 @@ ISRC (CD-TEXT): %u\n\
#define VALUE_UNSET "" #define VALUE_UNSET ""
/* /*
* *_get_* functions can return an int or char * * *_get_* functions can return an int or a char *
*/ */
typedef union { typedef union {
int ival; int ival;
@@ -64,45 +64,19 @@ char *progname;
void usage (int status) void usage (int status)
{ {
if (0 == status) { if (0 == status) {
fprintf(stdout, "%s: usage: cueprint [option...] [file...]\n", progname); printf("%s: usage: cueprint [option...] [file...]\n", progname);
fputs("\ printf("OPTIONS\n"
\n\ "-h, --help print usage\n"
OPTIONS\n\ "-i, --input-format cue|toc set format of file(s)\n"
-h, --help print usage\n\ "-n, --track-number <number> only print track information for single track\n"
-i, --input-format cue|toc set format of file(s)\n\ "-d, --disc-template <template> set disc template\n"
-n, --track-number <number> only print track information for single track\n\ "-t, --track-template <template> set track template\n"
-d, --disc-template <template> set disc template (see TEMPLATE EXPANSION)\n\ "-V, --version print version information\n"
-t, --track-template <template> set track template (see TEMPLATE EXPANSION)\n\ "\n"
-V, --version print version information\n\ "Default disc template: %s\n"
\n\ "Default track template: %s\n"
Template Expansion\n\ "See the manual page for more information.\n",
Disc:\n\ D_TEMPLATE, T_TEMPLATE);
%A - album arranger\n\
%C - album composer\n\
%G - album genre\n\
%M - album message\n\
%N - number of tracks\n\
%P - album performer\n\
%S - album songwriter\n\
%T - album title\n\
%U - album UPC/EAN\n\
Track:\n\
%a - track arranger\n\
%c - track composer\n\
%g - track genre\n\
%i - track ISRC\n\
%m - track message\n\
%n - track number\n\
%p - track perfomer\n\
%t - track title\n\
%u - track ISRC (CD-TEXT)\n\
\n\
Any other %<character> is expanded to that character. For example, to get a\n\
'%', use %%.\n\
\n\
", stdout);
fprintf(stdout, "default disc template is:\n%s\n", D_TEMPLATE);
fprintf(stdout, "default track template is:\n%s\n", T_TEMPLATE);
} else { } else {
fprintf(stderr, "run `%s --help' for usage\n", progname); fprintf(stderr, "run `%s --help' for usage\n", progname);
} }
@@ -117,6 +91,10 @@ void version ()
exit(0); exit(0);
} }
/*
* TODO: Shouldn't we be using vprintf() to help us out with this stuff?
* (Branden Robinson)
*/
void disc_field (char *conv, int length, Cd *cd, Value *value) void disc_field (char *conv, int length, Cd *cd, Value *value)
{ {
char *c; /* pointer to conversion character */ char *c; /* pointer to conversion character */
@@ -127,7 +105,7 @@ void disc_field (char *conv, int length, Cd *cd, Value *value)
c = conv + length - 1; c = conv + length - 1;
/* /*
* after setting value, set *c to specify value type * After setting value, set *c to specify value type:
* 'd' integer * 'd' integer
* 's' string * 's' string
* 'c' character * 'c' character
@@ -245,8 +223,8 @@ void track_field (char *conv, int length, Cd *cd, int trackno, Value *value)
} }
/* /*
* print a % conversion specification * Print a conversion specification.
* %[flag(s)][width][.precision]<conversion-char> * [flag(s)][width][.precision]<conversion-char>
*/ */
void print_conv (char *start, int length, Cd *cd, int trackno) void print_conv (char *start, int length, Cd *cd, int trackno)
{ {
@@ -255,7 +233,7 @@ void print_conv (char *start, int length, Cd *cd, int trackno)
char *c; /* pointer to conversion-char */ char *c; /* pointer to conversion-char */
/* TODO: use strndup? */ /* TODO: use strndup? */
conv = malloc ((unsigned) (length + 1)); conv = malloc((unsigned) (length + 1));
strncpy(conv, start, length); strncpy(conv, start, length);
conv[length] = '\0'; conv[length] = '\0';
@@ -376,8 +354,8 @@ int info (char *name, int format, int trackno, char *d_template, char *t_templat
} }
/* /*
* translate escape sequences in a string * Translate escape sequences in a string.
* string is overwritten and terminated * The string is overwritten and terminated.
* TODO: this does not handle octal and hexidecimal escapes * TODO: this does not handle octal and hexidecimal escapes
* except for \0 * except for \0
*/ */
@@ -437,9 +415,12 @@ void translate_escapes(char *s)
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
int format = UNKNOWN; int format = UNKNOWN;
int trackno = -1; /* track number (-1 = unspecified, 0 = disc info) */ int trackno = -1; /* track number (-1 = unspecified,
0 = disc info) */
char *d_template = NULL; /* disc template */ char *d_template = NULL; /* disc template */
char *t_template = NULL; /* track template */ char *t_template = NULL; /* track template */
int ret = 0; /* return value of info() */
/* option variables */ /* option variables */
int c; int c;
/* getopt_long() variables */ /* getopt_long() variables */
@@ -456,7 +437,7 @@ int main (int argc, char **argv)
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
progname = *argv; progname = argv[0];
while (-1 != (c = getopt_long(argc, argv, "hi:n:d:t:V", longopts, NULL))) { while (-1 != (c = getopt_long(argc, argv, "hi:n:d:t:V", longopts, NULL))) {
switch (c) { switch (c) {
@@ -469,7 +450,8 @@ int main (int argc, char **argv)
} else if (0 == strcmp("toc", optarg)) { } else if (0 == strcmp("toc", optarg)) {
format = TOC; format = TOC;
} else { } else {
fprintf(stderr, "%s: illegal format `%s'\n", progname, optarg); fprintf(stderr, "%s: error: unknown input file "
"format `%s'\n", progname, optarg);
usage(1); usage(1);
} }
break; break;
@@ -491,7 +473,7 @@ int main (int argc, char **argv)
} }
} }
/* if no disc or track template is set, use the defaults for both */ /* If no disc or track template is set, use the defaults for both. */
/* TODO: alternative to strdup to get variable strings? */ /* TODO: alternative to strdup to get variable strings? */
if (NULL == d_template && NULL == t_template) { if (NULL == d_template && NULL == t_template) {
d_template = strdup(D_TEMPLATE); d_template = strdup(D_TEMPLATE);
@@ -506,17 +488,24 @@ int main (int argc, char **argv)
} }
} }
/* translate escape sequences */ /* Translate escape sequences. */
translate_escapes(d_template); translate_escapes(d_template);
translate_escapes(t_template); translate_escapes(t_template);
/* What we do depends on the number of operands. */
if (optind == argc) { if (optind == argc) {
info("-", format, trackno, d_template, t_template); /* No operands: report information about stdin. */
ret = info("-", format, trackno, d_template, t_template);
} else { } else {
/* Report information about each operand. */
for (; optind < argc; optind++) { for (; optind < argc; optind++) {
info(argv[optind], format, trackno, d_template, t_template); ret = info(argv[optind], format, trackno, d_template, t_template);
/* Exit if info() returns nonzero. */
if (!ret) {
break;
}
} }
} }
return 0; return ret;
} }