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 libraries 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 */
74 #include <EUSCompat.h>
86 int use_octal = FALSE;
90 is_comment(char line[MAX_LINE_LEN])
95 if ( strcmp((char *)ch, COMMENT_SYMBOL) == 0 ) {
104 match_locale(char *locale, char line[MAX_LINE_LEN])
106 char loc[MAX_LINE_LEN];
108 if ( !isalpha(line[0]) ) {
111 (void) sscanf(line, "%s", loc);
112 if ( strcmp(loc, locale) == 0 ) {
120 * The following code was very similar to what was developped for dtmail's
121 * conversion mechanism. See libDtMail/RFC/RFCMIME.C ! Have fun !
123 #define WORKSIZE 1024
124 static void *_i18nwork1 = NULL;
125 static unsigned long _i18nsize1 = 0;
126 static int shouldAlloc1 = ~0;
127 static int isFirstCall = ~0;
128 static iconv_t CD = (iconv_t)-1;
129 static int amI_932 = ~0;
131 #ifdef ICONV_INBUF_CONST
132 # define ICONV_INBUF_TYPE const char **
134 # define ICONV_INBUF_TYPE char **
137 void _converter_( void *from, unsigned long from_len,
138 void **to, unsigned long *to_len )
143 size_t OutBytesLeft = 0;
144 size_t _OutBytesLeft = 0;
146 size_t converted_num = 0;
151 if ( shouldAlloc1 ) {
152 /* Obtain work area */
153 _i18nwork1 = (size_t *)malloc( WORKSIZE );
158 _i18nsize1 = WORKSIZE;
163 if ( ( CD = iconv_open( "IBM-eucJP", "IBM-932" ) ) == (iconv_t)-1 )
164 return; /* no converter */
165 amI_932 = !strncasecmp( "IBM-932", nl_langinfo( CODESET ), 7 );
170 if ( ( !amI_932 ) || ( CD == (iconv_t)-1 ) )
173 InBuf = (char *)from;
174 InBytesLeft = from_len;
175 OutBytesLeft = _i18nsize1;
176 OutBuf = (char *)_i18nwork1;
182 * +----------------------------+
184 * +----------------------------+
185 * <-------------------------->
194 * +----------------------------+
196 * +----------------------------+
197 * <-------------------------->
201 iconv_ret = iconv( CD, (ICONV_INBUF_TYPE)&InBuf, &InBytesLeft,
202 &OutBuf, &OutBytesLeft );
203 if ( iconv_ret == 0 ) {
207 * +----------------------------+
208 * |XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
209 * +----------------------------+
216 * +----------------------------+
217 * |XXXXXXXXXXXXXXXXX| | | |
218 * +----------------------------+
219 * <---------------> <-------->
220 * converted_num OutBytesLeft
222 converted_num = (unsigned long)((char *)OutBuf-(char *)_i18nwork1);
223 *to = (void *)_i18nwork1;
224 *to_len = (unsigned long)converted_num;
227 if ( errno == E2BIG ) {
228 /* Overflow. still data is left.
231 * +----------------------------+
232 * |XXXXXXXXXXXXXX| | | |
233 * +----------------------------+
240 * +----------------------------+
241 * |XXXXXXXXXXXXXXXXXXXXXXXXXXX |
242 * +----------------------------+
243 * <------------------------->
244 * converted_num OutBytesLeft=?
248 /* Check how many converted already. */
251 (unsigned long)((char *)OutBuf - (char *)_i18nwork1);
252 _i18nsize1 += WORKSIZE;
253 _p = realloc( _i18nwork1, _i18nsize1 );
264 OutBuf = (char *)((char*)_i18nwork1 + converted_num);
265 OutBytesLeft += WORKSIZE;
278 if ( _i18nsize1 > converted_num ) {
279 ((char *)_i18nwork1)[converted_num] = '\0';
280 } else { /* _i18nsize1 == converted_num */
284 _p = realloc( _i18nwork1, _i18nsize1 );
295 ((char *)_i18nwork1)[converted_num] = '\0';
300 /* The following routine is specific to using FMapType 3 composite fonts
301 * in postscript. Kanji, Asian specific?
304 euc_to_octal(char *srcStr)
308 static char dstStr[512];
310 int len = cm_strlen(srcStr);
313 memset(dstStr, 0, sizeof(dstStr));
315 bzero(dstStr, sizeof(dstStr));
317 for (i = 0; i < len; i++) {
319 if (!isEUC(srcStr[i])) {
321 /* NOT NEEDED FOR FMapType 4 (or 5)
322 cm_strcat(dstStr, "\\377\\000");
327 if (isEUC(srcStr[i])) {
329 /* NOT NEEDED FOR FMapType 4 (or 5)
330 cm_strcat(dstStr, "\\377\\001");
335 sprintf(buf, "\\%3.3o\\%3.3o", srcStr[i] & 0xff, srcStr[i+1] & 0xff);
339 sprintf(buf, "%c", srcStr[i]);
341 cm_strcat(dstStr, buf);
347 /* This routine should be in libdeskset.
348 * This routine uses fconvert() to avoid locale conversion.
350 /* 310 characters are the minimum needed to accommodate any double-precision
351 * value + 1 null terminator.
355 * Returns a null terminated formatted string.
356 * If error is encountered, such as malloc() failed, then return NULL.
357 * The caller of this function should beware that the return value is
358 * a static buffer declared within this function and the value of it may
362 cm_printf(double value, int decimal_pt)
367 int formatted_cnt = 0;
370 static char *formatted = NULL;
372 if ( formatted != NULL ) {
376 if ( (value == (double)0) && (decimal_pt == 0) ) {
377 formatted = (char *)cm_strdup("0");
380 if ( (buf = (char *)malloc(DBL_SIZE + decimal_pt)) == NULL ) {
383 if ( (formatted = (char *)calloc(1, DBL_SIZE + decimal_pt)) == NULL ) {
388 fconvert(value, decimal_pt, &deci_pt, &sign, buf);
389 #elif defined(CSRG_BASED)
390 snprintf(buf, decimal_pt, "%f", value);
392 /* this version, available on the HP and AIX machine is not reentrant. */
394 strcpy(buf, fcvt(value, decimal_pt, &deci_pt, &sign));
397 strcpy(formatted, "-");
399 buf_len = deci_pt + decimal_pt;
401 strncat(formatted, buf, deci_pt);
403 strcat(formatted, "0");
405 if ( deci_pt == buf_len ) {
406 strcat(formatted, "\0");
410 strcat(formatted, ".");
411 for ( formatted_cnt = strlen(formatted), buf_cnt = deci_pt; buf_cnt < buf_len; buf_cnt++, formatted_cnt++ ) {
412 formatted[formatted_cnt] = buf[buf_cnt];
414 formatted[formatted_cnt] = '\0';