1 /*******************************************************************************
5 ** $TOG: cm_i18n.c /main/9 1998/04/01 17:26:31 mgreess $
7 ** RESTRICTED CONFIDENTIAL INFORMATION:
9 ** The information in this document is subject to special
10 ** restrictions in a confidential disclosure agreement between
11 ** HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
12 ** document outside HP, IBM, Sun, USL, SCO, or Univel without
13 ** Sun's specific written approval. This document and all copies
14 ** and derivative works thereof must be returned or destroyed at
17 ** Copyright 1993 Sun Microsystems, Inc. All rights reserved.
19 *******************************************************************************/
22 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
23 * (c) Copyright 1993, 1994 International Business Machines Corp. *
24 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
25 * (c) Copyright 1993, 1994 Novell, Inc. *
40 This include has been left out for the moment because of a
41 definition of the symbol "single" in rtable4.h. This
42 should be fixed in the future.
44 #include <floatingpoint.h>
47 #include <sys/param.h> /* for MAXPATHLEN */
58 /* Iconv not defined for linux. Use the EUSCompat stubs instead. */
62 #include <EUSCompat.h>
66 int use_octal = FALSE;
70 is_comment(char line[MAX_LINE_LEN])
75 if ( strcmp((char *)ch, COMMENT_SYMBOL) == 0 ) {
84 match_locale(char *locale, char line[MAX_LINE_LEN])
86 char loc[MAX_LINE_LEN];
88 if ( !isalpha(line[0]) ) {
91 (void) sscanf(line, "%s", loc);
92 if ( strcmp(loc, locale) == 0 ) {
100 * The following code was very similar to what was developped for dtmail's
101 * conversion mechanism. See libDtMail/RFC/RFCMIME.C ! Have fun !
103 #define WORKSIZE 1024
104 static void *_i18nwork1 = NULL;
105 static unsigned long _i18nsize1 = 0;
106 static int shouldAlloc1 = ~0;
107 static int isFirstCall = ~0;
108 static iconv_t CD = (iconv_t)-1;
109 static int amI_932 = ~0;
111 #ifdef ICONV_INBUF_CONST
112 # define ICONV_INBUF_TYPE const char **
114 # define ICONV_INBUF_TYPE char **
117 void _converter_( void *from, unsigned long from_len,
118 void **to, unsigned long *to_len )
123 size_t OutBytesLeft = 0;
124 size_t _OutBytesLeft = 0;
126 size_t converted_num = 0;
131 if ( shouldAlloc1 ) {
132 /* Obtain work area */
133 _i18nwork1 = (size_t *)malloc( WORKSIZE );
138 _i18nsize1 = WORKSIZE;
143 if ( ( CD = iconv_open( "IBM-eucJP", "IBM-932" ) ) == (iconv_t)-1 )
144 return; /* no converter */
145 amI_932 = !strncasecmp( "IBM-932", nl_langinfo( CODESET ), 7 );
150 if ( ( !amI_932 ) || ( CD == (iconv_t)-1 ) )
153 InBuf = (char *)from;
154 InBytesLeft = from_len;
155 OutBytesLeft = _i18nsize1;
156 OutBuf = (char *)_i18nwork1;
162 * +----------------------------+
164 * +----------------------------+
165 * <-------------------------->
174 * +----------------------------+
176 * +----------------------------+
177 * <-------------------------->
181 iconv_ret = iconv( CD, (ICONV_INBUF_TYPE)&InBuf, &InBytesLeft,
182 &OutBuf, &OutBytesLeft );
183 if ( iconv_ret == 0 ) {
187 * +----------------------------+
188 * |XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
189 * +----------------------------+
196 * +----------------------------+
197 * |XXXXXXXXXXXXXXXXX| | | |
198 * +----------------------------+
199 * <---------------> <-------->
200 * converted_num OutBytesLeft
202 converted_num = (unsigned long)((char *)OutBuf-(char *)_i18nwork1);
203 *to = (void *)_i18nwork1;
204 *to_len = (unsigned long)converted_num;
207 if ( errno == E2BIG ) {
208 /* Overflow. still data is left.
211 * +----------------------------+
212 * |XXXXXXXXXXXXXX| | | |
213 * +----------------------------+
220 * +----------------------------+
221 * |XXXXXXXXXXXXXXXXXXXXXXXXXXX |
222 * +----------------------------+
223 * <------------------------->
224 * converted_num OutBytesLeft=?
228 /* Check how many converted already. */
231 (unsigned long)((char *)OutBuf - (char *)_i18nwork1);
232 _i18nsize1 += WORKSIZE;
233 _p = realloc( _i18nwork1, _i18nsize1 );
244 OutBuf = (char *)((char*)_i18nwork1 + converted_num);
245 OutBytesLeft += WORKSIZE;
258 if ( _i18nsize1 > converted_num ) {
259 ((char *)_i18nwork1)[converted_num] = '\0';
260 } else { /* _i18nsize1 == converted_num */
264 _p = realloc( _i18nwork1, _i18nsize1 );
275 ((char *)_i18nwork1)[converted_num] = '\0';
280 /* The following routine is specific to using FMapType 3 composite fonts
281 * in postscript. Kanji, Asian specific?
284 euc_to_octal(char *srcStr)
288 static char dstStr[512];
290 int len = cm_strlen(srcStr);
293 memset(dstStr, 0, sizeof(dstStr));
295 bzero(dstStr, sizeof(dstStr));
297 for (i = 0; i < len; i++) {
299 if (!isEUC(srcStr[i])) {
301 /* NOT NEEDED FOR FMapType 4 (or 5)
302 cm_strcat(dstStr, "\\377\\000");
307 if (isEUC(srcStr[i])) {
309 /* NOT NEEDED FOR FMapType 4 (or 5)
310 cm_strcat(dstStr, "\\377\\001");
315 sprintf(buf, "\\%3.3o\\%3.3o", srcStr[i] & 0xff, srcStr[i+1] & 0xff);
319 sprintf(buf, "%c", srcStr[i]);
321 cm_strcat(dstStr, buf);
327 /* This routine should be in libdeskset.
328 * This routine uses fconvert() to avoid locale conversion.
330 /* 310 characters are the minimum needed to accommodate any double-precision
331 * value + 1 null terminator.
335 * Returns a null terminated formatted string.
336 * If error is encountered, such as malloc() failed, then return NULL.
337 * The caller of this function should beware that the return value is
338 * a static buffer declared within this function and the value of it may
342 cm_printf(double value, int decimal_pt)
347 int formatted_cnt = 0;
350 static char *formatted = NULL;
352 if ( formatted != NULL ) {
356 if ( (value == (double)0) && (decimal_pt == 0) ) {
357 formatted = (char *)cm_strdup("0");
360 if ( (buf = (char *)malloc(DBL_SIZE + decimal_pt)) == NULL ) {
363 if ( (formatted = (char *)calloc(1, DBL_SIZE + decimal_pt)) == NULL ) {
368 fconvert(value, decimal_pt, &deci_pt, &sign, buf);
370 /* this version, available on the HP and AIX machine is not reentrant. */
372 strcpy(buf, fcvt(value, decimal_pt, &deci_pt, &sign));
375 strcpy(formatted, "-");
377 buf_len = deci_pt + decimal_pt;
379 strncat(formatted, buf, deci_pt);
381 strcat(formatted, "0");
383 if ( deci_pt == buf_len ) {
384 strcat(formatted, "\0");
388 strcat(formatted, ".");
389 for ( formatted_cnt = strlen(formatted), buf_cnt = deci_pt; buf_cnt < buf_len; buf_cnt++, formatted_cnt++ ) {
390 formatted[formatted_cnt] = buf[buf_cnt];
392 formatted[formatted_cnt] = '\0';