| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 1 | -- |
| 2 | -- Copyright (C) 2017 Nico Huber <nico.h@gmx.de> |
| 3 | -- |
| 4 | -- This program is free software; you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation; either version 2 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | -- |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | -- |
| 14 | |
| 15 | with Interfaces.C; |
| 16 | with Interfaces.C.Strings; |
| Nico Huber | def89eb | 2017-08-18 17:38:55 +0200 | [diff] [blame^] | 17 | with Ada.Directories; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 18 | |
| 19 | with HW.Debug; |
| 20 | |
| 21 | use Interfaces.C; |
| 22 | use Interfaces.C.Strings; |
| 23 | |
| 24 | package body HW.File is |
| 25 | |
| 26 | READ : constant := 16#01#; |
| 27 | WRITE : constant := 16#02#; |
| 28 | |
| 29 | function c_map |
| Nico Huber | a43b1ee | 2017-07-09 15:40:25 +0200 | [diff] [blame] | 30 | (addr : out Word64; |
| 31 | path : chars_ptr; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 32 | len : Word32; |
| Nico Huber | 967dd0d | 2017-07-09 15:51:08 +0200 | [diff] [blame] | 33 | off : Word32; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 34 | mode : Word32; |
| 35 | copy : int) |
| 36 | return int; |
| 37 | pragma Import (C, c_map, "hw_file_map"); |
| 38 | |
| 39 | procedure Map |
| Nico Huber | a43b1ee | 2017-07-09 15:40:25 +0200 | [diff] [blame] | 40 | (Addr : out Word64; |
| 41 | Path : in String; |
| Nico Huber | 967dd0d | 2017-07-09 15:51:08 +0200 | [diff] [blame] | 42 | Len : in Natural := 0; |
| 43 | Offset : in Natural := 0; |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 44 | Readable : in Boolean := False; |
| 45 | Writable : in Boolean := False; |
| 46 | Map_Copy : in Boolean := False; |
| 47 | Success : out Boolean) |
| 48 | is |
| 49 | use type HW.Word32; |
| 50 | |
| 51 | cpath : chars_ptr := New_String (Path); |
| 52 | ret : constant int := c_map |
| Nico Huber | a43b1ee | 2017-07-09 15:40:25 +0200 | [diff] [blame] | 53 | (addr => Addr, |
| 54 | path => cpath, |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 55 | len => Word32 (Len), |
| Nico Huber | 967dd0d | 2017-07-09 15:51:08 +0200 | [diff] [blame] | 56 | off => Word32 (Offset), |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 57 | mode => (if Readable then READ else 0) or |
| 58 | (if Writable then WRITE else 0), |
| 59 | copy => (if Map_Copy then 1 else 0)); |
| 60 | begin |
| Nico Huber | 32f1489 | 2017-06-18 02:35:37 +0200 | [diff] [blame] | 61 | pragma Warnings(GNAT, Off, """cpath"" modified*, but* referenced", |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 62 | Reason => "Free() demands to set it to null_ptr"); |
| 63 | Free (cpath); |
| Nico Huber | 32f1489 | 2017-06-18 02:35:37 +0200 | [diff] [blame] | 64 | pragma Warnings(GNAT, On, """cpath"" modified*, but* referenced"); |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 65 | Success := ret = 0; |
| 66 | |
| 67 | pragma Debug (not Success, Debug.Put ("Mapping failed: ")); |
| 68 | pragma Debug (not Success, Debug.Put_Int32 (Int32 (ret))); |
| 69 | pragma Debug (not Success, Debug.New_Line); |
| 70 | end Map; |
| 71 | |
| Nico Huber | def89eb | 2017-08-18 17:38:55 +0200 | [diff] [blame^] | 72 | procedure Size (Length : out Natural; Path : String) |
| 73 | with |
| 74 | SPARK_Mode => Off |
| 75 | is |
| 76 | use type Ada.Directories.File_Size; |
| 77 | Res_Size : Ada.Directories.File_Size; |
| 78 | begin |
| 79 | Res_Size := Ada.Directories.Size (Path); |
| 80 | Length := Natural (Res_Size); |
| 81 | exception |
| 82 | when others => |
| 83 | Length := 0; |
| 84 | end Size; |
| 85 | |
| Nico Huber | f03ef4f | 2017-03-04 13:57:09 +0100 | [diff] [blame] | 86 | end HW.File; |