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

« back to all changes in this revision

Viewing changes to src/json_dec.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_dec.c
 
3
 * Decoder for JSON format.
 
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 "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
 
 
35
/**
 
36
 * Process a log line.
 
37
 * @private
 
38
 * @returns 0 on success, something else otherwise.
 
39
 */
 
40
static inline int
 
41
processLn(ee_ctx ctx, es_str_t *ln,
 
42
          int (*cbNewEvt)(struct ee_event *event))
 
43
{
 
44
        int r;
 
45
        struct ee_event *event;
 
46
        char *str;
 
47
 
 
48
        str = es_str2cstr(ln, NULL);
 
49
        CHKN(event = ee_newEventFromJSON(ctx, str));
 
50
        free(str);
 
51
        CHKR(cbNewEvt(event));
 
52
        r = 0;
 
53
 
 
54
done:   return r;
 
55
}
 
56
 
 
57
 
 
58
int
 
59
ee_jsonDec(ee_ctx ctx, int (*cbGetLine)(es_str_t **ln),
 
60
          int (*cbNewEvt)(struct ee_event *event),
 
61
              es_str_t **errMsg)
 
62
{
 
63
        int r;
 
64
        int lnNbr;
 
65
        es_str_t *ln = NULL;
 
66
        char errMsgBuf[1024];
 
67
        size_t errlen;
 
68
        
 
69
        lnNbr = 1;
 
70
        r = cbGetLine(&ln);
 
71
        while(r == 0) {
 
72
                if((r = processLn(ctx, ln, cbNewEvt)) != 0) {
 
73
                        errlen = snprintf(errMsgBuf, sizeof(errMsgBuf),
 
74
                                          "error processing line %d", lnNbr);
 
75
                        *errMsg = es_newStrFromCStr(errMsgBuf, errlen);
 
76
                        goto done;
 
77
                }
 
78
                free(ln);
 
79
                r = cbGetLine(&ln);
 
80
                lnNbr++;
 
81
        }
 
82
        /* when we are done with the file, we need to check if there are
 
83
         * any objects to submit (usually there are!)
 
84
         */
 
85
 
 
86
        if(r == EE_EOF)
 
87
                r = 0;
 
88
done:
 
89
        return r;
 
90
}
 
91
/* vim :ts=4:sw=4 */