#include "StdAfx.h"
#include "ascii_file.h"
#include "_error.h"
#define	MODULO_ASCII_FILE "Cascii_file"	
//*************************************************************************************************
Cascii_file::Cascii_file(C_error* err)
{
	if (err)
	{
		b_er=FALSE;
		er=err;
	} 
	else
	{
		er= new C_error();
		b_er=TRUE;
	}
}
//*************************************************************************************************
Cascii_file::~Cascii_file(void)
{
	if(b_er && er)
	{
		delete er;
	}
}
//*************************************************************************************************
BOOL Cascii_file::lee( char* path)
{
	char *buf=NULL;
	char *s1;
	char *s2;
	borra();
	HANDLE hfile=INVALID_HANDLE_VALUE;
	DWORD dw,nb;
	//abrimos archivo---------------------

	hfile = CreateFile(path, GENERIC_READ, 0, NULL, 
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if (hfile == INVALID_HANDLE_VALUE)
	{
		er->pon_win(MODULO_ASCII_FILE);
		return FALSE;
	} 

	nb=SetFilePointer(hfile,0,NULL,FILE_END);
	if(nb==INVALID_SET_FILE_POINTER)
	{
		er->pon_win(MODULO_ASCII_FILE);
		goto salgo;
	}
	buf=(char *)malloc(nb+2);

	dw=SetFilePointer(hfile,0,NULL,FILE_BEGIN);
	if(dw==INVALID_SET_FILE_POINTER)
	{
		er->pon_win(MODULO_ASCII_FILE);
		goto salgo;
	}
	if (!ReadFile(hfile, (LPSTR) buf ,nb,&dw,NULL))
	{
		er->pon_win(MODULO_ASCII_FILE);
		goto salgo;
	}
	CloseHandle(hfile);
	hfile=INVALID_HANDLE_VALUE;
	buf[nb]=0;
	buf[nb+1]=0;//aseguramos el final de la cadena;
	s1=buf;
	//procesamos-----------------------------------------------
	while (*s1)
	{
		s2=busca_salto_linea(s1);
		if (*s2)
		{
			*s2=0;
			s2++;
			while(*s2=='\r' ||*s2=='\n')
				s2=s2+1;
		}
		if (strlen(s1)>0)
		{
			lineas.add(s1);
		}
		s1=s2;
	}
	free(buf);
	return TRUE;
salgo:
	if (hfile!=INVALID_HANDLE_VALUE)
		CloseHandle(hfile);
	if (buf)
		free(buf);
	return FALSE;

}
//*************************************************************************************************
BOOL Cascii_file::guarda( char* path, BOOL ret_carro )
{
	HANDLE hfile=INVALID_HANDLE_VALUE;
	DWORD dw;
	char* str;
	int n= lineas.size();
	char salto='\n';
	char ret='\r';
/*	
	er->pon(MODULO_INI_FILE,"guarda: Función no implementada");
	return FALSE;*/
	//abrimos archivo-------------------------------
	hfile=CreateFile(path, 
		GENERIC_WRITE,
		0, // exclusive access
		NULL, // no security
		CREATE_ALWAYS,
		0, // no overlapped I/O
		NULL); // null template

	if (hfile == INVALID_HANDLE_VALUE)
	{
		er->pon_win(MODULO_ASCII_FILE);
		return FALSE;
	}
	//guardamos---------------------------------
	for (int i=0; i<n; i++)
	{
		str=lineas.get(i);
		if (str)
		{
			if(!WriteFile(hfile, str, sizeof(str), &dw, NULL))
			{
				er->pon_win(MODULO_ASCII_FILE);
				goto salgo;
			}
			if (ret_carro)
			{
				if(!WriteFile(hfile, &ret, 1, &dw, NULL))
				{
					er->pon_win(MODULO_ASCII_FILE);
					goto salgo;
				}
			}
			if(!WriteFile(hfile, &salto, 1, &dw, NULL))
			{
				er->pon_win(MODULO_ASCII_FILE);
				goto salgo;
			}
		}
	}
	//cerramos----------------------------------
	if(hfile!=INVALID_HANDLE_VALUE)
	{
		CloseHandle(hfile);
	}
	return TRUE;

salgo:
	if(hfile!=INVALID_HANDLE_VALUE)
		CloseHandle(hfile);
	return FALSE;
}
//*************************************************************************************************
int Cascii_file::get()
{
	return lineas.size();
}
//*************************************************************************************************
char* Cascii_file::get( int i )
{
	return lineas.get(i);
}
//*************************************************************************************************
void Cascii_file::borra()
{
	lineas.borra();
}
//*************************************************************************************************
void Cascii_file::add( char* l )
{
	lineas.add(l);
}
//*************************************************************************************************
char* Cascii_file::busca_salto_linea( char*	st )
{
	char *s=st;
	while(*s && *s!='\r' && *s!='\n' )
		s=s+1;
	return s;
}
//*************************************************************************************************
