~ubuntu-branches/ubuntu/utopic/rhythmbox/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/rb-string-helpers.c

Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  arch-tag: Implementation of various string-related utility functions
3
 
 *
4
 
 *  Copyright (C) 2002 Jorn Baayen
5
 
 *  Copyright (C) 2003 Colin Walters <walters@verbum.org>
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2, or (at your option)
10
 
 *  any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 *
21
 
 */
22
 
 
23
 
#include <config.h>
24
 
#include <libgnome/gnome-i18n.h>
25
 
#include <glib.h>
26
 
#include <string.h>
27
 
#include <stdlib.h>
28
 
 
29
 
#include "rb-string-helpers.h"
30
 
 
31
 
static GHashTable *encodings;
32
 
 
33
 
/* stolen from gnome-desktop-item.c */
34
 
static gboolean
35
 
check_locale (const char *locale)
36
 
{
37
 
        GIConv cd = g_iconv_open ("UTF-8", locale);
38
 
        if ((GIConv)-1 == cd)
39
 
                return FALSE;
40
 
        g_iconv_close (cd);
41
 
        return TRUE;
42
 
}
43
 
 
44
 
/* stolen from gnome-desktop-item.c */
45
 
static void
46
 
insert_locales (GHashTable *encodings, char *enc, ...)
47
 
{
48
 
        va_list args;
49
 
        char *s;
50
 
 
51
 
        va_start (args, enc);
52
 
        for (;;) {
53
 
                s = va_arg (args, char *);
54
 
                if (s == NULL)
55
 
                        break;
56
 
                g_hash_table_insert (encodings, s, enc);
57
 
        }
58
 
        va_end (args);
59
 
}
60
 
 
61
 
/* stolen from gnome-desktop-item.c */
62
 
void
63
 
rb_string_helpers_init (void)
64
 
{
65
 
/* make a standard conversion table from the desktop standard spec */
66
 
        encodings = g_hash_table_new (g_str_hash, g_str_equal);
67
 
 
68
 
        /* "C" is plain ascii */
69
 
        insert_locales (encodings, "ASCII", "C", NULL);
70
 
 
71
 
        insert_locales (encodings, "ARMSCII-8", "by", NULL);
72
 
        insert_locales (encodings, "BIG5", "zh_TW", NULL);
73
 
        insert_locales (encodings, "CP1251", "be", "bg", NULL);
74
 
        if (check_locale ("EUC-CN")) {
75
 
                insert_locales (encodings, "EUC-CN", "zh_CN", NULL);
76
 
        } else {
77
 
                insert_locales (encodings, "GB2312", "zh_CN", NULL);
78
 
        }
79
 
        insert_locales (encodings, "EUC-JP", "ja", NULL);
80
 
        insert_locales (encodings, "UHC", "ko", NULL);
81
 
        /*insert_locales (encodings, "GEORGIAN-ACADEMY", NULL);*/
82
 
        insert_locales (encodings, "GEORGIAN-PS", "ka", NULL);
83
 
        insert_locales (encodings, "ISO-8859-1", "br", "ca", "da", "de", "en", "es", "eu", "fi", "fr", "gl", "it", "nl", "wa", "no", "pt", "pt", "sv", NULL);
84
 
        insert_locales (encodings, "ISO-8859-2", "cs", "hr", "hu", "pl", "ro", "sk", "sl", "sq", "sr", NULL);
85
 
        insert_locales (encodings, "ISO-8859-3", "eo", NULL);
86
 
        insert_locales (encodings, "ISO-8859-5", "mk", "sp", NULL);
87
 
        insert_locales (encodings, "ISO-8859-7", "el", NULL);
88
 
        insert_locales (encodings, "ISO-8859-9", "tr", NULL);
89
 
        insert_locales (encodings, "ISO-8859-13", "lt", "lv", "mi", NULL);
90
 
        insert_locales (encodings, "ISO-8859-14", "ga", "cy", NULL);
91
 
        insert_locales (encodings, "ISO-8859-15", "et", NULL);
92
 
        insert_locales (encodings, "KOI8-R", "ru", NULL);
93
 
        insert_locales (encodings, "KOI8-U", "uk", NULL);
94
 
        if (check_locale ("TCVN-5712")) {
95
 
                insert_locales (encodings, "TCVN-5712", "vi", NULL);
96
 
        } else {
97
 
                insert_locales (encodings, "TCVN", "vi", NULL);
98
 
        }
99
 
        insert_locales (encodings, "TIS-620", "th", NULL);
100
 
        /*insert_locales (encodings, "VISCII", NULL);*/
101
 
}
102
 
 
103
 
void
104
 
rb_string_helpers_shutdown (void)
105
 
{
106
 
        g_hash_table_destroy (encodings);
107
 
}
108
 
 
109
 
/* stolen from gnome-desktop-item.c */
110
 
static const char *
111
 
get_encoding_from_locale (const char *locale)
112
 
{
113
 
        char lang[3];
114
 
        const char *encoding;
115
 
 
116
 
        if (locale == NULL)
117
 
                return NULL;
118
 
 
119
 
        /* if locale includes encoding (that isn't UTF-8), use it */
120
 
        encoding = strchr (locale, '.');
121
 
        if (encoding != NULL && strncmp (encoding, ".UTF-8", 6)) {
122
 
                return encoding+1;
123
 
        }
124
 
 
125
 
        /* first try the entire locale (at this point ll_CC) */
126
 
        encoding = g_hash_table_lookup (encodings, locale);
127
 
        if (encoding != NULL)
128
 
                return encoding;
129
 
 
130
 
        /* Try just the language */
131
 
        strncpy (lang, locale, 2);
132
 
        lang[2] = '\0';
133
 
        return g_hash_table_lookup (encodings, lang);
134
 
}
135
 
 
136
 
char *
137
 
rb_unicodify (const char *str)
138
 
{
139
 
        char *ret = NULL;
140
 
        const char *char_encoding;
141
 
 
142
 
        /* Try validating it as UTF-8 first */
143
 
        if (g_utf8_validate (str, -1, NULL))
144
 
                return g_strdup (str);
145
 
 
146
 
        /* Failing that, try the legacy encoding associated
147
 
           with the locale. */
148
 
        char_encoding = get_encoding_from_locale (getenv ("LANG"));
149
 
        if (char_encoding == NULL)
150
 
                ret = NULL;
151
 
        else
152
 
                ret = g_convert (str, -1, "UTF-8", char_encoding,
153
 
                                 NULL, NULL, NULL);
154
 
        /* Failing that, try ISO-8859-1. */
155
 
        if (!ret)
156
 
                ret = g_convert (str, -1, "UTF-8", "ISO-8859-1",
157
 
                                 NULL, NULL, NULL);
158
 
 
159
 
        return ret;
160
 
}
161
 
 
162
 
int
163
 
rb_utf8_strncasecmp (gconstpointer a, gconstpointer b)
164
 
{
165
 
        char *al = g_utf8_casefold ((const char *) a, -1);
166
 
        char *bl = g_utf8_casefold ((const char *) b, -1);
167
 
        int ret = g_utf8_collate (al, bl);
168
 
        g_free (al);
169
 
        g_free (bl);
170
 
        return ret;
171
 
}
172
 
 
173
 
char *
174
 
rb_get_sort_key (const char *string)
175
 
{
176
 
        char *collated, *folded;
177
 
        folded = g_utf8_casefold (string, -1);
178
 
        collated = g_utf8_collate_key (folded, -1);
179
 
        g_free (folded);
180
 
        return collated;
181
 
}
182