#include "StdAfx.h"
#include "ManagerDbfGdataTable.h"
#include "GdataTable.h"
//***************************************************************************

ManagerDbfGdataTable::ManagerDbfGdataTable(void)
{
	_dt = NULL;
}
//***************************************************************************

ManagerDbfGdataTable::~ManagerDbfGdataTable(void)
{
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfSet( int nrow, int ncolm )
{
	_dt->delAll();
	_nrow = nrow;
	_ncolm = ncolm;
	return true;
}
//***************************************************************************
bool ManagerDbfGdataTable::DelCol(char* path, char* name)
{
	GdataTable tb;

	if (path)
	{
		if (!LeeDbf(path, &tb))
			return false;

		_dt = &tb;
	}
	else if (!_dt)
	{
		return false;
	}

	int ic = _dt->getInd(name);

	if (ic < 0)
	{
		return false;
	}

	_dt->removeColm(ic);

	if (path)
	{
		if (!grabaDbf(path, _dt))
			return false;
	}

	return true;
}
//***************************************************************************
bool ManagerDbfGdataTable::AddCol( char* path, char* name, BYTE type,IAddColDbf *dt, int size)
{
	GdataTable tb;

	if (path)
	{
		if (!LeeDbf(path, &tb))
			return false;

		_dt = &tb;
	}
	else if(!_dt)
	{
		return false;
	}

	int ic= _dt->getInd(name);

	if(ic<0)
	{
		//la crea
		if(!_dt->addColm(name,type,size))
			return false;
		ic= _dt->nColm()-1;
	}

	dt->size = _dt->getSize(ic);

	for(int i=0;i< _dt->nRow();i++)
	{
		dt->setData(i, _dt->get(i,ic));
	}

	if (path)
	{
		if (!grabaDbf(path, _dt))
			return false;
	}

	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfSetColm( char* name, BYTE type, int size )
{
	int t;
	switch(type)
	{
	case(DBF_TYPE_DATA_STR):
		size++;
		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 = _dt->addColm(name,t,size);
	if(res && _nrow>0 && _dt->nColm()>= _ncolm)
	{
		res = _dt->addRow(_nrow);
		_nrow =0;
	}
	return res;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfaddVal( int icol, int v )
{
	int* vv = _dt->getI(_nrow,icol);
	if(vv)
		*vv = v;
	return vv!= NULL;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfaddVal( int icol, double v )
{
	double* vv = _dt->getD(_nrow,icol);
	if(vv)
		*vv = v;
	return vv!= NULL;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfaddVal( int icol, __int64 v )
{
	__int64* vv = _dt->getI64(_nrow,icol);
	if(vv)
		*vv = v;
	return vv!= NULL;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfaddVal( int icol, char* v )
{
	char* vv = _dt->getS(_nrow,icol);
	if(vv)
		strcpy(vv,v);
	return vv!= NULL;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfaddVal( int icol, void* v )
{
	void* vv = _dt->getBin(_nrow,icol);
	if(vv)
		memcpy(vv,v,_dt->getSize(icol));
	return vv!= NULL;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfFinRow()
{
	_nrow++;
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfFinR()
{
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfGet( int *nrow, int *ncolm )
{
	*nrow = _dt->nRow();
	*ncolm = _dt->nColm();
	_nrow =0;
	return true;
}
//***************************************************************************
#define  MAX_NDECIMALS 10;
bool ManagerDbfGdataTable::DbfGetColm(int icolm, 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[_dt->getType(icolm)];

	*ndecimal = MAX_NDECIMALS;
	//copia segura de nombre------------------
	int i =0;
	char *st =_dt->getName(icolm);
	for(; i<10; i++)
	{
		if(!st[i])
			break;
		name[i]=st[i];
	}
	name[i]=0;
	*size = _dt->getSizeASCII(icolm);
	if (_dt->getType(icolm) == GdataTable::Tstring)
		*size = *size - 1;
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfGetVal(int irow,  int icol, int* v )
{
	*v = *_dt->getI(irow,icol);
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfGetVal(int irow,  int icol, double* v )
{
	*v = *_dt->getD(irow,icol);
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfGetVal(int irow,  int icol, __int64* v )
{
	*v = *_dt->getI64(irow,icol);
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfGetVal(int irow,  int icol, char** v )
{
	*v= _dt->getS(irow,icol);
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfGetVal(int irow,  int icol, void** v )
{
	*v= _dt->getBin(irow,icol);
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::DbfFinW()
{
	return true;
}
//***************************************************************************

bool ManagerDbfGdataTable::LeeDbf( char* path, GdataTable *db )
{
	_dt= db;
	return ManagerDbf::leer(path,this);
}
//***************************************************************************
bool ManagerDbfGdataTable::grabaDbf( char* path, GdataTable *db )
{
	_dt= db;
	return ManagerDbf::grabar(path,this);
}
//***************************************************************************
