2001-06-03 Hans Breuer <hans@breuer.org>
[dia.git] / lib / charconv.c
blob1465bb0d1f81c950f830e45cdf750d6325a37eec
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
5 * utf8 internally.
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.
23 #include <config.h>
25 #include <glib.h>
26 #include "charconv.h"
28 #ifdef HAVE_UNICODE
29 #include <string.h>
30 #include <unicode.h>
31 #include <errno.h>
33 #ifdef HAVE_LANGINFO_CODESET
34 #include <langinfo.h>
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 */
39 #define CODESET
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;
49 static void
50 check_conv_l2u(void){
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));
59 static void
60 check_conv_u2l(void){
61 if (conv_u2l==(unicode_iconv_t)0) {
62 conv_u2l = unicode_iconv_open(nl_langinfo(CODESET),"UTF-8");
67 extern gchar *
68 charconv_local8_to_utf8(const gchar *local)
70 const gchar *l = local;
71 int lleft = strlen(local);
72 gchar *utf,*u,*ures;
73 int uleft;
74 int lost = 0;
76 check_conv_l2u();
77 uleft = 6*lleft + 1;
78 u = utf = g_malloc(uleft);
79 *u = 0;
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,
83 &l,&lleft,
84 &u,&uleft);
85 *u = 0;
86 if (res==(ssize_t)-1) {
87 g_warning("unicode_iconv(l2u,...) failed, because '%s'",
88 strerror(errno));
89 break;
90 } else {
91 lost += (int)res; /* lost chars in the process. */
94 ures = g_strdup(utf); /* get the actual size. */
95 g_free(utf);
96 return ures;
99 extern gchar *
100 charconv_utf8_to_local8(const gchar *utf)
102 const gchar *u = utf;
103 int uleft = strlen(utf);
104 gchar *local,*l,*lres;
105 int lleft;
106 int lost = 0;
108 check_conv_u2l();
109 lleft = uleft;
110 l = local = g_malloc(lleft);
111 *l = 0;
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,
115 &u,&uleft,
116 &l,&lleft);
117 *l = 0;
118 if (res==(ssize_t)-1) {
119 g_warning("unicode_iconv(u2l,...) failed, because '%s'",
120 strerror(errno));
121 break;
122 } else {
123 lost += (int)res; /* lost chars in the process. */
126 lres = g_strdup(local); /* get the actual size. */
127 g_free(local);
128 return lres;
132 #else /* !HAVE_UNICODE */
134 extern gchar *
135 charconv_local8_to_utf8(const gchar *local)
137 return g_strdup(local);
140 extern gchar *
141 charconv_utf8_to_local8(const gchar *utf)
143 return g_strdup(utf);
146 #endif