#include "global.h"
#include "cstringtable.h"
#include "csyntactic.h"

CStringTable::CStringTable()
{
	names = _NEW(CMemory());
	pnames = _NEW(CMemory());
	count = 0;
}

CStringTable::~CStringTable()
{
	_DELETE(names); _DELETE(pnames);
}

void *CStringTable::GetPointer()
{
	return names->GetPointer();
}

DWORD CStringTable::GetSize()
{
	return names->GetSize();
}

DWORD CStringTable::GetCount()
{
	return count;
}

void CStringTable::DeleteAll()
{
	count = 0;
	names->DeleteData();
	pnames->DeleteData();
}

const char *CStringTable::Get(DWORD index)
{
	if (index>=count)
	{
#ifdef _DEBUG
		::MessageBox(0, "index>=count","CStringTable::Get", MB_OK);
#endif
		app.abort();
	}
	DWORD ptr = ((DWORD*)pnames->GetPointer())[index];
	return (char*)(ptr + (DWORD)names->GetPointer());
}

DWORD CStringTable::Index(char *strfind)
{
	//vrati poziciu daneho textu v tabulke
	for (DWORD i=0; i<count; i++)
	{
		DWORD ptr = ((DWORD*)pnames->GetPointer())[i];
		char *str = (char*)(ptr + (DWORD)names->GetPointer());
		if (!stricmp(str, strfind))//porovnaj bez case-sensitive
		{
			return i;
		}
	}
	return -1;
}

DWORD CStringTable::Add(char *str)
{
	DWORD p = names->AddData((void*)str, strlen(str)+1);// + (DWORD)names->GetPointer();
	pnames->AddData(&p, 4);//uloz pointer do pola pointrov
	return count++;
}

DWORD CStringTable::nAdd(char *str,DWORD len)
{
	DWORD p = names->AddData((void*)str, len);// + (DWORD)names->GetPointer();
	pnames->AddData(&p, 4);//uloz pointer do pola pointrov
	return count++;
}

DWORD CStringTable::Insert(char *str)
{
/*	if (!str[0])
	{
		//prazdny retazec pridame vzdy ako novu polozku
	}
	else
	{*/
		DWORD i = Index(str);
		if (i != -1)
			return i;
	//}

	//nenasiel sa, pridaj ho
	DWORD p = names->AddData((void*)str, strlen(str)+1);// + (DWORD)names->GetPointer();
	pnames->AddData(&p, 4);//uloz pointer do pola pointrov
	return count++;
}