Revision: 931 http://svn.sourceforge.net/geany/?rev=931&view=rev Author: eht16 Date: 2006-10-25 07:48:07 -0700 (Wed, 25 Oct 2006)
Log Message: ----------- Moved utils_convert_to_utf8() and utils_convert_to_utf8_from_charset() to encodings.c.
Modified Paths: -------------- trunk/ChangeLog trunk/src/document.c trunk/src/encodings.c trunk/src/encodings.h trunk/src/utils.c trunk/src/utils.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2006-10-25 14:38:48 UTC (rev 930) +++ trunk/ChangeLog 2006-10-25 14:48:07 UTC (rev 931) @@ -6,6 +6,9 @@ dialogs_show_error() to dialogs_show_msgbox(). * src/document.c: Added warning message when opening files that cannot be handled correctly and set them to read-only mode. + * src/document.c, src/encodings.c, src/encodings.h, src/utils.c, + src/utils.h: Moved utils_convert_to_utf8() and + utils_convert_to_utf8_from_charset() to encodings.c.
2006-10-24 Enrico Tröger enrico.troeger@uvena.de
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2006-10-25 14:38:48 UTC (rev 930) +++ trunk/src/document.c 2006-10-25 14:48:07 UTC (rev 931) @@ -462,7 +462,8 @@ } else { - gchar *converted_text = utils_convert_to_utf8_from_charset(*data, *size, forced_enc, FALSE); + gchar *converted_text = encodings_convert_to_utf8_from_charset( + *data, *size, forced_enc, FALSE); if (converted_text == NULL) { return FALSE; @@ -494,7 +495,8 @@ *bom = TRUE; if ((*enc)[4] != '8') // the BOM indicated something else than UTF-8 { - gchar *converted_text = utils_convert_to_utf8_from_charset(*data, *size, *enc, FALSE); + gchar *converted_text = encodings_convert_to_utf8_from_charset( + *data, *size, *enc, FALSE); if (converted_text == NULL) { g_free(*enc); @@ -518,7 +520,7 @@ } else { - gchar *converted_text = utils_convert_to_utf8(*data, *size, enc); + gchar *converted_text = encodings_convert_to_utf8(*data, *size, enc);
if (converted_text == NULL) {
Modified: trunk/src/encodings.c =================================================================== --- trunk/src/encodings.c 2006-10-25 14:38:48 UTC (rev 930) +++ trunk/src/encodings.c 2006-10-25 14:48:07 UTC (rev 931) @@ -286,3 +286,94 @@ } } } + + +/* Converts a string from the given charset to UTF-8. + * If fast is set, no further checks are performed. */ +gchar *encodings_convert_to_utf8_from_charset(const gchar *buffer, gsize size, + const gchar *charset, gboolean fast) +{ + gchar *utf8_content = NULL; + GError *conv_error = NULL; + gchar* converted_contents = NULL; + gsize bytes_written; + + g_return_val_if_fail(buffer != NULL, NULL); + g_return_val_if_fail(charset != NULL, NULL); + + converted_contents = g_convert(buffer, size, "UTF-8", charset, NULL, + &bytes_written, &conv_error); + + if (fast) + { + utf8_content = converted_contents; + if (conv_error != NULL) g_error_free(conv_error); + } + else if (conv_error != NULL || ! g_utf8_validate(converted_contents, bytes_written, NULL)) + { + if (conv_error != NULL) + { + geany_debug("Couldn't convert from %s to UTF-8 (%s).", charset, conv_error->message); + g_error_free(conv_error); + conv_error = NULL; + } + else + geany_debug("Couldn't convert from %s to UTF-8.", charset); + + utf8_content = NULL; + if (converted_contents != NULL) g_free(converted_contents); + } + else + { + geany_debug("Converted from %s to UTF-8.", charset); + utf8_content = converted_contents; + } + + return utf8_content; +} + + +gchar *encodings_convert_to_utf8(const gchar *buffer, gsize size, gchar **used_encoding) +{ + gchar *locale_charset = NULL; + gchar *utf8_content; + gchar *charset; + gboolean check_current = FALSE; + guint i; + + // current locale is not UTF-8, we have to check this charset + check_current = ! g_get_charset((const gchar**)&locale_charset); + + for (i = 0; i < GEANY_ENCODINGS_MAX; i++) + { + if (i == encodings[GEANY_ENCODING_NONE].idx) continue; + + if (check_current) + { + check_current = FALSE; + charset = locale_charset; + i = -1; + } + else + charset = encodings[i].charset; + + geany_debug("Trying to convert %d bytes of data from %s into UTF-8.", size, charset); + utf8_content = encodings_convert_to_utf8_from_charset(buffer, size, charset, FALSE); + + if (utf8_content != NULL) + { + if (used_encoding != NULL) + { + if (*used_encoding != NULL) + { + g_free(*used_encoding); + geany_debug("%s:%d", __FILE__, __LINE__); + } + *used_encoding = g_strdup(charset); + } + return utf8_content; + } + } + + return NULL; +}
Modified: trunk/src/encodings.h =================================================================== --- trunk/src/encodings.h 2006-10-25 14:38:48 UTC (rev 930) +++ trunk/src/encodings.h 2006-10-25 14:48:07 UTC (rev 931) @@ -67,7 +67,14 @@
void encodings_init(void);
+gchar *encodings_convert_to_utf8(const gchar *buffer, gsize size, gchar **used_encoding);
+/* Converts a string from the given charset to UTF-8. + * If fast is set, no further checks are performed. */ +gchar *encodings_convert_to_utf8_from_charset(const gchar *buffer, gsize size, + const gchar *charset, gboolean fast); + + /* * The original versions of the following tables are taken from profterm *
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2006-10-25 14:38:48 UTC (rev 930) +++ trunk/src/utils.c 2006-10-25 14:48:07 UTC (rev 931) @@ -226,8 +226,8 @@
if (tag->type & tag_types) { - if (! doc_is_utf8) utf8_name = utils_convert_to_utf8_from_charset(tag->name, -1, - doc_list[idx].encoding, TRUE); + if (! doc_is_utf8) utf8_name = encodings_convert_to_utf8_from_charset(tag->name, + -1, doc_list[idx].encoding, TRUE); else utf8_name = tag->name; if ((tag->atts.entry.scope != NULL) && isalpha(tag->atts.entry.scope[0])) { @@ -347,95 +347,6 @@ }
-/* Converts a string from the given charset to UTF-8. - * If fast is set, no further checks are performed. */ -gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const gchar *charset, - gboolean fast) -{ - gchar *utf8_content = NULL; - GError *conv_error = NULL; - gchar* converted_contents = NULL; - gsize bytes_written; - - g_return_val_if_fail(buffer != NULL, NULL); - g_return_val_if_fail(charset != NULL, NULL); - - converted_contents = g_convert(buffer, size, "UTF-8", charset, NULL, - &bytes_written, &conv_error); - - if (fast) - { - utf8_content = converted_contents; - if (conv_error != NULL) g_error_free(conv_error); - } - else if (conv_error != NULL || ! g_utf8_validate(converted_contents, bytes_written, NULL)) - { - if (conv_error != NULL) - { - geany_debug("Couldn't convert from %s to UTF-8 (%s).", charset, conv_error->message); - g_error_free(conv_error); - conv_error = NULL; - } - else - geany_debug("Couldn't convert from %s to UTF-8.", charset); - - utf8_content = NULL; - if (converted_contents != NULL) g_free(converted_contents); - } - else - { - geany_debug("Converted from %s to UTF-8.", charset); - utf8_content = converted_contents; - } - - return utf8_content; -} - - -gchar *utils_convert_to_utf8(const gchar *buffer, gsize size, gchar **used_encoding) -{ - gchar *locale_charset = NULL; - gchar *utf8_content; - gchar *charset; - gboolean check_current = FALSE; - guint i; - - // current locale is not UTF-8, we have to check this charset - check_current = ! g_get_charset((const gchar**)&locale_charset); - - for (i = 0; i < GEANY_ENCODINGS_MAX; i++) - { - if (check_current) - { - check_current = FALSE; - charset = locale_charset; - i = -1; - } - else - charset = encodings[i].charset; - - geany_debug("Trying to convert %d bytes of data from %s into UTF-8.", size, charset); - utf8_content = utils_convert_to_utf8_from_charset(buffer, size, charset, FALSE); - - if (utf8_content != NULL) - { - if (used_encoding != NULL) - { - if (*used_encoding != NULL) - { - g_free(*used_encoding); - geany_debug("%s:%d", __FILE__, __LINE__); - } - *used_encoding = g_strdup(charset); - } - return utf8_content; - } - } - - return NULL; -} - - /** * (stolen from anjuta and modified) * Search backward through size bytes looking for a '<', then return the tag if any
Modified: trunk/src/utils.h =================================================================== --- trunk/src/utils.h 2006-10-25 14:38:48 UTC (rev 930) +++ trunk/src/utils.h 2006-10-25 14:48:07 UTC (rev 931) @@ -44,13 +44,6 @@
gint utils_write_file(const gchar *filename, const gchar *text);
-gchar *utils_convert_to_utf8(const gchar *buffer, gsize size, gchar **used_encoding); - -/* Converts a string from the given charset to UTF-8. - * If fast is set, no further checks are performed. */ -gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const gchar *charset, - gboolean fast); - /** * (stolen from anjuta and modified) * Search backward through size bytes looking for a '<', then return the tag if any
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.