| 1 |
with Ada.Exceptions; |
| 2 |
|
| 3 |
with TOML.Generic_Dump; |
| 4 |
with TOML.Generic_Parse; |
| 5 |
|
| 6 |
package body TOML.File_IO is |
| 7 |
|
| 8 |
procedure Get |
| 9 |
(Stream : in out Ada.Text_IO.File_Type; |
| 10 |
EOF : out Boolean; |
| 11 |
Byte : out Character); |
| 12 |
-- Callback for Parse_File |
| 13 |
|
| 14 |
function Parse_File is new TOML.Generic_Parse |
| 15 |
(Input_Stream => Ada.Text_IO.File_Type, |
| 16 |
Get => Get); |
| 17 |
|
| 18 |
procedure Put_To_File (File : in out Ada.Text_IO.File_Type; Bytes : String); |
| 19 |
-- Callback for TOML.Generic_Dump |
| 20 |
|
| 21 |
procedure Dump_To_File is new TOML.Generic_Dump |
| 22 |
(Output_Stream => Ada.Text_IO.File_Type, |
| 23 |
Put => Put_To_File); |
| 24 |
|
| 25 |
--------- |
| 26 |
-- Get -- |
| 27 |
--------- |
| 28 |
|
| 29 |
procedure Get |
| 30 |
(Stream : in out Ada.Text_IO.File_Type; |
| 31 |
EOF : out Boolean; |
| 32 |
Byte : out Character) is |
| 33 |
begin |
| 34 |
EOF := False; |
| 35 |
Ada.Text_IO.Get_Immediate (Stream, Byte); |
| 36 |
exception |
| 37 |
when Ada.Text_IO.End_Error => |
| 38 |
EOF := True; |
| 39 |
end Get; |
| 40 |
|
| 41 |
----------------- |
| 42 |
-- Put_To_File -- |
| 43 |
----------------- |
| 44 |
|
| 45 |
procedure Put_To_File (File : in out Ada.Text_IO.File_Type; Bytes : String) |
| 46 |
is |
| 47 |
begin |
| 48 |
Ada.Text_IO.Put (File, Bytes); |
| 49 |
end Put_To_File; |
| 50 |
|
| 51 |
--------------- |
| 52 |
-- Load_File -- |
| 53 |
--------------- |
| 54 |
|
| 55 |
function Load_File (Filename : String) return Read_Result is |
| 56 |
use Ada.Exceptions, Ada.Text_IO; |
| 57 |
|
| 58 |
File : File_Type; |
| 59 |
begin |
| 60 |
begin |
| 61 |
Open (File, In_File, Filename); |
| 62 |
exception |
| 63 |
when Exc : Name_Error | Use_Error => |
| 64 |
return Create_Error |
| 65 |
("cannot open " & Filename & ": " & Exception_Message (Exc), |
| 66 |
No_Location); |
| 67 |
end; |
| 68 |
|
| 69 |
return Result : constant Read_Result := Parse_File (File) do |
| 70 |
Close (File); |
| 71 |
end return; |
| 72 |
end Load_File; |
| 73 |
|
| 74 |
------------------ |
| 75 |
-- Dump_To_File -- |
| 76 |
------------------ |
| 77 |
|
| 78 |
procedure Dump_To_File |
| 79 |
(Value : TOML_Value; File : in out Ada.Text_IO.File_Type) is |
| 80 |
begin |
| 81 |
Dump_To_File (File, Value); |
| 82 |
end Dump_To_File; |
| 83 |
|
| 84 |
end TOML.File_IO; |