#include "test.h"

int main(int argc, char *argv[])
{	
	MemoryManager memory;
	FILE* input;
	int *Filled, *SArray;
	int Constraints, Transitions, i, OFilled[2];
	issiph_r result;
	
	char IDesc[] = "matrix D, matrix Dm, matrix Dp, matrix PlaceSet";
	char ODesc[] = "arrayi IsSiphon, int NonSiphonPresent";
	
	matrix D, Dm, Dp, PlaceSet;
	
	/*Get a pointer to the input file.*/
	if (! (input = ParseCmdLine(argc,argv)))
	{
		return 0;
	}
	
	memory = CreateMemoryManager(5, 6, 0, 0);
	
	while (ParseStructure(input, IDesc, &Filled, &memory, &D, &Dm, &Dp, &PlaceSet))
	{			
		/*Display the problem.*/		
		DisplayStructure(IDesc, Filled, &D, &Dm, &Dp, &PlaceSet);
	
		/*If an incidence matrix was supplied fill in the i/o matrices.*/
		FillDmDp(Filled, &D, &Dm, &Dp, &memory);

		/*If there is only one column in the PlaceSet matrix, then convert it to an
		array.*/
		if (Filled[3] && NumberOfColumns(PlaceSet) == 1)
		{
			SArray = mcalloc(&memory, NumberOfRows(PlaceSet), sizeof(int));
			for (i = 0; i < NumberOfRows(PlaceSet); i++)
			{
				SArray[i] = GetMatrixEl(&PlaceSet, i, 0);
			}
		}
		
		/*Solve the problem.*/
		result = issiph(Filled[1] ? &Dm : 0, Filled[2] ? &Dp : 0, Filled[3] ? (NumberOfColumns(PlaceSet) > 1 ? (void*) &PlaceSet : (void*) SArray) : 0, Filled[3] ? NumberOfColumns(PlaceSet) > 1 : 0);
		
		/*Display the solution.*/
		OFilled[0] = result.flag ? 1 : 0;
		OFilled[1] = 1;
		DisplayStructure(ODesc, OFilled, result.flag, NumberOfColumns(PlaceSet), result.flx);
		DeallocateIssiph(&result);
		printf("---------------------------------------------------------------\n");
		FreeMemory(&memory);
		memory = CreateMemoryManager(5, 6, 0, 0);
	}
	FreeMemory(&memory);
	return 0;
}
