Rework check_input so that it can work with any widget that gives a string.
[anjuta-git-plugin.git] / libanjuta / anjuta-convert.c
blobb6f78f961b3c0ef285ea17ca038ee583fcce1d2d
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * gedit-convert.c
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.
30 #include <string.h>
31 #include <stdio.h>
33 #include <glib/gi18n.h>
35 #include "anjuta-convert.h"
37 GQuark
38 anjuta_convert_error_quark (void)
40 static GQuark quark;
41 if (!quark)
42 quark = g_quark_from_static_string ("anjuta_convert_error");
44 return quark;
47 static gchar *
48 anjuta_convert_to_utf8_from_charset (const gchar *content,
49 gsize len,
50 const gchar *charset,
51 gsize *new_len,
52 GError **error)
54 gchar *utf8_content = NULL;
55 GError *conv_error = NULL;
56 gchar* converted_contents = NULL;
57 gsize bytes_read;
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))
67 if (new_len != NULL)
68 *new_len = len;
70 return g_strndup (content, len);
72 else
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.");
76 return NULL;
79 converted_contents = g_convert (content,
80 len,
81 "UTF-8",
82 charset,
83 &bytes_read,
84 new_len,
85 &conv_error);
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
97 */
98 if ((conv_error != NULL) ||
99 !g_utf8_validate (converted_contents, *new_len, NULL) ||
100 (bytes_read != len))
103 if (converted_contents != NULL)
104 g_free (converted_contents);
106 if (conv_error != NULL)
107 g_propagate_error (error, conv_error);
108 else
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.");
114 else
116 g_return_val_if_fail (converted_contents != NULL, NULL);
118 utf8_content = converted_contents;
121 return utf8_content;
124 gchar *
125 anjuta_convert_to_utf8 (const gchar *content,
126 gsize len,
127 const AnjutaEncoding **encoding,
128 gsize *new_len,
129 GError **error)
131 g_return_val_if_fail (content != NULL, NULL);
132 g_return_val_if_fail (encoding != NULL, NULL);
134 if (len < 0)
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,
146 len,
147 charset,
148 new_len,
149 error);
151 else
153 /* Automatically detect the encoding used */
154 GSList *encodings;
155 GSList *start;
156 gchar *ret = NULL;
157 if (g_utf8_validate (content, len, NULL))
159 if (new_len != NULL)
160 *new_len = len;
162 return g_strndup (content, len);
164 else
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.");
170 return NULL;
173 start = encodings;
175 while (encodings != NULL)
177 const AnjutaEncoding *enc;
178 const gchar *charset;
179 gchar *utf8_content;
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,
187 len,
188 charset,
189 new_len,
190 NULL);
192 if (utf8_content != NULL)
194 *encoding = enc;
195 ret = utf8_content;
197 break;
200 encodings = g_slist_next (encodings);
203 if (ret == NULL)
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);
213 return ret;
216 g_return_val_if_reached (NULL);
219 gchar *
220 anjuta_convert_from_utf8 (const gchar *content,
221 gsize len,
222 const AnjutaEncoding *encoding,
223 gsize *new_len,
224 GError **error)
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);
234 if (len < 0)
235 len = strlen (content);
237 if (encoding == anjuta_encoding_get_utf8 ())
238 return g_strndup (content, len);
240 converted_contents = g_convert (content,
241 len,
242 anjuta_encoding_get_charset (encoding),
243 "UTF-8",
244 NULL,
245 &bytes_written,
246 &conv_error);
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);
258 else
260 if (new_len != NULL)
261 *new_len = bytes_written;
264 return converted_contents;