
// lkf5.cpp : Implementation of Clkf5

#include "stdafx.h"
#include <stdio.h>
#include "Lkfkdoc.h"
#include "lkf5.h"

extern "C" {
#include "..\..\c\lkf5defs.h"
#include "AcHelper.h"
}

#ifndef _WIN64
#define LKF_POINTER2LONG_SIMPLE 1
#endif

/////////////////////////////////////////////////////////////////////////////
// Clkf5

static void FillBSTR (BSTR *b, const char *t, unsigned int len = 0)
{
	if (b) {
		if (*b) SysFreeString (*b);

		if (len) *b = AcHelper::AllocBString (t,len);
		else     *b = AcHelper::AllocBString (t);
	}
}


Clkf5::Clkf5 (void)
{
	ctx = ::lkf_alloc_context (0, "", 0); /* V18.2 */
	pointerArray = 0;
	pointerArrayAlloc = pointerArrayCount = 0;
}

Clkf5::~Clkf5 (void)
{
	if (pointerArray) free (pointerArray);
	if (ctx) ::lkf_free_context (ctx);
}

long Clkf5::PointerToLong (void *ptr) 
{
#ifdef LKF_POINTER2LONG_SIMPLE
	return (long) ptr;
#else
	if (!ptr) return 0;
	if (pointerArrayAlloc == pointerArrayCount) 
	{
		void *old = pointerArray;
		pointerArray = (void **) malloc (sizeof(void *) * (pointerArrayAlloc+=10));
		if (old)
		{
			memcpy (pointerArray, old, sizeof (void *) * pointerArrayCount);
			free (old);
		}
		else 
			pointerArrayCount = 1;
	}
	pointerArray[pointerArrayCount] = ptr;
	return pointerArrayCount++;
#endif
}

void *Clkf5::LongToPointer (long l)
{
#ifdef LKF_POINTER2LONG_SIMPLE
	return (void *) l;
#else
	if (!l) return 0;
	if (l > 0 && l < (long) pointerArrayCount) return pointerArray[l];
	return 0; /* invalid pointer */
#endif
}


STDMETHODIMP Clkf5::lkf_alloc_context(long modelljahr, BSTR path, BSTR *error, long *context)
{
	long returncode;
	lkf_set_modelljahr (modelljahr, path, error, &returncode);

	if (!returncode) *context = 1; /* V18.2 */
	else *context = 0;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_set_modelljahr(long modelljahr, BSTR input_path, BSTR *errorstring, long *returncode)
{
	char e[1024];
	char *p = AcHelper::AllocCString (input_path);


	*returncode = ::lkf_set_modelljahr (ctx, modelljahr, p, e);
	if (*returncode) ctx = ::lkf_alloc_context(0,"",0); /* V18.2 */
	FillBSTR (errorstring, e);
	delete p;
	return S_OK;
}


STDMETHODIMP Clkf5::lkf_insert_aufenthalt(long aufenthalt, long *index)
{
	*index = ::lkf_insert_aufenthalt (ctx, (LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_line(BSTR line, long *returncode)
{
	char *l;
	l = AcHelper::AllocCString (line);
	*returncode = ::lkf_add_line (ctx, l);
	delete l;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_read_inputfile(BSTR inputfile, BSTR *error, long *returncode)
{
	char *i;
	char e[1024];

	i = AcHelper::AllocCString (inputfile);
	*returncode = ::lkf_read_inputfile (i, ctx, e);

	if (error) {
		if (*error) SysFreeString (*error);
		*error = AcHelper::AllocBString (e);
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_set_contextparameters(long cmb_jahr, long period_val, long lfdnr, long type, long flags)
{
	::lkf_set_contextparameters (ctx, cmb_jahr, period_val, lfdnr, (char) type, flags);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_context_scorvalue(long kanr, long fieldnumber, double *value)
{
	*value = ::lkf_get_context_scorvalue (ctx, kanr, fieldnumber);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_GetErrorAnz(long kanr, long *accepted, long *erroranz)
{
	int a;
	*erroranz = ::lkf_context_GetErrorAnz (ctx, kanr, &a);
	if (accepted) *accepted = a;
	return S_OK;
} 

STDMETHODIMP Clkf5::lkf_context_GetHinweisAnz(long kanr, long *accepted, long *erroranz)
{ /* V13.0 */
	int a;
	*erroranz = ::lkf_context_GetHinweisAnz (ctx, kanr, &a);
	if (accepted) *accepted = a;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_GetWarningAnz(long kanr, long *accepted, long *warninganz)
{
	int a;
	*warninganz = ::lkf_context_GetWarningAnz (ctx, kanr, &a);
	if (accepted) *accepted = a;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_read_flat_inputfile(BSTR inputfile, BSTR *error, long *returncode)
{
	// unsupported
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus(long aufenthalt, long *returncode)
{
	::lkf_plaus (ctx, (LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	*returncode = 0; 

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_GetErrorAnz(long aufenthalt, long *accepted, long *returncode)
{
	int a;
	*returncode = ::lkf_plaus_GetErrorAnz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), &a);
	if (accepted) *accepted = a;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_GetWarningAnz(long aufenthalt, long *accepted, long *warninganz)
{
	int a;
	*warninganz = ::lkf_plaus_GetWarningAnz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), &a);
	if (accepted) *accepted = a;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_GetInfoAnz(long aufenthalt, long *accepted, long *warninganz)
{
	int a;
	*warninganz = ::lkf_plaus_GetInfoAnz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), &a);
	if (accepted) *accepted = a;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_ErrorCode2Text(long nr, BSTR *text)
{
	if (*text) SysFreeString (*text);
	*text = AcHelper::AllocBString (::lkf_plaus_ErrorCode2Text (ctx, nr));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_WarningCode2Massnahme(long nr, long *massnahme)
{
	*massnahme = ::lkf_plaus_WarningCode2Massnahme (ctx, nr);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_WarningCode2Text(long nr, BSTR *text)
{
	if (*text) SysFreeString (*text);
	*text = AcHelper::AllocBString (::lkf_plaus_WarningCode2Text (ctx, nr));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_scoring(long aufenthalt, long *returncode)
{
	*returncode = ::lkf_scoring (ctx, (LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_alloc_aufenthalt(long *aufenthalt)
{
	*aufenthalt = (long) PointerToLong(::lkf_alloc_aufenthalt_context (ctx));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_free_aufenthalt(long aufenthalt)
{
	::lkf_free_aufenthalt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_reset_aufenthalt(long aufenthalt)
{
	::lkf_reset_aufenthalt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_abt(long aufenthalt, BSTR abtfc, BSTR abtdat, BSTR abtpos, long *index)
{
	char *f, *d, *p;

	f = AcHelper::AllocCString (abtfc);
	d = AcHelper::AllocCString (abtdat);
	p = AcHelper::AllocCString (abtpos);
	*index = ::lkf_add_abt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), f, d, p);
	delete f;
	delete d;
	delete p;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_diag(long aufenthalt, BSTR diag, BSTR diagtype, long *returncode)
{
	char *d, *t;
	
	d = AcHelper::AllocCString (diag);
	t = AcHelper::AllocCString (diagtype);
	*returncode = ::lkf_add_diag ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), d, t);
	delete d;
	delete t;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_mel(long aufenthalt, BSTR mel, BSTR melanz, long *index)
{
	char *m, *a;

	m = AcHelper::AllocCString (mel);
	a = AcHelper::AllocCString (melanz);
	*index = ::lkf_add_mel ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), m, a);
	delete m;
	delete a;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_akwarn(long aufenthalt, BSTR tcode, BSTR value, BSTR komm, long *index)
{
	char *t, *v, *k;
	
	t = AcHelper::AllocCString (tcode);
	v = AcHelper::AllocCString (value);
	k = AcHelper::AllocCString (komm);
	*index = ::lkf_add_akwarn ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), t, v, k);
	delete t;
	delete v;
	delete k;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_komm(long aufenthalt, BSTR komm, long *index)
{
	char *k;
	
	k = AcHelper::AllocCString (komm);
	*index = ::lkf_add_komm ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), k);
	delete k;	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_line_auf(long aufenthalt, BSTR line, long *ret)
{
	char *l;
	
	l = AcHelper::AllocCString (line);
	*ret = ::lkf_add_line_auf ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), l);
	delete l;
	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_firsterror(long aufenthalt, long *firsterror)
{
	*firsterror = PointerToLong (::lkf_auf_firsterror ((LKF_AUFENTHALT *) LongToPointer(aufenthalt)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_nexterror(long error, long *nexterror)
{
	*nexterror = PointerToLong (::lkf_auf_nexterror ((const LKF_ERROR_LIST *) LongToPointer(error)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_firstwarning(long aufenthalt, long *firstwarning)
{
	*firstwarning = PointerToLong (::lkf_auf_firstwarning ((LKF_AUFENTHALT *) LongToPointer(aufenthalt)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_nextwarning(long warning, long *nextwarning)
{
	*nextwarning = PointerToLong (::lkf_auf_nextwarning ((const LKF_ERROR_LIST *) LongToPointer(warning)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_firstinfo(long aufenthalt, long *firstwarning)
{
	*firstwarning = PointerToLong (::lkf_auf_firstinfo ((LKF_AUFENTHALT *) LongToPointer(aufenthalt)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_nextinfo(long warning, long *nextwarning)
{
	*nextwarning = PointerToLong (::lkf_auf_nextinfo ((const LKF_ERROR_LIST *) LongToPointer(warning)));
	return S_OK;
}


STDMETHODIMP Clkf5::lkf_auf_geterrorcode(long error, long *index, long *code)
{
	int i;
	*code = ::lkf_auf_geterrorcode ((const LKF_ERROR_LIST *) LongToPointer(error), &i);
	if (index) *index = i;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getwarningcode(long warning, long *index, long *code)
{
	int i;
	*code = ::lkf_auf_getwarningcode ((const LKF_ERROR_LIST *) LongToPointer(warning), &i);
	if (index) *index = i;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_maxsubgruppe(long aufenthalt, long *subgruppe)
{
	*subgruppe = ::lkf_auf_get_maxsubgruppe ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_subgruppenanz(long aufenthalt, long *anz)
{
	*anz = ::lkf_auf_get_subgruppenanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_scorvalue(long aufenthalt, long field, long *scorvalue)
{
	*scorvalue = ::lkf_auf_get_scorvalue ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), field);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_gruppe(long aufenthalt, BSTR *knoten, BSTR *gruppe)
{
	char k[10];
	char g[20];

	::lkf_auf_get_gruppe ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), g, k);
	if (knoten) {
		if (*knoten) SysFreeString (*knoten);
		*knoten = AcHelper::AllocBString (k);
	}
	if (gruppe) {
		if (*gruppe) SysFreeString (*gruppe);
		*gruppe = AcHelper::AllocBString (g);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_subgruppe_gruppe(long aufenthalt, long index, BSTR *knoten, BSTR *gruppe)
{
	char k[10];
	char g[20];

	::lkf_auf_get_subgruppe_gruppe ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, g, k);
	if (knoten) {
		if (*knoten) SysFreeString (*knoten);
		*knoten = AcHelper::AllocBString (k);
	}
	if (gruppe) {
		if (*gruppe) SysFreeString (*gruppe);
		*gruppe = AcHelper::AllocBString (g);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_subgruppe_scorvalue(long aufenthalt, long index, long field, long *scorvalue)
{
	*scorvalue = ::lkf_auf_get_subgruppe_scorvalue ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, field); 

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_compose_filename(BSTR ka, BSTR type, BSTR *filename)
{
	char *k, *t;
	char fn[20];

	k = AcHelper::AllocCString (ka);
	t = AcHelper::AllocCString (type);
	::lkf_compose_filename (ctx, k, *t, fn);

	if (*filename) SysFreeString (*filename);
	*filename = AcHelper::AllocBString (fn);
	delete k;
	delete t;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_write_file(BSTR filename, long kanr, long flags, BSTR *error, long *returncode)
{
	char *fn;
	char e[1024];

	fn = AcHelper::AllocCString (filename);
	*returncode = ::lkf_write_datafile (ctx, fn, 0, flags);

	if (error) {
		if (*error) SysFreeString (*error);
		*error = AcHelper::AllocBString (e);
	}
	delete fn;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_write_errorfile(BSTR filename, long kanr, long flags, BSTR inputfile, BSTR outputfile, /*[out]*/ BSTR *error, long *returncode)	
{
	char *fn, *ifn, *ofn;
	char e[1024];

	fn = AcHelper::AllocCString (filename);
	ifn = AcHelper::AllocCString (inputfile);
	ofn = AcHelper::AllocCString (outputfile);

	*returncode = ::lkf_write_errorfile (fn, ctx, kanr, flags, ifn, ofn, e);

	if (error) {
		if (*error) SysFreeString (*error);
		*error = AcHelper::AllocBString (e);
	}
	delete fn;
	delete ifn;
	delete ofn;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_aufenthalt_anz (long *anz)
{
	*anz = ::lkf_get_aufenthalt_anz (ctx);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_aufenthalt(long index, long *aufenthalt)
{
	*aufenthalt = PointerToLong (::lkf_get_aufenthalt (ctx, index));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_reset_aufenthalte()
{
	::lkf_reset_aufenthalte (ctx);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_reset_aufenthalt_package()
{
	::lkf_reset_aufenthalt_package (ctx);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_check_nombds(long *ret)
{
	long r = ::lkf_check_nombds (ctx, 0);
	if (ret) *ret = r;
	return S_OK;
}


STDMETHODIMP Clkf5::lkf_get_context_ka_anz(long *anz)
{
	*anz = ::lkf_get_context_ka_anz (ctx);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_context_ka(long i, long *aufanz, BSTR *kanr)
{
	const char *ka;
	unsigned int anz;

	ka = ::lkf_get_context_ka (ctx, i, &anz, 0);

	if (aufanz) *aufanz = (unsigned int) anz;
	if (kanr) {
		if (*kanr) SysFreeString (*kanr);
		*kanr = AcHelper::AllocBString (ka);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_reset_abt(long aufenthalt)
{
	::lkf_auf_reset_abt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_reset_mel(long aufenthalt)
{
	::lkf_auf_reset_mel ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_reset_diag(long aufenthalt)
{
	::lkf_auf_reset_diag ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}


STDMETHODIMP Clkf5::lkf_plaus_GetErrorText4(long aufenthalt, long error, BSTR *text, BSTR *field, long *returncode)
{
	char t[512];
	char f[96];

	::lkf_plaus_GetErrorText4 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (const LKF_ERROR_LIST *) LongToPointer(error), t, f);

	if (text) {
		if (*text) SysFreeString (*text);
		*text = AcHelper::AllocBString (t);
	}
	
	if (field) {
		if (*field) SysFreeString (*field);
		*field = AcHelper::AllocBString (f);
	}

	if (returncode) *returncode = 0;

	return S_OK;
}


STDMETHODIMP Clkf5::lkf_plaus_GetWarningText4(long aufenthalt, long error, BSTR *text, BSTR *field, long *returncode)
{
	char t[512];
	char f[96];

	::lkf_plaus_GetWarningText4 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (const LKF_ERROR_LIST *) LongToPointer(error), t, f);

	if (text) {
		if (*text) SysFreeString (*text);
		*text = AcHelper::AllocBString (t);
	}
	
	if (field) {
		if (*field) SysFreeString (*field);
		*field = AcHelper::AllocBString (f);
	}

	if (returncode) *returncode = 0;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_GetErrorText(long aufenthalt, long error, BSTR *text)
{


	char t[1024];

	::lkf_plaus_GetErrorText ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (const LKF_ERROR_LIST *) LongToPointer(error), t, 1024);

	if (text) {
		*text = AcHelper::AllocBString (t);
	}
	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_GetWarningText(long aufenthalt, long error, BSTR *text)
{
	char t[1024];

	::lkf_plaus_GetWarningText ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (const LKF_ERROR_LIST *) LongToPointer(error), t, 1024);

	if (text) {
		*text = AcHelper::AllocBString (t);
	}
	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_gruppe_mel(long aufenthalt, BSTR *mel)
{
	char m[6];
	if (!::lkf_auf_get_gruppe_mel ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), m)) m[0] = 0;

	if (mel) {
		if (*mel) SysFreeString (*mel);
		*mel = AcHelper::AllocBString (m);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_subgruppe_mel(long aufenthalt, long index, BSTR *mel)
{
	char m[8];

	if (!::lkf_auf_get_subgruppe_mel ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, m)) m[0] = 0;

	if (mel) {
		if (*mel) SysFreeString (*mel);
		*mel = AcHelper::AllocBString (m);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_root(long aufenthalt, BSTR *rootline)
{
	char line[1024];

	::lkf_auf_line_root ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), line);

	if (rootline) {
		if (*rootline) SysFreeString (*rootline);
		*rootline = AcHelper::AllocBString (line);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_abtanz(long aufenthalt, long *anzahl)
{
	*anzahl = ::lkf_auf_get_abtanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_melanz(long aufenthalt, long *anzahl)
{
	*anzahl = ::lkf_auf_get_melanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_diaganz(long aufenthalt, long *anzahl)
{
	*anzahl = ::lkf_auf_get_diaganz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_kostanz(long aufenthalt, long *anzahl)
{
	*anzahl = ::lkf_auf_get_kostanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_akwarnanz(long aufenthalt, long *anzahl)
{
	*anzahl = ::lkf_auf_get_akwarnanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_kommentaranz(long aufenthalt, long *anzahl)
{
	*anzahl = ::lkf_auf_get_kommentaranz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_abt(long aufenthalt, long i, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_abt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_diag(long aufenthalt, long i, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_diag ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_kost(long aufenthalt, long i, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_kost ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_akwarn(long aufenthalt, long i, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_akwarn ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}


STDMETHODIMP Clkf5::lkf_auf_line_akwarn2(long aufenthalt, long i, LONG* HitCount, BSTR *line)
{ /* V12.1 */
	char l[1024];
	unsigned int nHitCount;

	::lkf_auf_line_akwarn2 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l, &nHitCount);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	if (HitCount)
		*HitCount = (long) nHitCount;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_kommentar(long aufenthalt, long i, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_kommentar ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_mel(long aufenthalt, long i, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_mel ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), (unsigned int) i, l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_scoring(long aufenthalt, BSTR *line)
{
	char l[1024];

	::lkf_auf_line_scoring ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), l);

	if (line) {
		if (*line) SysFreeString (*line);
		*line = AcHelper::AllocBString (l);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_mel(long aufenthalt, long index, BSTR *mel, BSTR *melanz, long *returncode)
{
	char melbuf[8] /* V13.0: MELBUF-Länge 5->8 */, melanzbuf[5];
	long ret;

	ret = ::lkf_auf_get_mel ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, melbuf, melanzbuf);

	FillBSTR (mel, melbuf);
	FillBSTR (melanz, melanzbuf);
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_abt(long aufenthalt, long index, BSTR *abtfc, BSTR *abtdat, BSTR *abtpos, long *returncode)
{
	char abtfcbuf[9], abtdatbuf[9], abtposbuf[5];
	long ret;

	ret = ::lkf_auf_get_abt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, abtfcbuf, abtdatbuf, abtposbuf);
	
	FillBSTR (abtfc, abtfcbuf);
	FillBSTR (abtdat, abtdatbuf);
	FillBSTR (abtpos, abtposbuf);
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_diag(long aufenthalt, long index, BSTR *diag, BSTR *diagtype, long *returncode)
{
	char diagbuf[7], diagtypebuf[2];
	long ret;

	ret = ::lkf_auf_get_diag ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, diagbuf, diagtypebuf);
	diagtypebuf[1] = 0;

	FillBSTR (diag, diagbuf);
	FillBSTR (diagtype, diagtypebuf);
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_copy_aufenthalt(long aufenthalt, long *neuer_aufenthalt)
{
	*neuer_aufenthalt = PointerToLong (::lkf_copy_aufenthalt ((LKF_AUFENTHALT *) LongToPointer(aufenthalt)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getlineanz(long aufenthalt, long *lineanz)
{
	*lineanz = ::lkf_auf_getlineanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getline(long aufenthalt, long index, BSTR *line)
{
	char buf[2048];

	::lkf_auf_getline ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, buf);
	FillBSTR (line, buf);

	return S_OK;
}


STDMETHODIMP Clkf5::lkf_auf_get_field(long aufenthalt, long fieldnum, BSTR *fieldvalue)
{
	unsigned int len;
	const char *buf;
	int ret;

	ret = ::lkf_auf_get_field ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), fieldnum, &buf, &len);
	if (!ret) FillBSTR (fieldvalue, buf, len);
	else FillBSTR (fieldvalue, "");

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_line_svnr(long aufenthalt, BSTR *line)
{
	char buf[2048];

	::lkf_auf_line_svnr ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), buf);
	FillBSTR (line, buf);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_reset_ka(long *returncode)
{
	::lkf_reset_ka (ctx);
	*returncode = 0;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_ka_stats(long aufenthalt, long modus, long *returncode)
{
	*returncode = ::lkf_add_ka_stats (ctx, (LKF_AUFENTHALT *) LongToPointer(aufenthalt), modus);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_context_ka_abtanz(long ka, long *returncode)
{
	*returncode = ::lkf_get_context_ka_abtanz (ctx, ka);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_context_ka_abtstat(long ka, long abtnr, long *anz, long *bd, long *err, BSTR *abtfc)
{
	unsigned int a, b, e;
	const char *at;

	at = ::lkf_get_context_ka_abtstat (ctx, ka, abtnr, &a, &b, &e);

	*anz = a;
	*bd = b;
	*err = e;

	FillBSTR(abtfc,at);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_context_ka_optimized(long ka, long *anz, long *opt, double *maxpkt, double *actpkt, double *percentage)
{
	unsigned int a, o;
	*percentage = ::lkf_get_context_ka_optimized (ctx, ka, &a, &o, maxpkt, actpkt);
	*anz = a;
	*opt = o;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_is_optimizable(long aufenthalt, long *erg)
{
	*erg = ::lkf_auf_is_optimizable ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_opt_subgruppe(long aufenthalt, long *gruppe)
{
	*gruppe = ::lkf_auf_get_opt_subgruppe ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_opt_percentage(long aufenthalt, double *percentage)
{
	*percentage = ::lkf_auf_get_opt_percentage ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_kost(long aufenthalt, BSTR kost, long *returncode)
{
	long ret;
	char *k;

	k = AcHelper::AllocCString (kost);

	ret = ::lkf_add_kost ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), k);
	if (returncode) *returncode = ret;
	delete k;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_errorcode_minmax(long *pmin, long *pmax)
{
	unsigned int pm1, pm2;
	::lkf_getErrorCodeMinMax (ctx, &pm1, &pm2);
	*pmin = pm1;
	*pmax = pm2;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_warningcode_minmax(long *pmin, long *pmax)
{
	unsigned int pm1, pm2;
	::lkf_getWarningCodeMinMax (ctx, &pm1, &pm2);
	*pmin = pm1;
	*pmax = pm2;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_errorcode_count(long ka, long code, long *count)
{
	if (count) *count = ::lkf_context_errorcode_count (ctx, ka, code);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_warningcode_count(long ka, long code, long *count)
{
	if (count) *count = ::lkf_context_warningcode_count (ctx, ka, code);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_mel2(long aufenthalt, BSTR mel, BSTR melanz, BSTR meldat, long *ret)
{
	char *m, *a, *d;

	m = AcHelper::AllocCString (mel);
	a = AcHelper::AllocCString (melanz);
	d = AcHelper::AllocCString (meldat);
	*ret = ::lkf_add_mel2 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), m, a, d);
	delete m;
	delete a;
	delete d;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_mel6(long aufenthalt, BSTR mel, BSTR melanz, BSTR meldat, BSTR melpos, BSTR melzeit, BSTR seite, BSTR fcleist, BSTR kaleist, BSTR addinfo, long *ret)
{
	char *m, *a, *d, *cmelpos, *cmelzeit, *ckaleist, *cfcleist, *cseite, *caddinfo;

	m = AcHelper::AllocCString(mel);
	a = AcHelper::AllocCString(melanz);
	d = AcHelper::AllocCString(meldat);
	cmelpos = AcHelper::AllocCString(melpos);
	cmelzeit = AcHelper::AllocCString(melzeit);
	ckaleist = AcHelper::AllocCString(kaleist);
	cfcleist = AcHelper::AllocCString(fcleist);
	cseite = AcHelper::AllocCString(seite);
	caddinfo = AcHelper::AllocCString(addinfo);
	*ret = ::lkf_add_mel6((LKF_AUFENTHALT *)LongToPointer(aufenthalt), m, a, d, cmelpos, cmelzeit, cseite, cfcleist, ckaleist, caddinfo);
	delete m;
	delete a;
	delete d;
	delete cmelpos;
	delete cmelzeit;
	delete ckaleist;
	delete cfcleist;
	delete cseite;
	delete caddinfo;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_abtbd(long aufenthalt, long index, long *abtbd, long *returncode)
{
	long ret, abd;

	ret = ::lkf_auf_get_abtbd ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, &abd);
	if (abtbd) *abtbd = abd;
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_abtfield(long aufenthalt, long index,  long fieldnum, BSTR *fieldvalue)
{
	unsigned int len;
	char buf[66];

	len = ::lkf_auf_get_abtfield ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, fieldnum, buf, 66);
	if (len) FillBSTR (fieldvalue, buf, len);
	else FillBSTR (fieldvalue, "");

	return S_OK;

}


STDMETHODIMP Clkf5::lkf_auf_get_subgruppe_diag(long aufenthalt, long index, BSTR *diag)
{
	char d[14];

	if (!::lkf_auf_get_subgruppe_diag ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, d)) d[0] = 0;

	if (diag) {
		if (*diag) SysFreeString (*diag);
		*diag = AcHelper::AllocBString (d);
	}

	return S_OK;

}

STDMETHODIMP Clkf5::lkf_add_diag2(long aufenthalt, BSTR diag, BSTR diagtype, BSTR diagpos, long *returncode)
{
	char *d, *t, *p;
	
	d = AcHelper::AllocCString (diag);
	t = AcHelper::AllocCString (diagtype);
	p = AcHelper::AllocCString (diagpos);
	*returncode = ::lkf_add_diag2 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), d, t, p);
	delete d;
	delete t;
	delete p;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_mel2(long aufenthalt, long index, BSTR *mel, BSTR *melanz, BSTR *meldat, long *returncode)
{
	char melbuf[8] /* V13.0: MELBUF-Länge 5->8 */, melanzbuf[5], meldatbuf[9];
	long ret;

	ret = ::lkf_auf_get_mel2 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, melbuf, melanzbuf, meldatbuf);

	FillBSTR (mel, melbuf);
	FillBSTR (melanz, melanzbuf);
	FillBSTR (meldat, meldatbuf);
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_diag2(long aufenthalt, long index, BSTR *diag, BSTR *diagtype, BSTR *diagpos, long *returncode)
{
	char diagbuf[7], diagtypebuf[3], diagposbuf[5];
	long ret;

	ret = ::lkf_auf_get_diag2 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, diagbuf, diagtypebuf, diagposbuf);
	diagtypebuf[1] = 0;

	FillBSTR (diag, diagbuf);
	FillBSTR (diagtype, diagtypebuf);
	FillBSTR (diagpos, diagposbuf);
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_min_subgruppe(long aufenthalt, long *gruppe)
{
	int gr;
	gr = ::lkf_auf_get_min_subgruppe ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	if (gruppe) *gruppe = gr;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_add_mel3(long aufenthalt, BSTR mel, BSTR melanz, BSTR meldat, BSTR melpos, long *ret)
{
	char *m, *a, *d, *p;

	m = AcHelper::AllocCString (mel);
	a = AcHelper::AllocCString (melanz);
	d = AcHelper::AllocCString (meldat);
	p = AcHelper::AllocCString (melpos);
	*ret = ::lkf_add_mel3 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), m, a, d, p);
	delete m;
	delete a;
	delete d;
	delete p;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_mel3(long aufenthalt, long index, BSTR *mel, BSTR *melanz, BSTR *meldat, BSTR *melpos, long *returncode)
{
	char melbuf[8] /* V13.0: MELBUF-Länge 5->8 */, melanzbuf[5], meldatbuf[9], melposbuf[5];
	long ret;

	ret = ::lkf_auf_get_mel3 ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, melbuf, melanzbuf, meldatbuf, melposbuf);

	FillBSTR (mel, melbuf);
	FillBSTR (melanz, melanzbuf);
	FillBSTR (meldat, meldatbuf);
	FillBSTR (melpos, melposbuf);
	if (returncode) *returncode = ret;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_GetMeldeDiagAnz(long kanr, long *meldediagcount)
{
	if (meldediagcount) *meldediagcount = ::lkf_context_GetMeldeDiagAnz (ctx, kanr);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_meldediag_typ(long nr, BSTR *typ)
{
	char buf[1024];
	const char *meldediagtyp = ::lkf_plaus_meldediag_typ_fmt (ctx, (char) nr, buf);
	FillBSTR (typ, meldediagtyp);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_meldediag_text(long nr, BSTR *text)
{
	char buf[2048];
	const char *meldediagtext = ::lkf_plaus_meldediag_text_fmt (ctx, (char) nr, buf);
	FillBSTR (text, meldediagtext);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_firstmeldediag(long aufenthalt, long *meldediag)
{
	if (meldediag) *meldediag = PointerToLong (::lkf_auf_firstmeldediag ((LKF_AUFENTHALT *) LongToPointer(aufenthalt)));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_nextmeldediag(long meldediag, long *nextmeldediag)
{
	if (nextmeldediag) {
		if (meldediag) *nextmeldediag = PointerToLong (::lkf_auf_nextmeldediag ((const LKF_ERROR_LIST *) LongToPointer(meldediag)));
		else *nextmeldediag = 0;
	}

	return S_OK;
}


STDMETHODIMP Clkf5::lkf_auf_getmeldediag(long meldediag, long *index, long *code)
{
	char ccode;
	long ind;

	ccode = ::lkf_auf_getmeldediag ((const LKF_ERROR_LIST *) LongToPointer(meldediag), &ind);
	if (code) *code = ccode;
	if (index) *index = ind;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_plaus_GetMeldeDiagAnz(long aufenthalt, long *count)
{
	if (count) *count = ::lkf_plaus_GetMeldeDiagAnz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_write_meldediagfile(BSTR filename, long kanr, BSTR *error, long *returncode)
{
	char *fn;
	char e[1024];

	fn = AcHelper::AllocCString (filename);

	*returncode = ::lkf_write_meldediagfile (fn, (LKF_CONTEXT *) ctx, kanr, e);

	if (error) {
		if (*error) SysFreeString (*error);
		*error = AcHelper::AllocBString (e);
	}
	delete fn;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_set_npplaus_tracefilename(BSTR outputfilename)
{
	char *fn;

	fn = AcHelper::AllocCString (outputfilename);

	::lkf_set_npplaus_tracefilename ((LKF_CONTEXT *) ctx, fn); 

	delete fn;

	return S_OK;
}
STDMETHODIMP Clkf5::lkf_auf_getintlineanz(LONG aufenthalt, LONG* count)
{
	if (count) *count = ::lkf_auf_getintlineanz ((LKF_AUFENTHALT *) LongToPointer(aufenthalt));
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getintline(LONG aufenthalt, LONG index, BSTR* line)
{
	char s[1024];
	::lkf_auf_getintline ((LKF_AUFENTHALT *) LongToPointer(aufenthalt), index, s);
	FillBSTR (line, s);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getaddline(LONG index, BSTR* line)
{
	char s[2048];
	::lkf_getaddline ((LKF_CONTEXT *) ctx, index, s);
	FillBSTR (line, s);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_check(LONG modus, LONG* returncode)
{
	::lkf_check ((LKF_CONTEXT *) ctx, modus);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorCount(LONG* count)
{
	if (count) *count = ::lkf_getSatzartErrorCount (ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartError(LONG index, BSTR* line)
{
	char buf[2048];
	if (!::lkf_getSatzartError(ctx, (unsigned int) index, 2048, buf)) buf[0] = 0;
	FillBSTR (line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorHeadline(LONG Facility, BSTR* line)
{
	char buf[2048];
	if (!::lkf_getSatzartErrorHeadline (ctx, (unsigned int) Facility, 2048, buf)) buf[0] = 0;
	FillBSTR (line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_set_compatibilitymode(LONG compatibility)
{
	::lkf_set_compatibilitymode ((LKF_CONTEXT *) ctx, (unsigned int) compatibility);
	return S_OK;
}

unsigned int Clkf5::CreateUIntArray (VARIANT var, unsigned int *&array)
{
	unsigned int *fieldnumbers;
	unsigned int i, count;

	array = 0;

	if (var.vt & VT_ARRAY) {
	
		SAFEARRAY *pArray;

		
		if (var.vt & VT_BYREF) pArray = *var.pparray;
		else pArray = var.parray;
		if ((pArray->fFeatures & FADF_HAVEVARTYPE) && pArray->cDims == 1) {
			
			unsigned int element_size;
			element_size = pArray->cbElements;
			/* int datatype = ((unsigned long *)pArray)[-1]; */
			count = pArray->rgsabound[0].cElements;

			if (count > 0 && (element_size == 4 || element_size == 2)) {
				fieldnumbers = new unsigned int [count];

				if (element_size == 4) {
					unsigned long *p = (unsigned long *) pArray->pvData;
					for (i = 0; i < count; i++) fieldnumbers[i] = p[i];
				}
				else {
					unsigned short *p = (unsigned short *) pArray->pvData;
					for (i = 0; i < count; i++) fieldnumbers[i] = p[i];
				}

				array = fieldnumbers;
				return count;
				
			}
			else return 0;
		}
		else return 0;		
	}
	else if (var.vt & VT_BSTR)
	{ /* V10.1 - comma-separated strings werden auch akzeptiert */
		char *s = 0;	
		if (var.vt & VT_BYREF && var.pbstrVal && *var.pbstrVal)
			s = AcHelper::AllocCString (*var.pbstrVal);
		else
			s = AcHelper::AllocCString (var.bstrVal);
		
		if (s)
		{
			const char *pCurrent = s;

			count = 0;

			while (pCurrent)
			{
				if ((*pCurrent >= '0' && *pCurrent <= '9')  || *pCurrent == ' ')
				{
					pCurrent = strchr (pCurrent, ',');
					if (pCurrent) ++pCurrent;
					count++;
				}
				else
					pCurrent = 0;
			}
			
			if (!count)
				return 0;

			fieldnumbers = new unsigned int [count];

			count = 0;
			pCurrent = s;
			while (pCurrent)
			{
				if ((*pCurrent >= '0' && *pCurrent <= '9') || *pCurrent == ' ')
				{
					fieldnumbers[count] = atoi (pCurrent);
					pCurrent = strchr (pCurrent, ',');
					if (pCurrent) ++pCurrent;
					count++;
				}
				else
					pCurrent = 0;
			}
			delete s;
			array = fieldnumbers;
			return count;
		}
		else return 0;
	}
	else return 0;
}

void Clkf5::FreeUIntArray (unsigned int *&array)
{
	if (array) {
		delete array;
		array = 0;
	}
}

STDMETHODIMP Clkf5::lkf_setErrorFormat(LONG Facility, VARIANT FieldNums, LONG* ReturnCode)
{
	unsigned int *fields;
	unsigned int fieldcount;

	fields = 0;
	fieldcount = CreateUIntArray (FieldNums, fields);

	if (fields && fieldcount) {
		::lkf_setErrorFormat ((LKF_CONTEXT *) ctx, (unsigned int) Facility, fieldcount, fields);
		if (ReturnCode) *ReturnCode = 0;
		FreeUIntArray (fields);
	}
	else if (ReturnCode) *ReturnCode = 1;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_setErrorFilter(LONG Filter, LONG* ErrorCount)
{
	unsigned int errorcount;

	errorcount = ::lkf_setErrorFilter ((LKF_CONTEXT *) ctx, Filter);
	if (ErrorCount) *ErrorCount = (LONG) errorcount;

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_setErrorCountsFilter(LONG Filter, LONG* ErrorCount)
{
	unsigned int errorcount;

	errorcount = ::lkf_setErrorCountsFilter ((LKF_CONTEXT *) ctx, Filter);
	if (ErrorCount) *ErrorCount = (LONG) errorcount;

	return S_OK;
}
STDMETHODIMP Clkf5::lkf_int_scoring(LONG* ReturnCode)
{
	unsigned int returncode = ::lkf_int_scoring ((LKF_CONTEXT *) ctx);
	if (ReturnCode) *ReturnCode = returncode;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_setIntAbtData(BSTR ABTFC, LONG BettenAnzahl, LONG* ReturnCode)
{
	unsigned int ret;
	char *abtfc;
	abtfc = AcHelper::AllocCString (ABTFC);
	ret = ::lkf_setIntAbtData ((LKF_CONTEXT *) ctx, abtfc, (unsigned int) BettenAnzahl);
	if (ReturnCode) *ReturnCode = ret;
	delete abtfc;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getaddlineanz(LONG* Count)
{
	if (Count) *Count = ::lkf_getaddlineanz ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getAbtAnz(LONG* AbtAnz)
{
	if (AbtAnz) *AbtAnz = ::lkf_int_getAbtAnz ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getAnalyseHeadline(BSTR* Line)
{
	char buf[2048];
	::lkf_int_getAnalyseHeadline ((LKF_CONTEXT *) ctx, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getAnalyseData(LONG AbtNr, LONG Monat, BSTR* Line)
{
	char buf[2048];
	::lkf_int_getAnalyseData ((LKF_CONTEXT *) ctx, (unsigned int) AbtNr, (unsigned int) Monat, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getAbtHeadline(BSTR* Line)
{
	char buf[2048];
	::lkf_int_getAnalyseHeadline ((LKF_CONTEXT *) ctx, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getAbtData(LONG AbtNr, BSTR* Line)
{
	char buf[2048];
	::lkf_int_getAbtData ((LKF_CONTEXT *) ctx, (unsigned int) AbtNr, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltCount(LONG* Count)
{
	if (Count) *Count = ::lkf_int_getIntAufenthaltCount ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltTISSCount(LONG AufNr, LONG* Count)
{
	if (Count) *Count = ::lkf_int_getIntAufenthaltTISSCount ((LKF_CONTEXT *) ctx, (unsigned int) AufNr);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltDetailsHeadline(BSTR* Line)
{
	char buf[2048];
	::lkf_int_getIntAufenthaltDetailsHeadline ((LKF_CONTEXT *) ctx, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltDetails(LONG AufNr, BSTR* Line)
{
	char buf[2048];
	::lkf_int_getIntAufenthaltDetails ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltTRISSCount(LONG lIntAuf, LONG* Count)
{
	if (Count) *Count = ::lkf_int_getIntAufenthaltTRISSCount ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltSAPSCount(LONG lIntAuf, LONG* Count)
{
	if (Count) *Count = ::lkf_int_getIntAufenthaltSAPSCount ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltDetailsLine(LONG lIntAuf, LONG lSatzartNr, LONG lNr, BSTR* Line)
{
	char buf[2048];

	switch (lSatzartNr) 
	{
		case 1:
			::lkf_int_getIntAufenthaltSAPSDetails ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lNr, buf, 2048);
			break;
		case 2:
			::lkf_int_getIntAufenthaltTISSDetails ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lNr, buf, 2048);
			break;
		case 3:
			::lkf_int_getIntAufenthaltTRISSDetails ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lNr, buf, 2048);
			break;
		default:
			buf[0] = 0;
			break;
	}

	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltDetailsField(LONG lIntAuf, LONG lSatzartNr, LONG lNr, LONG lFieldNr, BSTR* Value)
{

	char buf[128];
	switch (lSatzartNr) 
	{
		case 0:
			::lkf_int_getIntAufenthaltField ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lFieldNr, buf, 128);
			break;
		case 1:
			::lkf_int_getIntAufenthaltSAPSField ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lNr, (unsigned int) lFieldNr, buf, 128);
			break;
		case 2:
			::lkf_int_getIntAufenthaltTISSField ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lNr, (unsigned int) lFieldNr, buf, 128);
			break;
		case 3:
			::lkf_int_getIntAufenthaltTRISSField ((LKF_CONTEXT *) ctx, (unsigned int) lIntAuf, (unsigned int) lNr, (unsigned int) lFieldNr, buf, 128);
			break;
		default:
			buf[0] = 0;
			break;
	}

	FillBSTR (Value, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_scoring(LONG AufenthaltCount, LONG* returncode)
{
	int retcode = ::lkf_context_scoring ((LKF_CONTEXT *) ctx, (unsigned int) AufenthaltCount);
	if (returncode) *returncode = retcode;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_plaus(LONG AufenthaltCount, LONG* returncode)
{
	int retcode = ::lkf_context_plaus ((LKF_CONTEXT *) ctx, (unsigned int) AufenthaltCount);
	if (returncode) *returncode = retcode;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_plaus_package(LONG IsFirstPackage, LONG* returncode)
{
	int retcode = ::lkf_context_plaus_package ((LKF_CONTEXT *) ctx, (int) IsFirstPackage);
	if (returncode) *returncode = retcode;
	return S_OK;
}


STDMETHODIMP Clkf5::lkf_setIntAbtDataIndex(LONG AbtIndex, LONG BettenAnzahl, LONG* ReturnCode)
{
	unsigned int ret;
	ret = ::lkf_setIntAbtDataIndex ((LKF_CONTEXT *) ctx, (unsigned int) AbtIndex, (unsigned int) BettenAnzahl);
	if (ReturnCode) *ReturnCode = ret;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getAbtFC(LONG AbtIndex, BSTR* ABTFC)
{
	char buf[10];
	buf[0] = 0;
	::lkf_getIntAbtCounter  ((LKF_CONTEXT *) ctx, (unsigned int) AbtIndex, buf, 0, 0, 0, 0);
	FillBSTR (ABTFC, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_set_abrechnungsjahr(LONG Abrechnungsjahr)
{
	::lkf_set_abrechnungsjahr ((LKF_CONTEXT *) ctx, (int) Abrechnungsjahr);

	return S_OK;
}


STDMETHODIMP Clkf5::lkf_set_contextflags(LONG Flags)
{
	::lkf_set_contextflags ((LKF_CONTEXT *) ctx, (int) Flags);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_setintcontextparameters(BSTR VonDatum, BSTR BisDatum, LONG Flags, LONG MaxStunden, LONG* Result)
{
	int result;
	char *sVonDatum, *sBisDatum;
	sVonDatum = AcHelper::AllocCString (VonDatum);
	sBisDatum = AcHelper::AllocCString (BisDatum);
	
	result = ::lkf_int_setintcontextparameters ((LKF_CONTEXT *) ctx, sVonDatum, sBisDatum, (unsigned int) Flags, (unsigned int) MaxStunden);
	if (Result) *Result = result;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltSAPSField(LONG AufNr, LONG SAPSNr, LONG FieldNr, BSTR* Field)
{
	char buf[128];
	::lkf_int_getIntAufenthaltSAPSField ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, (unsigned int) SAPSNr, (unsigned int) FieldNr, buf, 128);
	FillBSTR (Field, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltTISSField(LONG AufNr, LONG TISSNr, LONG FieldNr, BSTR* Field)
{
	char buf[128];
	::lkf_int_getIntAufenthaltTISSField ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, (unsigned int) TISSNr, (unsigned int) FieldNr, buf, 128);
	FillBSTR (Field, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltTRISSField(LONG AufNr, LONG TRISSNr, LONG FieldNr, BSTR* Field)
{
	char buf[128];
	::lkf_int_getIntAufenthaltTRISSField ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, (unsigned int) TRISSNr, (unsigned int) FieldNr, buf, 128);
	FillBSTR (Field, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltSAPSDetails(LONG AufNr, LONG SAPSNr, BSTR* Line)
{
	char buf[2048];
	::lkf_int_getIntAufenthaltSAPSDetails ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, (unsigned int) SAPSNr, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltTISSDetails(LONG AufNr, LONG TISSNr, BSTR* Line)
{
	char buf[2048];
	::lkf_int_getIntAufenthaltTISSDetails ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, (unsigned int) TISSNr, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_int_getIntAufenthaltTRISSDetails(LONG AufNr, LONG TRISSNr, BSTR* Line)
{
	char buf[2048];
	::lkf_int_getIntAufenthaltTRISSDetails ((LKF_CONTEXT *) ctx, (unsigned int) AufNr, (unsigned int) TRISSNr, buf, 2048);
	FillBSTR (Line, buf);	
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat(LONG Flags, LONG* Result)
{
	int ret = ::lkf_stat ((LKF_CONTEXT *) ctx);
	if (Result) *Result = ret;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getLineCount(LONG StatType, LONG* LineCount)
{
	if (LineCount) *LineCount = ::lkf_stat_getLineCount ((LKF_CONTEXT *) ctx, (unsigned int) StatType);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getHeadline(LONG StatType, BSTR* Line)
{
	char buf[1024];
	::lkf_stat_getHeadline ((LKF_CONTEXT *) ctx, (unsigned int) StatType, buf, 1024);
	FillBSTR (Line, buf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getLine(LONG StatType, LONG LineNr, BSTR* Line)
{
	char buf[1024];
	::lkf_stat_getLine  ((LKF_CONTEXT *) ctx, (unsigned int) StatType, (unsigned int) LineNr, buf, 1024);
	FillBSTR (Line, buf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getFieldByPosition(LONG StatType, LONG LineNr, LONG FieldPos, BSTR* FieldValue)
{
	char buf[128];
	::lkf_stat_getFieldByPosition ((LKF_CONTEXT *) ctx, (unsigned int) StatType, (unsigned int) LineNr, (unsigned int) FieldPos, buf, 128);
	FillBSTR (FieldValue, buf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getField(LONG StatType, LONG LineNr, LONG FieldId, BSTR* FieldValue)
{
	char buf[128];
	
	::lkf_stat_getField ((LKF_CONTEXT *) ctx, (unsigned int) StatType, (unsigned int) LineNr, (unsigned int) FieldId, buf, 128);
	FillBSTR (FieldValue, buf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getFieldCount(LONG StatType, LONG* ColumnCount)
{
	if (ColumnCount) *ColumnCount = ::lkf_stat_getFieldCount ((LKF_CONTEXT *) ctx, (unsigned int) StatType);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getFieldHeadlineByPosition(LONG StatType, LONG FieldPosition, BSTR* Headline)
{
	char buf[128];
	::lkf_stat_getFieldHeadlineByPosition ((LKF_CONTEXT *) ctx, (unsigned int) StatType, (unsigned int) FieldPosition, buf, 128);
	FillBSTR (Headline, buf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getFieldHeadline(LONG StatType, LONG FieldId, BSTR* Headline)
{
	char buf[128];
	::lkf_stat_getFieldHeadline ((LKF_CONTEXT *) ctx, (unsigned int) StatType, (unsigned int) FieldId, buf, 128);
	FillBSTR (Headline, buf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_getLineByLineInfo(LONG StatType, BSTR Info, LONG * LineNr)
{
	char *info;
	info = AcHelper::AllocCString (Info);
	if (LineNr) *LineNr = ::lkf_stat_getLineByLineInfo ((LKF_CONTEXT *) ctx, (unsigned int) StatType, info);
	delete info;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorFieldHeadline(LONG FieldNumber, BSTR* FieldHeadline)
{
	char sBuf[128];

	::lkf_getSatzartErrorFieldHeadline ((LKF_CONTEXT *) ctx, (unsigned int) FieldNumber, sBuf, 128);

	FillBSTR (FieldHeadline, sBuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorField(LONG ErrorIndex, LONG FieldNumber, BSTR* FieldValue)
{
	char sBuf[512];

	::lkf_getSatzartErrorField ((LKF_CONTEXT *) ctx, (unsigned int) ErrorIndex, (unsigned int) FieldNumber, sBuf, 512);

	FillBSTR (FieldValue, sBuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorSum_Count(LONG* Count)
{
	if (Count) *Count = ::lkf_getSatzartErrorSum_Count (ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorSum_CodeToId(BSTR ErrorCode, LONG* ErrorId)
{
	if (ErrorId) 
	{
		char *errorcode;
		errorcode = AcHelper::AllocCString (ErrorCode);
		*ErrorId = ::lkf_getSatzartErrorSum_CodeToId (ctx, errorcode);
		delete errorcode;
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_getSatzartErrorSum_Field(LONG ErrorId, LONG FieldNr, BSTR* ErrorText)
{
	char sBuf[512];
	::lkf_getSatzartErrorSum_Field (ctx, (unsigned int) ErrorId, (unsigned int) FieldNr, sBuf, 512);
	FillBSTR (ErrorText, sBuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_getAllErrorCount(LONG* Count)
{
	if (Count) *Count = ::lkf_context_getAllErrorCount (ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_getAllErrorInfo_CodeToId(BSTR ErrorCode, LONG* ErrorId)
{
	if (ErrorId) 
	{
		char *errorcode;
		errorcode = AcHelper::AllocCString (ErrorCode);
		*ErrorId = ::lkf_context_getAllErrorInfo_CodeToId (ctx, errorcode);
		delete errorcode;
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_getAllErrorInfo(LONG ErrorId, LONG FieldNr, BSTR* ErrorText)
{
	char sBuf[512];
	::lkf_context_getAllErrorInfo (ctx, (unsigned int) ErrorId, (unsigned int) FieldNr, sBuf, 512);
	FillBSTR (ErrorText, sBuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_convertMBDSErrorCode(LONG OldCode, BSTR* NewCode)
{
	char sBuf[6];
	unsigned int nNewCode = ::lkf_convertMBDSErrorCode (ctx, (unsigned int) OldCode);
	if (nNewCode > 0)
	{
		strcpy (sBuf, "EM");
		lkf_getErrorCodeFromInt (nNewCode, sBuf + 2);
	}
	else sBuf[0] = 0;
	FillBSTR (NewCode, sBuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_convertMBDSWarningCode(LONG OldCode, BSTR* NewCode)
{
	char sBuf[6];
	unsigned int nNewCode = ::lkf_convertMBDSWarningCode (ctx, (unsigned int) OldCode);
	if (nNewCode > 0)
	{
		strcpy (sBuf, "WM");
		lkf_getErrorCodeFromInt (nNewCode, sBuf + 2);
	}
	else sBuf[0] = 0;
	FillBSTR (NewCode, sBuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getProtokollHeadline(BSTR* Line)
{
	char buf[1024];

	if (Line) 
	{
		::lkf_npplaus_getProtokollHeadline ((LKF_CONTEXT *) ctx, buf, 1024);		
		FillBSTR (Line, buf);
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getWarningLineCount(LONG* Count)
{

	if (Count) *Count = ::lkf_npplaus_getWarningLineCount ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getProtokollLineCount(LONG* Count)
{
	if (Count) *Count = ::lkf_npplaus_getProtokollLineCount ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getWarningLine(LONG LineNr, BSTR* Line)
{
	char buf[1024];

	if (Line)
	{
		::lkf_npplaus_getWarningLine ((LKF_CONTEXT *) ctx, (unsigned int) LineNr, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getProtokollLine(LONG LineNr, BSTR* Line)
{
	char buf[1024];

	if (Line)
	{
		::lkf_npplaus_getProtokollLine ((LKF_CONTEXT *) ctx, (unsigned int) LineNr, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_sum_headline(BSTR* Line)
{
	char buf[1024];

	if (Line)
	{
		::lkf_npplaus_sum_headline ((LKF_CONTEXT *) ctx, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_sum_abtcount(LONG* AbtCount)
{
	if (AbtCount)
	{
		*AbtCount = ::lkf_npplaus_sum_abtcount ((LKF_CONTEXT *) ctx);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_sum_abtline(LONG AbtIndex, BSTR* Line)
{
	char buf[1024];

	if (Line)
	{
		::lkf_npplaus_sum_abtline ((LKF_CONTEXT *) ctx, AbtIndex, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getReasonCount(LONG WarningNr, LONG* ReasonCount)
{
	if (ReasonCount)
	{
		*ReasonCount = ::lkf_npplaus_getReasonCount ((LKF_CONTEXT *) ctx, WarningNr);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getReasonLine(LONG WarningNr, LONG ReasonNr, BSTR* Line)
{
	char buf[1024];

	if (Line)
	{
		::lkf_npplaus_getReasonLine ((LKF_CONTEXT *) ctx, WarningNr, ReasonNr, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_getInfoLine(LONG WarningNr, BSTR* Line)
{
	char buf[1024];

	if (Line)
	{
		::lkf_npplaus_getInfoLine ((LKF_CONTEXT *) ctx, WarningNr, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_tfsum_headline(BSTR* Headline)
{
	char buf[1024];
	if (Headline)
	{
		::lkf_npplaus_tfsum_headline ((LKF_CONTEXT *) ctx, buf, 1024);
		FillBSTR (Headline, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_critsum_headline(BSTR* Headline)
{
	char buf[1024];
	if (Headline)
	{
		::lkf_npplaus_critsum_headline ((LKF_CONTEXT *) ctx, buf, 1024);
		FillBSTR (Headline, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_tfsum(LONG AbtIndex, BSTR* Line)
{
	char buf[1024];
	if (Line)
	{
		::lkf_npplaus_tfsum ((LKF_CONTEXT *) ctx, AbtIndex, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_npplaus_critsum(LONG AbtIndex, BSTR* Line)
{
	char buf[1024];
	if (Line)
	{
		::lkf_npplaus_critsum  ((LKF_CONTEXT *) ctx, AbtIndex, buf, 1024);
		FillBSTR (Line, buf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getErrorLine(LONG Aufenthalt, LONG Err, BSTR* ErrorLine)
{ /* V10.1 */
	char sBuf[1024];
	LKF_AUFENTHALT *pAufenthalt = (LKF_AUFENTHALT *) LongToPointer(Aufenthalt);
	const LKF_ERROR_LIST *pError = (const LKF_ERROR_LIST *) LongToPointer(Err);

	if (ErrorLine)
	{
		::lkf_auf_getErrorLine (pAufenthalt, pError, sBuf, 1024);
		FillBSTR (ErrorLine, sBuf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getWarningLine(LONG Aufenthalt, LONG warn, BSTR* WarningLine)
{ /* V10.1 */
	char sBuf[1024];
	LKF_AUFENTHALT *pAufenthalt = (LKF_AUFENTHALT *) LongToPointer(Aufenthalt);
	const LKF_ERROR_LIST *pError = (const LKF_ERROR_LIST *) LongToPointer(warn);

	if (WarningLine)
	{
		::lkf_auf_getWarningLine (pAufenthalt, pError, sBuf, 1024);
		FillBSTR (WarningLine, sBuf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_getInfoLine(LONG Aufenthalt, LONG warn, BSTR* InfoLine)
{ /* V13.1 */
	char sBuf[1024];
	LKF_AUFENTHALT *pAufenthalt = (LKF_AUFENTHALT *) LongToPointer(Aufenthalt);
	const LKF_ERROR_LIST *pError = (const LKF_ERROR_LIST *)  LongToPointer(warn);

	if (InfoLine)
	{
		::lkf_auf_getInfoLine (pAufenthalt, pError, sBuf, 1024);
		FillBSTR (InfoLine, sBuf);
	}

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_trace_setLevel(LONG Facility, LONG TraceLevel)
{
	::lkf_trace_setLevel ((LKF_CONTEXT *) ctx, Facility, TraceLevel);
	return S_OK;
}


STDMETHODIMP Clkf5::lkf_trace_getLineCount(LONG* LineCount)
{
	int nLineCount = ::lkf_trace_getLineCount (ctx);
	if (LineCount)
		*LineCount = nLineCount;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_trace_getLine(LONG LineNr, BSTR* Line)
{
	const char *sLine = ::lkf_trace_getLine (ctx, LineNr);
	if (sLine)
		FillBSTR (Line, sLine);
	else
		FillBSTR (Line, "");

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_trace_getLevel(LONG Facility, LONG* DebugLevel)
{
	int nDebugLevel = ::lkf_trace_getLevel (ctx, Facility);
	if (DebugLevel)
		*DebugLevel = nDebugLevel;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_trace_getAufenthalt(BSTR* Aufenthaltszahl)
{
	const char *sPatId = ::lkf_trace_getAufenthalt ((LKF_CONTEXT *) ctx);
	FillBSTR (Aufenthaltszahl, sPatId);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_set_debug_trace(void)
{
	::lkf_set_debug_trace ((LKF_CONTEXT *) ctx);

	return S_OK;
}

STDMETHODIMP Clkf5::lkf_trace_setAufenthalt(BSTR Aufenthaltszahl)
{
	char *sPATID;
	sPATID = AcHelper::AllocCString (Aufenthaltszahl);
	::lkf_trace_setAufenthalt ((LKF_CONTEXT *) ctx, sPATID);
	delete sPATID;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_get_errortext(BSTR* Error)
{
	if (Error) {
		if (*Error) SysFreeString (*Error);
		*Error = AcHelper::AllocBString (::lkf_context_get_errortext ((LKF_CONTEXT *) ctx));
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_GetGlobalInfo(long type, long *info)
{
	if (info)
		*info = (long) ::lkf_context_GetGlobalInfo((LKF_CONTEXT *) ctx, (unsigned int) type);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_read_dat (BSTR fileName, LONG* numberOfLines)
{ /* V21.2 */
	char *sFileName;
	sFileName = AcHelper::AllocCString (fileName);
	*numberOfLines = (LONG) ::lkf_context_read_dat ((LKF_CONTEXT *) ctx, sFileName);
	delete sFileName;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_dat_line (LONG lineIndex, BSTR *line)
{ /* V21.2 */
	const char *s;
	if (*line) SysFreeString(*line);

	s = ::lkf_context_dat_line ((LKF_CONTEXT *) ctx, (unsigned int) lineIndex);
	if (s != 0)
		*line = AcHelper::AllocBString (s);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_free_dat ()
{ /* V21.2 */
	::lkf_context_free_dat ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_getSatzartErrorAnz (LONG flags, LONG* numberOfErrors) 
{
	*numberOfErrors = ::lkf_context_getSatzartErrorAnz ((LKF_CONTEXT *) ctx, flags);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_abtscor100 (long Aufenthalt, long abtIndex,BSTR *abtpos, long *ptp100, long *plp100, long *psum100) 
{
	LKF_AUFENTHALT *pAufenthalt = (LKF_AUFENTHALT *) LongToPointer(Aufenthalt);
	long lp100, tp100, p100;
	char abtposbuf[16];
	
	p100 = ::lkf_auf_get_abtscor100 (pAufenthalt, (unsigned int) abtIndex, abtposbuf, &tp100, &lp100);
	if (ptp100) *ptp100 = tp100;
	if (plp100) *plp100 = lp100;
	if (psum100) *psum100 = p100;
	FillBSTR (abtpos, abtposbuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_abtscor (long Aufenthalt, long abtIndex, BSTR *abtpos, double *ptp, double *plp, double *psum) 
{
	LKF_AUFENTHALT *pAufenthalt = (LKF_AUFENTHALT *) LongToPointer(Aufenthalt);
	double lp, tp, p;
	char abtposbuf[16];

	p = ::lkf_auf_get_abtscor (pAufenthalt, (unsigned int) abtIndex, abtposbuf, &tp, &lp);

	if (ptp) *ptp = tp;
	if (plp) *plp = lp;
	if (psum) *psum = p;
	FillBSTR (abtpos, abtposbuf);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_stat_package (long *ret) 
{
	*ret = ::lkf_stat_package ((LKF_CONTEXT *) ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_amb_counter (long counterIndex, long *value) 
{
	*value = ::lkf_get_amb_counter((LKF_CONTEXT *) ctx, (unsigned int) counterIndex);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_melIndexForPos (long Aufenthalt, BSTR melpos, long *value) 
{
	char *mp;
	
	mp = AcHelper::AllocCString (melpos);

	if (value)
		*value = ::lkf_auf_get_melIndexForPos ((LKF_AUFENTHALT *) LongToPointer(Aufenthalt), mp);

	delete mp;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_mel_field (long Aufenthalt, long melIndex, long melFieldIndex, long *value) 
{
	if (value)
		*value = ::lkf_auf_get_mel_field ((LKF_AUFENTHALT *) LongToPointer(Aufenthalt), melIndex, melFieldIndex);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_auf_get_mel_value (long Aufenthalt, long melIndex, long melFieldIndex, BSTR *value) 
{
	if (value) {
		char v[1024];
		::lkf_auf_get_mel_value ((LKF_AUFENTHALT *) LongToPointer(Aufenthalt), melIndex, melFieldIndex, 1024, v);
		FillBSTR (value, v);
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_context_set_modelparameter(long parameterIndex, BSTR parameterValue, long *retVal)
{
	char *val;
	int ret;
	val = AcHelper::AllocCString(parameterValue);
	ret = ::lkf_context_set_modelparameter((LKF_CONTEXT *)ctx, parameterIndex, val);
	delete val;
	if (retVal) *retVal = 0;
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_amb_kostenstelle_count(long *value)
{/* V2020.1 */
	if (value)
		*value = ::lkf_get_amb_kostenstelle_count((LKF_CONTEXT *)ctx);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_amb_kostenstelle_value(long index, long ambIndex, long *value)
{/* V2020.1 */
	if (value)
		*value = ::lkf_get_amb_kostenstelle_value((LKF_CONTEXT *)ctx, (unsigned int) index, (unsigned int) ambIndex);
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_amb_kostenstelle_stringvalue(long index, long ambIndex, BSTR *value)
{/* V2020.1 */
	if (value) {
		char buf[64];
		::lkf_get_amb_kostenstelle_stringvalue((LKF_CONTEXT *)ctx, (unsigned int)index, (unsigned int)ambIndex, buf, 64);
		FillBSTR(value, buf);
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_amb_kostenstelle_counter(BSTR kostenstelle, long ambIndex, long *value)
{/* V2020.1 */
	if (value) {
		char *sAbtFC;
		int counter;
		sAbtFC = AcHelper::AllocCString(kostenstelle);
		counter = ::lkf_get_amb_kostenstelle_counter((LKF_CONTEXT *)ctx, sAbtFC, (unsigned int) ambIndex);
		delete sAbtFC;
		*value = (long) counter;
	}
	return S_OK;
}

STDMETHODIMP Clkf5::lkf_get_amb_kostenstelle_index(BSTR kostenstelle, long *value)
{/* V2020.1 */
	
	if (value) {
		char *sAbtFC;
		int index;
		sAbtFC = AcHelper::AllocCString(kostenstelle);
		index = ::lkf_get_amb_kostenstelle_index((LKF_CONTEXT *)ctx, sAbtFC);
		delete sAbtFC;
		*value = (long)index;
	}

	return S_OK;
}


