/*=========================================================================
 *
 * GENFIND.CPP - Dave Humphrey (uesp@m0use.net) - August 1999
 *
 *=======================================================================*/

	/* Include Files */
#include "genfind.h"


#undef  __FUNC__
#define __FUNC__ "CFileBlock::Destroy"
/*=========================================================================
 *
 * Class CFileBlock Destructor
 *
 *=======================================================================*/
void CFileBlock::Destroy (void) {

	/* Clear all elements to 0 initially depending on platform */
  #if defined(_WIN32)
    BlockData.attrib = FA_NORMAL;
    BlockData.name[0] = NULL_CHAR;
    BlockData.time_create = -1;
    BlockData.time_access = -1;
    BlockData.time_write = -1;
    BlockData.size = 0;
  #elif defined(__MSDOS__)
    BlockData.ff_attrib = FA_NORMAL;
    BlockData.ff_name[0] = NULL_CHAR;
    BlockData.ff_ftime = 0;
    BlockData.ff_fsize = 0;
    BlockData.ff_fdate = 0;
  #endif

 }
/*=========================================================================
 *		End of Class CFileBlock Destructor
 *=======================================================================*/


#if defined(__MSDOS__)

#undef  __FUNC__
#define __FUNC__ "CFileBlock::GetWriteTime()"
/*===========================================================================
 *
 * Class CFileBlock Method - time_t GetWriteTime (void)
 *
 * MSDOS method which converts the file block's ftime time and data data
 * into a time_t value.
 *
 *=========================================================================*/
time_t CFileBlock::GetWriteTime (void) {
  struct ftime FileTime;
  struct tm    TimeM;
  ulong	       Value;
  
	/* Create a long value and assign the ftime to it */
  Value = (BlockData.ff_ftime &0xFFFF) + ((BlockData.ff_fdate << 16) & 0xFFFF000ul)
  FileTime = Value;
  
	/* Convert ftime to a tm time, hopefully */
  TimeM.tm_sec   = (FileTime.tsec/2);
  TimeM.tm_min   = FileTime.ft_min;
  TimeM.tm_hour  = FileTime.ft_hour;
  TimeM.tm_day   = FileTime.ft_mday;
  TimeM.tm_month = FileTime.ft_month;
  TimeM.tm_year  = FileTime.ft_year + 80;
  TimeM.tm_wday  = 0;
  TimeM.tm_yday  = 0;
  TimeM.tm_isdst = 0;
	
	/* Convert the tm time to a time_t value */
  return mktime(&TimeM);
 }
/*===========================================================================
 *		End of Class Method CFileBlock::GetWriteTime()
 *=========================================================================*/
#endif


#undef  __FUNC__
#define __FUNC__ "CFindFile::CFindFile()"
/*=========================================================================
 *
 * Class CFindFile Constructor
 *
 *=======================================================================*/
CFindFile::CFindFile (void) {
  FindHandle = NULL_FIND_HANDLE;
 }
/*=========================================================================
 *		End of Class CFindFile Constructor
 *=======================================================================*/


#undef  __FUNC__
#define __FUNC__ "CFindFile::Destroy()"
/*=========================================================================
 *
 * Class CFindFile Destructor
 *
 *=======================================================================*/
void CFindFile::Destroy (void) {
	
	/* Ensure the find handle is closed */
  Close();

  FindHandle = NULL_FIND_HANDLE;
  FileBlock.Destroy();
 }
/*=========================================================================
 *		End of Class CFindFile Destructor
 *=======================================================================*/


#undef  __FUNC__
#define __FUNC__ "CFindFile::Close()"
/*=========================================================================
 *
 * Class CFindFile Method - boolean Close (void);
 *
 * Closes a file find.  Returns TRUE on success or FALSE on error.
 * Must be called once a find is finished and before another search is
 * started.
 *
 *=======================================================================*/
boolean CFindFile::Close (void) {

	/* Make sure the filehandle is currently valid */
  if (FindHandle == NULL_FIND_HANDLE) return (FALSE);

	/* For MSDOS, TurboC */
#if defined(__MSDOS__)

  FindHandle = NULL_FIND_HANDLE;
  return (TRUE);

	/* For Windows, Visual C++ */
#elif defined(_WIN32)
  int Result;

	/* Call the Visual C++ function */
  Result = _findclose (FindHandle);
  FindHandle = NULL_FIND_HANDLE;
  return ((Result != 0) ? FALSE : TRUE);

#endif

	/* Undefined system in use */
  return (FALSE);
 }
/*=========================================================================
 *		End of Class Method CFindFile::Close()
 *=======================================================================*/


#undef  __FUNC__
#define __FUNC__ "CFindFile::FindFirst()"
/*===========================================================================
 *
 * Class CFindFile Method - boolean FindFirst (pFileSpec, Attribute);
 *
 * Finds the first occurence of the given file spec and attribute.
 * Returns FALSE on any error.
 *
 *=========================================================================*/
boolean CFindFile::FindFirst (const char* pFilespec, const int Attribute) {

	/* Close the previous find, if any */
  Close();

	/* For MSDOS, TurboC */
#if defined(__MSDOS__)
  int Result;

	/* Just call the standard DOS findfirst function */
  Result = findfirst (pFilespec, FileBlock.GetBlockPtr(), Attribute);
  FindHandle = 0;
  return ((Result != 0) ? FALSE : TRUE);

	/* For Windows, Visual C++ */
#elif defined(_WIN32)

	/* Call the Visual C++ function */
  FindHandle = _findfirst(pFilespec, FileBlock.GetBlockPtr());
  return ((FindHandle == -1) ? FALSE : TRUE);

#endif

	/* Undefined system in use */
  return (FALSE);
 }
/*===========================================================================
 *		End of Class Method CFindFile::FindFirst()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "CFindFile::FindNext()"
/*===========================================================================
 *
 * Class CFindFile Method - boolean FindNext (void);
 *
 * Finds the next file after a call to FindFirst(). Returns FALSE
 * on any error or TRUE on success.
 *
 *=========================================================================*/
boolean CFindFile::FindNext (void) {
  int Result;

	/* Make sure the current find handle is valid */
  if (FindHandle == NULL_FIND_HANDLE) return (FALSE);

	/* For MSDOS, TurboC */
#if defined(__MSDOS__)

	/* Just call the standard DOS findnext function */
  Result = findnext (FileBlock.GetBlockPtr());
  return ((Result != 0) ? FALSE : TRUE);
  
	/* For Windows, Visual C++ */
#elif defined(_WIN32)

	/* Call the Visual C++ function */
  Result = _findnext (FindHandle, FileBlock.GetBlockPtr());
  return ((Result == -1) ? FALSE : TRUE);

#endif

	/* Undefined system in use */
  return (FALSE);
 }
/*===========================================================================
 *		End of Class Method CFindFile::FindNext()
 *=========================================================================*/