#include "XFileFi.h"


/*@ XFileFind :: XFileFind(const char *m, const LONG o)
@group constructors/destructors
@remarks Construct a XFileFind-instance (do not forget to call the destructor)
@parameters <t 'ø' c=2>
				øchar * fileMask              ømask for the files to find(e.g. "*.EXE" )
            øLONG options                 øoptions to search, valid values are:
															<t 'ø' c=1>
                                             øXFILE_NORMAL
                                             øXFILE_READONLY
                                             øXFILE_HIDDEN
                                             øXFILE_SYSTEM
                                             øXFILE_DIRECTORY
                                             øXFILE_ARCHIVED
                                             øXMUST_HAVE_READONLY
                                             øXMUST_HAVE_HIDDEN
                                             øXMUST_HAVE_SYSTEM
                                             øXMUST_HAVE_DIRECTORY
                                             øXMUST_HAVE_ARCHIVED
															</t>
			</t>
*/
XFileFind :: XFileFind(const char *m, const LONG o)
{
	init = TRUE;
	mask = m;
	options = o;
	dir = HDIR_SYSTEM;
}


/*@ XFileFind::Find(XString * string)
@group find files
@remarks After you have created an instance you can start to find files. Find()
copy the filename found to the given bufer and returns TRUE, it returns FALSE if no file is found.
@parameters XString * buffer     buffer to hold the filename
@returns BOOL
*/
BOOL XFileFind::Find(XString * string)
{
	LONG res;
	ULONG count = 1;

	if (init)
	{
		res = DosFindFirst((PSZ) (char *) mask, &dir, options, &buffer, sizeof(buffer), &count, 1);
		init = FALSE;
	}
	else
		res = DosFindNext(dir, &buffer, sizeof(buffer), &count);
	if (res == 0)
	{
		memcpy(string->GetBuffer(buffer.cchName), buffer.achName, buffer.cchName);
		string->ReleaseBuffer(buffer.cchName);
		return TRUE;
	}
	else
		return FALSE;
}


/*@ XFileFind :: ~XFileFind()
@group constructors/destructors
@remarks Destroys a XFileFind-instance. Never forget to call the destructor!
*/
XFileFind :: ~XFileFind()
{
	DosFindClose(dir);
}
