00001
00020
00021
00022
00023
00024
00025
00026
00027 #include "app.h"
00028
00029 #ifdef CHARACTER_CONVERSION
00030 #ifdef HAVE_ICONV
00031
00032 #include <iconv.h>
00033 static iconv_t cd = (iconv_t)-1;
00034
00044 boolean
00045 charconv_iconv_setup(char *fromcode, char *tocode, boolean *enable_conv)
00046 {
00047
00048 if (cd != (iconv_t)-1) {
00049 if (iconv_close(cd) < 0) {
00050 perror("j_prinf_set_iconv");
00051 return FALSE;
00052 }
00053 cd = (iconv_t)-1;
00054 }
00055
00056 if (tocode == NULL) {
00057
00058 *enable_conv = FALSE;
00059 } else {
00060
00061 if (fromcode == NULL) {
00062 jlog("Error: charconv_iconv: charset names of both input and output should be given.\n");
00063 jlog("Error: charconv_iconf: use \"-charconv from to\" instead of \"-kanji\".\n");
00064 *enable_conv = FALSE;
00065 return FALSE;
00066 }
00067
00068 cd = iconv_open(tocode, fromcode);
00069 if (cd == (iconv_t)-1) {
00070
00071 jlog("Error: charconv_iconv: unknown charset name in \"%s\" or \"%s\"\n", fromcode, tocode);
00072 jlog("Error: charconv_iconv: do \"iconv --list\" to get the list of available charset names.\n");
00073 *enable_conv = FALSE;
00074 return FALSE;
00075 }
00076 *enable_conv = TRUE;
00077 }
00078 return TRUE;
00079 }
00080
00091 char *
00092 charconv_iconv(char *instr, char *outstr, int maxoutlen)
00093 {
00094 char *src, *dst;
00095 size_t srclen, dstlen;
00096 size_t ret;
00097
00098 if (cd == (iconv_t)-1) {
00099 fprintf(stderr, "InternalError: codeconv: conversion descriptor not allocated\n"); exit(-1);
00100 }
00101 srclen = strlen(instr)+1;
00102 dstlen = maxoutlen;
00103 src = instr;
00104 dst = outstr;
00105 ret = iconv(cd, &src, &srclen, &dst, &dstlen);
00106 if (ret == -1) {
00107 switch(errno) {
00108 case EILSEQ:
00109 fprintf(stderr, "InternalError: codeconv: invalid multibyte sequence in the input\n"); exit(-1);
00110 break;
00111 case EINVAL:
00112 fprintf(stderr, "InternalError: codeconv: incomplete multibyte sequence in the input\n"); exit(-1);
00113 break;
00114 case E2BIG:
00115 fprintf(stderr, "InternalError: codeconv: converted string size exceeded buffer (>%d)\n", maxoutlen); exit(-1);
00116 break;
00117 }
00118 }
00119
00120
00121 return(outstr);
00122 }
00123
00124 #endif
00125 #endif