
#include <windows.h>
#include <stdio.h>
#include <io.h>

#include "RawScanError.h"
#include "NDTSError.h"

typedef struct 
{
	RawScan_ReturnCode	code ;
	char *			codeString ;
} RawScan_Errors ;

RawScan_Errors RAW_SCAN_APIErrors[] = 
{
	{RawScan_Extract_OK,														"OK"},
	{RawScan_ExtractAScan_InvalidBScanIndex,									"RawScan_ExtractAScan fail: invalid B-scan index (use RawScan_GetNumberOfBScansPerBank to query maximum)"},
	{RawScan_ExtractAScan_InvalidAScanIndex,									"RawScan_ExtractAScan fail: invalid A-scan index (use RawScan_GetNumberOfAScansPerBScan to query maximum)"},
	{RawScan_ExtractAScan_InternalDataFormatError,							"RawScan_ExtractAScan fail: internal data format error"},
	{RawScan_ExtractAScan_AScanDesynchronisation,							"RawScan_ExtractAScan fail: A-scan desynchronisation"},
	{RawScan_ExtractAScan_RawScanDoesNotContainAScanData,					"RawScan_ExtractAScan fail: pRawScan does not contain A-scan data"},
	{RawScan_ExtractBScan_InvalidBScanIndex,									"RawScan_ExtractBScan fail: invalid B-scan index (use RawScan_GetNumberOfBScansPerBank to query maximum)"},
	{RawScan_ExtractBScan_InternalDataFormatError,							"RawScan_ExtractBScan fail: internal data format error"},
	{RawScan_ExtractBScan_AScanDesynchronisation,							"RawScan_ExtractBScan fail: A-scan desynchronisation"},
	{RawScan_ExtractBScan_RawScanDoesNotContainBScanData,					"RawScan_ExtractBScan fail: pRawScan does not contain B-scan data"},
	{RawScan_ExtractCScan_InvalidBScanBounds,								"RawScan_ExtractCScan fail: invalid B-scan bounds (bScanIndex + numberOfBScans must be less than or equal to number of B-scans in pRawScan)"},
	{RawScan_ExtractCScan_MissingCScan,									"RawScan_ExtractCScan fail: raw scan does not contain C-scan data (use RawScan_SetScanStorageOptions)"},
	{RawScan_ExtractCScan_OutOfMemory,									"RawScan_ExtractCScan fail: system is low on memory"},
	{RawScan_ExtractCScan_InvalidGate,									"RawScan_ExtractCScan fail: gate in RPN sequence does not exist"},
	{RawScan_ExtractCScan_InvalidRPNSequence,								"RawScan_ExtractCScan fail: invalid RPN sequence (see documentation for details)"},
	{RawScan_ExtractCScan_RawScanDoesNotContainGateData,					"RawScan_ExtractCScan fail: pRawScan does not contain gate data"},
	{RawScan_ExtractCScan_CScanDesynchronisation,							"RawScan_ExtractCScan fail: C-scan desynchronisation"},
	{RawScan_ExtractCScan_InternalDataFormatError,						"RawScan_ExtractCScan fail: Raw data header corrupt"},
	{RawScan_ExtractCScan_NotCompatibleWithSequencedFPGA,						"RawScan_ExtractCScan fail: Not Compatible With Sequenced FPGA"},
	{RawScan_ExtractGateValues_InvalidBScanIndex,							"RawScan_ExtractGateValues fail: invalid B-scan index (insufficient B-scans in memory)"},
	{RawScan_ExtractGateValues_InvalidAScanIndex,							"RawScan_ExtractGateValues fail: invalid A-scan index (insufficient A-scans per B-scan"},
	{RawScan_ExtractGateValues_RawScanDoesNotContainGateData,				"RawScan_ExtractGateValues fail: pRawScan does not contain gate data"},
	{RawScan_ExtractEncoderValue_MissingEncoderValues,					"RawScan_ExtractEncoderValue fail: missing encoder values (trigger must be set to RawScan_TEncoder and appropriate hardware connected)"},
	{RawScan_ExtractEncoderValue_InvalidBScanIndex,						"RawScan_ExtractEncoderValue fail: invalid B-scan index (insufficient B-scans in memory)"},
	{RawScan_ExtractEncoderValue_InvalidEncoderIndex,						"RawScan_ExtractEncoderValue fail: invalid encoder index (must be 0 to NUMBER_OF_ENCODERS - 1)"},
	{RawScan_ExtractEncoderValue_RawScanDoesNotContainEncoderData,		"RawScan_ExtractEncoderValue fail: pRawScan does not contain encoder data"},
	{RawScan_Alloc_OutOfMemory,											"RawScan - OutOfMemory"},
	{RawScan_MoveData_TooManyBScans,									"RawScan_MoveData_TooManyBScans - an attempt was made to create a raw scan over 10,000 blocks - app likely to not be keeping up with data collection"},
	{RawScan_LastError,													"Unknown"}
} ;

RAW_SCAN_API const char * CALL_CONV RawScan_GetCodeString(int errorCode)
{
	int i = 0 ;

	while (RAW_SCAN_APIErrors[i].code != errorCode && RAW_SCAN_APIErrors[i].code != RawScan_LastError)
		i++ ;

	return RAW_SCAN_APIErrors[i].codeString ;
}

void RawScan_HandleError(RawScan_ReturnCode errorCode) 
{
	NDTS_HandleError(errorCode);
}