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

« back to all changes in this revision

Viewing changes to src/field.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 field.c
 
3
 * Implements field 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(field->objID != ObjID_FIELD) { \
 
38
                r = -1; \
 
39
                goto done; \
 
40
        }
 
41
 
 
42
struct ee_field*
 
43
ee_newField(ee_ctx ctx)
 
44
{
 
45
        struct ee_field *field;
 
46
        if((field = malloc(sizeof(struct ee_field))) == NULL) goto done;
 
47
        field->objID = ObjID_FIELD;
 
48
        field->ctx = ctx;
 
49
        field->name = NULL;
 
50
        field->nVals = 0;
 
51
        field->valroot = field->valtail = NULL;
 
52
done:
 
53
        return field;
 
54
}
 
55
 
 
56
 
 
57
void
 
58
ee_deleteField(struct ee_field *field)
 
59
{
 
60
        struct ee_valnode *node, *nodeDel;
 
61
 
 
62
        assert(field->objID == ObjID_FIELD);
 
63
        es_deleteStr(field->name);
 
64
        if(field->nVals > 0) {
 
65
                ee_deleteValue(field->val);
 
66
        }
 
67
        if(field->nVals > 1) {
 
68
                node = field->valroot;
 
69
                while(node != NULL) {
 
70
                        nodeDel = node;
 
71
                        ee_deleteValue(nodeDel->val);
 
72
                        free(nodeDel);
 
73
                }
 
74
        }
 
75
        free(field);
 
76
}
 
77
 
 
78
struct ee_field*
 
79
ee_newFieldFromNV(ee_ctx __attribute__((unused)) ctx, char *name, struct ee_value *val)
 
80
{
 
81
        struct ee_field *field;
 
82
        assert(val->objID == ObjID_VALUE);
 
83
        if((field = ee_newField(ctx)) == NULL) goto done;
 
84
 
 
85
        if((field->name = es_newStrFromCStr(name, strlen(name))) == NULL) {
 
86
                free(field);
 
87
                field = NULL;
 
88
                goto done;
 
89
        }
 
90
 
 
91
        field->val = val;
 
92
 
 
93
done:
 
94
        return field;
 
95
}
 
96
 
 
97
 
 
98
/* In this version of the method, we simply create a copy of the field name. In
 
99
 * later versions, depending on our state and compliance level, we may use
 
100
 * a pointer to an in-memory representation of the dictionary entity instead.
 
101
 * rgerhards, 2010-10-26
 
102
 */
 
103
int
 
104
ee_nameField(struct ee_field *field, es_str_t *name)
 
105
{
 
106
        int r;
 
107
        assert(field->objID == ObjID_FIELD);
 
108
        if(field->name != NULL) {
 
109
                r = EE_FIELDHASNAME;
 
110
                goto done;
 
111
        }
 
112
        CHKN(field->name = es_strdup(name));
 
113
        r = 0;
 
114
done:
 
115
        return r;
 
116
}
 
117
 
 
118
 
 
119
int
 
120
ee_addValueToField(struct ee_field *field, struct ee_value *val)
 
121
{
 
122
        int r;
 
123
        struct ee_valnode *valnode;
 
124
        assert(field != NULL);assert(field->objID== ObjID_FIELD);
 
125
        assert(val != NULL);assert(val->objID == ObjID_VALUE);
 
126
 
 
127
        if(field->nVals == 0) {
 
128
                field->nVals = 1;
 
129
                field->val = val;
 
130
        } else if(field->nVals == LIBEE_CEE_MAX_VALS_PER_FIELD) {
 
131
                r = EE_TOOMANYVALUES;
 
132
                goto done;
 
133
        } else {
 
134
                /* we need to add to the list of values */
 
135
                CHKN(valnode = ee_newValnode());
 
136
                valnode->val = val;
 
137
                ++field->nVals;
 
138
                if(field->valtail == NULL) {
 
139
                        field->valroot = field->valtail = valnode;
 
140
                } else {
 
141
                        field->valtail->next = valnode;
 
142
                        field->valtail = valnode;
 
143
                }
 
144
        }
 
145
        r = 0;
 
146
done:
 
147
        return r;
 
148
}