blob: dd9d7f3bc617fbf08167e28b52a1eccd0eed57eb [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)
36 return int;
37 pragma Import (C, c_map, "hw_file_map");
38
39 procedure Map
Nico Hubera43b1ee2017-07-09 15:40:25 +020040 (Addr : out Word64;
41 Path : in String;
Nico Huber967dd0d2017-07-09 15:51:08 +020042 Len : in Natural := 0;
43 Offset : in Natural := 0;
Nico Huberf03ef4f2017-03-04 13:57:09 +010044 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 Hubera43b1ee2017-07-09 15:40:25 +020053 (addr => Addr,
54 path => cpath,
Nico Huberf03ef4f2017-03-04 13:57:09 +010055 len => Word32 (Len),
Nico Huber967dd0d2017-07-09 15:51:08 +020056 off => Word32 (Offset),
Nico Huberf03ef4f2017-03-04 13:57:09 +010057 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 Huber32f14892017-06-18 02:35:37 +020061 pragma Warnings(GNAT, Off, """cpath"" modified*, but* referenced",
Nico Huberf03ef4f2017-03-04 13:57:09 +010062 Reason => "Free() demands to set it to null_ptr");
63 Free (cpath);
Nico Huber32f14892017-06-18 02:35:37 +020064 pragma Warnings(GNAT, On, """cpath"" modified*, but* referenced");
Nico Huberf03ef4f2017-03-04 13:57:09 +010065 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 Huberdef89eb2017-08-18 17:38:55 +020072 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 Huberf03ef4f2017-03-04 13:57:09 +010086end HW.File;