1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3
* Copyright (C) 2013 Intel Corporation
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU Lesser General Public License as published by
7
* the Free Software Foundation.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this program; if not, see <http://www.gnu.org/licenses/>.
17
* Author: Tristan Van Berkom <tristanvb@openismus.com>
24
#include "e-alphabet-index-private.h"
26
/* C++ standard library */
35
#include <unicode/alphaindex.h>
37
using icu::AlphabeticIndex;
40
struct _EAlphabetIndex {
41
AlphabeticIndex *priv;
44
/* Create an AlphabetIndex for a given language code (normally
45
* language codes are 2 letter codes, eg. 'en' = English 'es' = Spanish.
48
_e_alphabet_index_cxx_new_for_language (const gchar *language)
50
UErrorCode status = U_ZERO_ERROR;
51
EAlphabetIndex *alphabet_index;
53
g_return_val_if_fail (language != NULL, NULL);
55
alphabet_index = g_slice_new (EAlphabetIndex);
56
alphabet_index->priv = new AlphabeticIndex (Locale (language), status);
58
return alphabet_index;
61
/* Frees an EAlphabetIndex and it's associated resources
64
_e_alphabet_index_cxx_free (EAlphabetIndex *alphabet_index)
67
delete alphabet_index->priv;
68
g_slice_free (EAlphabetIndex, alphabet_index);
72
/* Fetch the given index where 'word' should sort
75
_e_alphabet_index_cxx_get_index (EAlphabetIndex *alphabet_index,
78
UErrorCode status = U_ZERO_ERROR;
82
g_return_val_if_fail (alphabet_index != NULL, -1);
83
g_return_val_if_fail (word != NULL, -1);
85
string = icu::UnicodeString::fromUTF8 (word);
86
index = alphabet_index->priv->getBucketIndex (string, status);
91
/* Fetch the list of labels in the alphabetic index.
93
* Returns an array of UTF-8 labels for each alphabetic
94
* index position 'n_labels' long, the returned array
95
* of strings can be freed with g_strfreev()
97
* The underflow, overflow and inflow parameters will be
98
* set to the appropriate indexes (reffers to indexes in the
102
_e_alphabet_index_cxx_get_labels (EAlphabetIndex *alphabet_index,
108
UErrorCode status = U_ZERO_ERROR;
109
gchar **labels = NULL;
112
g_return_val_if_fail (alphabet_index != NULL, NULL);
113
g_return_val_if_fail (n_labels != NULL, NULL);
114
g_return_val_if_fail (underflow != NULL, NULL);
115
g_return_val_if_fail (inflow != NULL, NULL);
116
g_return_val_if_fail (overflow != NULL, NULL);
118
count = alphabet_index->priv->getBucketCount (status);
120
labels = g_new0 (gchar *, count + 1);
122
/* In case they are missing, they should be set to -1 */
123
*underflow = *inflow = *overflow = -1;
125
/* Iterate over the AlphabeticIndex and collect UTF-8 versions
126
* of the bucket labels
128
alphabet_index->priv->resetBucketIterator (status);
130
for (i = 0; alphabet_index->priv->nextBucket (status); i++) {
131
UAlphabeticIndexLabelType label_type;
132
UnicodeString ustring;
135
label_type = alphabet_index->priv->getBucketLabelType ();
137
switch (label_type) {
138
case U_ALPHAINDEX_UNDERFLOW: *underflow = i; break;
139
case U_ALPHAINDEX_INFLOW: *inflow = i; break;
140
case U_ALPHAINDEX_OVERFLOW: *overflow = i; break;
141
case U_ALPHAINDEX_NORMAL: /* do nothing */ break;
144
/* This is annoyingly heavy but not a function called
145
* very often, this could be improved by calling icu::UnicodeString::toUTF8()
146
* and implementing ICU's ByteSync class using glib's memory allocator.
148
ustring = alphabet_index->priv->getBucketLabel ();
149
string = ustring.toUTF8String (string);
150
labels[i] = g_strdup (string.c_str());