/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 ¸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. with Ada.Strings;
  23. with Ada.Strings.Fixed;
  24. with Ada.Strings.Maps;
  25. with Ada.Text_IO;
  26. package body Yolk.Config_File_Parser is
  27. Values : Defaults_Array_Type := Defaults;
  28. -----------
  29. -- Get --
  30. -----------
  31. function Get
  32. (Key : in Key_Type)
  33. return Boolean
  34. is
  35. begin
  36. return Boolean'Value (To_String (Values (Key)));
  37. exception
  38. when Constraint_Error =>
  39. raise Conversion_Error with Key_Type'Image (Key);
  40. end Get;
  41. -----------
  42. -- Get --
  43. -----------
  44. function Get
  45. (Key : in Key_Type)
  46. return Duration
  47. is
  48. begin
  49. return Duration'Value (To_String (Values (Key)));
  50. exception
  51. when Constraint_Error =>
  52. raise Conversion_Error with Key_Type'Image (Key);
  53. end Get;
  54. -----------
  55. -- Get --
  56. -----------
  57. function Get
  58. (Key : in Key_Type)
  59. return Float
  60. is
  61. begin
  62. return Float'Value (To_String (Values (Key)));
  63. exception
  64. when Constraint_Error =>
  65. raise Conversion_Error with Key_Type'Image (Key);
  66. end Get;
  67. -----------
  68. -- Get --
  69. -----------
  70. function Get
  71. (Key : in Key_Type)
  72. return Integer
  73. is
  74. begin
  75. return Integer'Value (To_String (Values (Key)));
  76. exception
  77. when Constraint_Error =>
  78. raise Conversion_Error with Key_Type'Image (Key);
  79. end Get;
  80. -----------
  81. -- Get --
  82. -----------
  83. function Get
  84. (Key : in Key_Type)
  85. return String
  86. is
  87. begin
  88. return To_String (Values (Key));
  89. end Get;
  90. -----------
  91. -- Get --
  92. -----------
  93. function Get
  94. (Key : in Key_Type)
  95. return Unbounded_String
  96. is
  97. begin
  98. return Values (Key);
  99. end Get;
  100. ---------------------------
  101. -- Has_Non_Empty_Value --
  102. ---------------------------
  103. function Has_Non_Empty_Value
  104. (Key : in Key_Type)
  105. return Boolean
  106. is
  107. begin
  108. return Values (Key) /= Null_Unbounded_String;
  109. end Has_Non_Empty_Value;
  110. -----------------
  111. -- Load_File --
  112. -----------------
  113. procedure Load_File
  114. (Config_File : in String)
  115. is
  116. use Ada.Strings;
  117. use Ada.Text_IO;
  118. File : File_Type;
  119. ---------------
  120. -- Get_Key --
  121. ---------------
  122. function Get_Key
  123. (Line : in String)
  124. return String;
  125. -- Find and return the Key part of the Line string. If no Key part is
  126. -- found, then return an empty string.
  127. function Get_Key
  128. (Line : in String)
  129. return String
  130. is
  131. Key_End : Natural;
  132. begin
  133. if Line /= "" then
  134. Key_End := Fixed.Index (Source => Line,
  135. Set => Maps.To_Set (Space),
  136. Going => Forward);
  137. if Key_End > Line'First then
  138. return Line (Line'First .. Key_End - 1);
  139. end if;
  140. end if;
  141. return Line;
  142. end Get_Key;
  143. -----------------
  144. -- Get_Value --
  145. -----------------
  146. function Get_Value
  147. (Key : in String;
  148. Line : in String)
  149. return Unbounded_String;
  150. -- Find and return the value part of Line as an Unbounded_String. If no
  151. -- Value part is found, return Null_Unbounded_String.
  152. function Get_Value
  153. (Key : in String;
  154. Line : in String)
  155. return Unbounded_String
  156. is
  157. begin
  158. if Key /= Line then
  159. return Trim
  160. (To_Unbounded_String (Line (Key'Last + 2 .. Line'Last)), Left);
  161. end if;
  162. return Null_Unbounded_String;
  163. end Get_Value;
  164. -------------------------------
  165. -- Is_Not_Empty_Or_Comment --
  166. -------------------------------
  167. function Is_Not_Empty_Or_Comment
  168. (Line : in String)
  169. return Boolean;
  170. -- Return True if Line contains an actual Key/Value pair, and not just
  171. -- an empty line or a comment.
  172. function Is_Not_Empty_Or_Comment
  173. (Line : in String)
  174. return Boolean
  175. is
  176. begin
  177. if Line'Length = 0 then
  178. return False;
  179. end if;
  180. if Line (Line'First .. Line'First) = "#" then
  181. return False;
  182. end if;
  183. if Line'Length > 1
  184. and then Line (Line'First .. Line'First + 1) = "--"
  185. then
  186. return False;
  187. end if;
  188. return True;
  189. end Is_Not_Empty_Or_Comment;
  190. begin
  191. Open (File => File,
  192. Mode => In_File,
  193. Name => Config_File);
  194. while not End_Of_File (File => File) loop
  195. declare
  196. Line : constant String := Fixed.Trim (Get_Line (File), Both);
  197. begin
  198. if Is_Not_Empty_Or_Comment (Line) then
  199. declare
  200. Key : constant String := Get_Key (Line);
  201. Value : constant Unbounded_String := Get_Value (Key, Line);
  202. begin
  203. Values (Key_Type'Value (Key)) := Value;
  204. exception
  205. when Constraint_Error =>
  206. raise Unknown_Key with
  207. "Unknown configuration key '" & Key & "' in file "
  208. & Config_File;
  209. end;
  210. end if;
  211. end;
  212. end loop;
  213. Close (File => File);
  214. exception
  215. when Name_Error | Use_Error | Device_Error =>
  216. raise Cannot_Open_Config_File with Config_File;
  217. end Load_File;
  218. begin
  219. Load_File (Config_File => Config_File);
  220. end Yolk.Config_File_Parser;