aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-unicode.c
blob: 8612048e21dee1c92dc866fb3fb64468a7e49213 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Authors: Lauris Kaplinski <lauris@helixcode.com>
 *         
 */

#include <config.h>
#include <unicode.h>
#include "e-unicode.h"

const gchar *
e_utf8_strstrcase (const gchar *haystack, const gchar *needle)
{
    gchar *p;
    unicode_char_t *huni, *nuni;
    unicode_char_t unival;
    gint hlen, nlen, hp, np;

    if (haystack == NULL) return NULL;
    if (needle == NULL) return NULL;
    if (strlen (needle) == 0) return haystack;

    huni = alloca (sizeof (unicode_char_t) * strlen (haystack));

    for (hlen = 0, p = unicode_get_utf8 (haystack, &unival); p && unival; hlen++, p = unicode_get_utf8 (p, &unival)) {
        huni[hlen] = unicode_tolower (unival);
    }

    if (!p) return NULL;
    if (hlen == 0) return NULL;

    nuni = alloca (sizeof (unicode_char_t) * strlen (needle));

    for (nlen = 0, p = unicode_get_utf8 (needle, &unival); p && unival; nlen++, p = unicode_get_utf8 (p, &unival)) {
        nuni[nlen] = unicode_tolower (unival);
    }

    if (!p) return NULL;
    if (nlen == 0) return NULL;

    if (hlen < nlen) return NULL;

    for (hp = 0; hp <= hlen - nlen; hp++) {
        for (np = 0; np < nlen; np++) {
            if (huni[hp + np] != nuni[np]) break;
        }
        if (np == nlen) return haystack + unicode_offset_to_index (haystack, hp);
    }

    return NULL;
}

gchar *
e_utf8_from_gtk_event_key (GtkWidget *widget, guint keyval, const gchar *string)
{
    /* test it out with iso-8859-1 */

    static gboolean uinit = FALSE;
    static gboolean uerror = FALSE;
    static unicode_iconv_t uiconv = (unicode_iconv_t) -1;
    char *new, *ob;
    size_t ibl, obl;

    if (uerror) return NULL;

    if (!string) return NULL;

    if (!uinit) {
        unicode_init ();
        uiconv = unicode_iconv_open ("UTF-8", "iso-8859-1");
        if (uiconv == (unicode_iconv_t) -1) {
            uerror = TRUE;
            return NULL;
        } else {
            uinit = TRUE;
        }
    }

    ibl = strlen (string);
    new = ob = g_new (gchar, ibl * 6 + 1);
    obl = ibl * 6 + 1;

    unicode_iconv (uiconv, &string, &ibl, &ob, &obl);

    *ob = '\0';

    return new;
}

gchar *
e_utf8_from_gtk_string (GtkWidget *widget, const gchar *string)
{
    /* test it out with iso-8859-1 */

    static gboolean uinit = FALSE;
    static gboolean uerror = FALSE;
    static unicode_iconv_t uiconv = (unicode_iconv_t) -1;
    char *new, *ob;
    size_t ibl, obl;

    if (uerror) return NULL;

    if (!string) return NULL;

    if (!uinit) {
        unicode_init ();
        uiconv = unicode_iconv_open ("UTF-8", "iso-8859-1");
        if (uiconv == (unicode_iconv_t) -1) {
            uerror = TRUE;
            return NULL;
        } else {
            uinit = TRUE;
        }
    }

    ibl = strlen (string);
    new = ob = g_new (gchar, ibl * 6 + 1);
    obl = ibl * 6 + 1;

    unicode_iconv (uiconv, &string, &ibl, &ob, &obl);

    *ob = '\0';

    return new;
}

gchar *
e_utf8_to_gtk_string (GtkWidget *widget, const gchar *string)
{
    /* test it out with iso-8859-1 */

    static gboolean uinit = FALSE;
    static gboolean uerror = FALSE;
    static unicode_iconv_t uiconv = (unicode_iconv_t) -1;
    char *new, *ob;
    size_t ibl, obl;

    if (uerror) return NULL;

    if (!string) return NULL;

    if (!uinit) {
        unicode_init ();
        uiconv = unicode_iconv_open ("iso-8859-1", "UTF-8");
        if (uiconv == (unicode_iconv_t) -1) {
            uerror = TRUE;
            return NULL;
        } else {
            uinit = TRUE;
        }
    }

    ibl = strlen (string);
    new = ob = g_new (gchar, ibl * 2 + 1);
    obl = ibl * 2 + 1;

    unicode_iconv (uiconv, &string, &ibl, &ob, &obl);

    *ob = '\0';

    return new;
}

gchar *
e_utf8_gtk_entry_get_text (GtkEntry *entry)
{
    gchar *s, *u;

    s = gtk_entry_get_text (entry);
    if (!s) return NULL;
    u = e_utf8_from_gtk_string ((GtkWidget *) entry, s);
    return u;
}

gchar *
e_utf8_gtk_editable_get_chars (GtkEditable *editable, gint start, gint end)
{
    gchar *s, *u;

    s = gtk_editable_get_chars (editable, start, end);
    if (!s) return NULL;
    u = e_utf8_from_gtk_string ((GtkWidget *) editable, s);
    g_free (s);
    return u;
}

void
e_utf8_gtk_entry_set_text (GtkEntry *entry, const gchar *text)
{
    gchar *s;

    if (!text) return;

    s = e_utf8_to_gtk_string ((GtkWidget *) entry, text);
    gtk_entry_set_text (entry, s);

    if (s) g_free (s);
}