libgpr2_24.0.0_eda3c693/src/lib/gpr2-environment.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
--
--  Copyright (C) 2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-Exception
--

with Ada.Environment_Variables;

package body GPR2.Environment is

   ------------
   -- Exists --
   ------------

   function Exists (Environment : Object; Key : String) return Boolean is
   begin
      return Environment.Dict.Contains (Key)
        or else (Environment.Inherit_Process_Env
                 and then Ada.Environment_Variables.Exists (Key));

   end Exists;

   ------------
   -- Insert --
   ------------

   procedure Insert
     (Environment : in out Object; Key : String; Value : String) is
      use GNATCOLL.OS.Process.Env_Dicts;

      C : constant Cursor := Environment.Dict.Find (Key);
   begin
      if C = No_Element then
         Insert (Environment.Dict, Key, Value);
      else
         Replace_Element (Environment.Dict, C, Value);
      end if;
   end Insert;

   -----------------
   -- Set_Inherit --
   -----------------

   procedure Set_Inherit (Environment : in out Object; Inherit : Boolean) is
   begin
      Environment.Inherit_Process_Env  := Inherit;
   end Set_Inherit;

   -----------
   -- Value --
   -----------

   function Value (Environment : Object; Key : String) return String is
      use GNATCOLL.OS.Process.Env_Dicts;

      C : constant Cursor := Environment.Dict.Find (Key);
   begin
      if C /= No_Element then
         return  Element (C);
      elsif Environment.Inherit_Process_Env  then
         return Ada.Environment_Variables.Value (Key);
      else
         raise Constraint_Error;
      end if;
   end Value;

   function Value
     (Environment : Object; Key : String; Default : String) return String is
   begin
      if Exists (Environment, Key) then
         return Value (Environment, Key);
      else
         return Default;
      end if;
   end Value;

end GPR2.Environment;