Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Work on charsets

  • Loading branch information...
commit 4be7c51d2ed571713637aaeece8b5ccb52a412ff 1 parent 3858ef1
@codebrainz authored
View
40 src/charsets.c
@@ -1,15 +1,6 @@
#include <glib.h>
#include "charsets.h"
-struct _SourceFileCharset
-{
- gint mib_enum;
- gchar *name;
- gchar *mime_name;
- gchar **aliases;
- gsize n_aliases;
-};
-
static SourceFileCharset **charset_table = NULL;
static guint charset_table_length = 0;
@@ -86,8 +77,8 @@ source_file_init_charset_table (const gchar *filename)
}
-static gboolean
-source_file_charset_equals (SourceFileCharset *charset, const gchar *charset_name)
+gboolean
+source_file_charset_equals (const SourceFileCharset *charset, const gchar *charset_name)
{
if (g_strcasecmp (charset->name, charset_name) == 0)
return TRUE;
@@ -109,17 +100,7 @@ source_file_charset_equals (SourceFileCharset *charset, const gchar *charset_nam
gboolean
source_file_charset_exists (const gchar *charset_name)
{
- guint i;
-
- source_file_init_charset_table (SOURCE_FILE_CHARSET_CONF);
-
- for (i = 0; i < charset_table_length; i++)
- {
- if (source_file_charset_equals (charset_table[i], charset_name))
- return TRUE;
- }
-
- return FALSE;
+ return source_file_lookup_charset (charset_name) != NULL;
}
@@ -140,6 +121,21 @@ source_file_lookup_charset (const gchar *charset_name)
}
+gchar *
+source_file_normalize_charset_name (const gchar *charset_name)
+{
+ const SourceFileCharset *charset;
+
+ if (!charset_name)
+ return NULL;
+
+ charset = source_file_lookup_charset (charset_name);
+
+ if (!charset)
+ return NULL;
+
+ return g_strdup (charset->name);
+}
View
16 src/charsets.h
@@ -3,7 +3,19 @@
typedef struct _SourceFileCharset SourceFileCharset;
-gboolean source_file_charset_exists (const gchar *charset_name);
-const SourceFileCharset *source_file_lookup_charset (const gchar *charset_name);
+struct _SourceFileCharset
+{
+ gint mib_enum;
+ gchar *name;
+ gchar *mime_name;
+ gchar **aliases;
+ gsize n_aliases;
+};
+
+gboolean source_file_charset_exists (const gchar *charset_name);
+const SourceFileCharset *source_file_lookup_charset (const gchar *charset_name);
+gboolean source_file_charset_equals (const SourceFileCharset *charset,
+ const gchar *charset_name);
+gchar *source_file_normalize_charset_name (const gchar *charset_name);
#endif /* __SOURCECHARSETS_H__ */
View
20 src/sourcefile.c
@@ -212,6 +212,7 @@ source_file_guess_charset (SourceFile *file, const gchar *buffer, gsize length)
gchar *charset = NULL;
GMatchInfo *info;
GError *error;
+ const SourceFileCharset *cs;
g_return_val_if_fail (SOURCE_IS_FILE (file), NULL);
g_return_val_if_fail (buffer, NULL);
@@ -284,7 +285,15 @@ source_file_guess_charset (SourceFile *file, const gchar *buffer, gsize length)
}
if (!charset)
- charset = g_strdup (SOURCE_FILE_FALLBACK_ENCODING);
+ charset = g_strdup (SOURCE_FILE_FALLBACK_CHARSET);
+
+ /* normalize the name */
+ cs = source_file_lookup_charset (charset);
+ if (cs)
+ {
+ g_free (charset);
+ charset = g_strdup (cs->name);
+ }
return charset;
}
@@ -629,8 +638,8 @@ source_file_save (SourceFile *file, const gchar *filename)
const gchar *
source_file_get_charset (SourceFile *file)
{
- g_return_val_if_fail (SOURCE_IS_FILE (file), NULL);
- return (const gchar *) file->priv->charset;
+ g_return_val_if_fail (SOURCE_IS_FILE (file), NULL);
+ return (const gchar *) file->priv->charset;
}
@@ -641,7 +650,10 @@ source_file_set_charset (SourceFile *file, const gchar *charset)
g_return_if_fail (charset);
g_free (file->priv->charset);
- file->priv->charset = g_strdup (charset);
+ file->priv->charset = source_file_normalize_charset_name (charset);
+
+ if (!file->priv->charset)
+ file->priv->charset = g_strdup (SOURCE_FILE_FALLBACK_CHARSET);
}
View
4 src/sourcefile.h
@@ -6,8 +6,8 @@
G_BEGIN_DECLS
-#define SOURCE_FILE_FALLBACK_ENCODING "ISO-8859-1"
-#define SOURCE_FILE_CHARSET_PATTERN \
+#define SOURCE_FILE_FALLBACK_CHARSET "ISO-8859-1"
+#define SOURCE_FILE_CHARSET_PATTERN \
"(coding|charset|codepage)\\s*[:=]\\s*[\"']?\\s*([^'\"\\s]+)\\s*[\"']?"
#define SOURCE_FILE_CHARSET_PATTERN_MATCH 2
Please sign in to comment.
Something went wrong with that request. Please try again.