#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

void DestroyList( TList * top )
{
//	LOG( "DestroyList()", LOG_MAX );

	TList * cur = top; unsigned long pos=0;
	TList * prev;

	while ( cur && cur->text)
	{
		free( cur->text );
		prev = cur;
		cur = cur->next;
		delete prev;
	}

//	LOG( "/DestroyList()", LOG_MAX );
}

TList * Add( TList * list, char * text, long size )
{
	if (!text)
		return list;

	list->text = new char[ size+1 ];
	memcpy( list->text, text, size );
	list->text[size] = 0;

//	logfmt( FLOG_MAX, "Added: '%s'", list->text );

	list->next = new TList;
	list->next->next = NULL;
	list->next->text = NULL;

	return list->next;
}


TList * AddF( TList * list, char * format, ... )
{
	if (!format)
		return list;

	int size;
	va_list ap;

	va_start( ap, format );
	size = vsnprintf( NULL, 0, format, ap ) + 1;
	list->text = new char[ size ];
	if ( !list->text) return list;		// out of mem => do nothing
	vsnprintf( list->text, size-1, format, ap );
	va_end( ap );
	list->text[size] = 0;

//	logfmt( FLOG_MAX, "Added: '%s'", list->text );

	list->next = new TList;
	list->next->next = NULL;
	list->next->text = NULL;

	return list->next;
}


void PrintList( TList * list )
{
//	LOG( "PrintList()", LOG_MAX );

	TList * cur = list;
	while (cur && cur->text)
	{
//		LOG( cur->text, LOG_MAX );
		cur = cur->next;
	}

//	LOG( "/PrintList()", LOG_MAX );
}
