ICU-1220 added iconv-compatibility option -c, and added -i for input

X-SVN-Rev: 7399
This commit is contained in:
Yves Arrouye 2002-01-07 21:34:41 +00:00
parent b2b5b7ff96
commit 4004c78ee9
3 changed files with 47 additions and 6 deletions

View file

@ -1,6 +1,6 @@
// -*- Coding: utf-8; -*- [all uconv resource files]
// Copyright (c) 2000 IBM, Inc. and Others.
// $Revision: 1.13 $
// $Revision: 1.14 $
//
// Root translation file for uconv messages.
// So you want to translate this file??? Great!
@ -30,7 +30,7 @@ root
lcUsageWord { "usage" }
ucUsageWord { "Usage" }
usage { "{0}: {1} [ -h, -?, --help ] [ -l, --list | --list-code code | --default-code | -L, --list-transliterators ] [ --canon ] [ -x transliterator ] -f, --from-code code -t, --to-code code [ file ]\n" }
usage { "{0}: {1} [ -h, -?, --help ] [ -l, --list | --list-code code | --default-code | -L, --list-transliterators ] [ --canon ] [ -x transliterator ] [ -c ] [ -i ] -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"
@ -39,6 +39,8 @@ root
" -L, --list-transliterators list all available transliterators\n"
" --canon print list in cnvrtrs.txt(5) format\n"
" -x transliterator run everything through transliterator\n"
" -c omit invalid characters from the output\n"
" -i ignore invalid sequences from the input\n"
" -f, --from-code code set the original encoding\n"
" -t, --to-code code set the destination encoding\n"
}
@ -55,6 +57,8 @@ root
cantOpenTranslit { "Couldn''t open transliteror {0}: {1}\n" } // 0: set, 1: err
cantSetCallback { "Couldn''t set transcoding callback: {0}\n" } // 0: err
cantOpenInputF { "Couldn''t open input file {0}: {1}.\n" } // 0: file, 1: strerror [OS error string]
cantWrite { "The converted text couldn't be written: {0}.\n" } // 0: OS error string

View file

@ -30,6 +30,12 @@
[
.BI "\-x" " trasnsliterator
]
[
.B "\-c"
]
[
.B "\-i"
]
.BI "\-f\fP, \fB\-\-from\-code" " encoding"
.BI "\-t\fP, \fB\-\-to\-code" " encoding"
[
@ -84,6 +90,12 @@ Run the transcoded Unicode data through the given
and use the transliterated data as input for the transcoding to
the the destination encoding.
.TP
.B "\-c"
Omit invalid characters from the output.
.TP
.B "\-i"
Ignore invalid sequences from the input.
.TP
.BI "\-f\fP, \fB\-\-from\-code" " encoding"
Set the original encoding of the data to
.IR encoding .

View file

@ -30,7 +30,6 @@
#include <stdlib.h>
#include "cmemory.h"
#include "cstring.h"
// This is the UConverter headerfile
#include "unicode/ucnv.h"
@ -247,7 +246,7 @@ static int printTransliterators(const char *pname, int canon) {
}
utrans_getAvailableID(i, buf, buflen);
if (len >= buflen) {
uprv_strcpy(buf + buflen - 4, "...");
strcpy(buf + buflen - 4, "...");
}
}
@ -271,7 +270,9 @@ static int printTransliterators(const char *pname, int canon) {
// Convert a file from one encoding to another
static UBool convertFile(const char* fromcpage,
UConverterToUCallback toucallback,
const char* tocpage,
UConverterFromUCallback fromucallback,
const char *translit,
FILE* infile,
FILE* outfile)
@ -286,6 +287,10 @@ static UBool convertFile(const char* fromcpage,
const size_t readsize = buffsize-1;
char* buff = 0;
UConverterFromUCallback oldfromucallback;
UConverterToUCallback oldtoucallback;
const void *oldcontext;
const UChar* cuniiter;
UChar* uniiter;
UChar* unibuff = 0;
@ -318,9 +323,14 @@ static UBool convertFile(const char* fromcpage,
u_wmsg_errorName(err));
goto error_exit;
}
ucnv_setToUCallBack(convfrom, toucallback, 0, &oldtoucallback, &oldcontext, &err);
if (U_FAILURE(err))
{
u_wmsg("cantSetCallback", u_wmsg_errorName(err));
goto error_exit;
}
convto = ucnv_open(tocpage, &err);
if (U_FAILURE(err))
{
UnicodeString str(tocpage,"");
@ -328,6 +338,12 @@ static UBool convertFile(const char* fromcpage,
u_wmsg_errorName(err));
goto error_exit;
}
ucnv_setFromUCallBack(convto, fromucallback, 0, &oldfromucallback, &oldcontext, &err);
if (U_FAILURE(err))
{
u_wmsg("cantSetCallback", u_wmsg_errorName(err));
goto error_exit;
}
// To ensure that the buffer always is of enough size, we
// must take the worst case scenario, that is the character in the codepage
@ -465,6 +481,9 @@ int main(int argc, char** argv)
const char *translit = 0;
const char* infilestr = 0;
UConverterFromUCallback fromucallback = UCNV_FROM_U_CALLBACK_SUBSTITUTE;
UConverterToUCallback toucallback = UCNV_TO_U_CALLBACK_SUBSTITUTE;
char** iter = argv+1;
char** end = argv+argc;
@ -545,6 +564,12 @@ int main(int argc, char** argv)
{
usage(pname, 0);
}
else if (!strcmp("-c", *iter)) {
fromucallback = UCNV_FROM_U_CALLBACK_SKIP;
}
else if (!strcmp("-i", *iter)) {
toucallback = UCNV_TO_U_CALLBACK_SKIP;
}
else if (**iter == '-' && (*iter)[1]) {
usage(pname, 1);
} else if (!infilestr) {
@ -613,7 +638,7 @@ int main(int argc, char** argv)
#endif
initMsg(pname);
if (!convertFile(fromcpage, tocpage, translit, infile, stdout))
if (!convertFile(fromcpage, toucallback, tocpage, fromucallback, translit, infile, stdout))
goto error_exit;
goto normal_exit;