Back to index

lightning-sunbird  0.9+nobinonly
convutf8.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 #include <string.h>
00040 
00041 #ifndef HAVE_LIBNLS
00042 
00043 #ifdef __cplusplus
00044 extern "C" {
00045 #endif
00046 
00047 extern char   *ldaptool_charset;
00048 char          *ldaptool_convdir = NULL;
00049 static        int charsetset = 0;
00050 char          *ldaptool_local2UTF8( const char *src );
00051 
00052 char *
00053 ldaptool_local2UTF8( const char *src )
00054 {
00055     char *utf8;
00056 
00057     charsetset = 0;
00058 
00059     if (src == NULL)
00060     {
00061        return NULL;
00062     }
00063     utf8 = strdup(src);
00064 
00065     return ( utf8 );
00066 }
00067 
00068 #else /* HAVE_LIBNLS */
00069 
00070 #define NSPR20
00071 
00072 #ifdef XP_WIN32
00073 #define  VC_EXTRALEAN
00074 #include <afxwin.h>
00075 #include <winnls.h>
00076 #endif
00077 
00078 extern char *ldaptool_charset;
00079 static int charsetset = 0;
00080 
00081 extern "C" {
00082 char *ldaptool_convdir = NULL;
00083 char *ldaptool_local2UTF8( const char * );
00084 }
00085 
00086 #ifndef XP_WIN32
00087 char * GetNormalizedLocaleName(void);
00088 
00089 #include "unistring.h"
00090 #include "nlsenc.h"
00091 
00092 extern NLS_StaticConverterRegistry _STATICLINK_NSJPN_;
00093 extern NLS_StaticConverterRegistry _STATICLINK_NSCCK_;
00094 extern NLS_StaticConverterRegistry _STATICLINK_NSSB_;
00095 
00096 char *
00097 GetNormalizedLocaleName(void)
00098 {
00099 #ifdef _HPUX_SOURCE
00100  
00101     int    len;
00102     char    *locale;
00103  
00104     locale = setlocale(LC_CTYPE, "");
00105     if (locale && *locale) {
00106         len = strlen(locale);
00107     } else {
00108         locale = "C";
00109         len = 1;
00110     }
00111  
00112     if ((!strncmp(locale, "/\x03:", 3)) &&
00113         (!strcmp(&locale[len - 2], ";/"))) {
00114         locale += 3;
00115         len -= 5;
00116     }
00117  
00118     locale = strdup(locale);
00119     if (locale) {
00120         locale[len] = 0;
00121     }
00122  
00123     return locale;
00124  
00125 #else
00126  
00127     char    *locale;
00128  
00129     locale = setlocale(LC_CTYPE, "");
00130     if (locale && *locale) {
00131         return strdup(locale);
00132     }
00133  
00134     return strdup("C");
00135  
00136 #endif
00137 }
00138 
00139 #if defined(IRIX)
00140 const char *CHARCONVTABLE[] =
00141 {
00142 "! This table maps the host's locale names to LIBNLS charsets",
00143 "!",
00144 "C:             ISO_8859-1:1987",
00145 "cs:            ISO_8859-2:1987",
00146 "da:            ISO_8859-1:1987",
00147 "de:            ISO_8859-1:1987",
00148 "de_AT:         ISO_8859-1:1987",
00149 "de_CH:         ISO_8859-1:1987",
00150 "en:            ISO_8859-1:1987",
00151 "en_AU:         ISO_8859-1:1987",
00152 "en_CA:         ISO_8859-1:1987",
00153 "en_TH:         ISO_8859-1:1987",
00154 "en_US:         ISO_8859-1:1987",
00155 "es:            ISO_8859-1:1987",
00156 "fi:            ISO_8859-1:1987",
00157 "fr:            ISO_8859-1:1987",
00158 "fr_BE:         ISO_8859-1:1987",
00159 "fr_CA:         ISO_8859-1:1987",
00160 "fr_CH:         ISO_8859-1:1987",
00161 "is:            ISO_8859-1:1987",
00162 "it:            ISO_8859-1:1987",
00163 "it_CH:         ISO_8859-1:1987",
00164 "ja_JP.EUC:     Extended_UNIX_Code_Packed_Format_for_Japanese",
00165 "ko_KR.euc:     EUC-KR",
00166 "nl:            ISO_8859-1:1987",
00167 "nl_BE:         ISO_8859-1:1987",
00168 "no:            ISO_8859-1:1987",
00169 "pl:            ISO_8859-2:1987",
00170 "pt:            ISO_8859-1:1987",
00171 "sh:            ISO_8859-2:1987",
00172 "sk:            ISO_8859-2:1987",
00173 "sv:            ISO_8859-1:1987",
00174 "zh_CN.ugb:     GB2312",
00175 "zh_TW.ucns:    cns11643_1",
00176 NULL
00177 };
00178 #elif defined(SOLARIS)
00179 const char *CHARCONVTABLE[] =
00180 {
00181 "! This table maps the host's locale names to LIBNLS charsets",
00182 "!",
00183 "C:             ISO_8859-1:1987",
00184 "ja:            Extended_UNIX_Code_Packed_Format_for_Japanese",
00185 "ja_JP.EUC:     Extended_UNIX_Code_Packed_Format_for_Japanese",
00186 "ja_JP.PCK:     Shift_JIS",
00187 "en:          ISO_8859-1:1987",
00188 "en_AU:              ISO_8859-1:1987",
00189 "en_CA:              ISO_8859-1:1987",
00190 "en_UK:              ISO_8859-1:1987",
00191 "en_US:              ISO_8859-1:1987",
00192 "es:          ISO_8859-1:1987",
00193 "es_AR:              ISO_8859-1:1987",
00194 "es_BO:              ISO_8859-1:1987",
00195 "es_CL:              ISO_8859-1:1987",
00196 "es_CO:              ISO_8859-1:1987",
00197 "es_CR:              ISO_8859-1:1987",
00198 "es_EC:              ISO_8859-1:1987",
00199 "es_GT:              ISO_8859-1:1987",
00200 "es_MX:              ISO_8859-1:1987",
00201 "es_NI:              ISO_8859-1:1987",
00202 "es_PA:              ISO_8859-1:1987",
00203 "es_PE:              ISO_8859-1:1987",
00204 "es_PY:              ISO_8859-1:1987",
00205 "es_SV:              ISO_8859-1:1987",
00206 "es_UY:              ISO_8859-1:1987",
00207 "es_VE:              ISO_8859-1:1987",
00208 "fr:          ISO_8859-1:1987",
00209 "fr_BE:              ISO_8859-1:1987",
00210 "fr_CA:              ISO_8859-1:1987",
00211 "fr_CH:              ISO_8859-1:1987",
00212 "de:          ISO_8859-1:1987",
00213 "de_AT:              ISO_8859-1:1987",
00214 "de_CH:              ISO_8859-1:1987",
00215 "nl:          ISO_8859-1:1987",
00216 "nl_BE:              ISO_8859-1:1987",
00217 "it:          ISO_8859-1:1987",
00218 "sv:          ISO_8859-1:1987",
00219 "no:          ISO_8859-1:1987",
00220 "da:          ISO_8859-1:1987",
00221 "iso_8859_1:    ISO_8859-1:1987",
00222 "japanese:      Extended_UNIX_Code_Packed_Format_for_Japanese",
00223 "ko:            EUC-KR",
00224 "zh:            GB2312",
00225 "zh_TW:         cns11643_1",
00226 NULL
00227 };
00228 #elif defined(OSF1)
00229 const char *CHARCONVTABLE[] =
00230 {
00231 "! This table maps the host's locale names to LIBNLS charsets",
00232 "!",
00233 "C:                     ISO_8859-1:1987",
00234 "cs_CZ.ISO8859-2:       ISO_8859-2:1987",
00235 "cs_CZ:                 ISO_8859-2:1987",
00236 "da_DK.ISO8859-1:       ISO_8859-1:1987",
00237 "de_CH.ISO8859-1:       ISO_8859-1:1987",
00238 "de_DE.ISO8859-1:       ISO_8859-1:1987",
00239 "en_GB.ISO8859-1:       ISO_8859-1:1987",
00240 "en_US.ISO8859-1:       ISO_8859-1:1987",
00241 "es_ES.ISO8859-1:       ISO_8859-1:1987",
00242 "fi_FI.ISO8859-1:       ISO_8859-1:1987",
00243 "fr_BE.ISO8859-1:       ISO_8859-1:1987",
00244 "fr_CA.ISO8859-1:       ISO_8859-1:1987",
00245 "fr_CH.ISO8859-1:       ISO_8859-1:1987",
00246 "fr_FR.ISO8859-1:       ISO_8859-1:1987",
00247 "hu_HU.ISO8859-2:       ISO_8859-2:1987",
00248 "hu_HU:                 ISO_8859-2:1987",
00249 "is_IS.ISO8859-1:       ISO_8859-1:1987",
00250 "it_IT.ISO8859-1:       ISO_8859-1:1987",
00251 "ja_JP.SJIS:            Shift_JIS",
00252 "ja_JP.eucJP:           Extended_UNIX_Code_Packed_Format_for_Japanese",
00253 "ja_JP:                 Extended_UNIX_Code_Packed_Format_for_Japanese",
00254 "ko_KR.eucKR:           EUC-KR",
00255 "ko_KR:                 EUC-KR",
00256 "nl_BE.ISO8859-1:       ISO_8859-1:1987",
00257 "nl_NL.ISO8859-1:       ISO_8859-1:1987",
00258 "no_NO.ISO8859-1:       ISO_8859-1:1987",
00259 "pl_PL.ISO8859-2:       ISO_8859-2:1987",
00260 "pl_PL:                 ISO_8859-2:1987",
00261 "pt_PT.ISO8859-1:       ISO_8859-1:1987",
00262 "sk_SK.ISO8859-2:       ISO_8859-2:1987",
00263 "sk_SK:                 ISO_8859-2:1987",
00264 "sv_SE.ISO8859-1:       ISO_8859-1:1987",
00265 "zh_CN:                 GB2312",
00266 "zh_HK.big5:            Big5",
00267 "zh_HK.eucTW:           cns11643_1",
00268 "zh_TW.big5:            Big5",
00269 "zh_TW.big5@chuyin:     Big5",
00270 "zh_TW.big5@radical:    Big5",
00271 "zh_TW.big5@stroke:     Big5",
00272 "zh_TW.eucTW:           cns11643_1",
00273 "zh_TW.eucTW@chuyin:    cns11643_1",
00274 "zh_TW.eucTW@radical:   cns11643_1",
00275 "zh_TW.eucTW@stroke:    cns11643_1",
00276 "zh_TW:                 cns11643_1",
00277 NULL
00278 };
00279 #elif defined(HPUX)
00280 const char *CHARCONVTABLE[] =
00281 {
00282 "! This table maps the host's locale names to LIBNLS charsets",
00283 "!",
00284 "C:                  ISO_8859-1:1987",
00285 "ja_JP:                     Extended_UNIX_Code_Packed_Format_for_Japanese",
00286 "ja_JP.SJIS:         Shift_JIS",
00287 "ja_JP.eucJP:        Extended_UNIX_Code_Packed_Format_for_Japanese",
00288 "es_ES:                     ISO_8859-1:1987",
00289 "es_ES.iso88591:     ISO_8859-1:1987",
00290 "sv_SE:                     ISO_8859-1:1987",
00291 "sv_SE.iso88591:     ISO_8859-1:1987",
00292 "da_DK:                     ISO_8859-1:1987",
00293 "da_DK.iso88591:     ISO_8859-1:1987",
00294 "nl_NL:                     ISO_8859-1:1987",
00295 "nl_NL.iso88591:     ISO_8859-1:1987",
00296 "en:                 ISO_8859-1:1987",
00297 "en_GB:                     ISO_8859-1:1987",
00298 "en_GB.iso88591:     ISO_8859-1:1987",
00299 "en_US:                     ISO_8859-1:1987",
00300 "en_US.iso88591:     ISO_8859-1:1987",
00301 "fi_FI:                     ISO_8859-1:1987",
00302 "fi_FI.iso88591:     ISO_8859-1:1987",
00303 "fr_CA:                     ISO_8859-1:1987",
00304 "fr_CA.iso88591:     ISO_8859-1:1987",
00305 "fr_FR:                     ISO_8859-1:1987",
00306 "fr_FR.iso88591:     ISO_8859-1:1987",
00307 "de_DE:                     ISO_8859-1:1987",
00308 "de_DE.iso88591:     ISO_8859-1:1987",
00309 "is_IS:                     ISO_8859-1:1987",
00310 "is_IS.iso88591:     ISO_8859-1:1987",
00311 "it_IT:                     ISO_8859-1:1987",
00312 "it_IT.iso88591:     ISO_8859-1:1987",
00313 "no_NO:                     ISO_8859-1:1987",
00314 "no_NO.iso88591:     ISO_8859-1:1987",
00315 "pt_PT:                     ISO_8859-1:1987",
00316 "pt_PT.iso88591:     ISO_8859-1:1987",
00317 "hu_HU:                     ISO_8859-2:1987",
00318 "hu_HU.iso88592:     ISO_8859-2:1987",
00319 "cs_CZ:                     ISO_8859-2:1987",
00320 "cs_CZ.iso88592:     ISO_8859-2:1987",
00321 "pl_PL:                     ISO_8859-2:1987",
00322 "pl_PL.iso88592:     ISO_8859-2:1987",
00323 "ro_RO:                     ISO_8859-2:1987",
00324 "ro_RO.iso88592:     ISO_8859-2:1987",
00325 "hr_HR:                     ISO_8859-2:1987",
00326 "hr_HR.iso88592:     ISO_8859-2:1987",
00327 "sk_SK:                     ISO_8859-2:1987",
00328 "sk_SK.iso88592:     ISO_8859-2:1987",
00329 "sl_SI:                     ISO_8859-2:1987",
00330 "sl_SI.iso88592:     ISO_8859-2:1987",
00331 "american.iso88591:     ISO_8859-1:1987",
00332 "bulgarian:             ISO_8859-2:1987",
00333 "c-french.iso88591:     ISO_8859-1:1987",
00334 "chinese-s:             GB2312",
00335 "chinese-t.big5:                Big5",
00336 "czech:                 ISO_8859-2:1987",
00337 "danish.iso88591:       ISO_8859-1:1987",
00338 "dutch.iso88591:                ISO_8859-1:1987",
00339 "english.iso88591:      ISO_8859-1:1987",
00340 "finnish.iso88591:      ISO_8859-1:1987",
00341 "french.iso88591:       ISO_8859-1:1987",
00342 "german.iso88591:       ISO_8859-1:1987",
00343 "hungarian:             ISO_8859-2:1987",
00344 "icelandic.iso88591:    ISO_8859-1:1987",
00345 "italian.iso88591:      ISO_8859-1:1987",
00346 "japanese.euc:          Extended_UNIX_Code_Packed_Format_for_Japanese",
00347 "japanese:              Shift_JIS",
00348 "katakana:              Shift_JIS",
00349 "korean:                        EUC-KR",
00350 "norwegian.iso88591:    ISO_8859-1:1987",
00351 "polish:                        ISO_8859-2:1987",
00352 "portuguese.iso88591:   ISO_8859-1:1987",
00353 "rumanian:              ISO_8859-2:1987",
00354 "serbocroatian:         ISO_8859-2:1987",
00355 "slovene:               ISO_8859-2:1987",
00356 "spanish.iso88591:      ISO_8859-1:1987",
00357 "swedish.iso88591:      ISO_8859-1:1987",
00358 NULL
00359 };
00360 #elif defined(AIX)
00361 const char *CHARCONVTABLE[] =
00362 {
00363 "! This table maps the host's locale names to LIBNLS charsets",
00364 "!",
00365 "C:                     ISO_8859-1:1987",
00366 "En_JP.IBM-932:         Shift_JIS",
00367 "En_JP:                 Shift_JIS",
00368 "Ja_JP.IBM-932:         Shift_JIS",
00369 "Ja_JP:                 Shift_JIS",
00370 "da_DK.ISO8859-1:       ISO_8859-1:1987",
00371 "da_DK:                 ISO_8859-1:1987",
00372 "de_CH.ISO8859-1:       ISO_8859-1:1987",
00373 "de_CH:                 ISO_8859-1:1987",
00374 "de_DE.ISO8859-1:       ISO_8859-1:1987",
00375 "de_DE:                 ISO_8859-1:1987",
00376 "en_GB.ISO8859-1:       ISO_8859-1:1987",
00377 "en_GB:                 ISO_8859-1:1987",
00378 "en_JP.IBM-eucJP:       Extended_UNIX_Code_Packed_Format_for_Japanese",
00379 "en_JP:                 Extended_UNIX_Code_Packed_Format_for_Japanese",
00380 "en_KR.IBM-eucKR:       EUC-KR",
00381 "en_KR:                 EUC-KR",
00382 "en_TW.IBM-eucTW:       cns11643_1",
00383 "en_TW:                 cns11643_1",
00384 "en_US.ISO8859-1:       ISO_8859-1:1987",
00385 "en_US:                 ISO_8859-1:1987",
00386 "es_ES.ISO8859-1:       ISO_8859-1:1987",
00387 "es_ES:                 ISO_8859-1:1987",
00388 "fi_FI.ISO8859-1:       ISO_8859-1:1987",
00389 "fi_FI:                 ISO_8859-1:1987",
00390 "fr_BE.ISO8859-1:       ISO_8859-1:1987",
00391 "fr_BE:                 ISO_8859-1:1987",
00392 "fr_CA.ISO8859-1:       ISO_8859-1:1987",
00393 "fr_CA:                 ISO_8859-1:1987",
00394 "fr_CH.ISO8859-1:       ISO_8859-1:1987",
00395 "fr_CH:                 ISO_8859-1:1987",
00396 "fr_FR.ISO8859-1:       ISO_8859-1:1987",
00397 "fr_FR:                 ISO_8859-1:1987",
00398 "is_IS.ISO8859-1:       ISO_8859-1:1987",
00399 "is_IS:                 ISO_8859-1:1987",
00400 "it_IT.ISO8859-1:       ISO_8859-1:1987",
00401 "it_IT:                 ISO_8859-1:1987",
00402 "ja_JP.IBM-eucJP:       Extended_UNIX_Code_Packed_Format_for_Japanese",
00403 "ja_JP:                 Extended_UNIX_Code_Packed_Format_for_Japanese",
00404 "ko_KR.IBM-eucKR:       EUC-KR",
00405 "ko_KR:                 EUC-KR",
00406 "nl_BE.ISO8859-1:       ISO_8859-1:1987",
00407 "nl_BE:                 ISO_8859-1:1987",
00408 "nl_NL.ISO8859-1:       ISO_8859-1:1987",
00409 "nl_NL:                 ISO_8859-1:1987",
00410 "no_NO.ISO8859-1:       ISO_8859-1:1987",
00411 "no_NO:                 ISO_8859-1:1987",
00412 "pt_PT.ISO8859-1:       ISO_8859-1:1987",
00413 "pt_PT:                 ISO_8859-1:1987",
00414 "sv_SE.ISO8859-1:       ISO_8859-1:1987",
00415 "sv_SE:                 ISO_8859-1:1987",
00416 "zh_TW.IBM-eucTW:       cns11643_1",
00417 "zh_TW:                 cns11643_1",
00418 NULL
00419 };
00420 #else   // sunos by default
00421 const char *CHARCONVTABLE[] =
00422 {
00423 "! This table maps the host's locale names to LIBNLS charsets",
00424 "!",
00425 "C:             ISO_8859-1:1987",
00426 "de:            ISO_8859-1:1987",
00427 "en_US:         ISO_8859-1:1987",
00428 "es:            ISO_8859-1:1987",
00429 "fr:            ISO_8859-1:1987",
00430 "iso_8859_1:    ISO_8859-1:1987",
00431 "it:            ISO_8859-1:1987",
00432 "ja:            Extended_UNIX_Code_Packed_Format_for_Japanese",
00433 "ja_JP.EUC:     Extended_UNIX_Code_Packed_Format_for_Japanese",
00434 "japanese:      Extended_UNIX_Code_Packed_Format_for_Japanese",
00435 "ko:            EUC-KR",
00436 "sv:            ISO_8859-1:1987",
00437 "zh:            GB2312",
00438 "zh_TW:         cns11643_1",
00439 NULL
00440 };
00441 #endif
00442  
00443 #define BSZ     256
00444  
00445 char *
00446 GetCharsetFromLocale(char *locale)
00447 {
00448     char *tmpcharset = NULL;
00449     char buf[BSZ];
00450     char *p;
00451     const char *line;
00452     int i=0;
00453  
00454     line = CHARCONVTABLE[i];
00455     while (line != NULL)
00456     {
00457        if (*line == 0)
00458        { 
00459           break;
00460        } 
00461  
00462        strcpy(buf, line);
00463        line = CHARCONVTABLE[++i];
00464  
00465        if (!*buf || *buf == '!')
00466        { 
00467           continue;
00468        } 
00469        p = strchr(buf, ':');
00470        if (p == NULL)
00471        { 
00472           tmpcharset = NULL;
00473           break;
00474        } 
00475        *p = 0;
00476        if (strcmp(buf, locale) == 0) {
00477           while (*++p == ' ' || *p == '\t')
00478              ;
00479           if (isalpha(*p)) {
00480              tmpcharset = strdup(p);
00481           } else
00482              tmpcharset = NULL;
00483  
00484           break;
00485        }
00486     }
00487     return tmpcharset;
00488 }
00489  
00490 #endif /* Not defined XP_WIN32 */
00491 
00492 #ifdef XP_WIN32
00493 char *_convertor(const char *instr, int bFromUTF8)
00494 {
00495     char  *outstr = NULL;
00496     int    inlen, wclen, outlen;
00497     LPWSTR wcstr;
00498          
00499     if (instr == NULL)
00500             return NULL;
00501 
00502     if ((inlen = strlen(instr)) <= 0)
00503             return NULL;
00504  
00505     /* output never becomes longer than input,
00506     ** thus we don't have to ask for the length
00507     */
00508     wcstr = (LPWSTR) malloc( sizeof( WCHAR ) * (inlen+1) );
00509     if (!wcstr)
00510         return NULL;
00511  
00512     wclen = MultiByteToWideChar(bFromUTF8 ? CP_UTF8 : CP_ACP, 0, instr,
00513                                  inlen, wcstr, inlen);
00514     outlen = WideCharToMultiByte(bFromUTF8 ? CP_ACP : CP_UTF8, 0, wcstr,
00515                                   wclen, NULL, 0, NULL, NULL);
00516  
00517     if (outlen > 0) {
00518         outstr = (char *) malloc(outlen + 2);
00519         outlen = WideCharToMultiByte(bFromUTF8 ? CP_ACP : CP_UTF8, 0, wcstr,
00520                                       wclen, outstr, outlen, NULL, NULL);
00521         if (outlen > 0)
00522             *(outstr+outlen) = _T('\0');
00523         else
00524             return NULL;
00525     }
00526     free( wcstr );
00527     return outstr;
00528 }
00529 #endif
00530 
00531 char *
00532 ldaptool_local2UTF8( const char *src )
00533 {
00534     char *utf8;
00535 #ifndef XP_WIN32
00536     char *locale, *newcharset;
00537     size_t outLen, resultLen;
00538     NLS_ErrorCode err;
00539  
00540     if (src == NULL)
00541     {
00542       return NULL;
00543     }
00544     else if (*src == 0)
00545     {
00546         utf8 = strdup(src);
00547         return utf8;
00548     }
00549  
00550     if( (ldaptool_charset != NULL) && (!strcmp( ldaptool_charset, "" ))
00551            && (!charsetset) )
00552     {
00553         locale = GetNormalizedLocaleName();
00554         ldaptool_charset = GetCharsetFromLocale(locale);
00555         free( locale );
00556         charsetset = 1;
00557     }
00558     else
00559     if( (ldaptool_charset != NULL) && strcmp( ldaptool_charset, "" )
00560            && (!charsetset) )
00561     {
00562         newcharset = GetCharsetFromLocale( ldaptool_charset );
00563         free( ldaptool_charset );
00564         ldaptool_charset = newcharset;
00565         charsetset = 1;
00566     }
00567 
00568     if (ldaptool_charset == NULL) {
00569         return strdup(src);
00570     }
00571 
00572     if (NLS_EncInitialize(NULL, ldaptool_convdir) != NLS_SUCCESS ||
00573        NLS_RegisterStaticLibrary(_STATICLINK_NSJPN_) != NLS_SUCCESS ||
00574        NLS_RegisterStaticLibrary(_STATICLINK_NSCCK_) != NLS_SUCCESS ||
00575        NLS_RegisterStaticLibrary(_STATICLINK_NSSB_) != NLS_SUCCESS) {
00576         return strdup(src);
00577     }
00578 
00579     outLen = NLS_GetResultBufferSize( (byte *) src,
00580                                       strlen( src ) * sizeof(char), 
00581                                       ldaptool_charset,
00582                                       NLS_ENCODING_UTF_8 ); 
00583 
00584     utf8 =  (char *) malloc( outLen/sizeof(UniChar) ); 
00585     if( utf8 == NULL )
00586        return strdup(src);
00587 
00588     err = NLS_ConvertBuffer( ldaptool_charset,
00589                              NLS_ENCODING_UTF_8, 
00590                              (byte*)src,
00591                              strlen(src) * sizeof(char),
00592                              (byte*)utf8,
00593                              outLen, 
00594                              &resultLen ); 
00595 
00596     NLS_EncTerminate();
00597  
00598 #else
00599     utf8 = _convertor(src, FALSE);
00600     if( utf8 == NULL )
00601         utf8 = strdup(src);
00602 #endif
00603  
00604     return utf8;
00605 }
00606 #endif /* HAVE_LIBNLS */
00607 
00608 #ifndef HAVE_LIBNLS
00609 #ifdef __cplusplus
00610 }
00611 #endif
00612 #endif
00613