/*===========================================================================
 *
 * File:	BuildInfo.CPP
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	Friday, December 08, 2000
 *
 * Implements the CBuildInfo class.
 *
 *=========================================================================*/
 
	/* Include Files */
#include "buildinfo.h"


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::CBuildInfo()"
/*===========================================================================
 *
 * Class CBuildInfo Constructor
 *
 *=========================================================================*/
CBuildInfo::CBuildInfo () {
  BuildNumber = 0; 
  ReleaseBuilds = 0;
  DebugBuilds = 0;
  pBuildFile = NULL;
 }
/*===========================================================================
 *		End of Class CBuildInfo Constructor
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::~CBuildInfo()"
/*===========================================================================
 *
 * Class CBuildInfo Destructor
 *
 *=========================================================================*/
CBuildInfo::~CBuildInfo () {
  Destroy();
  DestroyPointerArray(pBuildFile);
 }
/*===========================================================================
 *		End of Class CBuildInfo Destructor
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::Destroy()"
/*===========================================================================
 *
 * Class CBuildInfo Method - void Destroy (void)
 *
 *=========================================================================*/
void CBuildInfo::Destroy (void) {
  BuildNumber = 0;
  ReleaseBuilds = 0;
  DebugBuilds = 0;
 }
/*===========================================================================
 *		End of Class Method CBuildInfo::Destroy()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::LoadBuild()"
/*===========================================================================
 *
 * Class CBuildInfo Method - boolean LoadBuild (const char* pFilename)
 *
 * Attempt to load and parse a build information file.  Returns FALSE
 * on any error.
 *
 *=========================================================================*/
boolean CBuildInfo::LoadBuild (const char* pFilename) {
  FILE* pFileHandle;
  char  LineBuffer[BUILDINFO_LINE_SIZE + 1];
  int   Result;

	/* Use the default filename if none was given */
  if (pFilename == NULL) pFilename = pBuildFile;

	/* Attempt to open the file */
  pFileHandle = openfile(pFilename, "rb");
  if (pFileHandle ==  NULL) return (TRUE);
  
  	/* Destroy the current build information  */
  Destroy();

	/* Save the build filename if it has changed */
  if (pBuildFile != pFilename) SetFilename(pFilename);

	/* Read in the entire file */
  while (!feof(pFileHandle)) {

		/* Read in one line from the file */
    Result = read_eol(pFileHandle, LineBuffer, BUILDINFO_LINE_SIZE);
    if (Result == READ_MSL) read_eol(pFileHandle);

		/* Parse the line */
    Result = ParseLine(LineBuffer);
   }

  fclose (pFileHandle);
  return (TRUE);
 }
/*===========================================================================
 *		End of Class Method CBuildInfo::LoadBuild()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::ParseDefine()"
/*===========================================================================
 *
 * Class CBuildInfo Method - boolean ParseDefine (char* pString)
 *
 * Parses a #define command from the build info file.  Returns FALSE
 * on any error.  Protected class method.
 *
 *=========================================================================*/
boolean CBuildInfo::ParseDefine (char* pString) {

	/* Remove leading whitespace from command */
  pString = ltrim(pString);

  if (strncmp(pString, "BUILD_NUMBER ", 13) == 0) {
    BuildNumber = atoi(pString + 13);
    return (TRUE);
   }
  else if (strncmp(pString, "RELEASE_BUILDS ", 15) == 0) {
    ReleaseBuilds = atoi(pString + 15);
    return (TRUE);
   }
  else if (strncmp(pString, "DEBUG_BUILDS ", 13) == 0) {
    DebugBuilds = atoi(pString + 13);
    return (TRUE);
   }

	/* Unknown command */
  return (FALSE);
 }
/*===========================================================================
 *		End of Class Method CBuildInfo::ParseDefine()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::ParseLine()"
/*===========================================================================
 *
 * Class CBuildInfo Method - boolean ParseLine (char* pString)
 *
 * Parses a line input from the build info file.  Returns FALSE on any
 * error.  Protected class method.
 *
 *=========================================================================*/
boolean CBuildInfo::ParseLine (char* pString) {

	/* Remove leading and trailing whitespace */
  pString = trim(pString);

	/* Look for a 'define' statement at the start of the line */
  if (strncmp(pString, "#define ", 8) == 0) {
    return (ParseDefine(pString + 8)); 
   }

	/* Unknown command */
  return (FALSE);
 }
/*===========================================================================
 *		End of Class Method CBuildInfo::ParseLine()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CBuildInfo::SaveBuild()"
/*===========================================================================
 *
 * Class CBuildInfo Method - boolean SaveBuild (const char* pFilename)
 *
 * Saves the build information to the given file.  Returns FALSE on any
 * error.
 *
 *=========================================================================*/
boolean CBuildInfo::SaveBuild (const char* pFilename) {
  FILE* pFileHandle;
  int   Result;

	/* Change the filename if required and open file */
  if (pFilename != NULL) SetFilename(pFilename);
  pFileHandle = openfile(pBuildFile, "wb");
  if (pFileHandle == NULL) return (FALSE);

	/* Output the build information */
  Result = fprintf (pFileHandle, "#define BUILD_NUMBER %d\n", BuildNumber);
  Result = fprintf (pFileHandle, "#define RELEASE_BUILDS %d\n", ReleaseBuilds);
  Result = fprintf (pFileHandle, "#define DEBUG_BUILDS %d\n", DebugBuilds);
  
  fclose (pFileHandle);

	/* Check for any output errors */
  if (Result < 0) {
    SET_EXT_ERROR(ERR_WRITE);
    return (FALSE);
   }

  return (TRUE);
 }
/*===========================================================================
 *		End of Class Method CBuildInfo::SaveBuild()
 *=========================================================================*/