/*
Unit conversion module 
A DLL example for CalcNOW

G. Mason
*/

#include <windows.h>
#include <math.h>
#include "SNDLLCommons.h"


// required dll entry point
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call){
	case DLL_PROCESS_ATTACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

// required dll module
extern "C" short calcMain(short id, bool alt, double theStack[10],  DLLEXTRAS* extras){
	short rtnState = 0;
	short i;
	double displaytorad = 1;
	double temp;

	// degrees or radians
	if (extras->flags[DEGFLAG])
		displaytorad =  (PI/180.0);// deg rad converstions


	// act on a given key
	switch (id) {
		// cm > in
		case R1C1:
			if (alt)
			theStack[0] *= 2.54;
			else
			theStack[0] /= 2.54;

			rtnState = NOACTION;
			break;

		// m > ft
		case R1C2:
			if (alt)
			theStack[0] /= 3.280833;
			else
			theStack[0] *= 3.280833;

			rtnState = NOACTION;
			break;

		// km > mi
		case R1C3:
			if (alt)
			theStack[0] /= 0.62137;
			else
			theStack[0] *= 0.62137;
			rtnState = NOACTION;
			break;

		// C > F
		case R2C1:
			if (alt)
			theStack[0] = (theStack[0] - 32.0)*5.0/9.0;
			else
			theStack[0] = theStack[0]*9.0/5.0 + 32.0;
			rtnState = NOACTION;
			break;
	
		// liter > gal
		case R2C2:
			if (alt)
			theStack[0] /= 0.26417762;
			else
			theStack[0] *= 0.26417762;
			rtnState = NOACTION;
			break;

		// cc > floz
		case R2C3:
			if (alt)
			theStack[0] *= 29.5737;
			else
			theStack[0] /= 29.5737;
			rtnState = NOACTION;
			break;


		// gm > oz (avoirdupois)
		case R3C1:
			if (alt)
			theStack[0] *= 28.349523;
			else
			theStack[0] /= 28.349523;
			rtnState = NOACTION;
			break;

		// kg > lbm
		case R3C2:
			if (alt)
			theStack[0] /= 2.2046223;
			else
			theStack[0] *= 2.2046223;
			rtnState = NOACTION;
			break;

		// N > lb
		case R3C3: 
			if (alt)
			theStack[0] *= 4.448;
			else
			theStack[0] /= 4.448;
			rtnState = NOACTION;
			break;

		// cm2 > in2
		case R4C1: 
			if (alt)
			theStack[0] *= 2.54*2.54;
			else
			theStack[0] /= 2.54*2.54;
			rtnState = NOACTION;
			break;

		// m2 > ft2
		case R4C2: 
			if (alt)
			theStack[0] /= 3.280833*3.280833;
			else
			theStack[0] *= 3.280833*3.280833;
			rtnState = NOACTION;
			break;

		// in2 > ft2
		case R4C3:
			if (alt)
			theStack[0] *= 144;
			else
			theStack[0] /= 144;
			rtnState = NOACTION;
			break;


		// cm3>in3
		case R5C1:
			if (alt)
			theStack[0] *= 2.54*2.54*2.54;
			else
			theStack[0] /= 2.54*2.54*2.54;
			rtnState = NOACTION;
			break;

		// in3>gal
		case R5C2:
			if (alt)
			theStack[0] *= 231.0;
			else
			theStack[0] /= 231.0;
			rtnState = NOACTION;
			break;

		// in3>ft3
		case R5C3:
			if (alt)
			theStack[0] *= 12.0*12.0*12.0;
			else
			theStack[0] /= 12.0*12.0*12.0;
			rtnState = NOACTION;
			break;

		// oz>lb
		case R6C1:
			if (alt)
			theStack[0] *= 16.0;
			else
			theStack[0] /= 16.0;
			rtnState = NOACTION;
			break;

		// ozfl>gal
		case R6C2:
			if (alt)
			theStack[0] *= 128.0;
			else
			theStack[0] /= 128.0;
			rtnState = NOACTION;
			break;

	}
	return rtnState;
};