Skip to content

Commit

Permalink
Added -n switch to cwcomment to print column headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Jun 9, 2020
1 parent 862e5db commit d0d4a25
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
136 changes: 125 additions & 11 deletions src/cwtools/cwcomment.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ int max_field = 2;

char program_name[20] = "cwcomment";

int print_header = 0;


/*************************************************************************
* Functions to output fields
*************************************************************************/
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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;

0 comments on commit d0d4a25

Please sign in to comment.