1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 * This file is part of gedit
6 * Copyright (C) 2003 - Paolo Maggi
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., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * Modified by the gedit Team, 2003. See the AUTHORS file for a
26 * list of people on the gedit Team.
27 * See the ChangeLog files for a list of changes.
33 #include <glib/gi18n.h>
35 #include "anjuta-convert.h"
38 anjuta_convert_error_quark (void)
42 quark
= g_quark_from_static_string ("anjuta_convert_error");
48 anjuta_convert_to_utf8_from_charset (const gchar
*content
,
54 gchar
*utf8_content
= NULL
;
55 GError
*conv_error
= NULL
;
56 gchar
* converted_contents
= NULL
;
59 g_return_val_if_fail (content
!= NULL
, NULL
);
60 g_return_val_if_fail (len
> 0, NULL
);
61 g_return_val_if_fail (charset
!= NULL
, NULL
);
63 if (strcmp (charset
, "UTF-8") == 0)
65 if (g_utf8_validate (content
, len
, NULL
))
70 return g_strndup (content
, len
);
74 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
75 _("The file you are trying to open contains an invalid byte sequence."));
81 converted_contents
= g_convert (content
,
89 /* There is no way we can avoid to run g_utf8_validate on the converted text.
91 * <paolo> hmmm... but in that case g_convert should fail
92 * <owen> paolo: g_convert() doesn't necessarily have the same definition
93 * <owen> GLib just uses the system's iconv
94 * <owen> paolo: I think we've explained what's going on.
95 * I have to define it as NOTABUG since g_convert() isn't going to
96 * start post-processing or checking what iconv() does and
97 * changing g_utf8_valdidate() wouldn't be API compatible even if I
98 * thought it was right
100 if ((conv_error
!= NULL
) ||
101 !g_utf8_validate (converted_contents
, *new_len
, NULL
) ||
105 if (converted_contents
!= NULL
)
106 g_free (converted_contents
);
108 if (conv_error
!= NULL
)
109 g_propagate_error (error
, conv_error
);
112 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
113 _("The file you are trying to open contains an invalid byte sequence."));
118 g_return_val_if_fail (converted_contents
!= NULL
, NULL
);
120 utf8_content
= converted_contents
;
127 anjuta_convert_to_utf8 (const gchar
*content
,
129 const AnjutaEncoding
**encoding
,
133 g_return_val_if_fail (content
!= NULL
, NULL
);
134 g_return_val_if_fail (encoding
!= NULL
, NULL
);
137 len
= strlen (content
);
139 if (*encoding
!= NULL
)
141 const gchar
* charset
;
143 charset
= anjuta_encoding_get_charset (*encoding
);
145 g_return_val_if_fail (charset
!= NULL
, NULL
);
147 return anjuta_convert_to_utf8_from_charset (content
,
155 /* Automatically detect the encoding used */
159 if (g_utf8_validate (content
, len
, NULL
))
164 return g_strndup (content
, len
);
168 g_set_error (error
, ANJUTA_CONVERT_ERROR
,
169 ANJUTA_CONVERT_ERROR_AUTO_DETECTION_FAILED
,
170 _("Anjuta was not able to automatically determine "
171 "the encoding of the file you want to open."));
177 while (encodings
!= NULL
)
179 const AnjutaEncoding
*enc
;
180 const gchar
*charset
;
183 enc
= (const AnjutaEncoding
*)encodings
->data
;
185 charset
= anjuta_encoding_get_charset (enc
);
186 g_return_val_if_fail (charset
!= NULL
, NULL
);
188 utf8_content
= anjuta_convert_to_utf8_from_charset (content
,
194 if (utf8_content
!= NULL
)
202 encodings
= g_slist_next (encodings
);
207 g_set_error (error
, ANJUTA_CONVERT_ERROR
,
208 ANJUTA_CONVERT_ERROR_AUTO_DETECTION_FAILED
,
209 _("Anjuta was not able to automatically determine "
210 "the encoding of the file you want to open."));
213 g_slist_free (start
);
218 g_return_val_if_reached (NULL
);
222 anjuta_convert_from_utf8 (const gchar
*content
,
224 const AnjutaEncoding
*encoding
,
228 GError
*conv_error
= NULL
;
229 gchar
*converted_contents
= NULL
;
230 gsize bytes_written
= 0;
232 g_return_val_if_fail (content
!= NULL
, NULL
);
233 g_return_val_if_fail (g_utf8_validate (content
, len
, NULL
), NULL
);
234 g_return_val_if_fail (encoding
!= NULL
, NULL
);
237 len
= strlen (content
);
239 if (encoding
== anjuta_encoding_get_utf8 ())
240 return g_strndup (content
, len
);
242 converted_contents
= g_convert (content
,
244 anjuta_encoding_get_charset (encoding
),
250 if (conv_error
!= NULL
)
252 if (converted_contents
!= NULL
)
254 g_free (converted_contents
);
255 converted_contents
= NULL
;
258 g_propagate_error (error
, conv_error
);
263 *new_len
= bytes_written
;
266 return converted_contents
;