src: new option --verbose|-V in the uchardet CLI tool.

This new option will give the whole candidate list as well as their
respective confidence (ordered by higher to lower).
This commit is contained in:
Jehan 2020-04-23 16:43:08 +02:00
parent 4da22cca97
commit ae4e3a7cbe

View File

@ -47,7 +47,8 @@
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
void detect(FILE * fp) void detect(FILE *fp,
bool verbose)
{ {
uchardet_t handle = uchardet_new(); uchardet_t handle = uchardet_new();
@ -63,11 +64,27 @@ void detect(FILE * fp)
} }
uchardet_data_end(handle); uchardet_data_end(handle);
const char * charset = uchardet_get_charset(handle); if (verbose)
if (*charset) {
printf("%s\n", charset); size_t candidates = uchardet_get_candidates(handle);
else size_t i;
printf("unknown\n");
printf("\n");
for (i = 0; i < candidates; i++)
{
printf("\t%s (%f)\n",
uchardet_get_encoding(handle, i),
uchardet_get_confidence(handle, i));
}
}
else
{
const char * charset = uchardet_get_encoding(handle, 0);
if (*charset)
printf("%s\n", charset);
else
printf("unknown\n");
}
uchardet_delete(handle); uchardet_delete(handle);
} }
@ -92,6 +109,7 @@ void show_usage()
printf("Options:\n"); printf("Options:\n");
printf(" -v, --version Print version and build information.\n"); printf(" -v, --version Print version and build information.\n");
printf(" -h, --help Print this help.\n"); printf(" -h, --help Print this help.\n");
printf(" -v, --verbose Show all candidates and their confidence value.\n");
printf("\n"); printf("\n");
} }
@ -101,12 +119,14 @@ int main(int argc, char ** argv)
{ {
{ "version", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "verbose", no_argument, NULL, 'V' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
bool end_options = false; bool end_options = false;
bool verbose = false;
static int oc; static int oc;
while((oc = getopt_long(argc, argv, "vh", longopts, NULL)) != -1) while((oc = getopt_long(argc, argv, "vhV", longopts, NULL)) != -1)
{ {
switch (oc) switch (oc)
{ {
@ -116,6 +136,9 @@ int main(int argc, char ** argv)
case 'h': case 'h':
show_usage(); show_usage();
return 0; return 0;
case 'V':
verbose = true;
break;
case '?': case '?':
printf("Please use %s --help.\n", argv[0]); printf("Please use %s --help.\n", argv[0]);
return 1; return 1;
@ -128,13 +151,18 @@ int main(int argc, char ** argv)
(argc == 2 && strcmp(argv[1], "--") == 0)) (argc == 2 && strcmp(argv[1], "--") == 0))
{ {
// No file arg, use stdin by default // No file arg, use stdin by default
detect(f); detect(f, verbose);
} }
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
const char *filename = argv[i]; const char *filename = argv[i];
if (! end_options && strcmp(filename, "--") == 0) if (strcmp(filename, "-V") == 0 ||
strcmp(filename, "--verbose") == 0)
{
continue;
}
else if (! end_options && strcmp(filename, "--") == 0)
{ {
end_options = true; end_options = true;
continue; continue;
@ -151,7 +179,7 @@ int main(int argc, char ** argv)
{ {
printf("%s: ", filename); printf("%s: ", filename);
} }
detect(f); detect(f, verbose);
} }
return error_seen; return error_seen;