From 546efe9fbd6c3c651c39b369e97d0835230a0f9f Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Sun, 15 May 2011 12:02:28 +0100 Subject: Refactor generate_uudmap.c to use a helper function to output init blocks. In future, this will allow it to generate other output formats without duplicating code. --- generate_uudmap.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'generate_uudmap.c') diff --git a/generate_uudmap.c b/generate_uudmap.c index 2c3e24a267..6159259add 100644 --- a/generate_uudmap.c +++ b/generate_uudmap.c @@ -12,17 +12,11 @@ "hello world" won't port easily to it. */ #include -void output_block_to_file(const char *progname, const char *filename, - const char *block, size_t count) { - FILE *const out = fopen(filename, "w"); +static void +format_char_block(FILE *out, const void *thing, size_t count) { + const char *block = (const char *)thing; - if (!out) { - fprintf(stderr, "%s: Could not open '%s': %s\n", progname, filename, - strerror(errno)); - exit(1); - } - - fputs("{\n ", out); + fputs(" ", out); while (count--) { fprintf(out, "%d", *block); block++; @@ -33,7 +27,24 @@ void output_block_to_file(const char *progname, const char *filename, } } } - fputs("\n}\n", out); + fputc('\n', out); +} + +static void +output_to_file(const char *progname, const char *filename, + void (format_function)(FILE *out, const void *thing, size_t count), + const void *thing, size_t count) { + FILE *const out = fopen(filename, "w"); + + if (!out) { + fprintf(stderr, "%s: Could not open '%s': %s\n", progname, filename, + strerror(errno)); + exit(1); + } + + fputs("{\n", out); + format_function(out, thing, count); + fputs("}\n", out); if (fclose(out)) { fprintf(stderr, "%s: Could not close '%s': %s\n", progname, filename, @@ -69,7 +80,8 @@ int main(int argc, char **argv) { */ PL_uudmap[(U8)' '] = 0; - output_block_to_file(argv[0], argv[1], PL_uudmap, sizeof(PL_uudmap)); + output_to_file(argv[0], argv[1], &format_char_block, + (const void *)PL_uudmap, sizeof(PL_uudmap)); for (bits = 1; bits < 256; bits++) { if (bits & 1) PL_bitcount[bits]++; @@ -82,9 +94,8 @@ int main(int argc, char **argv) { if (bits & 128) PL_bitcount[bits]++; } - output_block_to_file(argv[0], argv[2], PL_bitcount, sizeof(PL_bitcount)); + output_to_file(argv[0], argv[2], &format_char_block, + (const void *)PL_bitcount, sizeof(PL_bitcount)); return 0; } - - -- cgit v1.2.1