1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * charconv.c: simple wrapper around unicode_iconv until we get dia to use
6 * Copyright (C) 2001 Cyrille Chepelov <chepelov@calixo.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #ifdef HAVE_LANGINFO_CODESET
35 #else /* HAVE_LANGINFO_CODESET */
36 #ifndef EFAULT_8BIT_CHARSET /* this is a hack until dia talks utf8 internally. */
37 #define EFAULT_8BIT_CHARSET "ISO-8859-1"
38 #endif /* !EFAULT_8BIT_CHARSET */
40 static const char *nl_langinfo(void) {
41 return EFAULT_8BIT_CHARSET
;
44 #endif /* HAVE_LANGINFO_CODESET */
46 static unicode_iconv_t conv_u2l
= (unicode_iconv_t
)0;
47 static unicode_iconv_t conv_l2u
= (unicode_iconv_t
)0;
51 if (conv_u2l
==(unicode_iconv_t
)0) {
52 conv_u2l
= unicode_iconv_open(nl_langinfo(CODESET
),"UTF-8");
54 if (conv_l2u
==(unicode_iconv_t
)0) {
55 conv_l2u
= unicode_iconv_open("UTF-8",nl_langinfo(CODESET
));
61 if (conv_u2l
==(unicode_iconv_t
)0) {
62 conv_u2l
= unicode_iconv_open(nl_langinfo(CODESET
),"UTF-8");
68 charconv_local8_to_utf8(const gchar
*local
)
70 const gchar
*l
= local
;
71 int lleft
= strlen(local
);
78 u
= utf
= g_malloc(uleft
);
80 unicode_iconv(conv_l2u
,NULL
,NULL
,NULL
,NULL
); /* reset the state machines */
81 while ((uleft
) && (lleft
)) {
82 ssize_t res
= unicode_iconv(conv_l2u
,
86 if (res
==(ssize_t
)-1) {
87 g_warning("unicode_iconv(l2u,...) failed, because '%s'",
91 lost
+= (int)res
; /* lost chars in the process. */
94 ures
= g_strdup(utf
); /* get the actual size. */
100 charconv_utf8_to_local8(const gchar
*utf
)
102 const gchar
*u
= utf
;
103 int uleft
= strlen(utf
);
104 gchar
*local
,*l
,*lres
;
110 l
= local
= g_malloc(lleft
);
112 unicode_iconv(conv_u2l
,NULL
,NULL
,NULL
,NULL
); /* reset the state machines */
113 while ((uleft
) && (lleft
)) {
114 ssize_t res
= unicode_iconv(conv_u2l
,
118 if (res
==(ssize_t
)-1) {
119 g_warning("unicode_iconv(u2l,...) failed, because '%s'",
123 lost
+= (int)res
; /* lost chars in the process. */
126 lres
= g_strdup(local
); /* get the actual size. */
132 #else /* !HAVE_UNICODE */
135 charconv_local8_to_utf8(const gchar
*local
)
137 return g_strdup(local
);
141 charconv_utf8_to_local8(const gchar
*utf
)
143 return g_strdup(utf
);