alr2appimage_0.9.3_fba60fa7/src/alire_toml.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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
with Ada.Directories;
with Ada.Text_IO;
with Ada.Strings.Unbounded;

with TOML.File_IO;

package body Alire_TOML is

   use TOML;

   Filename : constant String := "alire.toml";

   Config : TOML_Value;

   function Read_Field (Key : String; Default : String := "") return String is
   begin
      if Config /= No_TOML_Value and then
        Config.Has (Key => Key)
      then

         return Config.Get (Key).As_String;
      else
         return Default;
      end if;

   exception
      when others =>

         Ada.Text_IO.Put_Line (Ada.Text_IO.Standard_Error,
                               "Error: invalid format for " &
                                Key & " in " & Filename);
         return Default;
   end Read_Field;

   function Read_String_List
     (Key :  String) return String_Vectors.Vector is

      Default : String_Vectors.Vector renames String_Vectors.Empty_Vector;
      Value : String_Vectors.Vector := Default;
   begin

      if Config /= No_TOML_Value and then
        Config.Has (Key => Key)
      then
         declare
            TOML_Tags : TOML.TOML_Value renames Config.Get (Key);
         begin
            for Index in 1 .. TOML_Tags.Length loop
               String_Vectors.Container.Append (Value, TOML_Tags.Item (Index).As_String);
            end loop;
         end;
      end if;

      return Value;

   exception
      when others =>

         Ada.Text_IO.Put_Line (Ada.Text_IO.Standard_Error,
                               "Error: invalid format for " &
                                 Key & " in " & Filename);
         return Default;

   end Read_String_List;

   procedure Load_Alire is
   begin

      if not Ada.Directories.Exists (Filename) then

         Ada.Text_IO.Put_Line
           (Ada.Text_IO.Standard_Error,
            "Error: not inside an Alire workspace (alire.toml not found)");

         raise Load_Error;
      end if;

      declare
         Result : constant TOML.Read_Result :=
           TOML.File_IO.Load_File (Filename);
      begin
         if Result.Success then
            Config := Result.Value;
         else
            Ada.Text_IO.Put (Ada.Text_IO.Standard_Error,
                             "Error: while loading " & Filename & ":" &
                               Result.Location.Line'Image & ":" &
                               Result.Location.Column'Image & ": ");
            Ada.Text_IO.Put_Line
              (Ada.Text_IO.Standard_Error,
               Ada.Strings.Unbounded.To_String (Result.Message));

            raise Load_Error with Ada.Strings.Unbounded.To_String (Result.Message);

         end if;
      end;

   end Load_Alire;

end Alire_TOML;