File: gucharmap-codepoint-list.c

package info (click to toggle)
gucharmap 1%3A1.6.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,544 kB
  • ctags: 805
  • sloc: sh: 8,511; xml: 6,611; ansic: 6,609; makefile: 178
file content (180 lines) | stat: -rw-r--r-- 5,029 bytes parent folder | download
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
/* $Id: gucharmap-codepoint-list.c,v 1.6 2005/09/08 13:35:59 behdad Exp $ */
/*
 * Copyright (c) 2004 Noah Levitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include "config.h"
#include <glib.h>
#include "gucharmap-codepoint-list.h"

typedef struct _DefaultCodepointListPrivate DefaultCodepointListPrivate;

struct _DefaultCodepointListPrivate
{
  gunichar start;
  gunichar end;
};

#define GUCHARMAP_CODEPOINT_LIST_GET_PRIVATE(o) \
            (G_TYPE_INSTANCE_GET_PRIVATE ((o), gucharmap_codepoint_list_get_type (), DefaultCodepointListPrivate))

static gunichar 
default_get_char (GucharmapCodepointList *list, 
                  gint                    index)
{
  DefaultCodepointListPrivate *priv = GUCHARMAP_CODEPOINT_LIST_GET_PRIVATE (list);

  if (index > (gint)priv->end - priv->start)
    return (gunichar)(-1);
  else
    return (gunichar) priv->start + index;
}

static gint 
default_get_index (GucharmapCodepointList *list, 
                   gunichar                wc)
{
  DefaultCodepointListPrivate *priv = GUCHARMAP_CODEPOINT_LIST_GET_PRIVATE (list);

  if (wc < priv->start || wc > priv->end)
    return -1;
  else
    return wc - priv->start;
}

static gint
default_get_last_index (GucharmapCodepointList *list)
{
  DefaultCodepointListPrivate *priv = GUCHARMAP_CODEPOINT_LIST_GET_PRIVATE (list);

  return priv->end - priv->start;
}

static void
gucharmap_codepoint_list_class_init (GucharmapCodepointListClass *clazz)
{
  g_type_class_add_private (clazz, sizeof (DefaultCodepointListPrivate));

  /* the default implementation is all unicode codepoints in order */
  clazz->get_char = default_get_char;
  clazz->get_index = default_get_index;
  clazz->get_last_index = default_get_last_index;
}

GType
gucharmap_codepoint_list_get_type (void)
{
  static GType t = 0;

  if (t == 0)
    {
      static const GTypeInfo type_info =
        {
          sizeof (GucharmapCodepointListClass),
          NULL,
          NULL,
          (GClassInitFunc) gucharmap_codepoint_list_class_init,
          NULL,
          NULL,
          sizeof (GucharmapCodepointList),
          0,
          NULL
        };

      t = g_type_register_static (G_TYPE_OBJECT, "GucharmapCodepointList", &type_info, 0);
    }

  return t;
}

/**
 * gucharmap_codepoint_list_get_char:
 * @list: a #GucharmapCodepointList
 * @index: index indicating which character to get
 * 
 * Return value: code point at index @index in the codepoint list, or
 *   (gunichar)(-1) if @index is beyond the last index.
 **/
gunichar 
gucharmap_codepoint_list_get_char (GucharmapCodepointList *list, 
                                   gint                    index)
{
  g_return_val_if_fail (IS_GUCHARMAP_CODEPOINT_LIST (list), (gunichar)(-1));

  return GUCHARMAP_CODEPOINT_LIST_GET_CLASS (list)->get_char (list, index);
}

/**
 * gucharmap_codepoint_list_get_index:
 * @list: a #GucharmapCodepointList
 * @wc: character for which to find the index
 * 
 * Return value: index of @wc, or -1 if @wc is not in this
 * codepoint list.
 **/
gint
gucharmap_codepoint_list_get_index (GucharmapCodepointList *list, 
                                    gunichar                wc)
{
  g_return_val_if_fail (IS_GUCHARMAP_CODEPOINT_LIST (list), -1);

  return GUCHARMAP_CODEPOINT_LIST_GET_CLASS (list)->get_index (list, wc);
}

/**
 * gucharmap_codepoint_list_get_last_index:
 * @list: a #GucharmapCodepointList
 *
 * Return value: last index in this codepoint list.
 **/
gint
gucharmap_codepoint_list_get_last_index (GucharmapCodepointList *list)
{
  g_return_val_if_fail (IS_GUCHARMAP_CODEPOINT_LIST (list), -1);

  return GUCHARMAP_CODEPOINT_LIST_GET_CLASS (list)->get_last_index (list);
}

/**
 * gucharmap_codepoint_list_new:
 *
 * Creates a new codepoint list.
 *
 * Return value: the newly-created #GucharmapCodepointList. Use
 * g_object_unref() to free the result.
 **/
GucharmapCodepointList * 
gucharmap_codepoint_list_new (gunichar start,
                              gunichar end)
{
  GucharmapCodepointList *list;
  DefaultCodepointListPrivate *priv;

  list = GUCHARMAP_CODEPOINT_LIST (g_object_new (gucharmap_codepoint_list_get_type (), NULL));
  priv = GUCHARMAP_CODEPOINT_LIST_GET_PRIVATE (list);

  /* XXX: what to do if start > end, etc */

  priv->start = start;

  if (end <= UNICHAR_MAX)
    priv->end = end;
  else
    priv->end = UNICHAR_MAX;

  return list;
}