/*
    CaPriCe Forever - Amstrad CPC 464/664/6128 emulator
    Copyright (C) 2017 by Frédéric Coste

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef TRAWContainerH
#define TRAWContainerH


//=================================
// Options
//
#undef USE_CAPSLIB
#define USE_CAPSLIB
//=================================


#ifdef USE_CAPSLIB
#include "CommonTypes.h"
#include "CapsAPI.h"
#endif /* USE_CAPSLIB */
#include "..\..\Engine\Native_CPC.h"
#include "..\..\Common\RAW.h"
#include "TRAWTrack.h"

// Additional errors
#define errRAWFileDoesNotExist            0x1101
#define errRAWBadMemoryAllocation         0x1102
#define errRAWBadStructure                0x1103
#define errRAWBadRecord                   0x1104
#define errRAWBadPlatform                 0x1105
#define errRAWCAPSBaseError               0x1200



// CAPSImg DLL Prototypes
#ifdef USE_CAPSLIB
typedef SDWORD (__cdecl *CAPSInitPtr)(void);
typedef SDWORD (__cdecl *CAPSExitPtr)(void);
typedef SDWORD (__cdecl *CAPSAddImagePtr)(void);
typedef SDWORD (__cdecl *CAPSLockImageMemoryPtr)(SDWORD id, PUBYTE buffer, UDWORD length, UDWORD flag);
typedef SDWORD (__cdecl *CAPSLoadImagePtr)(SDWORD id, UDWORD flag);
typedef SDWORD (__cdecl *CAPSGetImageInfoPtr)(PCAPSIMAGEINFO pi, SDWORD id);
typedef SDWORD (__cdecl *CAPSLockTrackPtr)(PVOID ptrackinfo, SDWORD id, UDWORD cylinder, UDWORD head, UDWORD flag);
typedef SDWORD (__cdecl *CAPSUnlockTrackPtr)(SDWORD id, UDWORD cylinder, UDWORD head);
typedef SDWORD (__cdecl *CAPSUnlockAllTracksPtr)(SDWORD id);
typedef SDWORD (__cdecl *CAPSUnlockImagePtr)(SDWORD id);
typedef SDWORD (__cdecl *CAPSRemImagePtr)(SDWORD id);
#endif /* USE_CAPSLIB */


class TRAWContainer
{
private:
  //
  // Members
  //
  TList* mTracksList;
  bool mCAPSLibPresent;
#ifdef USE_CAPSLIB
  String mCAPSLibFilename;
  HINSTANCE mCAPSLibHandle;
  CAPSInitPtr CAPSInit;
  CAPSExitPtr CAPSExit;
  CAPSAddImagePtr CAPSAddImage;
  CAPSLockImageMemoryPtr CAPSLockImageMemory;
  CAPSLoadImagePtr CAPSLoadImage;
  CAPSGetImageInfoPtr CAPSGetImageInfo;
  CAPSLockTrackPtr CAPSLockTrack;
  CAPSUnlockTrackPtr CAPSUnlockTrack;
  CAPSUnlockAllTracksPtr CAPSUnlockAllTracks;
  CAPSUnlockImagePtr CAPSUnlockImage;
  CAPSRemImagePtr CAPSRemImage;
#endif /* USE_CAPSLIB */

  //
  // Methods
  //
  tUChar* _fastcall CheckCAPSRecord(tUChar* Data, Err* Error);
  tUChar* _fastcall ReadDATARecord(tUChar* Data, Err* Error);
  tUChar* _fastcall ReadTRCKRecord(tUChar* Data, Err* Error);
	void __fastcall StrCopy(tChar* destP, tChar* srcP);
  tULong __fastcall GetDSKSize(void);
  TRAWTrack* __fastcall FindTrack(tUChar track, tUChar head);

public:
  //
  // Properties
  //
  __property tULong DSKSize = { read=GetDSKSize };

  //
  // API
  //
  __fastcall TRAWContainer();
  __fastcall ~TRAWContainer();

  Err __fastcall Load(AnsiString Filename);
  Err __fastcall Load(tUChar* FileContent, int iLength);
  tULong __fastcall BuildDSKStructure(tUChar* DSKData, tULong DSKSize);
};

//---------------------------------------------------------------------------
#endif

