ada_toml_0.3.0_862d230b/src/toml-file_io.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
with Ada.Exceptions;

with TOML.Generic_Dump;
with TOML.Generic_Parse;

package body TOML.File_IO is

   procedure Get
     (Stream : in out Ada.Text_IO.File_Type;
      EOF    : out Boolean;
      Byte   : out Character);
   --  Callback for Parse_File

   function Parse_File is new TOML.Generic_Parse
     (Input_Stream => Ada.Text_IO.File_Type,
      Get          => Get);
   
   procedure Put_To_File (File : in out Ada.Text_IO.File_Type; Bytes : String);
   --  Callback for TOML.Generic_Dump

   procedure Dump_To_File is new TOML.Generic_Dump
     (Output_Stream => Ada.Text_IO.File_Type,
      Put           => Put_To_File);   

   ---------
   -- Get --
   ---------

   procedure Get
     (Stream : in out Ada.Text_IO.File_Type;
      EOF    : out Boolean;
      Byte   : out Character) is
   begin
      EOF := False;
      Ada.Text_IO.Get_Immediate (Stream, Byte);
   exception
      when Ada.Text_IO.End_Error =>
         EOF := True;
   end Get;   

   -----------------
   -- Put_To_File --
   -----------------

   procedure Put_To_File (File : in out Ada.Text_IO.File_Type; Bytes : String)
   is
   begin
      Ada.Text_IO.Put (File, Bytes);
   end Put_To_File;

   ---------------
   -- Load_File --
   ---------------

   function Load_File (Filename : String) return Read_Result is
      use Ada.Exceptions, Ada.Text_IO;

      File : File_Type;
   begin
      begin
         Open (File, In_File, Filename);
      exception
         when Exc : Name_Error | Use_Error =>
            return Create_Error
              ("cannot open " & Filename & ": " & Exception_Message (Exc),
               No_Location);
      end;

      return Result : constant Read_Result := Parse_File (File) do
         Close (File);
      end return;
   end Load_File;

   ------------------
   -- Dump_To_File --
   ------------------

   procedure Dump_To_File
     (Value : TOML_Value; File : in out Ada.Text_IO.File_Type) is
   begin
      Dump_To_File (File, Value);
   end Dump_To_File;   
   
end TOML.File_IO;