/*===========================================================================
 *
 * File:	DL_Dos.CPP
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	Monday, May 21, 2001
 *
 * Implements DOS related functions.
 *
 *=========================================================================*/

	/* Include Files */
#include "dos/dl_dos.h"
#include "dl_str.h"
#include "dl_chr.h"
#include <limits.h>


/*===========================================================================
 *
 * Begin Local Variable Definitions
 *
 *=========================================================================*/
  DEFINE_FILE();
/*===========================================================================
 *		End of Local Variable Definitions
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean DOSInputFloat (Value, pPrompt);
 *
 * Inputs a floating point value from stdin. Returns FALSE on any error.  
 * If pPrompt is not NULL, that text is displayed before retrieving input 
 * string. The input must be a valid floating point number for the function
 * to return TRUE.  Accepts numbers up to 255 bytes in size.
 *
 *=========================================================================*/
boolean DOSInputFloat (float& Value, const char* pPrompt) {
  //DEFINE_FUNCTION("DOSInputFloat()");
  char    InputBuffer[256];
  char*   pParseErr;
  boolean Result;

	/* Attempt to input string */
  Result = DOSInputString(InputBuffer, 256, pPrompt);
  if (!Result) return (FALSE);

	/* Attempt to convert input to floating point */
  Value = (float) strtod(trim(InputBuffer), &pParseErr);

  if (*pParseErr != NULL_CHAR) {
    ErrorHandler.AddError(ERR_BADINPUT, "Failed to convert '%s' to a floating point number!", InputBuffer);
    return (FALSE);
   }

  return (TRUE);
 }
/*===========================================================================
 *		End of Function DOSInputFloat()
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean DOSInputInt (Value, pPrompt);
 *
 * Inputs an integer value from stdin. Returns FALSE on any error.  
 * If pPrompt is not NULL, that text is displayed before retrieving input 
 * string. The input must be a valid floating point number for the function
 * to return TRUE.  Accepts numbers up to 255 bytes in size.
 *
 *=========================================================================*/
boolean DOSInputInt (int& Value, const char* pPrompt) {
  //DEFINE_FUNCTION("DOSInputInt()");
  long    lValue;
  boolean Result;

	/* Attempt to input string */
  Result = DOSInputLong(lValue, pPrompt);
  if (!Result) return (FALSE);

	/* Check input value */
  if (lValue > (long)INT_MAX || lValue < (long)INT_MIN) {
    ErrorHandler.AddError(ERR_BADINPUT, "The number '%ld' is not a valid integer!", lValue);
    return (FALSE);
   }

  Value = (int) lValue;
  return (TRUE);
 }
/*===========================================================================
 *		End of Function DOSInputInt()
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean DOSInputLong (Value, pPrompt);
 *
 * Inputs an long integer value from stdin. Returns FALSE on any error.  
 * If pPrompt is not NULL, that text is displayed before retrieving input 
 * string. The input must be a valid long integer for the function
 * to return TRUE.  Accepts numbers up to 255 bytes in size.
 *
 *=========================================================================*/
boolean DOSInputLong (long& Value, const char* pPrompt) {
  //DEFINE_FUNCTION("DOSInputLong()");
  char    InputBuffer[256];
  char*   pParseErr;
  boolean Result;

	/* Attempt to input string */
  Result = DOSInputString(InputBuffer, 256, pPrompt);
  if (!Result) return (FALSE);

	/* Attempt to convert input to a long */
  Value = strtol(trim(InputBuffer), &pParseErr, 0);

  if (*pParseErr != NULL_CHAR) {
    ErrorHandler.AddError(ERR_BADINPUT, "Failed to convert '%s' to a long integer!", InputBuffer);
    return (FALSE);
   }

  return (TRUE);
 }
/*===========================================================================
 *		End of Function DOSInputLong()
 *=========================================================================*/


/*===========================================================================
 *
 * Function - boolean DOSInputString (pInputBuffer, MaxSize,  pPrompt);
 *
 * Inputs a string up to MaxSize bytes in size from stdin into the given 
 * buffer. Returns FALSE on any error.  If pPrompt is not NULL, that text
 * is displayed before retrieving input string.  The input buffer does
 * not contain the ending LF/CR character.
 *
 *=========================================================================*/
boolean DOSInputString (char* pInputBuffer, const size_t MaxSize, const char* pPrompt) {
  DEFINE_FUNCTION("DOSInputString()");
  char* pResult;

	/* Ensure valid input */
  ASSERT(pInputBuffer != NULL && MaxSize > 0);
  if (pPrompt != NULL) printf("%s", pPrompt);

	/* Attempt to input string */
  pResult = fgets(pInputBuffer, (int)MaxSize, stdin);

  if (pResult == NULL) {
    ErrorHandler.AddError(ERR_SYSTEM, errno, "Failed to input string from user!");
    return (FALSE);
   }

	/* Remove the newline character from end of string */
  chrdellast(pInputBuffer);
  return (TRUE);
 }
/*===========================================================================
 *		End of Function DOSInputString()
 *=========================================================================*/