#include "test.h"

int main(int argc, char* argv[])
{
	FILE *input;
	if (! (input = ParseCmdLine(argc, argv)))
	{
		return 0;
	}
	char iDesc[] = "matrix D, matrix Dm, matrix Dp, indexarray Tuc, indexarray Tuo, arrayi IncludeT, arrayi ExcludeT, arrayi B, arrayi B0, matrix L, matrix L0, string WriteLog, string EnforceLive";
	char oDesc[] = "matrix Lf, arrayi Bf, matrix L0f, arrayi B0f, string how";
	int *iFill;
	int oFill[5];
	int BCount, B0Count, i;
	matrix D;
	char WriteLog[128] = "", EnforceLive[128] = "";
	dp_p p;
	
	for (i = 0; i < 5; i++)
	{
		oFill[i] = 1;
	}
	
	MemoryManager mem = CreateMemoryManager(10, 10, 0, 0);
	memset(&p, 0, sizeof(dp_p));
	
	setverbose(VRB_MAX);
	
	//Before we begin, delete any old log files.
	remove("dp.log");
	remove("dp.dat");
	int transitions;
	
	while (ParseStructure(input, iDesc, &iFill, &mem, &D, &p.Dm, &p.Dp, &p.Tuc, &p.TucCount, &p.Tuo, &p.TuoCount, &p.IncludeT, &p.IncludeCount, &p.ExcludeT, &p.ExcludeCount, &p.B, &BCount, &p.B0, &B0Count, &p.L, &p.L0, WriteLog, EnforceLive))
	{
		transitions = (iFill[0] ? NumberOfColumns(D) : (iFill[1] ? NumberOfColumns(p.Dm) : (iFill[2] ? NumberOfColumns(p.Dp) : 0)));
		DisplayStructure(iDesc, iFill, &D, &p.Dm, &p.Dp, p.Tuc, p.TucCount, NumberOfColumns(p.Dm), p.Tuo, p.TuoCount, NumberOfColumns(p.Dm), p.IncludeT, p.IncludeCount, p.ExcludeT, p.ExcludeCount, p.B, BCount, p.B0, B0Count, &p.L, &p.L0, WriteLog, EnforceLive);
		FillDmDp(iFill, &D, &p.Dm, &p.Dp, &mem);
		/*Log writing defaults to on.*/
		if (strcmp(WriteLog, "off")) p.option |= DP_OPT_WRITELOG;
		if (WriteLog[0] && strcmp(WriteLog, "on"))
		{
			printf("Warning: Unrecognized value for option WriteLog: '%s'\n", WriteLog);
			printf("Should be 'off' or 'on'. Defaulting to 'on'.\n");			
		}
		/*T-liveness enforcement defaults to off.*/
		if (! strcmp(EnforceLive, "on")) p.option |= DP_OPT_ENFORCELIVE;
		else if (EnforceLive[0] && strcmp(EnforceLive, "off"))
		{
			printf("Warning: Unrecognized value for option EnforceLive: '%s'\n", EnforceLive);
			printf("Should be 'off' or 'on'. Defaulting to 'off'.\n");
		}
		
		//For the duration of a test, append to the log files.
		p.option |= DP_OPT_APPENDLOGS;
		
		dp_r res = dp(&p);
		printf("Solution:\n");
		DisplayStructure(oDesc, oFill, &res.Lf, res.Bf, NumberOfRows(res.Lf), &res.L0f, res.B0f, NumberOfRows(res.L0f), res.how);
		printf("-------------------------------------------------------------------------------\n\n");
		FreeMemory(&mem);
		DeallocateDp(&res);
		
		mem = CreateMemoryManager(10, 10, 0, 0);
		memset(&p, 0, sizeof(dp_p));
		WriteLog[0] = '\0';
		EnforceLive[0] = '\0';
	}
	FreeMemory(&mem);
	return 0;
}
