2 ** Copyright 2000-2003 Double Precision, Inc.
3 ** See COPYING for distribution information.
5 ** $Id: unicode.c,v 1.9 2004/02/08 04:59:15 mrsam Exp $
8 #include "unicode_config.h"
15 const char *unicode_default_chset()
17 const char *p
=UNICODECHARSET
;
22 return (unicode_ISO8859_1
.chset
);
25 const struct unicode_info
*unicode_find(const char *chset
)
30 if (!chset
) /* Default character set */
31 return (&unicode_ISO8859_1
);
40 if (strcmp(unicode_ISO8859_1
.chset
, p
) == 0)
43 return (&unicode_ISO8859_1
);
46 for (i
=0; unicode_chsetlist
[i
].chsetname
; i
++)
47 if (strcmp(unicode_chsetlist
[i
].chsetname
, p
) == 0)
50 return (unicode_chsetlist
[i
].ptr
);
56 char *unicode_convert(const char *txt
, const struct unicode_info
*from
,
57 const struct unicode_info
*to
)
63 if (strcmp(from
->chset
, to
->chset
) == 0) /* NOOP */
66 uc
=(*from
->c2u
)(from
, txt
, &dummy
);
75 s
=(*to
->u2c
)(to
, uc
, &dummy
);
85 char *unicode_convert_fromchset(const char *txt
, const char *from
,
86 const struct unicode_info
*to
)
88 const struct unicode_info
*fromu
=unicode_find(from
);
95 return (unicode_convert(txt
, fromu
, to
));
99 ** Convert being character sets, except ignore errors.
107 static int ux_alloc(struct ux_buf
*p
, size_t l
)
116 newbuf
=p
->buffer
? realloc(p
->buffer
, l
):malloc(l
);
126 char *unicode_xconvert(const char *txt
, const struct unicode_info
*from
,
127 const struct unicode_info
*to
)
132 struct ux_buf dst_str
;
134 char *orig_str
=strdup(txt
);
139 if (strcmp(from
->chset
, to
->chset
) == 0) /* NOOP */
145 if (ux_alloc(&dst_str
, strlen(txt
)*2))
162 if (from
->flags
& UNICODE_REPLACEABLE
)
164 uc
=(*from
->c2u
)(from
, cur_conv
, NULL
);
168 free(dst_str
.buffer
);
173 uc
=(*from
->c2u
)(from
, cur_conv
, &dummy
);
182 free(dst_str
.buffer
);
186 /* Error converting original text to unicode.
187 ** Back up, and convert all the characters up until
188 ** the error character.
193 save_char
=cur_conv
[dummy
];
197 uc
=(*from
->c2u
)(from
, cur_conv
, &dummy2
);
198 cur_conv
[dummy
]=save_char
;
203 free(dst_str
.buffer
);
208 /* Ok, now convert unicode to dest charset, using the same
209 ** trial-and-error process.
218 for (cnt_done
=0; ucptr
[cnt_done
]; cnt_done
++)
221 if (to
->flags
& UNICODE_REPLACEABLE
)
223 s
=(*to
->u2c
)(to
, ucptr
, NULL
);
227 free(dst_str
.buffer
);
233 s
=(*to
->u2c
)(to
, ucptr
, &dummy
);
237 unicode_char save_char
;
242 free(dst_str
.buffer
);
249 save_char
=ucptr
[dummy
];
251 s
=(*to
->u2c
)(to
, ucptr
, &dummy2
);
252 ucptr
[dummy
]=save_char
;
257 free(dst_str
.buffer
);
263 if (ux_alloc(&dst_str
,
264 strlen(dst_str
.buffer
)+strlen(s
)+2))
268 free(dst_str
.buffer
);
273 strcat(dst_str
.buffer
, s
);
278 strcat(dst_str
.buffer
, ".");
289 if (ux_alloc(&dst_str
, strlen(dst_str
.buffer
)+1))
292 free(dst_str
.buffer
);
299 strcat(dst_str
.buffer
, buf
);
305 return (dst_str
.buffer
);