PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/src/yolk-config_file_parser.adb

http://github.com/ThomasLocke/yolk
Ada | 260 lines | 160 code | 40 blank | 60 comment | 8 complexity | b1b5b129763d4e44860bd69073736a5f MD5 | raw file
  1-------------------------------------------------------------------------------
  2--                                                                           --
  3--                   Copyright (C) 2010-, Thomas Løcke                   --
  4--                                                                           --
  5--  This library is free software;  you can redistribute it and/or modify    --
  6--  it under terms of the  GNU General Public License  as published by the   --
  7--  Free Software  Foundation;  either version 3,  or (at your  option) any  --
  8--  later version. This library is distributed in the hope that it will be   --
  9--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
 10--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
 11--                                                                           --
 12--  As a special exception under Section 7 of GPL version 3, you are         --
 13--  granted additional permissions described in the GCC Runtime Library      --
 14--  Exception, version 3.1, as published by the Free Software Foundation.    --
 15--                                                                           --
 16--  You should have received a copy of the GNU General Public License and    --
 17--  a copy of the GCC Runtime Library Exception along with this program;     --
 18--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
 19--  <http://www.gnu.org/licenses/>.                                          --
 20--                                                                           --
 21-------------------------------------------------------------------------------
 22
 23with Ada.Strings;
 24with Ada.Strings.Fixed;
 25with Ada.Strings.Maps;
 26with Ada.Text_IO;
 27
 28package body Yolk.Config_File_Parser is
 29
 30   Values : Defaults_Array_Type := Defaults;
 31
 32   -----------
 33   --  Get  --
 34   -----------
 35
 36   function Get
 37     (Key : in Key_Type)
 38      return Boolean
 39   is
 40   begin
 41      return Boolean'Value (To_String (Values (Key)));
 42   exception
 43      when Constraint_Error =>
 44         raise Conversion_Error with Key_Type'Image (Key);
 45   end Get;
 46
 47   -----------
 48   --  Get  --
 49   -----------
 50
 51   function Get
 52     (Key : in Key_Type)
 53      return Duration
 54   is
 55   begin
 56      return Duration'Value (To_String (Values (Key)));
 57   exception
 58      when Constraint_Error =>
 59         raise Conversion_Error with Key_Type'Image (Key);
 60   end Get;
 61
 62   -----------
 63   --  Get  --
 64   -----------
 65
 66   function Get
 67     (Key : in Key_Type)
 68      return Float
 69   is
 70   begin
 71      return Float'Value (To_String (Values (Key)));
 72   exception
 73      when Constraint_Error =>
 74         raise Conversion_Error with Key_Type'Image (Key);
 75   end Get;
 76
 77   -----------
 78   --  Get  --
 79   -----------
 80
 81   function Get
 82     (Key : in Key_Type)
 83      return Integer
 84   is
 85   begin
 86      return Integer'Value (To_String (Values (Key)));
 87   exception
 88      when Constraint_Error =>
 89         raise Conversion_Error with Key_Type'Image (Key);
 90   end Get;
 91
 92   -----------
 93   --  Get  --
 94   -----------
 95
 96   function Get
 97     (Key : in Key_Type)
 98      return String
 99   is
100   begin
101      return To_String (Values (Key));
102   end Get;
103
104   -----------
105   --  Get  --
106   -----------
107
108   function Get
109     (Key : in Key_Type)
110      return Unbounded_String
111   is
112   begin
113      return Values (Key);
114   end Get;
115
116   ---------------------------
117   --  Has_Non_Empty_Value  --
118   ---------------------------
119
120   function Has_Non_Empty_Value
121     (Key : in Key_Type)
122      return Boolean
123   is
124   begin
125      return Values (Key) /= Null_Unbounded_String;
126   end Has_Non_Empty_Value;
127
128   -----------------
129   --  Load_File  --
130   -----------------
131
132   procedure Load_File
133     (Config_File : in String)
134   is
135      use Ada.Strings;
136      use Ada.Text_IO;
137
138      File : File_Type;
139
140      ---------------
141      --  Get_Key  --
142      ---------------
143
144      function Get_Key
145        (Line : in String)
146         return String;
147      --  Find and return the Key part of the Line string. If no Key part is
148      --  found, then return an empty string.
149
150      function Get_Key
151        (Line : in String)
152         return String
153      is
154         Key_End : Natural;
155      begin
156         if Line /= "" then
157            Key_End := Fixed.Index (Source => Line,
158                                    Set    => Maps.To_Set (Space),
159                                    Going  => Forward);
160            if Key_End > Line'First then
161               return Line (Line'First .. Key_End - 1);
162            end if;
163         end if;
164
165         return Line;
166      end Get_Key;
167
168      -----------------
169      --  Get_Value  --
170      -----------------
171
172      function Get_Value
173        (Key   : in String;
174         Line  : in String)
175         return Unbounded_String;
176      --  Find and return the value part of Line as an Unbounded_String. If no
177      --  Value part is found, return Null_Unbounded_String.
178
179      function Get_Value
180        (Key   : in String;
181         Line  : in String)
182         return Unbounded_String
183      is
184      begin
185         if Key /= Line then
186            return Trim
187              (To_Unbounded_String (Line (Key'Last + 2 .. Line'Last)), Left);
188         end if;
189
190         return Null_Unbounded_String;
191      end Get_Value;
192
193      -------------------------------
194      --  Is_Not_Empty_Or_Comment  --
195      -------------------------------
196
197      function Is_Not_Empty_Or_Comment
198        (Line : in String)
199         return Boolean;
200      --  Return True if Line contains an actual Key/Value pair, and not just
201      --  an empty line or a comment.
202
203      function Is_Not_Empty_Or_Comment
204        (Line : in String)
205         return Boolean
206      is
207      begin
208         if Line'Length = 0 then
209            return False;
210         end if;
211
212         if  Line (Line'First .. Line'First) = "#" then
213            return False;
214         end if;
215
216         if Line'Length > 1
217           and then Line (Line'First .. Line'First + 1) = "--"
218         then
219            return False;
220         end if;
221
222         return True;
223      end Is_Not_Empty_Or_Comment;
224   begin
225      Open (File => File,
226            Mode => In_File,
227            Name => Config_File);
228
229      while not End_Of_File (File => File) loop
230         declare
231            Line  : constant String := Fixed.Trim (Get_Line (File), Both);
232         begin
233            if Is_Not_Empty_Or_Comment (Line) then
234               declare
235                  Key   : constant String := Get_Key (Line);
236                  Value : constant Unbounded_String := Get_Value (Key, Line);
237               begin
238                  Values (Key_Type'Value (Key)) := Value;
239
240               exception
241                  when Constraint_Error =>
242                     raise Unknown_Key with
243                       "Unknown configuration key '" & Key & "' in file "
244                         & Config_File;
245               end;
246            end if;
247         end;
248      end loop;
249
250      Close (File => File);
251   exception
252      when Name_Error | Use_Error | Device_Error =>
253         raise Cannot_Open_Config_File with Config_File;
254   end Load_File;
255
256begin
257
258   Load_File (Config_File => Config_File);
259
260end Yolk.Config_File_Parser;