/*=========================================================================
 *
 * GENUTIL.H - 29 March 1999, Dave Humphrey (uesp@m0use.net)
 *
 *=======================================================================*/
#ifndef __GENUTIL_H
#define __GENUTIL_H

#ifdef _DEBUG
#ifdef _WIN32
  //#define _CRTDBG_MAP_ALLOC 
  #include "CRTDBG.H "
#endif
#endif


	/* Required Include Files */
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <time.h>


/*===========================================================================
 *
 * Begin Defines
 *
 *=========================================================================*/

	/* Program identification */
  #define GENUTIL_NAME    "GENUTIL.CPP"
  #define GENUTIL_VERSION "1.0"
  #define GENUTIL_AUTHOR  "Dave Humphrey (uesp@m0use.net)"
  #define GENUTIL_DATE    "29 March 1999"

	/* Define the NUL and other characters */
  #define NULL_CHAR '\0'
  #define DEL_CHAR  '\b'
  #define ESC_CHAR  '\x1B'
  #define CR_CHAR   '\n'
  #define LF_CHAR   '\r'

	/* Standard comment character in INI/CFG files etc... */
  #define DEFAULT_COMMENT_CHAR '#'

	/* Exchanges two numbers (must be integers) fast via XOR */
  #define SWAP(x, y) { x ^= y; y ^= x; x ^= y; }

	/* Constants rounded to 21 decimals */
#ifndef M_PI
  #define M_E         2.71828182845904523536
  #define M_LOG2E     1.44269504088896340736
  #define M_LOG10E    0.434294481903251827651
  #define M_LN2       0.693147180559945309417
  #define M_LN10      2.30258509299404568402
  #define M_PI        3.14159265358979323846
  #define M_PI_2      1.57079632679489661923
  #define M_PI_4      0.785398163397448309616
  #define M_1_PI      0.318309886183790671538
  #define M_2_PI      0.636619772367581343076
  #define M_1_SQRTPI  0.564189583547756286948 
  #define M_2_SQRTPI  1.12837916709551257390
  #define M_SQRT2     1.41421356237309504880
  #define M_SQRT_2    0.707106781186547524401
#endif

	/* Standard variable checking routines */
  #define CHECK_FILE(pHandle, FailReturn) { if (pHandle == NULL) { SET_EXT_ERROR2(ERR_NULL, "Invalid NULL file handle received!"); return (FailReturn); } }
  #define CHECK_FILE_NR(pHandle) CHECK_FILE(pHandle, ;)
  #define CHECK_POINTER(pPointer, FailReturn) { if (pPointer == NULL) { SET_EXT_ERROR2(ERR_NULL, "Invalid NULL pointer received!"); return (FailReturn); } }
  #define CHECK_POINTER_NR(pPointer) CHECK_POINTER(pPointer, ;)
  #define CHECK_INDEX(Index, MaxIndex, FailReturn) { if ((Index) < 0 || (Index) >= (MaxIndex)) { SET_EXT_ERROR4(ERR_INDEX, "Invalid index %d received for a valid range of 0 to %d!", (Index), (MaxIndex)); return (FailReturn); } }
  #define CHECK_INDEX_NR(Index, MaxIndex) CHECK_INDEX(Index, MaxIndex, ;)
  #define CHECK_VALIDINDEX(Index, MaxIndex, FailReturn) { if (!IsValidIndex(Index)) { SET_EXT_ERROR4(ERR_INDEX, "Invalid index %d received for a valid range of 0 to %d!", (Index), (MaxIndex)); return (FailReturn); } }
  #define CHECK_MAXINDEX(Index, MaxIndex, FailReturn) if ((Index) >= ((MaxIndex) - 1)) { SET_EXT_ERROR3(ERR_MAXINDEX, "Exceeded array limit of %d items!", (MaxIndex)); return (FailReturn); }
  #define CHECK_NULLPOINTER(pVar, FailReturn) if ((pVar) == NULL) { SET_EXT_ERROR3(ERR_NULL, "Received an invalid NULL pointer (%s)", #pVar); return (FailReturn); }
  #define DELETE_POINTERARRAY(pVar, MaxIndex) { for (int LoopCounter = 0; LoopCounter < MaxIndex; LoopCounter++) { DestroyPointer(pVar[LoopCounter]); } MaxIndex = 0; }

  #define IMPLEMENT_ISVALIDINDEX(MaxIndex) boolean IsValidIndex (const int Index) { return ((Index >= 0 && Index < (MaxIndex)) ? TRUE : FALSE); }
  #define IMPLEMENT_SETSTRING(Method, pVar) void Method (const char* pString) { DestroyPointerArray(pVar); (pVar) = CreateString(pString); }
  #define IMPLEMENT_SETSTRING1(VarName)   IMPLEMENT_SETSTRING(Set##VarName, p##VarName)
		
/*===========================================================================
 *		End of Defines
 *=========================================================================*/


/*=========================================================================
 *
 * Begin Type Definitions
 *
 *=======================================================================*/

	/* Defines the standard boolean type if not previously defined. 
	 * Use unsigned char to match that used in MSVC++. */
#ifndef __RPCNDR_H__
  #define __BOOLEAN_DEF
  typedef unsigned char boolean;  

  #ifndef TRUE
    #define TRUE 1
    #define FALSE 0
  #endif

#endif


	/* Basic unsigned types */
  typedef unsigned char  byte;
  typedef unsigned char  uchar;
  typedef unsigned long  ulong;
  typedef unsigned short ushort;
  typedef unsigned int   uint;

/*=========================================================================
 *		End of Type Definitions
 *=======================================================================*/


/*===========================================================================
 *
 * Begin External Variable Definitions
 *
 *=========================================================================*/
  
/*===========================================================================
 *	      End of External Variable Definitions
 *=========================================================================*/


/*===========================================================================
 *
 * Begin Forward Function and Procedure Declarations
 *
 * For more information on these routines, see the file GENUTIL.CPP, as well
 * as possibly any struct/class used
 *
 *=========================================================================*/

	/* Return the month abbrievation of the zero-based month index (0=Jan) */
  char *GetMonthAbbr (const int MonthIndex);

	/* Return the month name of the zero-based month index (0=January) */
  char *GetMonthName (const int MonthIndex);

	/* Returns TRUE if the Character is One of the Comment Chars */
  boolean IsComment (const char ch);

	/* Returns TRUE if the character is a punctuation type character.
	 * ',.?"[];:' etc... */
  boolean IsExtPunct (const unsigned char ch);
  boolean IsPunctuation (const unsigned char ch);

	/* Parses a 'Var = Value' type string */
  boolean ParseVarValue (char* pString, char** pVar, char** pValue);

	/* Returns 2 to the power of p */
  unsigned long powl2 (const int p);
  unsigned int pow2 (const int p);

	/* Removes any simple comments from the given string */
  char* RemoveComments (char* pString, const char CommentChar = DEFAULT_COMMENT_CHAR);

	/* Returns the sign of the Expression (-1/0/1) */
  int sign (const int i);

	/* Swaps two values via XOR */
  void swap (int &i1, int &i2);

/*===========================================================================
 *		End of Forward Function and Procedure Declarations
 *=========================================================================*/


	/* More required Includes which depend on the above definitions */
#include "genmem.h"
#include "genstrin.h"
#include "generr.h"
#include "fileutil.h"

#endif
/*=========================================================================
 *		End of File GENUTIL.H
 *=======================================================================*/