From 972d061e902983c524e126455994ef2b6edf8ab3 Mon Sep 17 00:00:00 2001 From: Loic Le Loarer Date: Thu, 16 Jul 2015 00:59:58 +0200 Subject: [PATCH 1/3] Allow multiple filename in the command line --- src/tools/uchardet.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/tools/uchardet.cpp b/src/tools/uchardet.cpp index b2b6bea..efa589b 100644 --- a/src/tools/uchardet.cpp +++ b/src/tools/uchardet.cpp @@ -88,7 +88,7 @@ void show_usage() { show_version(); printf("Usage:\n"); - printf(" uchardet [Options] [File]\n"); + printf(" uchardet [Options] [File]...\n"); printf("\n"); printf("Options:\n"); printf(" -v, --version Print version and build information.\n"); @@ -122,20 +122,24 @@ int main(int argc, char ** argv) } } - FILE * f = stdin; - if (argc == 2) + FILE * f = NULL; + int error_seen = 0; + for (int i = 1; i < argc; i++) { - f = fopen(argv[1], "r"); + char *filename = argv[i]; + f = fopen(filename, "r"); if (f == NULL) { - fprintf(stderr, "Cannot open file.\n"); - return 1; + fprintf(stderr, "Cannot open file '%s'\n", filename); + error_seen = 1; + continue; } + if (argc > 2) { + printf("%s: ", filename); + } + detect(f); + fclose(f); } - detect(f); - - fclose(f); - - return 0; + return error_seen; } From 1c89a2f8fff4ea272ece3304f38e229a2901a5b9 Mon Sep 17 00:00:00 2001 From: Loic Le Loarer Date: Thu, 16 Jul 2015 01:15:08 +0200 Subject: [PATCH 2/3] Use stdin by default as before --- src/tools/uchardet.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/tools/uchardet.cpp b/src/tools/uchardet.cpp index efa589b..b78bab2 100644 --- a/src/tools/uchardet.cpp +++ b/src/tools/uchardet.cpp @@ -122,11 +122,16 @@ int main(int argc, char ** argv) } } - FILE * f = NULL; + FILE * f = stdin; int error_seen = 0; + if (argc < 2) + { + // No file arg, use stdin by default + detect(f); + } for (int i = 1; i < argc; i++) { - char *filename = argv[i]; + const char *filename = argv[i]; f = fopen(filename, "r"); if (f == NULL) { @@ -134,11 +139,11 @@ int main(int argc, char ** argv) error_seen = 1; continue; } - if (argc > 2) { + if (argc > 2) + { printf("%s: ", filename); } detect(f); - fclose(f); } return error_seen; From 07af96b3a783ccc5679b0cc0ab7b4046e03ac3de Mon Sep 17 00:00:00 2001 From: Loic Le Loarer Date: Thu, 16 Jul 2015 01:20:03 +0200 Subject: [PATCH 3/3] Use perror for error report --- src/tools/uchardet.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tools/uchardet.cpp b/src/tools/uchardet.cpp index b78bab2..6ea5131 100644 --- a/src/tools/uchardet.cpp +++ b/src/tools/uchardet.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #ifndef VERSION #define VERSION "Unknown" @@ -135,7 +136,7 @@ int main(int argc, char ** argv) f = fopen(filename, "r"); if (f == NULL) { - fprintf(stderr, "Cannot open file '%s'\n", filename); + perror(filename); error_seen = 1; continue; }