File: ezapi1.c

package info (click to toggle)
libee 0.4.1-2.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,708 kB
  • sloc: sh: 10,219; ansic: 3,362; makefile: 84
file content (112 lines) | stat: -rw-r--r-- 2,950 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * @file ezAPI1.c
 * @brief A very basic test for the event class.
 *
 * @author Rainer Gerhards <rgerhards@adiscon.com>
 *
 *//*
 * Libee - An Event Expression Library inspired by CEE
 * Copyright 2010 by Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of liblognorm.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL v2.1 can be found in the file "COPYING" in this distribution.
 */
#include "config.h"
#include <stdio.h>
#include <getopt.h>
#include <libestr.h>
#include "config.h"
#include "libee/libee.h"

static ee_ctx ctx;

void
dbgCallBack(void __attribute__((unused)) *cookie, char *msg,
	    size_t __attribute__((unused)) lenMsg)
{
	printf("libee: %s\n", msg);
}

void errout(char *errmsg)
{
	fprintf(stderr, "%s\n", errmsg);
	exit(1);
}


int main(int argc, char *argv[])
{
	int opt;
	FILE *fpIn = stdin;
	es_str_t *out;
	char namebuf[1024];
	char valbuf[1024];
	es_str_t *str;
	struct ee_event *event;

	while((opt = getopt(argc, argv, "i:")) != -1) {
		switch (opt) {
		case 'i':
			if((fpIn = fopen(optarg, "r")) == NULL) {
				perror(optarg);
				exit(1);
			}
			break;
		default:
			printf("invalid option '%c' or value missing - "
			       "terminating...\n", opt);
			exit (1);
			break;
		}
	}

	if(strcmp(VERSION, ee_version())) {
		char buf[1024];
		sprintf(buf, "loaded library version %s does not match "
		       "to-be-tested version %s.", VERSION, ee_version());
		errout(buf);
	}

	if((ctx = ee_initCtx()) == NULL) {
		errout("Could not initialize libee context");
	}

	ee_setDebugCB(ctx, dbgCallBack, NULL);

	if((event = ee_newEvent(ctx)) == NULL)
		errout("could not create event");
	/* add fields
	 * file contains name, value on seperatelines */
	while(!feof(fpIn)) {
		if(fgets(namebuf, sizeof(namebuf), fpIn) != NULL) {
			namebuf[strlen(namebuf)-1] = '\0'; /* strip '\n' */
			if(fgets(valbuf, sizeof(valbuf), fpIn) == NULL)
				errout("invalid test case file format!");
			valbuf[strlen(valbuf)-1] = '\0'; /* strip '\n' */
			str = es_newStrFromCStr(valbuf, strlen(valbuf));
			ee_addStrFieldToEvent(event, namebuf, str);
		}
	}

	ee_fmtEventToRFC5424(event, &out);
	printf("Formatted event: '%s'\n", es_str2cstr(out, NULL));
	es_deleteStr(out);

	ee_exitCtx(ctx);
	return 0;
}