Julius 4.2
julius/charconv.c
説明を見る。
00001 
00025 /*
00026  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00027  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00028  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00029  * All rights reserved
00030  */
00031 
00032 #include "app.h"
00033 
00034 #ifdef CHARACTER_CONVERSION
00035 
00036 static boolean convert_enabled = FALSE; 
00037 
00046 static boolean
00047 charconv_setup_real(char *fromcode, char *tocode)
00048 {
00049   boolean enabled;
00050   boolean ret;
00051 
00052   /* call environment-specific setup function */
00053 #ifdef HAVE_ICONV
00054   ret = charconv_iconv_setup(fromcode, tocode, &enabled);
00055 #endif
00056 #ifdef USE_LIBJCODE
00057   ret = charconv_libjcode_setup(fromcode, tocode, &enabled);
00058 #endif
00059 #ifdef USE_WIN32_MULTIBYTE
00060   ret = charconv_win32_setup(fromcode, tocode, &enabled);
00061 #endif
00062 
00063   /* store whether conversion should be enabled or not to outer variable */
00064   convert_enabled = enabled;
00065 
00066   /* return the status */
00067   return(ret);
00068 }
00069 
00080 char *
00081 charconv(char *instr, char *outstr, int maxoutlen)
00082 {
00083   char *ret;
00084 
00085   /* if diabled return instr itself */
00086   if (convert_enabled == FALSE) return(instr); /* no conversion */
00087 
00088   /* call environment-specific conversion function */
00089 #ifdef HAVE_ICONV
00090   ret = charconv_iconv(instr, outstr, maxoutlen);
00091 #endif
00092 #ifdef USE_LIBJCODE
00093   ret = charconv_libjcode(instr, outstr, maxoutlen);
00094 #endif
00095 #ifdef USE_WIN32_MULTIBYTE
00096   ret = charconv_win32(instr, outstr, maxoutlen);
00097 #endif
00098 
00099   /* return pointer to the buffer (either instr or outstr) that have the
00100      resulting string */
00101   return(ret);
00102 }
00103 
00104 #endif /* CHARACTER_CONVERSION */
00105 
00106 /************************************************************************/
00107 
00108 static char *from_code = NULL;
00109 static char *to_code = NULL;
00110 
00111 static boolean
00112 opt_charconv(Jconf *jconf, char *arg[], int argnum)
00113 {
00114 #ifdef CHARACTER_CONVERSION
00115   if (from_code) free(from_code);
00116   if (to_code) free(to_code);
00117   from_code = strcpy((char*)mymalloc(strlen(arg[0])+1), arg[0]);
00118   to_code = strcpy((char*)mymalloc(strlen(arg[1])+1), arg[1]);
00119 #else
00120   fprintf(stderr, "Warning: character set conversion disabled, option \"-charconv\" ignored\n");
00121 #endif
00122   return TRUE;
00123 }
00124 static boolean
00125 opt_nocharconv(Jconf *jconf, char *arg[], int argnum)
00126 {
00127 #ifdef CHARACTER_CONVERSION
00128   if (from_code) free(from_code);
00129   if (to_code) free(to_code);
00130   from_code = NULL;
00131   to_code = NULL;
00132 #else
00133   fprintf(stderr, "Warning: character set conversion disabled, option \"-nocharconv\" ignored\n");
00134 #endif
00135   return TRUE;
00136 }
00137 static boolean
00138 opt_kanji(Jconf *jconf, char *arg[], int argnum)
00139 {
00140 #ifdef CHARACTER_CONVERSION
00141   if (from_code) free(from_code);
00142   if (to_code) free(to_code);
00143   from_code = NULL;
00144   if (!strcmp(arg[0], "noconv")) {
00145     to_code = NULL;
00146   } else {
00147     to_code = strcpy((char*)mymalloc(strlen(arg[0])+1),arg[0]);
00148   }
00149 #else
00150   fprintf(stderr, "Warning: character set conversion disabled, option \"-kanji\" ignored\n");
00151 #endif
00152   return TRUE;
00153 }
00154 
00155 void
00156 charconv_add_option()
00157 {
00158   j_add_option("-charconv", 2, 2, "convert character set for output", opt_charconv);
00159   j_add_option("-nocharconv", 0, 0, "disable charconv", opt_nocharconv);
00160   j_add_option("-kanji", 1, 1, "convert character set for output", opt_kanji);
00161 }
00162 
00163 boolean
00164 charconv_setup()
00165 {
00166 #ifdef CHARACTER_CONVERSION
00167   if (from_code != NULL && to_code != NULL) {
00168     if (charconv_setup_real(from_code, to_code) == FALSE) {
00169       fprintf(stderr, "Error: character set conversion setup failed\n");
00170       return FALSE;
00171     }
00172   }
00173 #endif /* CHARACTER_CONVERSION */
00174   return TRUE;
00175 }