blob: 9755d13953d79948fd92b72a0519ab88cb374eaf [file] [log] [blame]
Nico Huberf03ef4f2017-03-04 13:57:09 +01001--
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
15with Interfaces.C;
16with Interfaces.C.Strings;
Nico Huberdef89eb2017-08-18 17:38:55 +020017with Ada.Directories;
Nico Huberf03ef4f2017-03-04 13:57:09 +010018
19with HW.Debug;
20
21use Interfaces.C;
22use Interfaces.C.Strings;
23
24package body HW.File is
25
26 READ : constant := 16#01#;
27 WRITE : constant := 16#02#;
28
29 function c_map
Nico Hubera43b1ee2017-07-09 15:40:25 +020030 (addr : out Word64;
31 path : chars_ptr;
Nico Huberf03ef4f2017-03-04 13:57:09 +010032 len : Word32;
Nico Huber967dd0d2017-07-09 15:51:08 +020033 off : Word32;
Nico Huberf03ef4f2017-03-04 13:57:09 +010034 mode : Word32;
35 copy : int)
Nico Huber3a9d9a02026-04-01 20:50:10 +000036 return int
37 with
38 SPARK_Mode => Off;
Nico Huberf03ef4f2017-03-04 13:57:09 +010039 pragma Import (C, c_map, "hw_file_map");
40
41 procedure Map
Nico Hubera43b1ee2017-07-09 15:40:25 +020042 (Addr : out Word64;
43 Path : in String;
Nico Huber967dd0d2017-07-09 15:51:08 +020044 Len : in Natural := 0;
45 Offset : in Natural := 0;
Nico Huberf03ef4f2017-03-04 13:57:09 +010046 Readable : in Boolean := False;
47 Writable : in Boolean := False;
48 Map_Copy : in Boolean := False;
49 Success : out Boolean)
50 is
51 use type HW.Word32;
52
53 cpath : chars_ptr := New_String (Path);
54 ret : constant int := c_map
Nico Hubera43b1ee2017-07-09 15:40:25 +020055 (addr => Addr,
56 path => cpath,
Nico Huberf03ef4f2017-03-04 13:57:09 +010057 len => Word32 (Len),
Nico Huber967dd0d2017-07-09 15:51:08 +020058 off => Word32 (Offset),
Nico Huberf03ef4f2017-03-04 13:57:09 +010059 mode => (if Readable then READ else 0) or
60 (if Writable then WRITE else 0),
61 copy => (if Map_Copy then 1 else 0));
62 begin
Nico Huber32f14892017-06-18 02:35:37 +020063 pragma Warnings(GNAT, Off, """cpath"" modified*, but* referenced",
Nico Huberf03ef4f2017-03-04 13:57:09 +010064 Reason => "Free() demands to set it to null_ptr");
65 Free (cpath);
Nico Huber32f14892017-06-18 02:35:37 +020066 pragma Warnings(GNAT, On, """cpath"" modified*, but* referenced");
Nico Huberf03ef4f2017-03-04 13:57:09 +010067 Success := ret = 0;
68
69 pragma Debug (not Success, Debug.Put ("Mapping failed: "));
70 pragma Debug (not Success, Debug.Put_Int32 (Int32 (ret)));
71 pragma Debug (not Success, Debug.New_Line);
72 end Map;
73
Nico Huberdef89eb2017-08-18 17:38:55 +020074 procedure Size (Length : out Natural; Path : String)
75 with
76 SPARK_Mode => Off
77 is
78 use type Ada.Directories.File_Size;
79 Res_Size : Ada.Directories.File_Size;
80 begin
81 Res_Size := Ada.Directories.Size (Path);
82 Length := Natural (Res_Size);
83 exception
84 when others =>
85 Length := 0;
86 end Size;
87
Nico Huberf03ef4f2017-03-04 13:57:09 +010088end HW.File;