1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--         Localization, Internationalization, Globalization for Ada        --
6--                                                                          --
7--                              Tools Component                             --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2011-2012, Vadim Godunko <vgodunko@gmail.com>                --
12-- All rights reserved.                                                     --
13--                                                                          --
14-- Redistribution and use in source and binary forms, with or without       --
15-- modification, are permitted provided that the following conditions       --
16-- are met:                                                                 --
17--                                                                          --
18--  * Redistributions of source code must retain the above copyright        --
19--    notice, this list of conditions and the following disclaimer.         --
20--                                                                          --
21--  * Redistributions in binary form must reproduce the above copyright     --
22--    notice, this list of conditions and the following disclaimer in the   --
23--    documentation and/or other materials provided with the distribution.  --
24--                                                                          --
25--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
26--    contributors may be used to endorse or promote products derived from  --
27--    this software without specific prior written permission.              --
28--                                                                          --
29-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
30-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
31-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
32-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
33-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
34-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
35-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
36-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
37-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
38-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
39-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
40--                                                                          --
41------------------------------------------------------------------------------
42--  $Revision: 2457 $ $Date: 2012-02-02 18:56:17 +0400 (Thu, 02 Feb 2012) $
43------------------------------------------------------------------------------
44
45package body Configure.Component_Switches is
46
47   --------------------
48   -- Component_Name --
49   --------------------
50
51   function Create
52    (Name           : String;
53     Description    : String;
54     Enabled        : Boolean := True;
55     Libdir_Enabled : Boolean := False) return Component_Switches is
56   begin
57      return
58       (Name           => +Name,
59        Description    => +Description,
60        Enable         => Enabled,
61        Libdir_Enabled => Libdir_Enabled,
62        others         => <>);
63   end Create;
64
65   -------------------
66   -- Enable_Libdir --
67   -------------------
68
69   procedure Enable_Libdir
70    (Self    : in out Component_Switches'Class;
71     Enabled : Boolean) is
72   begin
73      Self.Libdir_Enabled := Enabled;
74   end Enable_Libdir;
75
76   ----------
77   -- Help --
78   ----------
79
80   function Help
81    (Self : Component_Switches'Class) return Unbounded_String_Vector
82   is
83      procedure Append
84       (Help        : in out Unbounded_String_Vector;
85        Switch      : Unbounded_String;
86        Description : Unbounded_String);
87
88      ------------
89      -- Append --
90      ------------
91
92      procedure Append
93       (Help        : in out Unbounded_String_Vector;
94        Switch      : Unbounded_String;
95        Description : Unbounded_String)
96      is
97         Stop : constant := 26;
98         Line : Unbounded_String := "  " & Switch;
99
100      begin
101         if Length (Line) > Stop then
102            Help.Append (Line);
103            Help.Append (Stop * ' ' & Description);
104
105         else
106            Head (Line, Stop, ' ');
107            Append (Line, Description);
108            Help.Append (Line);
109         end if;
110      end Append;
111
112   begin
113      return Result : Unbounded_String_Vector do
114         if Self.Enable then
115            Append
116             (Result, "--enable-" & Self.Name, "enable " & Self.Description);
117
118         else
119            Append
120             (Result, "--disable-" & Self.Name, "enable " & Self.Description);
121         end if;
122
123         if Self.Libdir_Enabled then
124            Append
125             (Result,
126              "--with-" & Self.Name & "-libdir=ARG",
127              +"  lookup for libraries in ARG");
128         end if;
129      end return;
130   end Help;
131
132   -------------------------
133   -- Is_Enable_Specified --
134   -------------------------
135
136   function Is_Enable_Specified
137    (Self : Component_Switches'Class) return Boolean is
138   begin
139      return Self.Enable_Specified;
140   end Is_Enable_Specified;
141
142   ----------------
143   -- Is_Enabled --
144   ----------------
145
146   function Is_Enabled (Self : Component_Switches'Class) return Boolean is
147   begin
148      return Self.Enable;
149   end Is_Enabled;
150
151   -------------------------
152   -- Is_Libdir_Specified --
153   -------------------------
154
155   function Is_Libdir_Specified
156    (Self : Component_Switches'Class) return Boolean is
157   begin
158      return Self.Libdir_Specified;
159   end Is_Libdir_Specified;
160
161   ------------
162   -- Libdir --
163   ------------
164
165   function Libdir (Self : Component_Switches'Class) return Unbounded_String is
166   begin
167      --  XXX Synthetic value should be constructed then necessary.
168
169      return Self.Libdir;
170   end Libdir;
171
172   --------------------
173   -- Parse_Switches --
174   --------------------
175
176   procedure Parse_Switches
177    (Self      : in out Component_Switches'Class;
178     Arguments : in out Unbounded_String_Vector)
179   is
180      Enable   : constant Unbounded_String := "--enable-" & Self.Name;
181      Disable  : constant Unbounded_String := "--disable-" & Self.Name;
182      Within   : constant Unbounded_String := "--with-" & Self.Name;
183      Without  : constant Unbounded_String := "--without-" & Self.Name;
184      Libdir   : constant Unbounded_String := "--with-" & Self.Name & "-libdir";
185      Index    : Positive := 1;
186
187   begin
188      while Index <= Integer (Arguments.Length) loop
189         declare
190            Argument : constant Unbounded_String := Arguments.Element (Index);
191
192         begin
193            if Argument = Enable or Argument = Within then
194               Self.Enable := True;
195               Self.Enable_Specified := True;
196               Arguments.Delete (Index);
197
198            elsif Argument = Disable or Argument = Without then
199               Self.Enable := False;
200               Self.Enable_Specified := True;
201               Arguments.Delete (Index);
202
203            elsif Self.Libdir_Enabled
204              and then Length (Argument) > Length (Libdir)
205              and then Slice (Argument, 1, Length (Libdir)) = Libdir
206              and then Element (Argument, Length (Libdir) + 1) = '='
207            then
208               Self.Libdir :=
209                 Unbounded_Slice
210                  (Argument, Length (Libdir) + 2, Length (Argument));
211               Self.Libdir_Specified := True;
212               Self.Enable := True;
213               Arguments.Delete (Index);
214
215            else
216               Index := Index + 1;
217            end if;
218         end;
219      end loop;
220   end Parse_Switches;
221
222end Configure.Component_Switches;
223