/src/yolk-config_file_parser.ads

http://github.com/ThomasLocke/yolk · Ada · 122 lines · 35 code · 15 blank · 72 comment · 0 complexity · 16907aff824240eb7a0d4f8b9b452c3a 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. -- This package provides simple access to configuration files.
  23. -- The format is:
  24. -- KEY VALUE
  25. --
  26. -- Comments are prefixed with a # or a --
  27. --
  28. -- # This is a comment
  29. -- -- This is also a comment
  30. --
  31. -- Blank lines and comments are ignored and so is pre-/postfixed whitespace,
  32. -- so this:
  33. --
  34. -- [lots of whitespace]KEY[lots of whitespace]VALUE[lots of whitespace]
  35. --
  36. -- is treated as:
  37. --
  38. -- KEY VALUE
  39. --
  40. -- Values containing whitespace, eg. full sentences and similar, are returned
  41. -- as is. It is not necessary to quote such values, so this:
  42. --
  43. -- KEY some value with whitespace
  44. --
  45. -- is perfectly valid, and will, when calling Get (KEY), return:
  46. --
  47. -- some value with whitespace
  48. --
  49. -- If VALUE is True or False (case-insensitive), then the KEY can be returned
  50. -- as both a String or as a Boolean.
  51. --
  52. -- Trying to convert VALUE to an incompatible type, e.g. a Float to a Boolean
  53. -- will raise the Conversion_Error exception. It will NOT return some dummy
  54. -- value.
  55. --
  56. -- To clear a default value, simply add the key to the configuration file,
  57. -- with no value set. Conversely, you must omit (or comment) keys for which
  58. -- you want to use the default value.
  59. with Ada.Strings.Unbounded;
  60. generic
  61. use Ada.Strings.Unbounded;
  62. type Key_Type is (<>);
  63. type Defaults_Array_Type is array (Key_Type) of Unbounded_String;
  64. Defaults : in Defaults_Array_Type;
  65. Config_File : in String;
  66. package Yolk.Config_File_Parser is
  67. Unknown_Key : exception;
  68. -- Is raised when an unknown KEY has been found in the config file.
  69. Cannot_Open_Config_File : exception;
  70. -- Is raised when the given config file cannot be opened, eg. due to bad
  71. -- path.
  72. Conversion_Error : exception;
  73. -- Is raised when a value cannot be converted to a specific type.
  74. function Get
  75. (Key : in Key_Type)
  76. return Boolean;
  77. function Get
  78. (Key : in Key_Type)
  79. return Duration;
  80. function Get
  81. (Key : in Key_Type)
  82. return Float;
  83. function Get
  84. (Key : in Key_Type)
  85. return Integer;
  86. function Get
  87. (Key : in Key_Type)
  88. return String;
  89. function Get
  90. (Key : in Key_Type)
  91. return Unbounded_String;
  92. -- Get the VALUE for Key and convert it to target type.
  93. -- Exceptions:
  94. -- Conversion_Error
  95. function Has_Non_Empty_Value
  96. (Key : in Key_Type)
  97. return Boolean;
  98. -- Return True if Key is _not_ a Null_Unbounded_String.
  99. procedure Load_File
  100. (Config_File : in String);
  101. -- Load the config file Config_File. This can be done over and over as many
  102. -- times as necessary. The values from the latest file overwrites the
  103. -- previous values.
  104. -- Exceptions:
  105. -- Cannot_Open_Ini_File
  106. -- Unknown_Key
  107. end Yolk.Config_File_Parser;