]>
Commit | Line | Data |
---|---|---|
19235870 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- P R J . E X T -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
fbf5a39b | 9 | -- Copyright (C) 2000-2003 Free Software Foundation, Inc. -- |
19235870 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
13 | -- ware Foundation; either version 2, or (at your option) any later ver- -- | |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- | |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
18 | -- Public License distributed with GNAT; see file COPYING. If not, write -- | |
19 | -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- | |
20 | -- MA 02111-1307, USA. -- | |
21 | -- -- | |
22 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 23 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
19235870 RK |
24 | -- -- |
25 | ------------------------------------------------------------------------------ | |
26 | ||
fbf5a39b AC |
27 | with Namet; use Namet; |
28 | with Osint; use Osint; | |
29 | with Prj.Com; use Prj.Com; | |
30 | with Types; use Types; | |
31 | ||
19235870 RK |
32 | with GNAT.HTable; |
33 | with GNAT.OS_Lib; use GNAT.OS_Lib; | |
19235870 RK |
34 | |
35 | package body Prj.Ext is | |
36 | ||
37 | package Htable is new GNAT.HTable.Simple_HTable | |
38 | (Header_Num => Header_Num, | |
fbf5a39b AC |
39 | Element => Name_Id, |
40 | No_Element => No_Name, | |
19235870 RK |
41 | Key => Name_Id, |
42 | Hash => Hash, | |
43 | Equal => "="); | |
fbf5a39b AC |
44 | -- External references are stored in this hash table, either by procedure |
45 | -- Add (directly or through a call to function Check) or by function | |
46 | -- Value_Of when an environment variable is found non empty. Value_Of | |
47 | -- first for external reference in this table, before checking the | |
48 | -- environment. Htable is emptied (reset) by procedure Reset. | |
19235870 RK |
49 | |
50 | --------- | |
51 | -- Add -- | |
52 | --------- | |
53 | ||
54 | procedure Add | |
55 | (External_Name : String; | |
56 | Value : String) | |
57 | is | |
58 | The_Key : Name_Id; | |
fbf5a39b | 59 | The_Value : Name_Id; |
19235870 RK |
60 | |
61 | begin | |
fbf5a39b AC |
62 | Name_Len := Value'Length; |
63 | Name_Buffer (1 .. Name_Len) := Value; | |
64 | The_Value := Name_Find; | |
19235870 RK |
65 | Name_Len := External_Name'Length; |
66 | Name_Buffer (1 .. Name_Len) := External_Name; | |
fbf5a39b | 67 | Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len)); |
19235870 RK |
68 | The_Key := Name_Find; |
69 | Htable.Set (The_Key, The_Value); | |
70 | end Add; | |
71 | ||
72 | ----------- | |
73 | -- Check -- | |
74 | ----------- | |
75 | ||
76 | function Check (Declaration : String) return Boolean is | |
77 | begin | |
78 | for Equal_Pos in Declaration'Range loop | |
19235870 RK |
79 | if Declaration (Equal_Pos) = '=' then |
80 | exit when Equal_Pos = Declaration'First; | |
81 | exit when Equal_Pos = Declaration'Last; | |
82 | Add | |
83 | (External_Name => | |
84 | Declaration (Declaration'First .. Equal_Pos - 1), | |
85 | Value => | |
86 | Declaration (Equal_Pos + 1 .. Declaration'Last)); | |
87 | return True; | |
88 | end if; | |
19235870 RK |
89 | end loop; |
90 | ||
91 | return False; | |
92 | end Check; | |
93 | ||
fbf5a39b AC |
94 | ----------- |
95 | -- Reset -- | |
96 | ----------- | |
97 | ||
98 | procedure Reset is | |
99 | begin | |
100 | Htable.Reset; | |
101 | end Reset; | |
102 | ||
19235870 RK |
103 | -------------- |
104 | -- Value_Of -- | |
105 | -------------- | |
106 | ||
107 | function Value_Of | |
108 | (External_Name : Name_Id; | |
fbf5a39b AC |
109 | With_Default : Name_Id := No_Name) |
110 | return Name_Id | |
19235870 | 111 | is |
fbf5a39b AC |
112 | The_Value : Name_Id; |
113 | Name : String := Get_Name_String (External_Name); | |
19235870 RK |
114 | |
115 | begin | |
fbf5a39b AC |
116 | Canonical_Case_File_Name (Name); |
117 | Name_Len := Name'Length; | |
118 | Name_Buffer (1 .. Name_Len) := Name; | |
119 | The_Value := Htable.Get (Name_Find); | |
19235870 | 120 | |
fbf5a39b | 121 | if The_Value /= No_Name then |
19235870 RK |
122 | return The_Value; |
123 | end if; | |
124 | ||
125 | -- Find if it is an environment. | |
126 | -- If it is, put the value in the hash table. | |
127 | ||
128 | declare | |
fbf5a39b | 129 | Env_Value : String_Access := Getenv (Name); |
19235870 RK |
130 | |
131 | begin | |
132 | if Env_Value /= null and then Env_Value'Length > 0 then | |
fbf5a39b AC |
133 | Name_Len := Env_Value'Length; |
134 | Name_Buffer (1 .. Name_Len) := Env_Value.all; | |
135 | The_Value := Name_Find; | |
19235870 | 136 | Htable.Set (External_Name, The_Value); |
fbf5a39b | 137 | Free (Env_Value); |
19235870 RK |
138 | return The_Value; |
139 | ||
140 | else | |
fbf5a39b | 141 | Free (Env_Value); |
19235870 RK |
142 | return With_Default; |
143 | end if; | |
144 | end; | |
145 | end Value_Of; | |
146 | ||
147 | end Prj.Ext; |