--------------------------------------------------------------------------------
--                                                                            --
-- Copyright (C) 2004, RISC OS Ada Library (RASCAL) developers.               --
--                                                                            --
-- This library is free software; you can redistribute it and/or              --
-- modify it under the terms of the GNU Lesser General Public                 --
-- License as published by the Free Software Foundation; either               --
-- version 2.1 of the License, or (at your option) any later version.         --
--                                                                            --
-- This library 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           --
-- Lesser General Public License for more details.                            --
--                                                                            --
-- You should have received a copy of the GNU Lesser General Public           --
-- License along with this library; if not, write to the Free Software        --
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    --
--                                                                            --
--------------------------------------------------------------------------------

-- $Author$
-- $Date$
-- $Revision$

with RASCAL.Memory;           use RASCAL.Memory;
with RASCAL.Utility;          use RASCAL.Utility;
with RASCAL.OS;               use RASCAL.OS;
with RASCAL.FileName;

with Kernel;                  use Kernel;
with Interfaces.C;            use Interfaces.C;
with System.Storage_Elements; use System.Storage_Elements;
with Ada.Strings.Fixed;       use Ada.Strings.Fixed;


package body RASCAL.CDFS is

   CDFS_ConvertDriveToDevice : constant Interfaces.C.unsigned := 16#41E80#;
   CD_DiscUsed               : constant Interfaces.C.unsigned := 16#41253#;
    
   --

   function Convert_DriveToDevice (Drive : in Integer) return Integer is

      Register : aliased Kernel.swi_regs;
      Error    : oserror_access;
   begin
      Register.R(0) := int(Drive);
      Error := Kernel.swi(CDFS_ConvertDriveToDevice,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("CDFS.Convert_DriveToDevice: " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
      return Integer(Register.R(1));
   end Convert_DriveToDevice;

   --

   procedure Get_CD_Size (Block         : in out Disc_Used_Mode_1_Type;
                          Control_Block : in out CDFS_Control_Block_Type) is

      Register : aliased Kernel.swi_regs;
      Error    : oserror_access;
   begin
      Register.R(0) := 0;
      Register.R(1) := Adr_To_Int(Block'Address);
      Register.R(2) := 0;
      Register.R(3) := 0;
      Register.R(4) := 0;
      Register.R(5) := 0;
      Register.R(6) := 0;
      Register.R(7) := Adr_To_Int(Control_Block'Address);

      Error := Kernel.swi(CD_DiscUsed,Register'Access,Register'Access);
      if Error /= null then
         pragma Debug(Report("CDFS.Get_CD_Size (1): " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Get_CD_Size;

   --

   procedure Get_CD_Size (Block         : in out Disc_Used_Mode_2_Type;
                          Control_Block : in out CDFS_Control_Block_Type) is

      Register : aliased Kernel.swi_regs;
      Error    : oserror_access;
   begin
      Register.R(0) := 1;
      Register.R(1) := Adr_To_Int(Block'Address);
      Register.R(2) := 0;
      Register.R(3) := 0;
      Register.R(4) := 0;
      Register.R(5) := 0;
      Register.R(6) := 0;
      Register.R(7) := Adr_To_Int(Control_Block'Address);
      Error := Kernel.swi(CD_DiscUsed,Register'Access,Register'Access);

      if Error /= null then
         pragma Debug(Report("CDFS.Get_CD_Size (2): " & To_Ada(Error.ErrMess)));
         OS.Raise_Error(Error);
      end if;
   end Get_CD_Size;

   --
   
end RASCAL.CDFS;
