~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/unicode/e-unicode.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-21 16:16:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020421161627-s07yahahm817qxs6
Tags: upstream-1.0.3
ImportĀ upstreamĀ versionĀ 1.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/* 
 
3
 * e-unicode.c - utf-8 support functions for gal
 
4
 * Copyright 2000, 2001, Ximian, Inc.
 
5
 *
 
6
 * Authors:
 
7
 *   Lauris Kaplinski <lauris@ximian.com>
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Library General Public
 
11
 * License, version 2, as published by the Free Software Foundation.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Library General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Library General Public
 
19
 * License along with this library; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
21
 * 02111-1307, USA.
 
22
 */
 
23
 
 
24
#include <config.h>
 
25
 
 
26
#include "e-unicode.h"
 
27
#include "e-iconv.h"
 
28
 
 
29
#include <string.h>
 
30
#include <iconv.h>
 
31
#include <stdlib.h>
 
32
 
 
33
gchar *
 
34
e_utf8_to_iconv_string_sized (iconv_t ic, const gchar *string, gint bytes)
 
35
{
 
36
        char *new, *ob;
 
37
        const char *ib;
 
38
        size_t ibl, obl;
 
39
 
 
40
        if (!string) return NULL;
 
41
 
 
42
        if (ic == (iconv_t) -1) {
 
43
                gint len;
 
44
                const gchar *u;
 
45
                gunichar uc;
 
46
 
 
47
                new = g_new (unsigned char, bytes * 4 + 1);
 
48
                u = string;
 
49
                len = 0;
 
50
 
 
51
                while ((u) && (u - string < bytes)) {
 
52
                        u = e_unicode_get_utf8 (u, &uc);
 
53
                        new[len++] = uc & 0xff;
 
54
                }
 
55
                new[len] = '\0';
 
56
                return new;
 
57
        }
 
58
 
 
59
        ib = string;
 
60
        ibl = bytes;
 
61
        new = ob = g_new (gchar, ibl * 4 + 1);
 
62
        obl = ibl * 4 + 1;
 
63
 
 
64
        while (ibl > 0) {
 
65
                e_iconv (ic, &ib, &ibl, &ob, &obl);
 
66
                if (ibl > 0) {
 
67
                        gint len;
 
68
                        if ((*ib & 0x80) == 0x00) len = 1;
 
69
                        else if ((*ib &0xe0) == 0xc0) len = 2;
 
70
                        else if ((*ib &0xf0) == 0xe0) len = 3;
 
71
                        else if ((*ib &0xf8) == 0xf0) len = 4;
 
72
                        else {
 
73
                                g_warning ("Invalid UTF-8 sequence");
 
74
                                break;
 
75
                        }
 
76
                        ib += len;
 
77
                        ibl = bytes - (ib - string);
 
78
                        if (ibl > bytes) ibl = 0;
 
79
                        *ob++ = '_';
 
80
                        obl--;
 
81
                }
 
82
        }
 
83
 
 
84
        *ob = '\0';
 
85
 
 
86
        return new;
 
87
}
 
88
 
 
89
gchar *
 
90
e_utf8_to_locale_string_sized (const gchar *string, gint bytes)
 
91
{
 
92
        iconv_t ic;
 
93
        char *ret;
 
94
 
 
95
        if (!string) return NULL;
 
96
 
 
97
        ic = e_iconv_open(e_iconv_locale_charset(), "utf-8");
 
98
        ret = e_utf8_to_iconv_string_sized (ic, string, bytes);
 
99
        e_iconv_close(ic);
 
100
 
 
101
        return ret;
 
102
}
 
103
 
 
104
gchar *
 
105
e_utf8_to_locale_string (const gchar *string)
 
106
{
 
107
        if (!string) return NULL;
 
108
        return e_utf8_to_locale_string_sized (string, strlen (string));
 
109
}
 
110
 
 
111
gchar *
 
112
e_unicode_get_utf8 (const gchar *text, gunichar *out)
 
113
{
 
114
        *out = g_utf8_get_char (text);
 
115
        return (*out == (gunichar)-1) ? NULL : g_utf8_next_char (text);
 
116
}
 
117