From eccaac09ccde3a69a91f65d4dd1c09d8a0ab53d6 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Tue, 25 Nov 2008 12:13:51 +0100 Subject: [PATCH] New API functions iso_set_local_charset() and iso_get_local_charset() --- libisofs/ecma119.c | 6 +++++- libisofs/fs_image.c | 6 +++++- libisofs/libisofs.h | 30 ++++++++++++++++++++++++++++++ libisofs/util.c | 31 ++++++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/libisofs/ecma119.c b/libisofs/ecma119.c index f411f02..52be0c5 100644 --- a/libisofs/ecma119.c +++ b/libisofs/ecma119.c @@ -911,9 +911,13 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) target->catalog = src->bootcat; /* default to locale charset */ + /* ??? ts Nov 25 2008 : + Shouldn't this go to library initialization or even to app ? + */ setlocale(LC_CTYPE, ""); - target->input_charset = strdup(nl_langinfo(CODESET)); + target->input_charset = strdup(iso_get_local_charset(0)); + if (target->input_charset == NULL) { iso_image_unref(src); free(target); diff --git a/libisofs/fs_image.c b/libisofs/fs_image.c index d9a6212..9a0a014 100644 --- a/libisofs/fs_image.c +++ b/libisofs/fs_image.c @@ -1996,8 +1996,12 @@ int iso_image_filesystem_new(IsoDataSource *src, struct iso_read_opts *opts, data->dir_mode = opts->dir_mode & ~S_IFMT; data->msgid = msgid; + /* ??? ts Nov 25 2008 : + Shouldn't this go to library initialization or even to app ? + */ setlocale(LC_CTYPE, ""); - data->local_charset = strdup(nl_langinfo(CODESET)); + + data->local_charset = strdup(iso_get_local_charset(0)); if (data->local_charset == NULL) { ret = ISO_OUT_OF_MEM; goto fs_cleanup; diff --git a/libisofs/libisofs.h b/libisofs/libisofs.h index 5fe91db..1deb5c1 100644 --- a/libisofs/libisofs.h +++ b/libisofs/libisofs.h @@ -835,6 +835,36 @@ int iso_init(); */ void iso_finish(); +/** + * Override the reply of libc function nl_langinfo(CODESET) which may or may + * not give the name of the character set which is in effect for your + * environment. So this call can compensate for inconsistent terminal setups. + * Another use case is to choose UTF-8 as intermediate character set for a + * conversion from an exotic input character set to an exotic output set. + * + * @param name + * Name of the character set to be assumed as "local" one. + * @param flag + * Unused yet. Submit 0. + * @return + * 1 indicates success, <=0 failure + * + * @since 0.6.12 + */ +int iso_set_local_charset(char *name, int flag); + +/** + * Obtain the local charset as currently assumed by libisofs. + * The result points to internal memory. It is volatile and must not be + * altered. + * + * @param flag + * Unused yet. Submit 0. + * + * @since 0.6.12 + */ +char *iso_get_local_charset(int flag); + /** * Create a new image, empty. * diff --git a/libisofs/util.c b/libisofs/util.c index 6317a8b..d28df37 100644 --- a/libisofs/util.c +++ b/libisofs/util.c @@ -40,6 +40,30 @@ int int_pow(int base, int power) return result; } +/* This static variable can override the locale's charset by its getter + function which should be used whenever the local character set name + is to be inquired. I.e. instead of calling nl_langinfo(CODESET) directly. + If the variable is empty then it forwards nl_langinfo(CODESET). +*/ +static char libisofs_local_charset[4096]= {""}; + +/* API function */ +int iso_set_local_charset(char *name, int flag) +{ + if(strlen(name) >= sizeof(libisofs_local_charset)) + return(0); + strcpy(libisofs_local_charset, name); + return 1; +} + +/* API function */ +char *iso_get_local_charset(int flag) +{ + if(libisofs_local_charset[0]) + return libisofs_local_charset; + return nl_langinfo(CODESET); +} + int strconv(const char *str, const char *icharset, const char *ocharset, char **output) { @@ -1219,8 +1243,13 @@ char *ucs2str(const char *buf, size_t len) out = alloca(outbytes); /* convert to local charset */ + + /* ??? ts Nov 25 2008 : + Shouldn't this go to library initialization or even to app ? + */ setlocale(LC_CTYPE, ""); - conv = iconv_open(nl_langinfo(CODESET), "UCS-2BE"); + + conv = iconv_open(iso_get_local_charset(0), "UCS-2BE"); if (conv == (iconv_t)(-1)) { return NULL; }