#include "stdafx.h"

#include "OutputCode.h"

int sOutputCode::fGetIntFind()
{
	return vvi;
}

sCode * sOutputCode::fGetCodeToInt(int vInt)
{
	return szBuffer4[vInt];
}

bool sOutputCode::fInputStr(char *vStr)
{
	if (vStr == NULL) return FALSE;
	if (memcmp(vStr,".0",2) == 0)
	{
		vvi = vvi;
	}
	szBuffer4[vCountBuffer4] = new sCode;
	szBuffer4[vCountBuffer4]->vSize = strlen(vStr);
	szBuffer4[vCountBuffer4]->pCode = new char[szBuffer4[vCountBuffer4]->vSize];
	memcpy(szBuffer4[vCountBuffer4]->pCode,vStr,szBuffer4[vCountBuffer4]->vSize);
	vCountBuffer4++;
	return TRUE;
}

bool sOutputCode::fInputCode(sCode *vInputCode)
{
	if (vInputCode == NULL) return FALSE;
	if (memcmp(vInputCode->pCode,".0",2) == 0)
	{
		vvi = vvi;
	}
	szBuffer4[vCountBuffer4] = new sCode;
	szBuffer4[vCountBuffer4]->vSize = vInputCode->vSize;
	szBuffer4[vCountBuffer4]->pCode = new char[szBuffer4[vCountBuffer4]->vSize];
	memcpy(szBuffer4[vCountBuffer4]->pCode,vInputCode->pCode,szBuffer4[vCountBuffer4]->vSize);
	vCountBuffer4++;
	return TRUE;
}

bool sOutputCode::fOutputCode(sCode *vOutputCode)
{
	if (vOutputCode == NULL) return FALSE;
	if (vCountBuffer4 <= 0) return FALSE;
	if (vCountBuffer4 > 0)
	{
		vOutputCode->vSize = 0;
		for (vvi = 0; vvi < vCountBuffer4; vvi++)
		{
			vOutputCode->vSize += szBuffer4[vvi]->vSize;
		}
		vOutputCode->pCode = (char*) malloc(sizeof(char) * vOutputCode->vSize); // выделить память
		pStr = vOutputCode->pCode;
		for (vvi = 0; vvi < vCountBuffer4; vvi++)
		{
			if (szBuffer4[vvi]->vSize > 0)
			{
				memcpy(pStr,szBuffer4[vvi]->pCode,szBuffer4[vvi]->vSize);
				pStr += szBuffer4[vvi]->vSize;
			}
		}
	}
	return TRUE;
}

bool sOutputCode::fFreeMem()
{
	if (vCountBuffer4 > 0)
	{
		for (vvi = 0; vvi < vCountBuffer4; vvi++)
		{
			if (szBuffer4[vvi]->vSize > 0 && szBuffer4[vvi]->pCode) delete szBuffer4[vvi]->pCode;
			szBuffer4[vvi]->pCode = NULL;
			szBuffer4[vvi]->vSize = 0;
			if (szBuffer4[vvi]) delete szBuffer4[vvi];
			szBuffer4[vvi] = NULL;
		}
	}
	if (szBuffer4) delete szBuffer4;
	szBuffer4 = NULL;
	return TRUE;
}

bool sOutputCode::fFindCode(sCode *vCode)
{
	if (vCode == NULL) return FALSE;
	vLenStr = vCode->vSize;
	if (vCountBuffer4 > 0)
	{
		for (vvi = 0; vvi < vCountBuffer4; vvi++)
		{
			if (szBuffer4[vvi]->vSize > 0 &&
				szBuffer4[vvi]->pCode)
			{
				if (vLenStr == szBuffer4[vvi]->vSize &&
					memcmp(szBuffer4[vvi]->pCode,vCode->pCode,szBuffer4[vvi]->vSize) == 0)
				{
					return TRUE;
				}
			}
		}
	}
	return FALSE;
}

bool sOutputCode::fFindStr(char *vStr)
{
	if (vStr == NULL) return FALSE;
	vLenStr = strlen(vStr);
	if (vCountBuffer4 > 0)
	{
		for (vvi = 0; vvi < vCountBuffer4; vvi++)
		{
			if (szBuffer4[vvi]->vSize > 0 &&
				szBuffer4[vvi]->pCode)
			{
				if (vLenStr == szBuffer4[vvi]->vSize &&
					memcmp(szBuffer4[vvi]->pCode,vStr,szBuffer4[vvi]->vSize) == 0)
				{
					return TRUE;
				}
			}
		}
	}
	return FALSE;
}

sOutputCode::sOutputCode()
{
	szBuffer4 = new sCode*[SIZE_BUF_4_2];
	vCountBuffer4 = 0;
}

sOutputCode::sOutputCode(int vSizeMass)
{
	szBuffer4 = new sCode*[vSizeMass];
	vCountBuffer4 = 0;
}

sOutputCode::~sOutputCode()
{
	fFreeMem();
}