#include "test.h"

static void GetOutFilled(reduce_r* result, int OutFilled[]);

int main(int argc, char* argv[])
{
	FILE* input;
	matrix L;
	int BCount;
	int *B, *InFilled;
	int OutFilled[4];
	reduce_r result;
	MemoryManager memory;
	char InputDesc[] = "matrix L, arrayi B";
	char OutputDesc[] = "matrix Lf, arrayi Bf, arrayi RowsKept, int How";
	
	/*Get the input file.*/
	if (! (input = ParseCmdLine(argc, argv)))
	{
		return 0;
	}
	
	memory = CreateMemoryManager(4, 1, 0, 0);
	while (ParseStructure(input, InputDesc, &InFilled, &memory, &L, &B, &BCount))
	{
		/*Display the problem*/
		printf("Problem:\n");
		DisplayStructure(InputDesc, InFilled, &L, B, BCount);
		
		/*Solve the problem*/
		result = reduce(InFilled[0] ? &L : 0, InFilled[1] ? B : 0);
		
		/*Display the solution*/
		GetOutFilled(&result, OutFilled);
		printf("Result:\n");
		DisplayStructure(OutputDesc, OutFilled, &result.Lf, result.Bf, NumberOfRows(result.Lf), result.indf, NumberOfRows(result.Lf), result.how);
		FreeMemory(&memory);
		DeallocateReduce(&result);
		memory = CreateMemoryManager(4, 1, 0, 0);
		printf("-----------------------------------------------------------------\n");
	}
	FreeMemory(&memory);
	return 0;
}

void GetOutFilled(reduce_r* result, int OutFilled[])
{
	/*Clear OutFilled*/
	memset(OutFilled, 0, 4 * sizeof(int));
	
	/*Set OutFilled flags only if the corresponding pointers are not null.*/
	if (NumberOfRows(result->Lf)) OutFilled[0] = 1;
	if (result->Bf) OutFilled[1] = 1;
	if (result->indf) OutFilled[2] = 1;
	/*How is always filled since it is just an integer type.*/
	OutFilled[3] = 1;
}
