~ubuntu-branches/ubuntu/saucy/libee/saucy

« back to all changes in this revision

Viewing changes to src/event.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 event.c
 
3
 * Implements event 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
#include <libestr.h>
 
31
 
 
32
#include "libee/libee.h"
 
33
#include "libee/internal.h"
 
34
#include "libee/field.h"
 
35
#include "libee/value.h"
 
36
 
 
37
#define ERR_ABORT {r = 1; goto done; }
 
38
 
 
39
#define CHECK_FIELD \
 
40
        if(event->objID != ObjID_EVENT) { \
 
41
                r = -1; \
 
42
                goto done; \
 
43
        }
 
44
 
 
45
 
 
46
struct ee_event*
 
47
ee_newEvent(ee_ctx __attribute__((unused)) ctx)
 
48
{
 
49
        struct ee_event *event;
 
50
        if((event = malloc(sizeof(struct ee_event))) == NULL)
 
51
                goto done;
 
52
 
 
53
        event->objID = ObjID_EVENT;
 
54
        event->ctx = ctx;
 
55
        event->fields = NULL;
 
56
        event->tags = NULL;
 
57
 
 
58
done:
 
59
        return event;
 
60
}
 
61
 
 
62
 
 
63
void
 
64
ee_deleteEvent(struct ee_event *event)
 
65
{
 
66
        assert(event != NULL);assert(event->objID == ObjID_EVENT);
 
67
        if(event->tags != NULL)
 
68
                ee_deleteTagbucket(event->tags);
 
69
        if(event->fields != NULL)
 
70
                ee_deleteFieldbucket(event->fields);
 
71
        free(event);
 
72
}
 
73
 
 
74
 
 
75
int
 
76
ee_addTagToEvent(struct ee_event *event, char *tag)
 
77
{
 
78
        int r = -1;
 
79
 
 
80
        assert(event != NULL);assert(event->objID == ObjID_EVENT);
 
81
        if(event->tags == NULL)
 
82
                if((event->tags = ee_newTagbucket(event->ctx)) == NULL)
 
83
                        goto done;
 
84
 
 
85
        r = ee_addTagToBucket(event->tags, strdup(tag));
 
86
        
 
87
done:
 
88
        return r;
 
89
}
 
90
 
 
91
 
 
92
int
 
93
ee_addFieldToEvent(struct ee_event *event, struct ee_field *field)
 
94
{
 
95
        int r;
 
96
 
 
97
        assert(event != NULL);assert(event->objID == ObjID_EVENT);
 
98
        if(event->fields == NULL) {
 
99
                CHKN(event->fields = ee_newFieldbucket(event->ctx));
 
100
        }
 
101
 
 
102
        r = ee_addFieldToBucket(event->fields, field);
 
103
        
 
104
done:
 
105
        return r;
 
106
}
 
107
 
 
108
 
 
109
int
 
110
ee_addStrFieldToEvent(struct ee_event *event, char *fieldname, es_str_t *value)
 
111
{
 
112
        int r = -1;
 
113
        struct ee_field *field = NULL;
 
114
        struct ee_value *val = NULL;
 
115
 
 
116
        assert(event != NULL);assert(event->objID == ObjID_EVENT);
 
117
        if(event->fields == NULL)
 
118
                if((event->fields = ee_newFieldbucket(event->ctx)) == NULL)
 
119
                        goto done;
 
120
//printf("addStrField: %s/%s\n", fieldname, es_str2cstr(value, NULL));
 
121
 
 
122
        if((val = ee_newValue(event->ctx)) == NULL) goto done;
 
123
        if((r = ee_setStrValue(val, value)) != 0) goto done;
 
124
        if((field = ee_newFieldFromNV(event->ctx, fieldname, val)) == NULL) goto done;
 
125
        if((r = ee_addFieldToBucket(event->fields, field)) != 0) goto done;
 
126
 
 
127
done:
 
128
        if(r != 0) {
 
129
                /* central error cleanup */
 
130
                if(val != NULL)
 
131
                        ee_deleteValue(val);
 
132
                if(field != NULL)
 
133
                        ee_deleteField(field);
 
134
        }
 
135
 
 
136
        return r;
 
137
}