//
// An implementation of the ccCheck library by Andy Woodhouse.
// This implementation has been done as the author does not have access to the
// matched sets of headers and libraries used by ATOS.
//
// The functions are matched to the standard .h definition by Dave Yates of ATOS
// 

#include "stdafx.h"
#include "ccCheck.h"

// Forward/external function reference
void Debug(LPCSTR szFmt, ...);

/*
class ccCheck
{
public:
ccCheck(const string & header = "");
virtual ~ccCheck();
public:
void setHeader(const string & header);
bool checkDevice(int device);
bool checkIndex(int index);
bool checkSource(int index);
bool checkRange(int start, int end);
bool checkIndexRange(int start, int end);
bool checkDatabase(int db, int max = 10);
bool checkV1Database(int db);
bool checkString(const string &);
bool checkString(const string &, int);
bool checkWorkstation(int ws);
void log(const string & log);
private:
string m_header;
};

*/

ccCheck::ccCheck(const string & header)
{
	// Parameter header is the string to be prepended to reports sent to Debug
	m_header = header;
}

ccCheck::~ccCheck()
{
	m_header = "";
}
void ccCheck::setHeader(const string & header)
{
	// Parameter header is the string to be prepended to reports sent to Debug.
	// This function allows overriding or updating the set by the class constructor
	m_header = header;
}

bool ccCheck::checkDevice(int device)
{
	// Check device number. This value must be between 1 and 999 inclusive. Returns true on error
	return ((device < 1) || (device > 999)) ? true : false;
}

bool ccCheck::checkIndex(int index)
{
	// Check index (slot, or destination number). This value must be between 1 and 4096 inclusive.
	// Returns true on error
	return ((index < 1) || (index>4096)) ? true : false;
}

bool ccCheck::checkSource(int index)
{
	// Check source index. This value must be between -1 and 4096 inclusive. Returns true on error
	return ((index < -1) || (index > 4096)) ? true : false;
}

bool ccCheck::checkRange(int start, int end)
{
	// Returns true on start > end The values of start and end are not checked otherwise
	return (start > end) ? true : false;
}

bool ccCheck::checkIndexRange(int start, int end)
{
	// Check range and index values. This function uses checkIndex() to validate start and end values.
	// Returns true when either start or end are invalid indexes or start > end
	return (ccCheck::checkIndex(start) || ccCheck::checkIndex(end));
}

bool ccCheck::checkDatabase(int db, int max)
{
	// Check BNCS database number. Default is range 0 to 9 Returns true on error.
	return ((db < 0) || (db >= max)) ? true : false;
}

bool ccCheck::checkV1Database(int db)
{
	// Check V1 Colledia Control database number. Valid values are 0 or 1.
	// Returns true on error
	return ((db < 0) || (db>1)) ? true : false;
}

bool ccCheck::checkString(const string & myText)
{
	// Check Colledia Control string - these are almost always limited to 255 characters
	// Returns true on error
	return (myText.length() > 255) ? true : false;
}

bool ccCheck::checkString(const string & myText, int len)
{
	// Some guesswork here as the properties are not defined in the Developer documentation.
	// We will assume that the test is for a valid string between 0 and len
	int myTextLength = myText.length();

	return ((myTextLength < 0) || (myTextLength > len)) ? true : false;
}

bool ccCheck::checkWorkstation(int ws)
{
	// Check workstation number. This value must be between 1 and 999 inclusive
	return ((ws < 1) || (ws > 999)) ? true : false;
}

void ccCheck::log(const string & log)
{
	// Write message to debug window.
	// Need to add the defined header to the log text. 
	Debug((m_header + ": " + log).c_str());
}


