Julius 4.2
julius/charconv_iconv.c
説明を見る。
00001 
00020 /*
00021  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00022  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00023  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00024  * All rights reserved
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   /* clear already allocated descriptor */
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     /* disable conversion */
00058     *enable_conv = FALSE;
00059   } else {
00060     /* check for codes */
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     /* allocate conversion descriptor */
00068     cd = iconv_open(tocode, fromcode);
00069     if (cd == (iconv_t)-1) {
00070       /* allocation failed */
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, (ICONV_CONST char **)&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   /* outstr always holds the result */
00121   return(outstr);
00122 }
00123 
00124 #endif /* HAVE_ICONV */
00125 #endif /* CHARACTER_CONVERSION */