/**------------------------------------------------------------------------
 * @file
 *
 * Copyright 2001 by the University of Toronto (UT).
 *
 *                       All Rights Reserved
 * 
 * Permission to copy and modify this software and its documentation 
 * only for internal use in your organization is hereby granted, 
 * provided that this notice is retained thereon and on all copies.  
 * UT makes no representations as to the suitability and operability 
 * of this software for any purpose.  It is provided "as is" without 
 * express or implied warranty.
 *
 * UT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
 * SHALL UT BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * No other rights, including for example, the right to redistribute 
 * this software and its documentation or the right to prepare 
 * derivative works, are granted unless specifically provided in a 
 * separate license agreement.
 *
 * Author: Diego Macrini based on Ali Shokoufandeh's original code
 * 
 * $Log: HelperFunctions.cpp,v $
 * Revision 1.2  2002/06/17 23:09:46  dmac
 * Class LogFile added.
 *
 * Revision 1.1  2002/06/13 21:47:49  dmac
 * Added file.
 *
 *
 *-----------------------------------------------------------------------*/

#include <stdio.h> 
#include <stdarg.h>
#include <math.h>
#include "HelperFunctions.h"

#ifdef WIN32
#define vsnprintf _vsnprintf
#endif

using namespace std;

double SIMILARITY(double x, double y, const double& minVal /*= .1*/)
{
	x = fabs(x);
	y = fabs(y);

	if (x > y)
	{
		if (y == 0)
			return MIN(minVal, x) / x;
		else
			return y / x;
	}
	else if (y > x)
	{
		if (x == 0)
			return MIN(minVal, y) / y;
		else
			return x / y;
	}

	return 1;
}

LogFile::LogFile(const char* szFileName)
{
	open(szFileName, ios::out | ios::trunc);
}

void LogFile::Print(const char* szFormat, ...)
{
	va_list args;

	va_start(args, szFormat);

	//vfprintf((FILE*)this->rdbuf()->fd(), szFormat, args);
	const int size = 512;
	char szBuff[size];

	vsnprintf(szBuff, size, szFormat, args);
   
	va_end(args);

	(*this) << szBuff;
}

