#include "stdafx.h"
#include "output.h"
#include "buffer.h"
#include "LKFStatic.h"
#include "Protocol.h"

extern "C" {
#include "../../c/lkf5defs.h"
}

#include "time.h"

#define FLAG_ALLE_AUFENTHALTE   (1<<8)

// *************************************
// implementation of base class 'Output'
// *************************************

Output::Output (unsigned int flags)
{
	m_nFlags = flags;
	m_InputFileName = 0;
	m_OutputFileName = 0;
}

Output::~Output ()
{
	if (m_InputFileName) delete m_InputFileName;
	if (m_OutputFileName) delete m_OutputFileName;
}

int Output::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	pBuf->SetDefaultOutputFilename (GetDefaultOutputFilename());
	return 1;
}

const char *Output::GetDefaultOutputFilename()
{
	return "";
}

void Output::SetInputFileName (const char *InputFilename)
{
	if (m_InputFileName) delete m_InputFileName;
	m_InputFileName = LocalStringCopy (InputFilename);
}

void Output::SetOutputFileName (const char *OutputFilename)
{
	if (m_OutputFileName) delete m_OutputFileName;
	m_OutputFileName = LocalStringCopy (OutputFilename);
}

char *Output::LocalStringCopy (const char *str) const
{
	if (str) {
		char *buf = new char [strlen(str + 1)];
		strcpy (buf, str);
		return buf;
	}
	else return 0;
}



// **********************************************
// implementation of subclass 'OutputScoringFile'
// **********************************************

void log (const char *s);

OutputScoringFile::~OutputScoringFile ()
{}

int OutputScoringFile::FillBuffer(LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	long aufanz, auflinecount, i, j;
	LKF_AUFENTHALT *aufenthalt;
	char l[1024];

#ifdef LKFSERVER_LOG_MODE			
	static int cnt = 0;
	char lbuf[2048];
#endif

	Output::FillBuffer (ctx, pBuf);

	aufanz = lkf_get_aufenthalt_anz (ctx);

	for (i = 0; i < aufanz; i++) {
		aufenthalt = lkf_get_aufenthalt (ctx, i);
		if ((m_nFlags&FLAG_ALLE_AUFENTHALTE) || lkf_auf_is_marked(aufenthalt)) {	
			auflinecount = lkf_auf_getlineanz (aufenthalt);

			for (j = 0; j < auflinecount; j++) 
			{			
				lkf_auf_getline (aufenthalt, j, l);
				pBuf->AddLine (l);
			}
		}
	}

	return 1;
}

// ************************************************
// implemtation of subclass 'OutputNPPlausFile'
// ************************************************

OutputPlausNPFile::~OutputPlausNPFile ()
{}

int OutputPlausNPFile::FillBuffer(LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	const char *ka;

	Output::FillBuffer (ctx, pBuf);

	ka = lkf_get_context_ka (ctx, 0, 0, 0);
	if (ka) {
		unsigned int i, count;
		char buf[512];

		lkf_npplaus_getProtokollHeadline (ctx, buf, 512);
		pBuf->AddLine (buf);
				
		count = lkf_npplaus_getWarningLineCount (ctx);
		for (i = 0; i < count; i++) {
			lkf_npplaus_getWarningLine (ctx, i, buf, 512);
			pBuf->AddLine (buf);

			if (m_nFlags & 4)
			{
				lkf_npplaus_getInfoLine (ctx, i, buf, 512);
				if (buf[0]) pBuf->AddLine (buf);
			}

			if (m_nFlags & 2)
			{
				unsigned int nReasonCount, j;

				nReasonCount = lkf_npplaus_getReasonCount (ctx, i);

				for (j = 0; j < nReasonCount; j++)
				{
					lkf_npplaus_getReasonLine (ctx, i, j, buf, 512);
					pBuf->AddLine (buf);
				}
			}

			
/* 
			lkf_npplaus_getWarningInfo (ctx, i, buf, 512);
			pBuf->AddLine (buf);
*/
		}

		if (m_nFlags & 1) { 
			count = lkf_npplaus_getProtokollLineCount (ctx);
			for (i = 0; i < count; i++) {
				lkf_npplaus_getProtokollLine (ctx, i, buf, 512);
				pBuf->AddLine (buf);
			}
		}
	}

	return 1;
}

// ********************************************
// implementation of subclass 'OutputNPSumFile'
// ********************************************

OutputNPSumFile::~OutputNPSumFile ()
{}

int OutputNPSumFile::FillBuffer(LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int i, count;
	char buf[512];

	Output::FillBuffer (ctx, pBuf);

	if (lkf_npplaus_sum_headline (ctx, buf, 512))
		pBuf->AddLine (buf);

	count = lkf_npplaus_sum_abtcount (ctx);

	for (i = 0; i < count; i++) {
		if (lkf_npplaus_sum_abtline (ctx, i, buf, 512))
			pBuf->AddLine (buf);
	}

	/* V10.1 */
	lkf_npplaus_tfsum_headline (ctx, buf, 512);
	pBuf->AddLine (buf);

	for (i = 0; i < count; i++)
	{
		lkf_npplaus_tfsum (ctx, i, buf, 512);
		pBuf->AddLine (buf);
	}

	lkf_npplaus_critsum_headline (ctx, buf, 512);
	pBuf->AddLine (buf);

	for (i = 0; i < count; i++)
	{
		lkf_npplaus_critsum (ctx, i, buf, 512);
		pBuf->AddLine (buf);
	}

	return 1;
}

// ************************************************
// implementation of subclass 'OutputMeldeDiagFile'
// ************************************************

OutputMeldeDiagFile::OutputMeldeDiagFile (unsigned int flags) : Output (flags)
{
	m_ModelYear = 0;
}

OutputMeldeDiagFile::~OutputMeldeDiagFile ()
{}

int OutputMeldeDiagFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	char line[512];
	const char *ka;


	Output::FillBuffer (ctx, pBuf);

	char ka_text[32] = "";      /* V14.2: 24->32 */
	time_t tim;					/* Datentyp zur Ermittlung von akt. Uhrzeit und Datum */
	struct tm *tm;				/* Struktur fuer akt. Uhrzeit und Datum */

	ka = lkf_get_context_ka (ctx, 0, 0, 0);

	if (ka) {
	
		unsigned int aufanz, i;
		LKF_AUFENTHALT *pAufenthalt;
		LKF_ERROR_LIST *pMeldeDiag;
		lkf_KA_to_Name (ctx, ka, ka_text);

		tim = time (0);
		tm = localtime (&tim);

		if (m_ModelYear)
			sprintf (line, "LKF%4d    ;            ;            ; %4.4s %20.20s", m_ModelYear, ka, ka_text);
		else
			sprintf (line, "LKF        ;            ;            ; %4.4s %20.20s", ka, ka_text);
	
		pBuf->AddLine (line);
		pBuf->AddLine ("           ;            ;            ;");
		sprintf (line, "%02d.%02d.%04d ; %02d:%02d      ;            ;", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min);
		pBuf->AddLine (line);
		pBuf->AddLine ("           ;            ;            ;");
		pBuf->AddLine ("KANR       ; AUFZAHL    ; DIAGNOSE   ; KOMMENTAR");
		
		aufanz = lkf_get_aufenthalt_anz (ctx);
	
		for (i = 0; i < aufanz; i++) 
		{
			pAufenthalt = lkf_get_aufenthalt (ctx, i);

			if ((m_nFlags&FLAG_ALLE_AUFENTHALTE) || lkf_auf_is_marked(pAufenthalt)) {
				pMeldeDiag = lkf_auf_firstmeldediag (pAufenthalt);

				while (pMeldeDiag) {
			
					lkf_plaus_GetMeldeDiagText (pAufenthalt, pMeldeDiag, line, 512);
					pMeldeDiag = lkf_auf_nextmeldediag (pMeldeDiag);
					pBuf->AddLine (line);
				}
			}
		}
	
	}
	return 1;
}

// ************************************************
// implementation of subclass 'OutputEWFile'
// ************************************************

OutputPlausEWFile::OutputPlausEWFile (unsigned int flags, bool iserror) : Output (flags)
{ m_IsErrorFile = iserror; m_ModelYear = 0; }


OutputPlausEWFile::~OutputPlausEWFile ()
{}

int OutputPlausEWFile::FillBuffer(LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	int aufanz, i;
	char line[1024];
	LKF_AUFENTHALT *pAufenthalt;
	LKF_ERROR_LIST *pError;
	Output::FillBuffer (ctx, pBuf);
	WriteHeader (ctx, pBuf);

	aufanz = lkf_get_aufenthalt_anz (ctx);

	for (i = 0; i < aufanz; i++) {
		pAufenthalt = lkf_get_aufenthalt (ctx, i);
		if ((m_nFlags&FLAG_ALLE_AUFENTHALTE) || lkf_auf_is_marked(pAufenthalt)) {
			if (m_IsErrorFile) {
				pError = lkf_auf_firsterror (pAufenthalt);
				while (pError) {
					lkf_plaus_GetErrorText (pAufenthalt, pError, line, 1024);
					pError = lkf_auf_nexterror (pError);
					pBuf->AddLine (line);
				}
			}
			else {
				pError = lkf_auf_firstwarning (pAufenthalt);
				while (pError) {
					lkf_plaus_GetWarningText (pAufenthalt, pError, line, 1024);
					pError = lkf_auf_nextwarning (pError);
					pBuf->AddLine (line);
				}
			}
		}
	}

	WriteFooter (ctx, pBuf);
	return 1;
}

void OutputPlausEWFile::WriteHeader (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	char buf[1024], buf2[1024];
	const char *ka, *tabstring;
	char ka_text[32] = "";      /* V14.2: 24->32 */
	time_t tim;					/* Datentyp zur Ermittlung von akt. Uhrzeit und Datum */
	struct tm *tm;				/* Struktur fuer akt. Uhrzeit und Datum */

	int addpos;

	ka = lkf_get_context_ka (ctx, 0, 0, 0);
	lkf_KA_to_Name (ctx, ka, ka_text);

	tim = time (0);
	tm = localtime (&tim);

	if (m_IsErrorFile) 
		tabstring = "            ;        ;            ;            ;          ;             ;           ;";
	else
		tabstring = "            ;        ;            ;            ;          ;           ;             ;           ;";

	if (m_ModelYear)
		sprintf (buf, "LKF%4d    ;            ;%s %4.4s %20.20s", m_ModelYear, tabstring, ka, ka_text);
	else
		sprintf (buf, "LKF        ;            ;%s %4.4s %20.20s", tabstring, ka, ka_text);

	pBuf->AddLine (buf);
	sprintf (buf, "           ;            ;%s", tabstring);
	addpos = (int) strlen (buf);
	
	if (m_OutputFileName) {
		sprintf (buf + addpos, " Eingabedatei:%s\n", m_OutputFileName);
		pBuf->AddLine (buf);
	}
	
	sprintf (buf2, "%02d.%02d.%04d ; %02d:%02d      ;%s ", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tabstring);
	if (m_IsErrorFile) strcat (buf2, "Fehlerprotokoll (nicht scorefähige Fälle)");
	else strcat (buf2, "Warnungsprotokoll (scorefähige Fälle)");
	pBuf->AddLine (buf2);
	
	buf[addpos] = 0;
	pBuf->AddLine (buf);
	
	if (m_IsErrorFile) 
		pBuf->AddLine ("KANR       ; AUFZAHL    ; GEBDAT     ; GESCHL ; AUFDAT     ; ENTDAT     ; ENTABT   ; WERT        ; ERRORCODE ; KOMMENTAR");
	else
		pBuf->AddLine ("KANR       ; AUFZAHL    ; GEBDAT     ; GESCHL ; AUFDAT     ; ENTDAT     ; ENTABT   ; MASSNAHME ; WERT        ; ERRORCODE ; KOMMENTAR");

}

void OutputPlausEWFile::WriteFooter (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	char buf[1024], buf2[1024];
	int addpos, aufanz;
	sprintf (buf, "   ;            ;            ;        ;            ;            ;");
	addpos = (int) strlen (buf);

	if (m_IsErrorFile) 
		sprintf (buf2, "        %s          ;             ;           ;        ", buf);
	else
		sprintf (buf2, "        %s          ;           ;             ;           ;        ", buf);
	pBuf->AddLine (buf2);

	aufanz = lkf_get_aufenthalt_anz (ctx);

	if (m_IsErrorFile) {
		unsigned int error_anz;
		int accepted_anz;
		error_anz = lkf_context_GetErrorAnz (ctx, 0, &accepted_anz);
		sprintf (buf2, "Ergebnis%s          ;             ;           ; %d Fehler/ %d akzeptierte Fehler/ %d Geprüfte Fälle", buf, 
						error_anz, accepted_anz, aufanz);
	}
	else {
		unsigned int error_anz;
		int accepted_anz;
		error_anz = lkf_context_GetWarningAnz (ctx, 0, &accepted_anz);
		sprintf (buf2, "Ergebnis%s          ;           ;             ;           ; %d Warnungen/ %d akzeptierte Warnungen/ %d Geprüfte Fälle", buf, 
						error_anz, accepted_anz, aufanz);
	}
	pBuf->AddLine (buf2);
}



// ************************************************
// implementation of subclass 'OutputPlausErrorFile'
// ************************************************

OutputPlausErrorFile::~OutputPlausErrorFile ()
{}

// ************************************************
// implementation of subclass 'OutputPlausWarningFile'
// ************************************************

OutputPlausWarningFile::~OutputPlausWarningFile ()
{}

// ************************************************
// implementation of subclass 'OutputOptimizerFile'
// ************************************************

OutputScoringOptimizerFile::~OutputScoringOptimizerFile ()
{}

int OutputScoringOptimizerFile::FillBuffer(LKF_CONTEXT *ctx, BufferChar *pBuf)
{

	long aufanz, i, j;
	LKF_AUFENTHALT *aufenthalt;
	char ident_buf[256]; /* V22.1 - bigger Buffer */
	char buf[1024], scorvalbuf[20];
	int min_subgruppe;
	int opt_subgruppe;
	double min_opt_percentage;
	long count;
	char diag[20];
	char gruppe[12];
	char knoten[4];
	unsigned long scorvalue;

	Output::FillBuffer (ctx, pBuf);

	aufanz = lkf_get_aufenthalt_anz (ctx);

	for (i = 0; i < aufanz; i++) {
		aufenthalt = lkf_get_aufenthalt (ctx, i);
		if ((m_nFlags&FLAG_ALLE_AUFENTHALTE) || lkf_auf_is_marked(aufenthalt)) {
			lkf_auf_get_ident (aufenthalt, ident_buf);
			lkf_auf_get_fieldvalue (aufenthalt, SCORFIELD_SCGES, scorvalbuf, 20);

			if (lkf_auf_is_optimizable (aufenthalt)) {
				sprintf (buf, "1;%s;%10.10s;OPT", ident_buf, scorvalbuf);
				pBuf->AddLine (buf);
				min_subgruppe = lkf_auf_get_min_subgruppe (aufenthalt);
				opt_subgruppe = lkf_auf_get_opt_subgruppe (aufenthalt);
				min_opt_percentage = lkf_auf_get_opt_percentage (aufenthalt);

				sprintf (buf, "2;%s;%5d;%5d;%7.5f", ident_buf, min_subgruppe, opt_subgruppe, min_opt_percentage);
				pBuf->AddLine (buf);

				count = lkf_auf_get_subgruppenanz (aufenthalt);
				for (j = 0; j < count; j++) {
					lkf_auf_get_subgruppe_diag (aufenthalt, j, diag);
					lkf_auf_get_subgruppe_gruppe (aufenthalt, j, gruppe, knoten);		
					scorvalue = lkf_auf_get_subgruppe_scorvalue (aufenthalt, j, SCORFIELD_SCGES);
					sprintf (buf, "3;%s;%3d;%6.6s;%8.8s;%1.1s;%10d", ident_buf, j, diag, gruppe, knoten, scorvalue);
					pBuf->AddLine (buf);
				}
			}
			else {
				sprintf (buf, "1;%s;%10.10s;NOOPT", ident_buf, scorvalbuf);
				pBuf->AddLine (buf);
			}
		}
	}

	return 1;
}

// *******************************************************
// implementation of subclass 'OutputScoringCSVFile'
// *******************************************************

OutputScoringCSVFile::OutputScoringCSVFile (unsigned int count, unsigned int *fieldnumbers, unsigned int flags) : Output (flags)
{
	if (count > 0) {
		m_FieldNumbers = (unsigned int *) malloc (sizeof(unsigned int) * count);
		memcpy (m_FieldNumbers, fieldnumbers, sizeof(unsigned int) * count);
	}
	else m_FieldNumbers = 0;
	m_Count = count;
}

OutputScoringCSVFile::~OutputScoringCSVFile ()
{
	if (m_FieldNumbers) delete m_FieldNumbers;
}

int OutputScoringCSVFile::FillBuffer(LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	long aufanz, i;
	LKF_AUFENTHALT *aufenthalt;
	char l[1024];
	Output::FillBuffer (ctx, pBuf);

	lkf_define_custom_line (ctx, m_Count, m_FieldNumbers, 1, 0);

	aufanz = lkf_get_aufenthalt_anz (ctx);

	for (i = 0; i < aufanz; i++) {
		aufenthalt = lkf_get_aufenthalt (ctx, i);
	
		if ((m_nFlags&FLAG_ALLE_AUFENTHALTE) || lkf_auf_is_marked(aufenthalt)) {
			lkf_auf_line_custom (aufenthalt, l);
			pBuf->AddLine (l);
		}
	}
	return 1;
}

// *******************************************************
// implementation of subclass 'OutputIntensivScoringFile'
// *******************************************************

int OutputIntensivScoringFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	int emptyline;
	unsigned int i, j, anz, subanz;
	unsigned int flag;
	char buf[1024];
	Output::FillBuffer (ctx, pBuf);

	emptyline = 0;
	flag = m_nFlags;
	if (!flag) flag = FLAG_OUTPUT_INTALL;

	anz = lkf_int_getAbtAnz (ctx);

	if (anz > 0) {

		if (flag & FLAG_OUTPUT_INTANALYSE) {

			if (flag & FLAG_OUTPUT_HEADLINE)
			{
				lkf_int_getAnalyseHeadline (ctx, buf, 1024);
				pBuf->AddLine (buf);
			}

			for (i = 0; i < anz; i++)
			{
				for (j = 1; j < 13; j++) 
				{
					if (lkf_int_getAnalyseData (ctx, i, j, buf, 1024))
						pBuf->AddLine (buf);
				}
			}

			emptyline = 1;

		}

		if (flag & FLAG_OUTPUT_INTEINSTUFUNG) {
			if (emptyline) pBuf->AddLine ("");
			if (flag & FLAG_OUTPUT_HEADLINE) 
			{
				lkf_int_getAbtHeadline (ctx, buf, 1024);
				pBuf->AddLine (buf);
			}

			for (i = 0; i < anz; i++) 
			{
				lkf_int_getAbtData (ctx, i, buf, 1024);
				pBuf->AddLine (buf);
			}
			emptyline = 1;
		}
	}


	if (flag & FLAG_OUTPUT_ANYDETAIL) {
		anz = lkf_int_getIntAufenthaltCount (ctx);

		if (anz > 0) {
			if (emptyline) pBuf->AddLine ("");
			
			if (flag & FLAG_OUTPUT_HEADLINE) 
			{
				lkf_int_getIntAufenthaltDetailsHeadline (ctx, buf, 1024);
				pBuf->AddLine (buf);
			}

			for (i = 0; i < anz; i++) 
			{
				if (flag & FLAG_OUTPUT_AUF_DETAILS)
				{
					lkf_int_getIntAufenthaltDetails (ctx, i, buf, 1024);
					pBuf->AddLine (buf);
				}

				if (flag & FLAG_OUTPUT_SAPSII_DETAILS)
				{
					// if (subanz) pBuf->AddLine ("");

					subanz = lkf_int_getIntAufenthaltSAPSCount (ctx, i);

					for (j = 0; j < subanz; j++) 
					{
						lkf_int_getIntAufenthaltSAPSDetails (ctx, i, j, buf, 1024);
						pBuf->AddLine (buf);
					}				
				}

				if (flag & FLAG_OUTPUT_TISS_DETAILS)
				{
					subanz = lkf_int_getIntAufenthaltTISSCount (ctx, i);

					for (j = 0; j < subanz; j++) 
					{
						lkf_int_getIntAufenthaltTISSDetails (ctx, i, j, buf, 1024);
						pBuf->AddLine (buf);
					}
				}

				if (flag & FLAG_OUTPUT_TRISS_DETAILS)
				{
					// if (subanz) pBuf->AddLine ("");
					subanz = lkf_int_getIntAufenthaltTRISSCount (ctx, i);

					for (j = 0; j < subanz; j++) 
					{
						lkf_int_getIntAufenthaltTRISSDetails (ctx, i, j, buf, 1024);
						pBuf->AddLine (buf);
					}
				}
			}
		}
	}
	
	return 1;
}

OutputIntensivScoringFile::~OutputIntensivScoringFile ()
{
}

// ********************************************
// implementation of subclass 'OutputStammFile'
// ********************************************

OutputStammFile::OutputStammFile (const char *stammfile, unsigned int flags) : Output (flags)
{
	pFilename = new char [strlen (stammfile) + 1];
	strcpy (pFilename, stammfile);
}

OutputStammFile::~OutputStammFile ()
{
	delete pFilename;
}

int OutputStammFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	char buf[1024], filename[1024];
	size_t siz;
	const char *CurrentRootPath = GetStaticObject()->GetCurrentRootPath();
	lkf_get_stamm_path (ctx, pFilename, filename, 1024);

	FILE *fp = fopen (filename, "r");

	if (fp) {
		while (fgets (buf, 1024, fp)) {
			siz = strlen (buf);
			while (buf[siz-1] == 10 || buf[siz-1] == 13) --siz;
			pBuf->AddLine (buf);
		}
		fclose (fp);
	}
	return 1;
}

// ********************************************
// implementation of subclass 'OutputCheckFile'
// ********************************************

OutputCheckFile::~OutputCheckFile ()
{
}

int OutputCheckFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int anz, i;
	char buf[512];
	Output::FillBuffer (ctx, pBuf);

	lkf_getSatzartErrorHeadline (ctx, 0, 512, buf);
	pBuf->AddLine (buf);

	if (nType & 2) anz = lkf_setErrorFilter (ctx, LKF_ERRORFILTER_ALLINFOS);
	else if (nType & 1) anz = lkf_setErrorFilter (ctx, LKF_ERRORFILTER_ALLWARNINGS);
	else anz = lkf_setErrorFilter (ctx, LKF_ERRORFILTER_ALLERRORS);

	if (anz > 0) {
		for (i = 0; i < anz; i++) {
			lkf_getSatzartError (ctx, i, 512, buf);
			pBuf->AddLine (buf);
		}
	}

	return 1;
}

// ***********************************************
// implementation of subclass 'OutputProtocolFile'
// ***********************************************

OutputProtocolFile::OutputProtocolFile (unsigned int flags) : Output (flags)
{
}

int OutputProtocolFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int i, j, object_count, call_count;
	char buf[1024];
	Protocol *pProtocol = GetStaticProtocolObject();
	const ProtocolObject *pObject;
	const ProtocolCall   *pCall;
	
	pProtocol->EnterCS();

	object_count = pProtocol->GetObjectCount ();

	for (i = 0; i < object_count; i++) 
	{
		pObject = pProtocol->GetObject (i);
		pObject->GetLine (i, buf, 1024);
		pBuf->AddLine (buf);

		call_count = pObject->GetCallCount ();

		for (j = 0; j < call_count; j++) {
			pCall = pObject->GetCall (j);
			pCall->GetLine (i, j, buf, 1024);
			pBuf->AddLine (buf);
		}
	}
	pProtocol->LeaveCS();
	return 1;
}

OutputProtocolFile::~OutputProtocolFile ()
{
}


// **********************************************
// implementation of subclass 'OutputSatzartFile'
// **********************************************

OutputSatzartFile::OutputSatzartFile (unsigned int modelljahr, unsigned int flags) : Output (flags)
{
	m_ModellJahr = modelljahr;
}

OutputSatzartFile::~OutputSatzartFile ()
{
}

void OutputSatzartFile::FillCheckInfo (LKF_CONTEXT *pContext, BufferChar *pBuf, const void *pCheckInfo, const char *sFieldIndex, const char *sErrorIndex)
{
	const void *pNextCheck, *pNextCheckErr;
	char buf[2048], linebuf[1024];
	char sNextErrorIndex[20];

	lkf_Satzart_getCheckInfo (pContext, pCheckInfo, sFieldIndex, sErrorIndex, buf, 2048);
	pBuf->AddLine (buf);

	if (lkf_Satzart_getCheckDetails (pContext, pCheckInfo, buf, 2048) > 0)
	{
		unsigned int lastpos, pos = 0;
		while (buf[pos])
		{
			lastpos = pos;
			while (buf[pos] && buf[pos] != '\n') ++pos;
			buf[pos] = 0;
			sprintf (linebuf, "D;%s                         ; %-16.16s;\"%s\"",sFieldIndex, sErrorIndex, buf + lastpos);
			pBuf->AddLine (linebuf);
			pos++;
		}
	}

	pNextCheck = lkf_Satzart_getNextCheck (pContext, pCheckInfo, 1);
	pNextCheckErr = lkf_Satzart_getNextCheck (pContext, pCheckInfo, 0);

	if (pNextCheck) {
		if (pNextCheck == pNextCheckErr) 
			sprintf (sNextErrorIndex, "%s.->", sErrorIndex);
		else 
			sprintf (sNextErrorIndex, "%s.ok", sErrorIndex);
		sNextErrorIndex[18] = 0;
		FillCheckInfo (pContext, pBuf, pNextCheck, sFieldIndex, sNextErrorIndex);
	}

	if (pNextCheckErr && pNextCheckErr != pNextCheck)
	{
		sprintf (sNextErrorIndex, "%s.er", sErrorIndex);
		sNextErrorIndex[18] = 0;
		FillCheckInfo (pContext, pBuf, pNextCheckErr, sFieldIndex, sNextErrorIndex);
	}
}

int OutputSatzartFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	const void *pCheckInfo;
	unsigned int satzart_count, feld_count, i, j;
	const LKF_SATZART_DEFS *pSatzartDefs;
	char buf[1024];
	char fieldindex[128];
	Output::FillBuffer (ctx, pBuf);
	sprintf (buf, "SATZART-DEFINITIONEN fuer Modelljahr %d", m_ModellJahr);
	pBuf->AddLine (buf);

	pSatzartDefs = lkf_Satzart_getDefinition (m_ModellJahr);
	satzart_count = lkf_Satzart_getCount (pSatzartDefs);

	for (i = 0; i < satzart_count; i++) 
	{
		if (lkf_Satzart_getSatzartDef (pSatzartDefs, i, buf, 1024)) pBuf->AddLine (buf);
		if (lkf_Satzart_getSatzartKeyInfo (pSatzartDefs, i, buf, 1024)) pBuf->AddLine (buf);

		feld_count = lkf_Satzart_getSatzartFieldCount (pSatzartDefs, i);

		for (j = 0; j < feld_count; j++) 
		{
			if (lkf_Satzart_getSatzartFieldInfo (pSatzartDefs, i, j, buf, 1024)) pBuf->AddLine (buf);
			if (m_nFlags & OUTPUTFILE_CHECKDETAILS) 
			{
				pCheckInfo = lkf_Satzart_getSatzartFieldCheck (ctx, pSatzartDefs, i, j); /* V10.2 */
				if (pCheckInfo) 
				{
					lkf_Satzart_getSatzartFieldTextId (pSatzartDefs, i, j, fieldindex, 128);
					FillCheckInfo (ctx, pBuf, pCheckInfo, fieldindex, "");
				}
			}
		}

		/* V9.1: Generelle Checks für diese Satzart */
		pCheckInfo = lkf_Satzart_getSatzartFieldCheck (ctx, pSatzartDefs, i, (1<<16)); /* V10.2 */
		if (pCheckInfo) 
		{
			lkf_Satzart_getSatzartFieldTextId (pSatzartDefs, i, (1<<16), fieldindex, 128);
			FillCheckInfo (ctx, pBuf, pCheckInfo, fieldindex, "");
		}
	}

	/* V9.1: Generelle Checks Satzart-übergreifend */
	i = 0;
	while (pCheckInfo = lkf_Satzart_getSatzartFieldCheck (ctx, pSatzartDefs, (1<<16), i)) /* V10.2 */
	{
		lkf_Satzart_getSatzartFieldTextId (pSatzartDefs, (1<<16), i, fieldindex, 128);
		FillCheckInfo (ctx, pBuf, pCheckInfo, fieldindex, "");
		++i;
	}

	/* V14.2: Generelle Checks Satzart-übergreifend Berichtswesen*/
	i = 0;
	while (pCheckInfo = lkf_Satzart_getSatzartFieldCheck (ctx, pSatzartDefs, (1<<17), i))
	{
		lkf_Satzart_getSatzartFieldTextId (pSatzartDefs, (1<<16), i, fieldindex, 128);
		FillCheckInfo (ctx, pBuf, pCheckInfo, fieldindex, "");
		++i;
	}


	return 1;
}

// *******************************************
// implementation of subclass 'OutputDataFile'
// *******************************************

OutputDataFile::OutputDataFile (unsigned int flags) : Output (flags)
{
}

int OutputDataFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int aufanz, linecount, i, j;
	unsigned int show_mbds, show_intscoring;
	LKF_AUFENTHALT *aufenthalt;
	char l[2048];

	Output::FillBuffer (ctx, pBuf);

	aufanz = lkf_get_aufenthalt_anz (ctx);
		
	// Export Scoring-Data

	show_mbds = !(m_nFlags & OUTPUTFLAG_NOMBDS);
	show_intscoring = !(m_nFlags & OUTPUTFLAG_NOINT);

	if (show_mbds || show_intscoring) {
		for (i = 0; i < aufanz; i++) {
			aufenthalt = lkf_get_aufenthalt (ctx, i);
			if ((m_nFlags&FLAG_ALLE_AUFENTHALTE) || lkf_auf_is_marked(aufenthalt)) {	
				
				if (show_mbds)
				{
					linecount = lkf_auf_getlineanz (aufenthalt);
					for (j = 0; j < linecount; j++) {	
						lkf_auf_getline (aufenthalt, j, l);
						if (*l) pBuf->AddLine (l);
					}
				}
				if (show_intscoring) 
				{
					linecount = lkf_auf_getintlineanz (aufenthalt);
					for (j = 0; j < linecount; j++) 
					{
						lkf_auf_getintline (aufenthalt, j, l);
						if (*l) pBuf->AddLine (l);
					}
				}
			}
		}
	}

	// Export Additional Data
	if (!(m_nFlags & OUTPUTFLAG_NOADD)) {
		linecount = lkf_getaddlineanz (ctx);
		for (i = 0; i < linecount; i++) 
		{
			lkf_getaddline (ctx, i, l);
			if (*l) pBuf->AddLine (l);
		}
	}

	return 1;
}

OutputDataFile::~OutputDataFile () 
{
}

// ************************************************
// implementation of subclass 'OutputErrorListFile'
// ************************************************

OutputErrorListFile::OutputErrorListFile (unsigned int flags) : Output (flags)
{
}

int OutputErrorListFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int usagecount, errorcount, i, j;
	char buf[1024];
	LKF_SATZART_ERRORINFO_LIST *pList;
	const LKF_SATZART_ERRORINFO *pErrorInfo;
	const LKF_SATZART_ERRORINFO_USAGE *pUsage;
	Output::FillBuffer (ctx, pBuf);

	pList = lkf_create_errorinfo (ctx, m_nFlags);

	lkf_errorinfo_getHeadline (pList, buf, 1024);
	pBuf->AddLine (buf);

	errorcount = lkf_errorinfo_getCount (pList);

	for (i = 0; i < errorcount; i++) 
	{
		pErrorInfo = lkf_errorinfo_getError (pList, i);
		lkf_errorinfo_getErrorLine (pErrorInfo, buf, 1024);
		pBuf->AddLine (buf);

		usagecount = lkf_errorinfo_getErrorUsageCount (pErrorInfo);

		for (j = 0; j < usagecount; j++) 
		{
			pUsage = lkf_errorinfo_getErrorUsage (pErrorInfo, j);
			lkf_errorinfo_getErrorUsageLine (pUsage, buf, 1024);
			pBuf->AddLine (buf);
		}
	}

	lkf_free_errorinfo (pList);
	return 1;
}

OutputErrorListFile::~OutputErrorListFile ()
{
}


// *******************************************
// implementation of subclass 'OutputStatFile'
// *******************************************

OutputStatFile::OutputStatFile (unsigned int flags) : Output (flags)
{
}

int OutputStatFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int i, nCount, nType;
	char buf[1024];
	Output::FillBuffer (ctx, pBuf);

	for (nType = 0; nType < 4; nType++)
	{
		lkf_stat_getHeadline (ctx, nType, buf, 1024);
		pBuf->AddLine (buf);

		nCount = lkf_stat_getLineCount (ctx, nType);
	
		for (i = 0; i < nCount; i++)
		{
			lkf_stat_getLine (ctx, nType, i, buf, 1024);
			pBuf->AddLine (buf);
		}
	
		pBuf->AddLine ("");
	}

/*	pBuf->AddLine (buf);*/
	return 1;
}

OutputStatFile::~OutputStatFile ()
{
}


// ********************************************
// implementation of subclass 'OutputTraceFile'
// ********************************************

OutputTraceFile::OutputTraceFile (unsigned int flags) : Output (flags)
{
}

int OutputTraceFile::FillBuffer (LKF_CONTEXT *ctx, BufferChar *pBuf)
{
	unsigned int i, nCount;
	const char *sLine;
	Output::FillBuffer (ctx, pBuf);

	nCount = ::lkf_trace_getLineCount (ctx);

	for (i = 0; i < nCount; i++)
	{
		sLine = ::lkf_trace_getLine (ctx, i);
		if (sLine) pBuf->AddLine (sLine);
	}

	return 1;
}

OutputTraceFile::~OutputTraceFile ()
{
}