#include "test.h"

int main(int argc, char* argv[])
{
  char InDesc[] = "matrix L, arrayd B, arrayd F, arrayd HiBound, arrayd LoBound, arrayi IntList, arrayi CType";
	char OutDesc[] = "string Message, arrayd Solution";
	
	ipsolve_r result;
  matrix L;  
  double *B, *F, *ub, *lb;
  int *IntIntList, *IntCType;
	int numB, numF, numUB, numLB, numIntList, numCType;
	short *IntList, *ctype;

	int *InFilled, OutFilled[2];
	int i;
	
	FILE* input;
	
	MemoryManager mem;
	
	/*Get the input file.*/
	if (! (input = ParseCmdLine(argc,argv)))
	{
		return 0;
	}
	
	mem = CreateMemoryManager(10, 10, 0, 0);
	
	while (ParseStructure(input, InDesc, &InFilled, &mem, &L, &B, &numB, &F, &numF, &ub, &numUB, &lb, &numLB, &IntIntList, &numIntList, &IntCType, &numCType))
	{
		/*Display the problem.*/
		DisplayStructure(InDesc, InFilled, &L, B, numB, F, numF, ub, numUB, lb, numLB, IntIntList, numIntList, IntCType, numCType);
		
		/*Convert the integer arrays to shorts arrays.*/
		if (InFilled[5])
		{
			IntList = mcalloc(&mem, numIntList, sizeof(short));
			for (i = 0; i < numIntList; i++)
			{
				IntList[i] = (short) IntIntList[i];
			}
		}
		if (InFilled[6])
		{
			ctype = mcalloc(&mem, numCType, sizeof(short));
			for (i = 0; i < numCType; i++)
			{
				ctype[i] = (short) IntCType[i];
			}
		}
		
		/*Solve the problem.
		ipsolve_r ipsolve(matrix *L0, double* B0, double *Cost0, short int *IntList0, double *HiBound0, double *LoBound0, short int *ConstraintType0)*/
		result = ipsolve(InFilled[0] ? &L : 0, InFilled[1] ? B : 0, InFilled[2] ? F : 0, InFilled[5] ? IntList : 0, InFilled[3] ? ub : 0, InFilled[4] ? lb : 0, InFilled[6] ? ctype : 0);
		
		/*Fill the output display mask.*/
		OutFilled[0] = 1;
		OutFilled[1] = (result.res) ? 1 : 0;
		
		/*Display the solution.*/
		DisplayStructure(OutDesc, OutFilled, result.mhow, result.res, NumberOfColumns(L));
		
		FreeMemory(&mem);
		DeallocateIpsolve(&result);
		mem = CreateMemoryManager(10, 10, 0, 0);
	}
	FreeMemory(&mem);
	return 0;
}
