1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
3 * Copyright (C) 2001 Ximian Inc.
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
33 #include "camel-index.h"
34 #include "camel-object.h"
38 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
40 #define CAMEL_INDEX_VERSION (0x01)
42 struct _CamelIndexPrivate {
46 #define _PRIVATE(o) (((CamelIndex *)(o))->priv)
48 #define CI_CLASS(o) ((CamelIndexClass *)(((CamelObject *)o)->klass))
50 /* ********************************************************************** */
52 /* ********************************************************************** */
54 static CamelObjectClass *camel_index_parent;
57 camel_index_class_init(CamelIndexClass *klass)
59 camel_index_parent = CAMEL_OBJECT_CLASS(camel_type_get_global_classfuncs(camel_object_get_type()));
63 camel_index_init(CamelIndex *idx)
65 struct _CamelIndexPrivate *p;
67 p = _PRIVATE(idx) = g_malloc0(sizeof(*p));
69 idx->version = CAMEL_INDEX_VERSION;
73 camel_index_finalise(CamelIndex *idx)
80 camel_index_get_type(void)
82 static CamelType type = CAMEL_INVALID_TYPE;
84 if (type == CAMEL_INVALID_TYPE) {
85 type = camel_type_register(camel_object_get_type(), "CamelIndex",
87 sizeof (CamelIndexClass),
88 (CamelObjectClassInitFunc) camel_index_class_init,
90 (CamelObjectInitFunc) camel_index_init,
91 (CamelObjectFinalizeFunc) camel_index_finalise);
98 camel_index_new(const char *path, int flags)
100 CamelIndex *idx = (CamelIndex *)camel_object_new(camel_index_get_type());
102 camel_index_construct(idx, path, flags);
108 camel_index_construct(CamelIndex *idx, const char *path, int flags)
111 idx->path = g_strdup_printf("%s.index", path);
116 camel_index_rename(CamelIndex *idx, const char *path)
118 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
120 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
121 return CI_CLASS(idx)->rename(idx, path);
129 camel_index_set_normalise(CamelIndex *idx, CamelIndexNorm func, void *data)
131 g_return_if_fail (CAMEL_IS_INDEX (idx));
133 idx->normalise = func;
134 idx->normalise_data = data;
138 camel_index_sync(CamelIndex *idx)
140 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
142 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
143 return CI_CLASS(idx)->sync(idx);
151 camel_index_compress(CamelIndex *idx)
153 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
155 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
156 return CI_CLASS(idx)->compress(idx);
164 camel_index_delete(CamelIndex *idx)
168 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
170 if ((idx->state & CAMEL_INDEX_DELETED) == 0) {
171 ret = CI_CLASS(idx)->delete(idx);
172 idx->state |= CAMEL_INDEX_DELETED;
182 camel_index_has_name(CamelIndex *idx, const char *name)
184 g_return_val_if_fail (CAMEL_IS_INDEX (idx), FALSE);
186 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
187 return CI_CLASS(idx)->has_name(idx, name);
193 camel_index_add_name(CamelIndex *idx, const char *name)
195 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
197 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
198 return CI_CLASS(idx)->add_name(idx, name);
204 camel_index_write_name(CamelIndex *idx, CamelIndexName *idn)
206 g_return_val_if_fail (CAMEL_IS_INDEX (idx), -1);
208 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
209 return CI_CLASS(idx)->write_name(idx, idn);
217 camel_index_find_name(CamelIndex *idx, const char *name)
219 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
221 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
222 return CI_CLASS(idx)->find_name(idx, name);
228 camel_index_delete_name(CamelIndex *idx, const char *name)
230 g_return_if_fail (CAMEL_IS_INDEX (idx));
232 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
233 CI_CLASS(idx)->delete_name(idx, name);
237 camel_index_find(CamelIndex *idx, const char *word)
239 char *b = (char *)word;
240 CamelIndexCursor *ret;
242 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
244 if ((idx->state & CAMEL_INDEX_DELETED) != 0)
248 b = idx->normalise(idx, word, idx->normalise_data);
250 ret = CI_CLASS(idx)->find(idx, b);
259 camel_index_words(CamelIndex *idx)
261 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
263 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
264 return CI_CLASS(idx)->words(idx);
270 camel_index_names(CamelIndex *idx)
272 g_return_val_if_fail (CAMEL_IS_INDEX (idx), NULL);
274 if ((idx->state & CAMEL_INDEX_DELETED) == 0)
275 return CI_CLASS(idx)->names(idx);
280 /* ********************************************************************** */
282 /* ********************************************************************** */
284 static CamelObjectClass *camel_index_name_parent;
286 #define CIN_CLASS(o) ((CamelIndexNameClass *)(((CamelObject *)o)->klass))
289 camel_index_name_class_init(CamelIndexNameClass *klass)
291 camel_index_name_parent = CAMEL_OBJECT_CLASS(camel_type_get_global_classfuncs(camel_object_get_type()));
295 camel_index_name_init(CamelIndexName *idn)
300 camel_index_name_finalise(CamelIndexName *idn)
303 camel_object_unref((CamelObject *)idn->index);
307 camel_index_name_get_type(void)
309 static CamelType type = CAMEL_INVALID_TYPE;
311 if (type == CAMEL_INVALID_TYPE) {
312 type = camel_type_register(camel_object_get_type(), "CamelIndexName",
313 sizeof (CamelIndexName),
314 sizeof (CamelIndexNameClass),
315 (CamelObjectClassInitFunc) camel_index_name_class_init,
317 (CamelObjectInitFunc) camel_index_name_init,
318 (CamelObjectFinalizeFunc) camel_index_name_finalise);
325 camel_index_name_new(CamelIndex *idx, const char *name)
327 CamelIndexName *idn = (CamelIndexName *)camel_object_new(camel_index_name_get_type());
330 camel_object_ref((CamelObject *)idx);
336 camel_index_name_add_word(CamelIndexName *idn, const char *word)
338 char *b = (char *)word;
340 if (idn->index->normalise)
341 b = idn->index->normalise(idn->index, word, idn->index->normalise_data);
343 CIN_CLASS(idn)->add_word(idn, b);
350 camel_index_name_add_buffer(CamelIndexName *idn, const char *buffer, size_t len)
352 return CIN_CLASS(idn)->add_buffer(idn, buffer, len);
355 /* ********************************************************************** */
356 /* CamelIndexCursor */
357 /* ********************************************************************** */
359 static CamelObjectClass *camel_index_cursor_parent;
361 #define CIC_CLASS(o) ((CamelIndexCursorClass *)(((CamelObject *)o)->klass))
364 camel_index_cursor_class_init(CamelIndexCursorClass *klass)
366 camel_index_cursor_parent = CAMEL_OBJECT_CLASS(camel_type_get_global_classfuncs(camel_object_get_type()));
370 camel_index_cursor_init(CamelIndexCursor *idc)
375 camel_index_cursor_finalise(CamelIndexCursor *idc)
378 camel_object_unref((CamelObject *)idc->index);
382 camel_index_cursor_get_type(void)
384 static CamelType type = CAMEL_INVALID_TYPE;
386 if (type == CAMEL_INVALID_TYPE) {
387 type = camel_type_register(camel_object_get_type(), "CamelIndexCursor",
388 sizeof (CamelIndexCursor),
389 sizeof (CamelIndexCursorClass),
390 (CamelObjectClassInitFunc) camel_index_cursor_class_init,
392 (CamelObjectInitFunc) camel_index_cursor_init,
393 (CamelObjectFinalizeFunc) camel_index_cursor_finalise);
400 camel_index_cursor_new(CamelIndex *idx, const char *name)
402 CamelIndexCursor *idc = (CamelIndexCursor *)camel_object_new(camel_index_cursor_get_type());
405 camel_object_ref((CamelObject *)idx);
411 camel_index_cursor_next(CamelIndexCursor *idc)
413 return CIC_CLASS(idc)->next(idc);
417 camel_index_cursor_reset(CamelIndexCursor *idc)
419 CIC_CLASS(idc)->reset(idc);