#ifndef __OOL_XIO_H__
#define __OOL_XIO_H__

/*===========================================================================*/
/* OOL ------------------- the Open Object Library ------------- OS/2 Beta 1 */
/*===========================================================================*/
/*                              class: XIO                                   */
/*                       derived from: XObject                               */
/*                        last update: 9/96                                  */
/*                      programmed by: Stefan von Brauk (sbrauk@gwdg.de)     */
/*===========================================================================*/

#include "xobject.h"
#include "xstring.h"
#include "xdate.h"
#include "xtime.h"


#ifdef OOL_FOR_OS2_X86
   //seek relative positions
   #define XFILE_BEGIN                   0
   #define XFILE_CURRENT                 1
   #define XFILE_END                     2
#endif


class _export_ XIO: public XObject
{
   protected:
      HFILE handle;
      ULONG pointerPos;
      XIO() { handle = NULLHANDLE; pointerPos = 0;}
      ~XIO() { if(handle) Close();}
   public:
      virtual void Close() { DosClose(handle); handle = NULLHANDLE; }
      BOOL IsOpen() const { return handle!=NULLHANDLE ? TRUE : FALSE; }
      ULONG GetPointerPos( void ) const { return pointerPos; }
      virtual ULONG Read( PVOID buffer, const ULONG sizeofbuffer)
            {
               ULONG r;
               DosRead(handle, buffer, sizeofbuffer, &r);
               return r;
            }
      ULONG Read( char& value ) { return Read( &value, sizeof(char) ); }
      ULONG Read( SHORT& value ) { return Read( &value, sizeof(SHORT) ); }
      ULONG Read( LONG& value ) { return Read( &value, sizeof(LONG) ); }
      ULONG Read( XDate& date ) { return Read( (PVOID) &date.d, sizeof(date.d) ); }
      ULONG Read( XTime& time ) { return Read( (PVOID) &time.t, sizeof(time.t) ); }
      ULONG Read( XString& string, ULONG length)
	         {
               ULONG r = Read( (PVOID) string.GetBuffer( (ULONG) length ), length);
               string.ReleaseBuffer(r-1);
               return r;
            }
      ULONG Seek( const LONG pos, const ULONG relativePos = XFILE_BEGIN){ return DosSetFilePtr(handle, pos, relativePos, &pointerPos); }
      virtual ULONG Write( const PVOID buffer, const ULONG sizeofbuffer)
            {
               ULONG r;
               DosWrite(handle, buffer, sizeofbuffer, &r);
               return r;
            }
      ULONG Write( char value ) { return Write( &value, sizeof(char) ); }
      ULONG Write( SHORT value ) { return Write( &value, sizeof(SHORT) ); }
      ULONG Write( LONG value ) { return Write( &value, sizeof(LONG) ); }
      ULONG Write( XDate& date ) { return Write( &date.d, sizeof(date.d) ); }
      ULONG Write( XTime& time ) { return Write( &time.t, sizeof(time.t) ); }
      ULONG Write( XString& string ) { return Write( (const PVOID) (char*) string, string.GetLength()+1); }
};

#endif
