diff options
author | Nicholas Clark <nick@ccl4.org> | 2011-05-15 12:02:28 +0100 |
---|---|---|
committer | Nicholas Clark <nick@ccl4.org> | 2011-06-11 10:39:59 +0200 |
commit | 546efe9fbd6c3c651c39b369e97d0835230a0f9f (patch) | |
tree | f5e253b9ec4fea05c3c75644884a5081f4519b17 /generate_uudmap.c | |
parent | 2d1f1fe5758d1f7ec388be4bc09f029dd0df5b7c (diff) | |
download | perl-546efe9fbd6c3c651c39b369e97d0835230a0f9f.tar.gz |
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.
Diffstat (limited to 'generate_uudmap.c')
-rw-r--r-- | generate_uudmap.c | 41 |
1 files changed, 26 insertions, 15 deletions
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 <errno.h> -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; } - - |