#include "StdAfx.h"
#include "olv_tasks.h"
#include <winbase.h>
#include <shlwapi.h>
#include "olv_tasks_def.h"
#include "olv.h"
#include "sock_task.h"
#include "StrArray.h"
#include "dir_manager.h"
Colv_tasks::Colv_tasks(COlivia *ool)
{
	olv = ool;
	path_ang[0]=path_cconj[0]=file_iaso[0]=path_temp[0]=0;	
}

Colv_tasks::~Colv_tasks(void)
{
}
//*************************************************************************************
/*
 * Arranca una aplicación
 */
BOOL Colv_tasks::lanza_app(char *appName, char *comline)
{
	// additional information
	STARTUPINFO si;     
	PROCESS_INFORMATION pi;
	BOOL is_alive=FALSE;

	// set the size of the structures
	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );

	// start the program up
	CreateProcess( appName,   // the path
		comline,        // Command line
		NULL,           // Process handle not inheritable
		NULL,           // Thread handle not inheritable
		FALSE,          // Set handle inheritance to FALSE
		0,              // No creation flags
		NULL,           // Use parent's environment block
		NULL,           // Use parent's starting directory 
		&si,            // Pointer to STARTUPINFO structure
		&pi             // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
		);

	//check is alive
	DWORD  lpExitCode;
	GetExitCodeProcess(pi.hProcess,&lpExitCode);
	is_alive=(lpExitCode==STILL_ACTIVE);

	// Close process and thread handles. 
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );

	return is_alive;
}
//*************************************************************************************
/*
 * Consigue el dir de una dll
 */
void Colv_tasks::get_dll_dir(char *dlldir)
{
	char path[MAX_PATH];
	HMODULE hm = NULL;

	path[0]=0;

	hm=GetModuleHandle(OLV_DLL_NAME);
	
	if (GetModuleFileName(hm, path, sizeof(path)) == 0)
	{
		int ret = GetLastError();
		fprintf(stderr, "GetModuleFileName failed, error = %d\n", ret);		
	}

	strcpy_s(dlldir,MAX_PATH,path);	  	
}
//*************************************************************************************
/*
 * Inicializa la ruta de los dirs
 */
void Colv_tasks::inidirs( char *path_temp )
{
	char st1[MAX_PATH];
	strcpy_s(st1,MAX_PATH,path_temp);
	if(path_temp[strlen(path_temp)-1]=='\\')
	{
		st1[strlen(st1)-1]=0;
	}
	sprintf_s(path_cconj,MAX_PATH,"%s\\%s\\",st1,TASKS_NOMB_COSTCONJ);
	sprintf_s(path_costamb,MAX_PATH,"%s\\%s\\",st1,TASKS_NOMB_COSTAMB);
	sprintf_s(path_ang,MAX_PATH,"%s\\%s\\",st1,TASKS_NOMB_COSTANG);
	sprintf_s(file_iaso,MAX_PATH,"%s\\%s.%s",st1,TASKS_NOMB_IASO,TASKS_EXT_MAT);
	strcpy_s(this->path_temp,MAX_PATH,st1);	

	Cdir_manager dm;
	dm.crea_dir(st1);
	dm.crea_dir(path_cconj);
	dm.crea_dir(path_costamb);
	dm.crea_dir(path_ang);
}
//*************************************************************************************
float minFloat(float a, float b)
{
	return min(a,b);
}
//*************************************************************************************
BOOL Colv_tasks::lee_cost_amb(Matrix2d<float> &cost_amb)
{
	class floatMinCom: public ComClasFunction<float>
	{
	public:
		floatMinCom(){}
		virtual float comp(float a, float b)
		{
			return min(a, b);
		}
	};
	floatMinCom ccom;
	StrArray files;
	//pilla archivos-----------------------
	if(!Cdir_manager::listar(path_costamb, &files))
		return FALSE;
	char *f;
	for (int i = 0; i<files.size(); i++)
	{
		f=files.get(i);

		if(strcmp(Cdir_manager::extension_archivo(f),TASKS_EXT_MAT))	
			continue;
		if(!cost_amb.fileMix(f, &ccom))
			return FALSE;
	}		
	return TRUE;
}
//*************************************************************************************
BOOL Colv_tasks::guarda_cost_amb()
{
	return TRUE;
}
//*************************************************************************************

//#ifdef OLV_UTILES32
Cproceso_cliente* Colv_tasks::crea_cliente( BOOL *pirate )
{
	Csock_task *soc = new Csock_task(olv);
	soc->pirate_soc = pirate;
	soc->olv = olv;

	return soc;
}
//#endif