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
);
73 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
74 "The file you are trying to open contains an invalid byte sequence.");
79 converted_contents
= g_convert (content
,
87 /* There is no way we can avoid to run g_utf8_validate on the converted text.
89 * <paolo> hmmm... but in that case g_convert should fail
90 * <owen> paolo: g_convert() doesn't necessarily have the same definition
91 * <owen> GLib just uses the system's iconv
92 * <owen> paolo: I think we've explained what's going on.
93 * I have to define it as NOTABUG since g_convert() isn't going to
94 * start post-processing or checking what iconv() does and
95 * changing g_utf8_valdidate() wouldn't be API compatible even if I
96 * thought it was right
98 if ((conv_error
!= NULL
) ||
99 !g_utf8_validate (converted_contents
, *new_len
, NULL
) ||
103 if (converted_contents
!= NULL
)
104 g_free (converted_contents
);
106 if (conv_error
!= NULL
)
107 g_propagate_error (error
, conv_error
);
110 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
111 "The file you are trying to open contains an invalid byte sequence.");
116 g_return_val_if_fail (converted_contents
!= NULL
, NULL
);
118 utf8_content
= converted_contents
;
125 anjuta_convert_to_utf8 (const gchar
*content
,
127 const AnjutaEncoding
**encoding
,
131 g_return_val_if_fail (content
!= NULL
, NULL
);
132 g_return_val_if_fail (encoding
!= NULL
, NULL
);
135 len
= strlen (content
);
137 if (*encoding
!= NULL
)
139 const gchar
* charset
;
141 charset
= anjuta_encoding_get_charset (*encoding
);
143 g_return_val_if_fail (charset
!= NULL
, NULL
);
145 return anjuta_convert_to_utf8_from_charset (content
,
153 /* Automatically detect the encoding used */
157 if (g_utf8_validate (content
, len
, NULL
))
162 return g_strndup (content
, len
);
166 g_set_error (error
, ANJUTA_CONVERT_ERROR
,
167 ANJUTA_CONVERT_ERROR_AUTO_DETECTION_FAILED
,
168 "anjuta was not able to automatically determine "
169 "the encoding of the file you want to open.");
175 while (encodings
!= NULL
)
177 const AnjutaEncoding
*enc
;
178 const gchar
*charset
;
181 enc
= (const AnjutaEncoding
*)encodings
->data
;
183 charset
= anjuta_encoding_get_charset (enc
);
184 g_return_val_if_fail (charset
!= NULL
, NULL
);
186 utf8_content
= anjuta_convert_to_utf8_from_charset (content
,
192 if (utf8_content
!= NULL
)
200 encodings
= g_slist_next (encodings
);
205 g_set_error (error
, ANJUTA_CONVERT_ERROR
,
206 ANJUTA_CONVERT_ERROR_AUTO_DETECTION_FAILED
,
207 "anjuta was not able to automatically determine "
208 "the encoding of the file you want to open.");
211 g_slist_free (start
);
216 g_return_val_if_reached (NULL
);
220 anjuta_convert_from_utf8 (const gchar
*content
,
222 const AnjutaEncoding
*encoding
,
226 GError
*conv_error
= NULL
;
227 gchar
*converted_contents
= NULL
;
228 gsize bytes_written
= 0;
230 g_return_val_if_fail (content
!= NULL
, NULL
);
231 g_return_val_if_fail (g_utf8_validate (content
, len
, NULL
), NULL
);
232 g_return_val_if_fail (encoding
!= NULL
, NULL
);
235 len
= strlen (content
);
237 if (encoding
== anjuta_encoding_get_utf8 ())
238 return g_strndup (content
, len
);
240 converted_contents
= g_convert (content
,
242 anjuta_encoding_get_charset (encoding
),
248 if (conv_error
!= NULL
)
250 if (converted_contents
!= NULL
)
252 g_free (converted_contents
);
253 converted_contents
= NULL
;
256 g_propagate_error (error
, conv_error
);
261 *new_len
= bytes_written
;
264 return converted_contents
;