summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2010-08-24 11:48:05 +0100
committerNicholas Clark <nick@ccl4.org>2010-10-11 17:05:52 +0100
commit1cf14755eb64fd00abc7ce4bc7143880ca12c911 (patch)
tree9e0030e9f56de19ed50232be14a03417dccc8b8f
parent54aaf9f7237b4743b19fb6476db526c3f8f88587 (diff)
downloadperl-nicholas/arenas.tar.gz
Refactor generate_uudmap.c to use a helper function to output init blocks.smoke-me/arenasnicholas/arenas
In future, this will allow it to generate other output formats without duplicating code.
-rw-r--r--generate_uudmap.c41
1 files changed, 26 insertions, 15 deletions
diff --git a/generate_uudmap.c b/generate_uudmap.c
index 2ca3c59dba..966e22e847 100644
--- a/generate_uudmap.c
+++ b/generate_uudmap.c
@@ -14,17 +14,11 @@
# undef strerror
#endif
-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++;
@@ -35,7 +29,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) {
*/
my_uudmap[(U8)' '] = 0;
- output_block_to_file(argv[0], argv[1], my_uudmap, sizeof(my_uudmap));
+ output_to_file(argv[0], argv[1], &format_char_block,
+ (const void *)my_uudmap, sizeof(my_uudmap));
for (bits = 1; bits < 256; bits++) {
if (bits & 1) my_bitcount[bits]++;
@@ -82,9 +94,8 @@ int main(int argc, char **argv) {
if (bits & 128) my_bitcount[bits]++;
}
- output_block_to_file(argv[0], argv[2], my_bitcount, sizeof(my_bitcount));
+ output_to_file(argv[0], argv[2], &format_char_block,
+ (const void *)my_bitcount, sizeof(my_bitcount));
return 0;
}
-
-