~ubuntu-branches/ubuntu/jaunty/gedit/jaunty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * gedit-convert.c
 * This file is part of gedit
 *
 * Copyright (C) 2003 - Paolo Maggi 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA. 
 */
 
/*
 * Modified by the gedit Team, 2003. See the AUTHORS file for a 
 * list of people on the gedit Team.  
 * See the ChangeLog files for a list of changes. 
 */

#include <string.h>
#include <stdio.h>

#include <glib/gi18n.h>

#include "gedit-convert.h"
#include "gedit-debug.h"
#include "gedit-prefs-manager.h"

GQuark 
gedit_convert_error_quark (void)
{
	static GQuark quark;
	if (!quark)
		quark = g_quark_from_static_string ("gedit_convert_error");

	return quark;
}

static gchar *
gedit_convert_to_utf8_from_charset (const gchar  *content,
				    gsize         len,
				    const gchar  *charset,
				    gsize        *new_len,
				    GError 	**error)
{
	gchar *utf8_content = NULL;
	GError *conv_error = NULL;
	gchar* converted_contents = NULL;
	gsize bytes_read;
	
	g_return_val_if_fail (content != NULL, NULL);
	g_return_val_if_fail (len > 0, NULL);
	g_return_val_if_fail (charset != NULL, NULL);

	gedit_debug_message (DEBUG_UTILS, "Trying to convert from %s to UTF-8", charset);
	
	if (strcmp (charset, "UTF-8") == 0)
	{
		if (g_utf8_validate (content, len, NULL))
		{
			if (new_len != NULL)
				*new_len = len;
					
			return g_strndup (content, len);
		}
		else
		{	
			g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
				     "The file you are trying to open contains an invalid byte sequence.");
		
			return NULL;
		}
	}
	
	converted_contents = g_convert (content, 
					len, 
					"UTF-8",
					charset, 
					&bytes_read, 
					new_len,
					&conv_error); 

	gedit_debug_message (DEBUG_UTILS, "Bytes read: %" G_GSIZE_FORMAT, bytes_read);

	/* There is no way we can avoid to run 	g_utf8_validate on the converted text.
	 *
	 * <paolo> hmmm... but in that case g_convert should fail 
	 * <owen> paolo: g_convert() doesn't necessarily have the same definition
         * <owen> GLib just uses the system's iconv
         * <owen> paolo: I think we've explained what's going on. 
         * I have to define it as NOTABUG since g_convert() isn't going to 
         * start post-processing or checking what iconv() does and 
         * changing g_utf8_valdidate() wouldn't be API compatible even if I 
         * thought it was right
	 */			
	if ((conv_error != NULL) || 
	    !g_utf8_validate (converted_contents, *new_len, NULL) ||
	    (bytes_read != len))
	{
		gedit_debug_message (DEBUG_UTILS, "Couldn't convert from %s to UTF-8.",
			   	     charset);
				
		if (converted_contents != NULL)
			g_free (converted_contents);

		if (conv_error != NULL)
			g_propagate_error (error, conv_error);		
		else
		{
			gedit_debug_message (DEBUG_UTILS, "The file you are trying to open contains "
					     "an invalid byte sequence.");
			
			g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
				     "The file you are trying to open contains an invalid byte sequence.");
		}	
	} 
	else 
	{
		gedit_debug_message (DEBUG_UTILS,
				     "Converted from %s to UTF-8 (newlen = %" G_GSIZE_FORMAT ").", 
				     charset, *new_len);

		g_return_val_if_fail (converted_contents != NULL, NULL); 

		utf8_content = converted_contents;
	}

	return utf8_content;
}

gchar *
gedit_convert_to_utf8 (const gchar          *content,
		       gsize                 len,
		       const GeditEncoding **encoding,
		       gsize                *new_len,
		       GError              **error)
{
	gedit_debug (DEBUG_UTILS);

	g_return_val_if_fail (content != NULL, NULL);
	g_return_val_if_fail (encoding != NULL, NULL);

	if (len < 0)
		len = strlen (content);

	if (*encoding != NULL)
	{
		const gchar* charset;
		
		charset = gedit_encoding_get_charset (*encoding);

		g_return_val_if_fail (charset != NULL, NULL);

		return gedit_convert_to_utf8_from_charset (content, 
							   len,
							   charset,
							   new_len,
							   error);
	}
	else
	{
		/* Automatically detect the encoding used */

		GSList *encodings;
		GSList *start;
		gchar *ret = NULL;

		gedit_debug_message (DEBUG_UTILS,
				     "Automatically detect the encoding used");

		encodings = gedit_prefs_manager_get_auto_detected_encodings ();

		if (encodings == NULL)
		{
			gedit_debug_message (DEBUG_UTILS, "encodings == NULL");

			if (g_utf8_validate (content, len, NULL))
			{
				gedit_debug_message (DEBUG_UTILS, "validate OK.");

				if (new_len != NULL)
					*new_len = len;
				
				return g_strndup (content, len);
			}
			else
			{
				gedit_debug_message (DEBUG_UTILS, "validate failed.");

				g_set_error (error, GEDIT_CONVERT_ERROR, 
					     GEDIT_CONVERT_ERROR_AUTO_DETECTION_FAILED,
				 	     "gedit was not able to automatically determine "
					     "the encoding of the file you want to open.");

				return NULL;
			}
		}

		gedit_debug_message (DEBUG_UTILS, "encodings != NULL");

		start = encodings;

		while (encodings != NULL) 
		{
			const GeditEncoding *enc;
			const gchar *charset;
			gchar *utf8_content;

			enc = (const GeditEncoding *)encodings->data;

			gedit_debug_message (DEBUG_UTILS, "Get charset");

			charset = gedit_encoding_get_charset (enc);
			g_return_val_if_fail (charset != NULL, NULL);

			gedit_debug_message (DEBUG_UTILS, "Trying to convert %lu bytes of data from '%s' to 'UTF-8'.", 
					(unsigned long) len, charset);

			utf8_content = gedit_convert_to_utf8_from_charset (content, 
									   len, 
									   charset, 
									   new_len,
									   NULL);

			if (utf8_content != NULL) 
			{
				*encoding = enc;
				ret = utf8_content;

				break;
			}

			encodings = g_slist_next (encodings);
		}

		if (ret == NULL)
		{
			gedit_debug_message (DEBUG_UTILS, "gedit has not been able to automatically determine the encoding of "
					     "the file you want to open.");

			g_set_error (error, GEDIT_CONVERT_ERROR,
				     GEDIT_CONVERT_ERROR_AUTO_DETECTION_FAILED,
			 	     "gedit was not able to automatically determine "
				     "the encoding of the file you want to open.");
		}

		g_slist_free (start);

		return ret;
	}

	g_return_val_if_reached (NULL);
}

gchar *
gedit_convert_from_utf8 (const gchar          *content, 
		         gsize                 len,
		         const GeditEncoding  *encoding,
			 gsize                *new_len,
			 GError 	     **error)
{
	GError *conv_error         = NULL;
	gchar  *converted_contents = NULL;
	gsize   bytes_written = 0;

	gedit_debug (DEBUG_UTILS);
	
	g_return_val_if_fail (content != NULL, NULL);
	g_return_val_if_fail (g_utf8_validate (content, len, NULL), NULL);
	g_return_val_if_fail (encoding != NULL, NULL);

	if (len < 0)
		len = strlen (content);

	if (encoding == gedit_encoding_get_utf8 ())
		return g_strndup (content, len);

	converted_contents = g_convert (content, 
					len, 
					gedit_encoding_get_charset (encoding), 
					"UTF-8",
					NULL, 
					&bytes_written,
					&conv_error); 

	if (conv_error != NULL)
	{
		gedit_debug_message (DEBUG_UTILS, "Cannot convert from UTF-8 to %s",
				     gedit_encoding_get_charset (encoding));

		if (converted_contents != NULL)
		{
			g_free (converted_contents);
			converted_contents = NULL;
		}

		g_propagate_error (error, conv_error);
	}
	else
	{
		if (new_len != NULL)
			*new_len = bytes_written;
	}

	return converted_contents;
}