2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /*******************************************************************************
27 ** $TOG: cm_i18n.c /main/9 1998/04/01 17:26:31 mgreess $
29 ** RESTRICTED CONFIDENTIAL INFORMATION:
31 ** The information in this document is subject to special
32 ** restrictions in a confidential disclosure agreement between
33 ** HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
34 ** document outside HP, IBM, Sun, USL, SCO, or Univel without
35 ** Sun's specific written approval. This document and all copies
36 ** and derivative works thereof must be returned or destroyed at
39 ** Copyright 1993 Sun Microsystems, Inc. All rights reserved.
41 *******************************************************************************/
44 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
45 * (c) Copyright 1993, 1994 International Business Machines Corp. *
46 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
47 * (c) Copyright 1993, 1994 Novell, Inc. *
62 This include has been left out for the moment because of a
63 definition of the symbol "single" in rtable4.h. This
64 should be fixed in the future.
66 #include <floatingpoint.h>
69 #include <sys/param.h> /* for MAXPATHLEN */
73 /* Iconv not defined for linux. Use the EUSCompat stubs instead. */
77 #include <EUSCompat.h>
89 int use_octal = FALSE;
93 is_comment(char line[MAX_LINE_LEN])
98 if ( strcmp((char *)ch, COMMENT_SYMBOL) == 0 ) {
107 match_locale(char *locale, char line[MAX_LINE_LEN])
109 char loc[MAX_LINE_LEN];
111 if ( !isalpha(line[0]) ) {
114 (void) sscanf(line, "%s", loc);
115 if ( strcmp(loc, locale) == 0 ) {
123 * The following code was very similar to what was developped for dtmail's
124 * conversion mechanism. See libDtMail/RFC/RFCMIME.C ! Have fun !
126 #define WORKSIZE 1024
127 static void *_i18nwork1 = NULL;
128 static unsigned long _i18nsize1 = 0;
129 static int shouldAlloc1 = ~0;
130 static int isFirstCall = ~0;
131 static iconv_t CD = (iconv_t)-1;
132 static int amI_932 = ~0;
134 #ifdef ICONV_INBUF_CONST
135 # define ICONV_INBUF_TYPE const char **
137 # define ICONV_INBUF_TYPE char **
140 void _converter_( void *from, unsigned long from_len,
141 void **to, unsigned long *to_len )
146 size_t OutBytesLeft = 0;
147 size_t _OutBytesLeft = 0;
149 size_t converted_num = 0;
154 if ( shouldAlloc1 ) {
155 /* Obtain work area */
156 _i18nwork1 = (size_t *)malloc( WORKSIZE );
161 _i18nsize1 = WORKSIZE;
166 if ( ( CD = iconv_open( "IBM-eucJP", "IBM-932" ) ) == (iconv_t)-1 )
167 return; /* no converter */
168 amI_932 = !strncasecmp( "IBM-932", nl_langinfo( CODESET ), 7 );
173 if ( ( !amI_932 ) || ( CD == (iconv_t)-1 ) )
176 InBuf = (char *)from;
177 InBytesLeft = from_len;
178 OutBytesLeft = _i18nsize1;
179 OutBuf = (char *)_i18nwork1;
185 * +----------------------------+
187 * +----------------------------+
188 * <-------------------------->
197 * +----------------------------+
199 * +----------------------------+
200 * <-------------------------->
204 iconv_ret = iconv( CD, (ICONV_INBUF_TYPE)&InBuf, &InBytesLeft,
205 &OutBuf, &OutBytesLeft );
206 if ( iconv_ret == 0 ) {
210 * +----------------------------+
211 * |XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
212 * +----------------------------+
219 * +----------------------------+
220 * |XXXXXXXXXXXXXXXXX| | | |
221 * +----------------------------+
222 * <---------------> <-------->
223 * converted_num OutBytesLeft
225 converted_num = (unsigned long)((char *)OutBuf-(char *)_i18nwork1);
226 *to = (void *)_i18nwork1;
227 *to_len = (unsigned long)converted_num;
230 if ( errno == E2BIG ) {
231 /* Overflow. still data is left.
234 * +----------------------------+
235 * |XXXXXXXXXXXXXX| | | |
236 * +----------------------------+
243 * +----------------------------+
244 * |XXXXXXXXXXXXXXXXXXXXXXXXXXX |
245 * +----------------------------+
246 * <------------------------->
247 * converted_num OutBytesLeft=?
251 /* Check how many converted already. */
254 (unsigned long)((char *)OutBuf - (char *)_i18nwork1);
255 _i18nsize1 += WORKSIZE;
256 _p = realloc( _i18nwork1, _i18nsize1 );
267 OutBuf = (char *)((char*)_i18nwork1 + converted_num);
268 OutBytesLeft += WORKSIZE;
281 if ( _i18nsize1 > converted_num ) {
282 ((char *)_i18nwork1)[converted_num] = '\0';
283 } else { /* _i18nsize1 == converted_num */
287 _p = realloc( _i18nwork1, _i18nsize1 );
298 ((char *)_i18nwork1)[converted_num] = '\0';
303 /* The following routine is specific to using FMapType 3 composite fonts
304 * in postscript. Kanji, Asian specific?
307 euc_to_octal(char *srcStr)
311 static char dstStr[512];
313 int len = cm_strlen(srcStr);
316 memset(dstStr, 0, sizeof(dstStr));
318 bzero(dstStr, sizeof(dstStr));
320 for (i = 0; i < len; i++) {
322 if (!isEUC(srcStr[i])) {
324 /* NOT NEEDED FOR FMapType 4 (or 5)
325 cm_strcat(dstStr, "\\377\\000");
330 if (isEUC(srcStr[i])) {
332 /* NOT NEEDED FOR FMapType 4 (or 5)
333 cm_strcat(dstStr, "\\377\\001");
338 sprintf(buf, "\\%3.3o\\%3.3o", srcStr[i] & 0xff, srcStr[i+1] & 0xff);
342 sprintf(buf, "%c", srcStr[i]);
344 cm_strcat(dstStr, buf);
350 /* This routine should be in libdeskset.
351 * This routine uses fconvert() to avoid locale conversion.
353 /* 310 characters are the minimum needed to accommodate any double-precision
354 * value + 1 null terminator.
358 * Returns a null terminated formatted string.
359 * If error is encountered, such as malloc() failed, then return NULL.
360 * The caller of this function should beware that the return value is
361 * a static buffer declared within this function and the value of it may
365 cm_printf(double value, int decimal_pt)
370 int formatted_cnt = 0;
373 static char *formatted = NULL;
375 if ( formatted != NULL ) {
379 if ( (value == (double)0) && (decimal_pt == 0) ) {
380 formatted = (char *)cm_strdup("0");
383 if ( (buf = (char *)malloc(DBL_SIZE + decimal_pt)) == NULL ) {
386 if ( (formatted = (char *)calloc(1, DBL_SIZE + decimal_pt)) == NULL ) {
391 fconvert(value, decimal_pt, &deci_pt, &sign, buf);
392 #elif defined(CSRG_BASED)
393 snprintf(buf, decimal_pt, "%f", value);
395 /* this version, available on the HP and AIX machine is not reentrant. */
397 strcpy(buf, fcvt(value, decimal_pt, &deci_pt, &sign));
400 strcpy(formatted, "-");
402 buf_len = deci_pt + decimal_pt;
404 strncat(formatted, buf, deci_pt);
406 strcat(formatted, "0");
408 if ( deci_pt == buf_len ) {
409 strcat(formatted, "\0");
413 strcat(formatted, ".");
414 for ( formatted_cnt = strlen(formatted), buf_cnt = deci_pt; buf_cnt < buf_len; buf_cnt++, formatted_cnt++ ) {
415 formatted[formatted_cnt] = buf[buf_cnt];
417 formatted[formatted_cnt] = '\0';