#include "StdAfx.h"
#include "IaDataTable.h"
#include "CartoBase.h"
#include "Fdbf.h"
//***************************************************************************
IaDataTable::IaDataTable(void)
{
	auxnrow=-1;
	auxref =-1;
	auxref = auxc=-1;

}
//***************************************************************************
IaDataTable::~IaDataTable(void)
{
}
//***************************************************************************
void* IaDataTable::IaGet( int refEnt )
{
	return NULL;
}
//***************************************************************************
void IaDataTable::CBSet( CartoBase* cb )
{
	_cb = cb;
}
//***************************************************************************
void IaDataTable::IaRemove( int refEnt )
{
	int icol = RefIndex[refEnt];
	if(icol<0)
		return;
	removeRow(RefIndex[refEnt]);
	RefIndex[refEnt]=-1;
	rowIndex[icol] = -1;
}
//***************************************************************************
void IaDataTable::IaRemoveAll()
{
	RefIndex.clear();
	rowIndex.clear();
	delAll();
}
//***************************************************************************
void IaDataTable::IaAdd( int refEnt )
{
	if(!addRow(1))
	{
		RefIndex[refEnt] = -1;
		return;
	}
	int iRow=nRow()-1;
	RefIndex[refEnt]=iRow;
	rowIndex[iRow] = refEnt;
}
//***************************************************************************
bool IaDataTable::IaSet( int nrow, int ncolm )
{
	IaRemoveAll();
	auxnrow = nrow;
	auxcolm =ncolm;
	return true;
}
//***************************************************************************
bool IaDataTable::IaSetColm( int icol, char* name, BYTE type, int size )
{
	int t;
	switch(type)
	{
	case(DBF_TYPE_DATA_STR):
		t = GdataTable::Tstring;
		break;
	case(DBF_TYPE_DATA_I64):
		t = GdataTable::Tint64;
		break;
	case(DBF_TYPE_DATA_D):
		t = GdataTable::Tdouble;
		break;
	case(DBF_TYPE_DATA_BOOL):
		t = GdataTable::Tbool;
		break;
	case(DBF_TYPE_DATA_BIN):
		t = GdataTable::Tbin;
		break;
	case(DBF_TYPE_DATA_I):
		t = GdataTable::Tint;
		break;
	default:
		return false;
	}
	bool res = addColm(name,t,size);
	if(res && auxcolm>0 && nColm()>= auxcolm)
	{
		res = addMemRow(auxnrow);
		auxnrow =0;
	}
	return res;
}
//***************************************************************************
bool IaDataTable::IaAddVal( int refEnt, int icol, int v )
{
	int _nrow;
	if(auxnrow>=0)
		_nrow =RefIndex[refEnt];
	else
		 _nrow = RefIndex[refEnt];
	int* vv = getI(auxnrow,icol);
	if(vv)
		*vv = v;
	return vv!= NULL;
}
//***************************************************************************
bool IaDataTable::IaAddVal( int refEnt, int icol, double v )
{
	int _nrow;
	if(auxnrow>=0)
		_nrow =RefIndex[refEnt];
	else
		_nrow = RefIndex[refEnt];
	double* vv = getD(_nrow,icol);
	if(vv)
		*vv = v;
	return vv!= NULL;
}
//***************************************************************************
bool IaDataTable::IaAddVal( int refEnt, int icol, __int64 v )
{
	int _nrow;
	if(auxnrow>=0)
		_nrow =RefIndex[refEnt];
	else
		_nrow = RefIndex[refEnt];
	__int64* vv = getI64(_nrow,icol);
	if(vv)
		*vv = v;
	return vv!= NULL;
}
//***************************************************************************
bool IaDataTable::IaAddVal( int refEnt, int icol, char* v )
{
	int _nrow;
	if(auxnrow>=0)
		_nrow =RefIndex[refEnt];
	else
		_nrow = RefIndex[refEnt];
	char* vv = getS(_nrow,icol);
	if(vv)
		strcpy(vv,v);
	return vv!= NULL;
}
//***************************************************************************
bool IaDataTable::IaAddVal( int refEnt, int icol, void* v )
{
	int _nrow;
	if(auxnrow>=0)
		_nrow =RefIndex[refEnt];
	else
		_nrow = RefIndex[refEnt];
	void* vv = getBin(_nrow,icol);
	if(vv)
		memcpy(vv,v, getSize(icol));
	return vv!= NULL;
}
//***************************************************************************
bool IaDataTable::IaFinRow( int refEnt )
{
	auxnrow++;
	return true;
}
//***************************************************************************
bool IaDataTable::IaFinR()
{
	auxnrow=-1;
	return true;
}
//***************************************************************************
bool IaDataTable::IaGetSize(int *ncolm )
{
	*ncolm =nColm();
	return true;
}
//***************************************************************************
#define  MAX_NDECIMALS 10;
bool IaDataTable::IaGetColm( int icol, char* name, BYTE *type, int *size, int*ndecimal )
{
	//pilla tipo----------------------
	static BYTE t[GdataTable::Tntip]={0,DBF_TYPE_DATA_BOOL,DBF_TYPE_DATA_I, DBF_TYPE_DATA_D,DBF_TYPE_DATA_I64, DBF_TYPE_DATA_STR, DBF_TYPE_DATA_BIN } ;
	*type = t[getType(icol)];
	*ndecimal = MAX_NDECIMALS;
	//copia segura de nombre------------------
	int i =0;
	char *st =getName(icol);
	for(; i<10; i++)
	{
		if(!st[i])
			break;
		name[i]=st[i];
	}
	name[i]=0;
	*size = getSizeASCII(icol);
	return *size>0;
}
//***************************************************************************
bool IaDataTable::IaGetVal( int refEnt, int icol, int* v )
{
	int irow;
	if(auxref == refEnt)
		irow = auxc;
	else
	{
		irow = RefIndex[refEnt];
		auxref = refEnt;
		auxc = irow;
	}
	if(irow<0)
		return false;
	*v = *getI(irow,icol);
	return true;
}
//***************************************************************************
bool IaDataTable::IaGetVal( int refEnt, int icol, double* v )
{
	int irow;
	if(auxref == refEnt)
		irow = auxc;
	else
	{
		irow = RefIndex[refEnt];
		auxref = refEnt;
		auxc = irow;
	}
	if(irow<0)
		return false;
	*v = *getD(irow,icol);
	return true;
}
//***************************************************************************
bool IaDataTable::IaGetVal( int refEnt, int icol, __int64* v )
{
	int irow;
	if(auxref == refEnt)
		irow = auxc;
	else
	{
		irow = RefIndex[refEnt];
		auxref = refEnt;
		auxc = irow;
	}
	if(irow<0)
		return false;
	*v = *getI64(irow,icol);
	return true;
}
//***************************************************************************
bool IaDataTable::IaGetVal( int refEnt, int icol, char** v )
{
	int irow;
	if(auxref == refEnt)
		irow = auxc;
	else
	{
		irow = RefIndex[refEnt];
		auxref = refEnt;
		auxc = irow;
	}
	if(irow<0)
		return false;
	*v= getS(irow,icol);
	return true;
}
//***************************************************************************
bool IaDataTable::IaGetVal( int refEnt, int icol, void** v )
{
	int irow;
	if(auxref == refEnt)
		irow = auxc;
	else
	{
		irow = RefIndex[refEnt];
		auxref = refEnt;
		auxc = irow;
	}
	if(irow<0)
		return false;
	*v= getBin(irow,icol);
	return true;
}
//***************************************************************************
bool IaDataTable::IaFinW()
{
	auxref = auxc=-1;
	return true;
}
//***************************************************************************