Blame src/liblognorm.c

Packit 1422b7
/* This file implements the liblognorm API.
Packit 1422b7
 * See header file for descriptions.
Packit 1422b7
 *
Packit 1422b7
 * liblognorm - a fast samples-based log normalization library
Packit 1422b7
 * Copyright 2013 by Rainer Gerhards and Adiscon GmbH.
Packit 1422b7
 *
Packit 1422b7
 * Modified by Pavel Levshin (pavel@levshin.spb.ru) in 2013
Packit 1422b7
 *
Packit 1422b7
 * This file is part of liblognorm.
Packit 1422b7
 *
Packit 1422b7
 * This library is free software; you can redistribute it and/or
Packit 1422b7
 * modify it under the terms of the GNU Lesser General Public
Packit 1422b7
 * License as published by the Free Software Foundation; either
Packit 1422b7
 * version 2.1 of the License, or (at your option) any later version.
Packit 1422b7
 *
Packit 1422b7
 * This library is distributed in the hope that it will be useful,
Packit 1422b7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1422b7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 1422b7
 * Lesser General Public License for more details.
Packit 1422b7
 *
Packit 1422b7
 * You should have received a copy of the GNU Lesser General Public
Packit 1422b7
 * License along with this library; if not, write to the Free Software
Packit 1422b7
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 1422b7
 *
Packit 1422b7
 * A copy of the LGPL v2.1 can be found in the file "COPYING" in this distribution.
Packit 1422b7
 */
Packit 1422b7
#include "config.h"
Packit 1422b7
#include <string.h>
Packit 1422b7
#include <errno.h>
Packit 1422b7
Packit 1422b7
#include "liblognorm.h"
Packit 1422b7
#include "lognorm.h"
Packit 1422b7
#include "annot.h"
Packit 1422b7
#include "samp.h"
Packit 1422b7
#include "v1_liblognorm.h"
Packit 1422b7
#include "v1_ptree.h"
Packit 1422b7
Packit 1422b7
#define CHECK_CTX \
Packit 1422b7
	if(ctx->objID != LN_ObjID_CTX) { \
Packit 1422b7
		r = -1; \
Packit 1422b7
		goto done; \
Packit 1422b7
	}
Packit 1422b7
Packit 1422b7
const char *
Packit 1422b7
ln_version(void)
Packit 1422b7
{
Packit 1422b7
	return VERSION;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
int
Packit 1422b7
ln_hasAdvancedStats(void)
Packit 1422b7
{
Packit 1422b7
#ifdef	ADVANCED_STATS
Packit 1422b7
	return 1;
Packit 1422b7
#else
Packit 1422b7
	return 0;
Packit 1422b7
#endif
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
ln_ctx
Packit 1422b7
ln_initCtx(void)
Packit 1422b7
{
Packit 1422b7
	ln_ctx ctx;
Packit 1422b7
	if((ctx = calloc(1, sizeof(struct ln_ctx_s))) == NULL)
Packit 1422b7
		goto done;
Packit 1422b7
Packit 1422b7
#ifdef HAVE_JSON_GLOBAL_SET_STRING_HASH
Packit 1422b7
	json_global_set_string_hash(JSON_C_STR_HASH_PERLLIKE);
Packit 1422b7
#endif
Packit 1422b7
#ifdef HAVE_JSON_GLOBAL_SET_PRINTBUF_INITIAL_SIZE
Packit 1422b7
	json_global_set_printbuf_initial_size(2048);
Packit 1422b7
#endif
Packit 1422b7
	ctx->objID = LN_ObjID_CTX;
Packit 1422b7
	ctx->dbgCB = NULL;
Packit 1422b7
	ctx->opts = 0;
Packit 1422b7
Packit 1422b7
	/* we add an root for the empty word, this simplifies parse
Packit 1422b7
	 * dag handling.
Packit 1422b7
	 */
Packit 1422b7
	if((ctx->pdag = ln_newPDAG(ctx)) == NULL) {
Packit 1422b7
		free(ctx);
Packit 1422b7
		ctx = NULL;
Packit 1422b7
		goto done;
Packit 1422b7
	}
Packit 1422b7
	/* same for annotation set */
Packit 1422b7
	if((ctx->pas = ln_newAnnotSet(ctx)) == NULL) {
Packit 1422b7
		ln_pdagDelete(ctx->pdag);
Packit 1422b7
		free(ctx);
Packit 1422b7
		ctx = NULL;
Packit 1422b7
		goto done;
Packit 1422b7
	}
Packit 1422b7
Packit 1422b7
done:
Packit 1422b7
	return ctx;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
void
Packit 1422b7
ln_setCtxOpts(ln_ctx ctx, const unsigned opts) {
Packit 1422b7
	ctx->opts |= opts;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
Packit 1422b7
int
Packit 1422b7
ln_exitCtx(ln_ctx ctx)
Packit 1422b7
{
Packit 1422b7
	int r = 0;
Packit 1422b7
Packit 1422b7
	CHECK_CTX;
Packit 1422b7
Packit 1422b7
	ln_dbgprintf(ctx, "exitCtx %p", ctx);
Packit 1422b7
	ctx->objID = LN_ObjID_None; /* prevent double free */
Packit 1422b7
	/* support for old cruft */
Packit 1422b7
	if(ctx->ptree != NULL)
Packit 1422b7
		ln_deletePTree(ctx->ptree);
Packit 1422b7
	/* end support for old cruft */
Packit 1422b7
	if(ctx->pdag != NULL)
Packit 1422b7
		ln_pdagDelete(ctx->pdag);
Packit 1422b7
	for(int i = 0 ; i < ctx->nTypes ; ++i) {
Packit 1422b7
		free((void*)ctx->type_pdags[i].name);
Packit 1422b7
		ln_pdagDelete(ctx->type_pdags[i].pdag);
Packit 1422b7
	}
Packit 1422b7
	free(ctx->type_pdags);
Packit 1422b7
	if(ctx->rulePrefix != NULL)
Packit 1422b7
		es_deleteStr(ctx->rulePrefix);
Packit 1422b7
	if(ctx->pas != NULL)
Packit 1422b7
		ln_deleteAnnotSet(ctx->pas);
Packit 1422b7
	free(ctx);
Packit 1422b7
done:
Packit 1422b7
	return r;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
Packit 1422b7
int
Packit 1422b7
ln_setDebugCB(ln_ctx ctx, void (*cb)(void*, const char*, size_t), void *cookie)
Packit 1422b7
{
Packit 1422b7
	int r = 0;
Packit 1422b7
Packit 1422b7
	CHECK_CTX;
Packit 1422b7
	ctx->dbgCB = cb;
Packit 1422b7
	ctx->dbgCookie = cookie;
Packit 1422b7
done:
Packit 1422b7
	return r;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
Packit 1422b7
int
Packit 1422b7
ln_setErrMsgCB(ln_ctx ctx, void (*cb)(void*, const char*, size_t), void *cookie)
Packit 1422b7
{
Packit 1422b7
	int r = 0;
Packit 1422b7
Packit 1422b7
	CHECK_CTX;
Packit 1422b7
	ctx->errmsgCB = cb;
Packit 1422b7
	ctx->errmsgCookie = cookie;
Packit 1422b7
done:
Packit 1422b7
	return r;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
int
Packit 1422b7
ln_loadSamples(ln_ctx ctx, const char *file)
Packit 1422b7
{
Packit 1422b7
	int r = 0;
Packit 1422b7
	const char *tofree;
Packit 1422b7
	CHECK_CTX;
Packit 1422b7
	ctx->conf_file = tofree = strdup(file);
Packit 1422b7
	ctx->conf_ln_nbr = 0;
Packit 1422b7
	++ctx->include_level;
Packit 1422b7
	r = ln_sampLoad(ctx, file);
Packit 1422b7
	--ctx->include_level;
Packit 1422b7
	free((void*)tofree);
Packit 1422b7
	ctx->conf_file = NULL;
Packit 1422b7
done:
Packit 1422b7
	return r;
Packit 1422b7
}
Packit 1422b7
Packit 1422b7
int
Packit 1422b7
ln_loadSamplesFromString(ln_ctx ctx, const char *string)
Packit 1422b7
{
Packit 1422b7
	int r = 0;
Packit 1422b7
	const char *tofree;
Packit 1422b7
	CHECK_CTX;
Packit 1422b7
	ctx->conf_file = tofree = strdup("--NO-FILE--");
Packit 1422b7
	ctx->conf_ln_nbr = 0;
Packit 1422b7
	++ctx->include_level;
Packit 1422b7
	r = ln_sampLoadFromString(ctx, string);
Packit 1422b7
	--ctx->include_level;
Packit 1422b7
	free((void*)tofree);
Packit 1422b7
	ctx->conf_file = NULL;
Packit 1422b7
done:
Packit 1422b7
	return r;
Packit 1422b7
}