#include "XFileInf.h"
#include "XFile.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/*@ 
@class XFile
@parent XIO
@type overview
@symbol _
*/


/*@ XFile::GetFileInfo(XFileInfo * info)
@group misc
@remarks Query information about files. To access information the
file must be open and XFILE_SHARE_DENYWRITE must be set!
@parameters XFileInfo * info        pointer to an instance of XFileInfo
@returns    LONG                    result of the operatingsystem
*/
LONG XFile::GetFileInfo(XFileInfo * info)
{
	return DosQueryFileInfo(handle, 1, &info->buffer, sizeof(info->buffer));
}


/*@ XFile::Open(const char *filename, const ULONG modeifexist, const ULONG modeopen, const ULONG size, const EAOP2 * ealist)
@group open/close
@remarks Open a file
@parameters <t 'ø' c=2>
				øchar * path                  øthe path of the file
            øULONG modeForOpen            øhow to open, possible values are:
														<t 'ø' c=2>
                                            øXFILE_REPLACE_EXISTING  øoverride existing filea
                                            øXFILE_OPEN_EXISTING     øopen if file exists
                                            øXFILE_FAIL_EXISTING     øcancel if the file exists
                                            øXFILE_FAIL_IF_NEW       øcancel if the file doesnït exist
                                            øXFILE_CREATE_IF_NEW     øcreate a new file if it doesnït exist
														</t>
                                         (can be or-ed)
            øULONG accessMode             ømode for file-sharing and access, possible values are:
														<t 'ø' c=2>
                                            øXFILE_SHARE_DENYREAD	ø
                                            øXFILE_SHARE_DENYWRITE	ø
                                            øXFILE_SHARE_DENYREADWRITE	ø
                                            øXFILE_SHARE_DENYNONE	ø
                                            øXFILE_READONLY	ø
                                            øXFILE_WRITEONLY	ø
                                            øXFILE_READWRITE	ø
														</t>
                                         (can be or-ed)
            øULONG size                   øsize to open (only if a file is created, default is 0)
            øEAOP2 * eaList               ølist with extended attributes (default is NULL)
				</t>
@returns    ULONG                        result returned by the OS
*/
ULONG XFile::Open(const char *filename, const ULONG modeifexist, const ULONG modeopen, const ULONG size, const EAOP2 * ealist)
{
	ULONG aktion;

	return DosOpen((PSZ) filename, &handle, &aktion, size, 0, modeifexist, modeopen, (PEAOP2) ealist);
}


/*@ XFile::IsDriveAvaible(const char drive)
@group disks
@remarks Query if a drive is avaible or not
@parameters char drive     the drive (A, B, ....)
@returns    BOOL result
*/
BOOL XFile::IsDriveAvaible(const char drive)
{
	SHORT number = drive - 'A';
	ULONG u1 = 0, u2 = 0;

	DosQueryCurrentDisk(&u1, &u2);
	return ((u2 << (31 - number) >> 31) ? TRUE : FALSE);
}


/*@ XFile::GetCurrentDirectory(XString * buffer)
@group directorys
@remarks Query the current directory
@parameters XString * buffer    buffer to hold the data
*/
void XFile::GetCurrentDirectory(XString * buffer)
{
	ULONG r = 512;

	DosQueryCurrentDir(0, (PCH) buffer->GetBuffer(512), &r);
	buffer->ReleaseBuffer();
}


/*@ XFile::SetDefaultDisk(const char drive)
@group disks
@remarks Set the dault disk
@parameters char drive     the drive (A, B, ....)
*/
BOOL XFile::SetDefaultDisk(const char drive)
{
	char d = toupper(drive);
	USHORT number = d - 'A' + 1;

	if (DosSetDefaultDisk(number) == 0)
		return TRUE;
	else
		return FALSE;
}


/*@ XFile::GetCurrentDisk(char &buffer)
@group disks
@remarks Query the current disk
@parameters char&  buffer      buffer to hold data (will contain A,B,..)
*/
void XFile::GetCurrentDisk(char &buffer)
{
	ULONG u1, u2;

	DosQueryCurrentDisk(&u1, &u2);
	buffer = (char) u1 + 'A';
}


/*@ XFile::CreateDirectory(const char *path)
@group directorys
@remarks Creates a directory
@parameters char * path    path of the directory to create
*/
ULONG XFile::CreateDirectory(const char *path)
{
	return DosCreateDir((PSZ) path, NULL);
}


/*@ XFile::DeleteDirectory(const char *path)
@group directorys
@remarks Delete a directory
@parameters char * path    path of the directory to delete
*/
ULONG XFile::DeleteDirectory(const char *path)
{
	return DosDeleteDir((PSZ) path);
}

////////////////docs only
/*@ XFileDialog::GetCommand()
@group misc
@remarks Returns the action of the user
@returns    SHORT                        result: USER_OK or USER_CANCEL
*/


/*@ XFileDialog::GetFileCount()
@group misc
@remarks Returns the number of files selected
@returns    SHORT                        count of files
*/

/*@ XIO::Read(PVOID, ULONG)
@group I/O
@definition Read(PVOID, ULONG)
@remarks Read from I/O
@parameters PVOID buffer                 pointer to memory<BR>
            ULONG size                   count of bytes to read
@returns    ULONG                        count of bytes which are read
*/


/*@ XIO::Read( XString&, ULONG)
@group I/O
@definiton Read( XString&, ULONG)
@remarks Read from I/O
@parameters XString&                 string-buffer<BR>
            ULONG size                   count of bytes to read
@returns    ULONG                        count of bytes which are read
*/


/*@ XIO::Read(LONG&)
@group I/O
@definiton Read(LONG&)
@remarks Read from I/O
@parameters LONG&				buffer
@returns    ULONG                        count of bytes which are read
*/

/*@ XIO::Read(SHORT&)
@definiton Read(SHORT&)
@group I/O
@remarks Read from I/O
@parameters SHORT&				buffer
@returns    ULONG                        count of bytes which are read
*/


/*@ XIO::Read(CHAR&)
@definiton Read(CHAR&)
@group I/O
@remarks Read from I/O
@parameters CHAR&				buffer
@returns    ULONG                        count of bytes which are read
*/


/*@ XIO::Read(XTime&)
@definiton Read(XTime&)
@group I/O
@remarks Read from I/O
@parameters XTime&				buffer
@returns    ULONG                        count of bytes which are read
*/


/*@ XIO::Read(XDate&)
@definiton Read(XDate&)
@group I/O
@remarks Read from I/O
@parameters XDate&				buffer
@returns    ULONG                        count of bytes which are read
*/


/*@ XIO::Write(PVOID, ULONG)
@group I/O
@remarks Write to I/O
@parameters PVOID buffer                 pointer to memory<BR>
            ULONG size                   count of bytes to write
@returns    ULONG                        count of bytes which are written
*/

/*@ XIO::Write(XString&)
@group I/O
@remarks Writes a string to I/O including terminating NULL.
@parameters XString&				string
@returns    ULONG                        count of bytes which are written
*/

/*@ XIO::Write(LONG)
@group I/O
@remarks Write LONG to I/O
@parameters LONG 				data
@returns    ULONG                        count of bytes which are written
*/

/*@ XIO::Write(short)
@group I/O
@remarks Write SHORT to I/O
@parameters SHORT			data
@returns    ULONG                        count of bytes which are written
*/

/*@ XIO::Write(char)
@group I/O
@remarks Write CHAR to I/O
@parameters CHAR			data
@returns    ULONG                        count of bytes which are written
*/

/*@ XIO::Write(XTime&)
@group I/O
@remarks Write a time to I/O
@parameters XTime&			data in OOL-format
@returns    ULONG                        count of bytes which are written
*/


/*@ XIO::Write(XDate&)
@group I/O
@remarks Write a date to I/O
@parameters XDate&			data in OOL-format
@returns    ULONG                        count of bytes which are written
*/


/*@ XIO::Close()
@group open/close
@remarks Close I/O
*/


/*@ XIO::GetPointerPos()
@group misc
@remarks Returns the position of the file-pointer relative to the beginning of I/O. Before calling
this function you must call XIO::Seek()! (e.g. Seek(0, XFILE_CURRENT) )
@returns    ULONG                        the position
*/


/*@ XIO::Seek()
@group misc
@remarks Seek in the file
@parameters <t 'ø' c=2>
				øULONG position               øposition to seek to relative to relPos
            øULONG relPos                 øposition relative to:
														<t 'ø' c=2>
                                             øXFILE_BEGIN     ørelative to the beginning of the file (default)
                                             øXFILE_CURRENT   ørelative to current position
                                             øXFILE_END       ørelative to the end of the file
														</t>
				</t>
@returns    ULONG                        count of bytes which are read
*/


/*@ XFile::Remove()
@group misc
@remarks Delete a file
@parameters char * fileName   name of the file to delete (including path if nessecary)
@returns    BOOL success
*/


/*@ XFile::Copy()
@group misc
@remarks Copy a file
@parameters char * from    file to copy (including path if nessecary)<BR>
            char * to      destination (including path if nessecary)
@returns    ULONG result   returncode from the OS.
*/


/*@ XFile::Move()
@group misc
@remarks Move a file
@parameters char * from    file to copy (including path if nessecary)<BR>
            char * to      destination (including path if nessecary). If the same directory is given,
                           Move() renames the file.
@returns    ULONG result   returncode from the OS.
*/


/*@ XFile::ResetBuffer()
@group misc
@remarks Wait until all writen datas are physicaly saved
@returns    ULONG result   returncode from the OS.
*/
