~ubuntu-branches/ubuntu/wily/libee/wily

« back to all changes in this revision

Viewing changes to src/json_event.c

  • Committer: Package Import Robot
  • Author(s): Pierre Chifflier
  • Date: 2012-02-21 22:42:25 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120221224225-8zbc41p3zysgf74d
Tags: 0.4.0-1
* Imported Upstream version 0.4.0
* Switch do DH 9
  - drop build-dep on hardening-wrapper, replace by dpkg-buildflags
  - enable multiarch support
* Update symbols file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file json_event.c
 
3
 * Supports creating an event out of an JSON string.
 
4
 *//* Libee - An Event Expression Library inspired by CEE
 
5
 * Copyright 2012 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 <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include "libestr.h"
 
29
#include "libee/libee.h"
 
30
#include "libee/fieldbucket.h"
 
31
#include "cjson/cjson.h"
 
32
 
 
33
int
 
34
callback(struct ee_fieldbucket *fields, char *name,int type,cJSON *item)
 
35
{
 
36
        struct ee_field *f;
 
37
        struct ee_value *val;
 
38
        char *valstr = NULL;
 
39
        es_str_t *estr;
 
40
 
 
41
//printf("callback: type %d, name %s\n", type, name);
 
42
        if(type == cJSON_Object)
 
43
                return 1; // TODO: support!
 
44
 
 
45
        if(type == cJSON_String) {
 
46
                valstr = item->valuestring;
 
47
        } else if(type == cJSON_Number) {
 
48
                valstr = cJSON_print_number(item);
 
49
        } else if(type == cJSON_NULL) {
 
50
                valstr = "-";
 
51
        } else if(type == cJSON_False) {
 
52
                valstr = "false";
 
53
        } else if(type == cJSON_True) {
 
54
                valstr = "true";
 
55
        }
 
56
//printf("callback: string value %s\n", valstr);
 
57
        
 
58
        estr = es_newStrFromCStr(valstr, strlen(valstr));
 
59
        val = ee_newValue(fields->ctx);
 
60
        ee_setStrValue(val, estr);
 
61
        f = ee_newFieldFromNV(fields->ctx, name, val);
 
62
        ee_addFieldToBucket(fields, f);
 
63
 
 
64
        if(type == cJSON_Number)
 
65
                free(valstr);
 
66
        return 1;
 
67
}
 
68
 
 
69
 
 
70
void parse_and_callback(struct ee_fieldbucket *fields, cJSON *item, char *prefix)
 
71
{
 
72
        char *name;
 
73
        char *newprefix;
 
74
        int lenprefix;
 
75
        int bNeedFree;
 
76
        int dorecurse;
 
77
//printf("parse_and_callback, item %p, item->string %p, prefix(%d): '%s'\n", item, item->string, strlen(prefix), prefix);
 
78
        while (item)
 
79
        {
 
80
                lenprefix = strlen(prefix);
 
81
                if(lenprefix == 0) {
 
82
                        newprefix = (item->string == NULL) ? "" : item->string;
 
83
                        bNeedFree = 0;
 
84
                } else {
 
85
                        name = (item->string == NULL) ? "*" : item->string;
 
86
                        newprefix=malloc(strlen(prefix)+strlen(name)+2);
 
87
                        sprintf(newprefix,"%s.%s",prefix,name);
 
88
                        bNeedFree = 1;
 
89
                }
 
90
                dorecurse = callback(fields, newprefix, item->type, item);
 
91
                if (item->child && dorecurse)
 
92
                        parse_and_callback(fields, item->child,newprefix);
 
93
                item=item->next;
 
94
                if(bNeedFree)
 
95
                        free(newprefix);
 
96
        }
 
97
}
 
98
 
 
99
struct ee_event*
 
100
ee_newEventFromJSON(ee_ctx ctx, char *str)
 
101
{
 
102
        struct cJSON *json;
 
103
        struct ee_event *e = NULL;
 
104
 
 
105
        json = cJSON_Parse(str);
 
106
        if(json == NULL)
 
107
                goto done;
 
108
        e = ee_newEvent(ctx);
 
109
        e->fields = ee_newFieldbucket(ctx);
 
110
        parse_and_callback(e->fields, json, "");
 
111
        cJSON_Delete(json);
 
112
done:
 
113
        return e;
 
114
}