~ubuntu-branches/ubuntu/utopic/libee/utopic-proposed

« back to all changes in this revision

Viewing changes to src/fieldbucket.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2010-12-11 12:37:09 UTC
  • Revision ID: james.westby@ubuntu.com-20101211123709-i8v7mpdtzhgjoqn5
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file fieldbucket.c
 
3
 * Implements fieldbucket object methods.
 
4
 *//* Libee - An Event Expression Library inspired by CEE
 
5
 * Copyright 2010 by Rainer Gerhards and Adiscon GmbH.
 
6
 *
 
7
 * This file is part of libee.
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2.1 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 *
 
23
 * A copy of the LGPL v2.1 can be found in the file "COPYING" in this distribution.
 
24
 */
 
25
#include "config.h"
 
26
#include <stdlib.h>
 
27
#include <stdio.h>
 
28
#include <stdarg.h>
 
29
#include <assert.h>
 
30
 
 
31
#include "libee/libee.h"
 
32
#include "libee/internal.h"
 
33
 
 
34
#define ERR_ABORT {r = 1; goto done; }
 
35
 
 
36
#define CHECK_FIELD \
 
37
        if(fieldbucket->objID != ObjID_FIELDBUCKET) { \
 
38
                r = -1; \
 
39
                goto done; \
 
40
        }
 
41
 
 
42
 
 
43
struct ee_fieldbucket*
 
44
ee_newFieldbucket(ee_ctx ctx)
 
45
{
 
46
        struct ee_fieldbucket *fieldbucket;
 
47
        if((fieldbucket = malloc(sizeof(struct ee_fieldbucket))) == NULL)
 
48
                goto done;
 
49
 
 
50
        fieldbucket->objID = ObjID_FIELDBUCKET;
 
51
        fieldbucket->ctx = ctx;
 
52
        fieldbucket->root = fieldbucket->tail = NULL;
 
53
 
 
54
done:   return fieldbucket;
 
55
}
 
56
 
 
57
 
 
58
void
 
59
ee_deleteFieldbucket(struct ee_fieldbucket *fieldbucket)
 
60
{
 
61
        struct ee_fieldbucket_listnode *node, *nodeDel;
 
62
 
 
63
        assert(fieldbucket->objID == ObjID_FIELDBUCKET);
 
64
        fieldbucket->objID = ObjID_DELETED;
 
65
        for(node = fieldbucket->root ; node != NULL ; ) {
 
66
                nodeDel = node;
 
67
                node = node->next;
 
68
                ee_deleteField(nodeDel->field);
 
69
                free(nodeDel);
 
70
        }
 
71
        free(fieldbucket);
 
72
}
 
73
 
 
74
 
 
75
/* TODO: when in validating mode, check duplicate field entries */
 
76
int
 
77
ee_addFieldToBucket(struct ee_fieldbucket *fieldb, struct ee_field *field)
 
78
{
 
79
        int r;
 
80
        struct ee_fieldbucket_listnode *node;
 
81
        assert(fieldb != NULL);assert(fieldb->objID == ObjID_FIELDBUCKET);
 
82
        assert(field != NULL);assert(field->objID == ObjID_FIELD);
 
83
 
 
84
        CHKN(node = malloc(sizeof(struct ee_fieldbucket_listnode)));
 
85
        node->field = field;
 
86
        node->next = NULL;
 
87
        if(fieldb->root == NULL) {
 
88
                fieldb->root = fieldb->tail = node;
 
89
        } else {
 
90
                fieldb->tail->next = node;
 
91
                fieldb->tail = node;
 
92
        }
 
93
        r = 0;
 
94
 
 
95
done:   return r;
 
96
}
 
97
 
 
98
 
 
99
/* Note: this function currently is quite performance-hungry. We should
 
100
 * replace all this searching by hashtable access some time in the future,
 
101
 * but for now the focus is on getting things done in a simple fashion, and
 
102
 * so the simple list search approach is good enough. But if you try to
 
103
 * optimize, do not optimize the list search but rather introduce the hash
 
104
 * table as second indexing structure! -- rgerhards, 2010-12-01
 
105
 */
 
106
struct ee_field*
 
107
ee_getBucketField(struct ee_fieldbucket *bucket, es_str_t *name)
 
108
{
 
109
        struct ee_fieldbucket_listnode *node;
 
110
 
 
111
        for(node = bucket->root ; node != NULL ; node = node->next) {
 
112
                if(!es_strcmp(name, node->field->name))
 
113
                        break;
 
114
        }
 
115
 
 
116
        return((node == NULL) ? NULL : node->field);
 
117
}
 
118
 
 
119
 
 
120
int
 
121
ee_getNumFieldVals(struct ee_field *field)
 
122
{
 
123
        assert(field != NULL);
 
124
        return(field->nVals);
 
125
}
 
126
 
 
127
 
 
128
/* TODO: this function currently assumes that the field has a string
 
129
 * representation, which for now is always true. Needs to be changed if
 
130
 * we change the representation!
 
131
 */
 
132
es_str_t*
 
133
ee_getFieldValueAsStr(struct ee_field *field, unsigned short n)
 
134
{
 
135
        es_str_t *str;
 
136
        assert(field != NULL);
 
137
 
 
138
        if(n >= field->nVals) {
 
139
                str = NULL;
 
140
                goto done;
 
141
        }
 
142
        if(n == 0) {
 
143
                str = es_strdup(field->val->val.str);
 
144
        } else {
 
145
                assert(0); // TODO: implement!
 
146
        }
 
147
done:
 
148
        return str;
 
149
}