diff --git a/ChangeLog.md b/ChangeLog.md index 201afae..40a35f6 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -19,6 +19,9 @@ - Made `cwbox -X` and `cwbox -S` robust to the absence of the team file or a roster file by implementing sensible defaults. +## Enhancements +- Implemented `-n` switch for `cwcomment` to print column headers. + ## Deprecations and removals - Support for the `KE` (strikeout + error) primary event type was removed. This was a very old Retrosheet code that has been diff --git a/src/cwtools/cwcomment.c b/src/cwtools/cwcomment.c index 82e3208..6426a5a 100644 --- a/src/cwtools/cwcomment.c +++ b/src/cwtools/cwcomment.c @@ -43,6 +43,9 @@ int max_field = 2; char program_name[20] = "cwcomment"; +int print_header = 0; + + /************************************************************************* * Functions to output fields *************************************************************************/ @@ -113,10 +116,20 @@ DECLARE_FIELDFUNC(cwcomment_comment) return chars; } -static field_func function_ptrs[] = { - cwcomment_game_id, /* 0 */ - cwcomment_event_number, /* 1 */ - cwcomment_comment /* 2 */ +/* + * convenient structure to hold all information relating to a field + * together in one place + */ +typedef struct field_struct { + field_func f; + char *header, *description; +} field_struct; + + +static field_struct field_data[] = { + /* 0 */ { cwcomment_game_id, "GAME ID", "game id" }, + /* 1 */ { cwcomment_event_number, "EVENT_ID", "event num" }, + /* 2 */ { cwcomment_comment, "COMMENT_TX", "comment text" } }; void @@ -139,7 +152,7 @@ cwcomment_process_game(CWGame *game, CWRoster *visitors, CWRoster *home) else { comma = 1; } - buf += (*function_ptrs[i])(buf, gameiter, 1); + buf += (*field_data[i].f)(buf, gameiter, 1); } } @@ -164,7 +177,7 @@ cwcomment_process_game(CWGame *game, CWRoster *visitors, CWRoster *home) else { comma = 1; } - buf += (*function_ptrs[i])(buf, gameiter, 0); + buf += (*field_data[i].f)(buf, gameiter, 0); } } @@ -199,6 +212,7 @@ cwcomment_print_help(void) fprintf(stderr, " Default is 0-2.\n"); fprintf(stderr, " -d print list of field numbers and descriptions\n\n"); fprintf(stderr, " -q operate quietly; do not output progress messages\n"); + fprintf(stderr, " -n print field names in first row of output\n\n"); exit(0); } @@ -208,14 +222,17 @@ void (*cwtools_print_help)(void) = cwcomment_print_help; void cwcomment_print_field_list(void) { + int i; + fprintf(stderr, "\nThese are the available fields and the numbers to use with the -f option\n"); fprintf(stderr, "to name them. All are included by default.\n"); fprintf(stderr, "\n"); fprintf(stderr, "number field\n"); fprintf(stderr, "------ -----\n"); - fprintf(stderr, "0 game id\n"); - fprintf(stderr, "1 event number\n"); - fprintf(stderr, "2 comment text\n"); + for (i = 0; i <= max_field; i++) { + fprintf(stderr, "%-2d %s\n", i, field_data[i].description); + } + fprintf(stderr, "\n"); exit(0); } @@ -237,6 +254,30 @@ void (*cwtools_print_welcome_message)(char *) = cwcomment_print_welcome_message; void cwcomment_initialize(void) { + int i, comma = 0; + char output_line[4096]; + char *buf; + + if (!ascii || !print_header) { + return; + } + + strcpy(output_line, ""); + buf = output_line; + + for (i = 0; i <= max_field; i++) { + if (fields[i]) { + if (ascii && comma) { + *(buf++) = ','; + } + else { + comma = 1; + } + buf += sprintf(buf, "\"%s\"", field_data[i].header); + } + } + printf("%s", output_line); + printf("\n"); } void (*cwtools_initialize)(void) = cwcomment_initialize; @@ -249,5 +290,78 @@ cwcomment_cleanup(void) void (*cwtools_cleanup)(void) = cwcomment_cleanup; -extern int cwtools_default_parse_command_line(int, char *argv[]); -int (*cwtools_parse_command_line)(int, char *argv[]) = cwtools_default_parse_command_line; +extern char year[5]; +extern char first_date[5]; +extern char last_date[5]; +extern char game_id[20]; +extern int ascii; +extern int quiet; + +extern void +cwtools_parse_field_list(char *text, int max_field, int *fields); + +int +cwcomment_parse_command_line(int argc, char *argv[]) +{ + int i; + strcpy(year, ""); + + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-a")) { + ascii = 1; + } + else if (!strcmp(argv[i], "-d")) { + (*cwtools_print_welcome_message)(argv[0]); + (*cwtools_print_field_list)(); + } + else if (!strcmp(argv[i], "-e")) { + if (++i < argc) { + strncpy(last_date, argv[i], 4); + } + } + else if (!strcmp(argv[i], "-h")) { + (*cwtools_print_welcome_message)(argv[0]); + (*cwtools_print_help)(); + } + else if (!strcmp(argv[i], "-q")) { + quiet = 1; + } + else if (!strcmp(argv[i], "-i")) { + if (++i < argc) { + strncpy(game_id, argv[i], 19); + } + } + else if (!strcmp(argv[i], "-f")) { + if (++i < argc) { + cwtools_parse_field_list(argv[i], max_field, fields); + } + } + else if (!strcmp(argv[i], "-n")) { + print_header = 1; + } + else if (!strcmp(argv[i], "-ft")) { + ascii = 0; + } + else if (!strcmp(argv[i], "-s")) { + if (++i < argc) { + strncpy(first_date, argv[i], 4); + } + } + else if (!strcmp(argv[i], "-y")) { + if (++i < argc) { + strncpy(year, argv[i], 5); + } + } + else if (argv[i][0] == '-') { + fprintf(stderr, "*** Invalid option '%s'.\n", argv[i]); + exit(1); + } + else { + break; + } + } + + return i; +} + +int (*cwtools_parse_command_line)(int, char *argv[]) = cwcomment_parse_command_line;