From ef243d3b7dc0dc009cdb493b96ce2b532480fe34 Mon Sep 17 00:00:00 2001 From: Yves Arrouye Date: Sat, 5 Jan 2002 01:08:01 +0000 Subject: [PATCH] ICU-1104 add a --list-code code option to generalize the --default-code one. A good use of this option is to know if a given name is valid, by calling 'uconv --list-code name 2>/dev/null' and checking for the exit code. Maybe a -q option is in order to make that easier. X-SVN-Rev: 7386 --- icu4c/source/extra/uconv/Makefile.in | 2 +- icu4c/source/extra/uconv/root.txt | 10 ++++--- icu4c/source/extra/uconv/uconv.1.in | 11 +++++++- icu4c/source/extra/uconv/uconv.cpp | 39 ++++++++++++++++++---------- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/icu4c/source/extra/uconv/Makefile.in b/icu4c/source/extra/uconv/Makefile.in index a98c1914be..00009e6f8e 100644 --- a/icu4c/source/extra/uconv/Makefile.in +++ b/icu4c/source/extra/uconv/Makefile.in @@ -31,7 +31,7 @@ SECTION = 1 ALL_MAN_FILES = $(TARGET).$(SECTION) ## Extra files to remove for 'make clean' -CLEANFILES = *~ $(DEPS) +CLEANFILES = *~ $(DEPS) $(ALL_MAN_FILES) ## Target information TARGET = uconv diff --git a/icu4c/source/extra/uconv/root.txt b/icu4c/source/extra/uconv/root.txt index 296086a63b..febd8df756 100644 --- a/icu4c/source/extra/uconv/root.txt +++ b/icu4c/source/extra/uconv/root.txt @@ -1,6 +1,6 @@ // -*- Coding: utf-8; -*- [all uconv resource files] // Copyright (c) 2000 IBM, Inc. and Others. -// $Revision: 1.10 $ +// $Revision: 1.11 $ // // Root translation file for uconv messages. // So you want to translate this file??? Great! @@ -30,11 +30,12 @@ root lcUsageWord { "usage" } ucUsageWord { "Usage" } - usage { "{0}: {1} [ -h, -?, --help ] [ -l, --list | --default-code ] [ --canon ] [ -L, --list-transliterators ] [ -x transliterator ] -f, --from-code code -t, --to-code code [ file ]\n" } + usage { "{0}: {1} [ -h, -?, --help ] [ -l, --list | --list-code code | --default-code ] [ --canon ] [ -L, --list-transliterators ] [ -x transliterator ] -f, --from-code code -t, --to-code code [ file ]\n" } help { "Options: -h, --help print this message\n" " -l, --list list all available encodings\n" - " --default-code print name of default encoding" + " --list-code code list only the given encoding\n" + " --default-code list only the default encoding\n" " --canon print list compatible with cnvrtrs.txt(5)\n" " -L, --list-transliterators list all available transliterators\n" " -x transliterator run everything through transliterator\n" @@ -44,7 +45,8 @@ root cantGetNames { "Couldn''t get available converter names.\n" } // 0: err cantGetNames { "Couldn''t get available converter names.\n" } // 0: err cantGetTag { "Couldn''t get standard tag name: {0}.\n" } // 0: err - + + noSuchCodeset { "Couldn''t find encoding: {0}.\n" } // 0: name of the encoding noFromCodeset { "No conversion from encoding given (use -f)\n" } noToCodeset { "No conversion to encoding given (use -t)\n" } diff --git a/icu4c/source/extra/uconv/uconv.1.in b/icu4c/source/extra/uconv/uconv.1.in index 0707bffa92..b5a5dd273a 100644 --- a/icu4c/source/extra/uconv/uconv.1.in +++ b/icu4c/source/extra/uconv/uconv.1.in @@ -18,6 +18,8 @@ [ .BI "\-l\fP, \fB\-\-list" | +.BI "\-l\fP, \fB\-\-list\-code" " code" +| .BI "\-\-default-code" ] [ @@ -48,8 +50,15 @@ Print help about usage and exit. .BI "\-l\fP, \fB\-\-list" List all the available encodings and exit. .TP +.BI "\-l\fP, \fB\-\-list\-code" " code" +List only the +.I code +encoding and exit. If +.I code +is not a proper encoding, exit with an error. +.TP .BI "\-\-default-code" -Print the name of the default encoding and exit. +List only the name of the default encoding and exit. .TP .BI "\--canon" If used with diff --git a/icu4c/source/extra/uconv/uconv.cpp b/icu4c/source/extra/uconv/uconv.cpp index 241e22d9ab..9c0e561d4b 100644 --- a/icu4c/source/extra/uconv/uconv.cpp +++ b/icu4c/source/extra/uconv/uconv.cpp @@ -80,26 +80,21 @@ static void initMsg(const char *pname) { } // Print all available codepage converters -static int printConverters(const char *pname, int defonly, int canon) +static int printConverters(const char *pname, const char *lookfor, int canon) { UErrorCode err = U_ZERO_ERROR; - const char *lookfor = 0; int32_t num; uint16_t num_stds; const char **stds; - if (defonly) { - - /* Find the name of the default converter, and either print it, - or find its real name (in case the function returns an alias) - and save it for later to recognize the real entry. */ - - lookfor = ucnv_getDefaultName(); + if (lookfor) { if (!canon) { printf("%s\n", lookfor); return 0; } else { + /* We've done that already except for the default name. Oh well. */ + const char *truename = ucnv_getAlias(lookfor, 0, &err); if (U_SUCCESS(err)) { lookfor = truename; @@ -422,7 +417,8 @@ int main(int argc, char** argv) const char *pname = *argv; - int printConvs = 0, printDef = 0, printCanon = 0; + int printConvs = 0, printCanon = 0; + const char *printName = 0; // First, get the arguments from command-line // to know the codepages to convert between @@ -445,8 +441,23 @@ int main(int argc, char** argv) { printConvs = 1; } - else if (strcmp("--default-code", *iter) == 0) { - printDef = 1; + else if (strcmp("--default-code", *iter) == 0) + { + printName = ucnv_getDefaultName(); + } + else if (strcmp("--list-code", *iter) == 0) { + iter++; + if (iter!=end) { + UErrorCode e = U_ZERO_ERROR; + printName = ucnv_getAlias(*iter, 0, &e); + if (U_FAILURE(e)) { + UnicodeString str(*iter); + initMsg(pname); + u_wmsg("noSuchCodeset", str.getBuffer()); + return 2; + } + } + else usage(pname, 1); } else if (strcmp("--canon", *iter) == 0) { printCanon = 1; @@ -464,8 +475,8 @@ int main(int argc, char** argv) } } - if (printConvs || printDef) { - return printConverters(pname, printDef, printCanon) ? 2 : 0; + if (printConvs || printName) { + return printConverters(pname, printName, printCanon) ? 2 : 0; } if (fromcpage==0 && tocpage==0)