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 */
80 /* Iconv not defined for linux. Use the EUSCompat stubs instead. */
84 #include <EUSCompat.h>
88 int use_octal = FALSE;
92 is_comment(char line[MAX_LINE_LEN])
97 if ( strcmp((char *)ch, COMMENT_SYMBOL) == 0 ) {
106 match_locale(char *locale, char line[MAX_LINE_LEN])
108 char loc[MAX_LINE_LEN];
110 if ( !isalpha(line[0]) ) {
113 (void) sscanf(line, "%s", loc);
114 if ( strcmp(loc, locale) == 0 ) {
122 * The following code was very similar to what was developped for dtmail's
123 * conversion mechanism. See libDtMail/RFC/RFCMIME.C ! Have fun !
125 #define WORKSIZE 1024
126 static void *_i18nwork1 = NULL;
127 static unsigned long _i18nsize1 = 0;
128 static int shouldAlloc1 = ~0;
129 static int isFirstCall = ~0;
130 static iconv_t CD = (iconv_t)-1;
131 static int amI_932 = ~0;
133 #ifdef ICONV_INBUF_CONST
134 # define ICONV_INBUF_TYPE const char **
136 # define ICONV_INBUF_TYPE char **
139 void _converter_( void *from, unsigned long from_len,
140 void **to, unsigned long *to_len )
145 size_t OutBytesLeft = 0;
146 size_t _OutBytesLeft = 0;
148 size_t converted_num = 0;
153 if ( shouldAlloc1 ) {
154 /* Obtain work area */
155 _i18nwork1 = (size_t *)malloc( WORKSIZE );
160 _i18nsize1 = WORKSIZE;
165 if ( ( CD = iconv_open( "IBM-eucJP", "IBM-932" ) ) == (iconv_t)-1 )
166 return; /* no converter */
167 amI_932 = !strncasecmp( "IBM-932", nl_langinfo( CODESET ), 7 );
172 if ( ( !amI_932 ) || ( CD == (iconv_t)-1 ) )
175 InBuf = (char *)from;
176 InBytesLeft = from_len;
177 OutBytesLeft = _i18nsize1;
178 OutBuf = (char *)_i18nwork1;
184 * +----------------------------+
186 * +----------------------------+
187 * <-------------------------->
196 * +----------------------------+
198 * +----------------------------+
199 * <-------------------------->
203 iconv_ret = iconv( CD, (ICONV_INBUF_TYPE)&InBuf, &InBytesLeft,
204 &OutBuf, &OutBytesLeft );
205 if ( iconv_ret == 0 ) {
209 * +----------------------------+
210 * |XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
211 * +----------------------------+
218 * +----------------------------+
219 * |XXXXXXXXXXXXXXXXX| | | |
220 * +----------------------------+
221 * <---------------> <-------->
222 * converted_num OutBytesLeft
224 converted_num = (unsigned long)((char *)OutBuf-(char *)_i18nwork1);
225 *to = (void *)_i18nwork1;
226 *to_len = (unsigned long)converted_num;
229 if ( errno == E2BIG ) {
230 /* Overflow. still data is left.
233 * +----------------------------+
234 * |XXXXXXXXXXXXXX| | | |
235 * +----------------------------+
242 * +----------------------------+
243 * |XXXXXXXXXXXXXXXXXXXXXXXXXXX |
244 * +----------------------------+
245 * <------------------------->
246 * converted_num OutBytesLeft=?
250 /* Check how many converted already. */
253 (unsigned long)((char *)OutBuf - (char *)_i18nwork1);
254 _i18nsize1 += WORKSIZE;
255 _p = realloc( _i18nwork1, _i18nsize1 );
266 OutBuf = (char *)((char*)_i18nwork1 + converted_num);
267 OutBytesLeft += WORKSIZE;
280 if ( _i18nsize1 > converted_num ) {
281 ((char *)_i18nwork1)[converted_num] = '\0';
282 } else { /* _i18nsize1 == converted_num */
286 _p = realloc( _i18nwork1, _i18nsize1 );
297 ((char *)_i18nwork1)[converted_num] = '\0';
302 /* The following routine is specific to using FMapType 3 composite fonts
303 * in postscript. Kanji, Asian specific?
306 euc_to_octal(char *srcStr)
310 static char dstStr[512];
312 int len = cm_strlen(srcStr);
315 memset(dstStr, 0, sizeof(dstStr));
317 bzero(dstStr, sizeof(dstStr));
319 for (i = 0; i < len; i++) {
321 if (!isEUC(srcStr[i])) {
323 /* NOT NEEDED FOR FMapType 4 (or 5)
324 cm_strcat(dstStr, "\\377\\000");
329 if (isEUC(srcStr[i])) {
331 /* NOT NEEDED FOR FMapType 4 (or 5)
332 cm_strcat(dstStr, "\\377\\001");
337 sprintf(buf, "\\%3.3o\\%3.3o", srcStr[i] & 0xff, srcStr[i+1] & 0xff);
341 sprintf(buf, "%c", srcStr[i]);
343 cm_strcat(dstStr, buf);
349 /* This routine should be in libdeskset.
350 * This routine uses fconvert() to avoid locale conversion.
352 /* 310 characters are the minimum needed to accommodate any double-precision
353 * value + 1 null terminator.
357 * Returns a null terminated formatted string.
358 * If error is encountered, such as malloc() failed, then return NULL.
359 * The caller of this function should beware that the return value is
360 * a static buffer declared within this function and the value of it may
364 cm_printf(double value, int decimal_pt)
369 int formatted_cnt = 0;
372 static char *formatted = NULL;
374 if ( formatted != NULL ) {
378 if ( (value == (double)0) && (decimal_pt == 0) ) {
379 formatted = (char *)cm_strdup("0");
382 if ( (buf = (char *)malloc(DBL_SIZE + decimal_pt)) == NULL ) {
385 if ( (formatted = (char *)calloc(1, DBL_SIZE + decimal_pt)) == NULL ) {
390 fconvert(value, decimal_pt, &deci_pt, &sign, buf);
391 #elif defined(CSRG_BASED)
392 snprintf(buf, decimal_pt, "%f", value);
394 /* this version, available on the HP and AIX machine is not reentrant. */
396 strcpy(buf, fcvt(value, decimal_pt, &deci_pt, &sign));
399 strcpy(formatted, "-");
401 buf_len = deci_pt + decimal_pt;
403 strncat(formatted, buf, deci_pt);
405 strcat(formatted, "0");
407 if ( deci_pt == buf_len ) {
408 strcat(formatted, "\0");
412 strcat(formatted, ".");
413 for ( formatted_cnt = strlen(formatted), buf_cnt = deci_pt; buf_cnt < buf_len; buf_cnt++, formatted_cnt++ ) {
414 formatted[formatted_cnt] = buf[buf_cnt];
416 formatted[formatted_cnt] = '\0';