From 3aecbfecb376d167960a80ffcf76f57d614bbce3 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Sat, 11 Feb 2017 10:41:58 +0000 Subject: [PATCH] printucddata: check all command-line arguments --- tests/printucddata.c | 29 +++++++++++++++++------------ tests/printucddata_cpp.cpp | 31 ++++++++++++++++++------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/tests/printucddata.c b/tests/printucddata.c index 09b5f03a..37cba1b1 100644 --- a/tests/printucddata.c +++ b/tests/printucddata.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2016 Reece H. Dunn + * Copyright (C) 2012-2017 Reece H. Dunn * * This file is part of ucd-tools. * @@ -152,22 +152,27 @@ void print_file(FILE *in) int main(int argc, char **argv) { - if (argc == 2) + FILE *in = NULL; + for (int argn = 1; argn != argc; ++argn) { - if (!strcmp(argv[1], "--stdin") || !strcmp(argv[1], "-")) - print_file(stdin); - else + const char *arg = argv[argn]; + if (!strcmp(arg, "--stdin") || !strcmp(arg, "-")) + in = stdin; + else if (in == NULL) { - FILE *in = fopen(argv[1], "r"); - if (in) - { - print_file(in); - fclose(in); - } - else + in = fopen(arg, "r"); + if (!in) fprintf(stdout, "cannot open `%s`\n", argv[1]); } } + + if (in == stdin) + print_file(stdin); + else if (in != NULL) + { + print_file(in); + fclose(in); + } else { for (codepoint_t c = 0; c <= 0x10FFFF; ++c) diff --git a/tests/printucddata_cpp.cpp b/tests/printucddata_cpp.cpp index 60a2b04f..f34c3fc3 100644 --- a/tests/printucddata_cpp.cpp +++ b/tests/printucddata_cpp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2016 Reece H. Dunn + * Copyright (C) 2012-2017 Reece H. Dunn * * This file is part of ucd-tools. * @@ -152,25 +152,30 @@ void print_file(FILE *in) int main(int argc, char **argv) { - if (argc == 2) + FILE *in = NULL; + for (int argn = 1; argn != argc; ++argn) { - if (!strcmp(argv[1], "--stdin") || !strcmp(argv[1], "-")) - print_file(stdin); - else + const char *arg = argv[argn]; + if (!strcmp(arg, "--stdin") || !strcmp(arg, "-")) + in = stdin; + else if (in == NULL) { - FILE *in = fopen(argv[1], "r"); - if (in) - { - print_file(in); - fclose(in); - } - else + in = fopen(arg, "r"); + if (!in) fprintf(stdout, "cannot open `%s`\n", argv[1]); } } + + if (in == stdin) + print_file(stdin); + else if (in != NULL) + { + print_file(in); + fclose(in); + } else { - for (ucd::codepoint_t c = 0; c <= 0x10FFFF; ++c) + for (codepoint_t c = 0; c <= 0x10FFFF; ++c) uprintf(stdout, c, "%pH %s %C %c %UH %LH %TH %W\n"); } return 0;