1 ------------------------------------------------------------------------------
4 -- Copyright (C) 2007-2010 --
5 -- Pascal Obry - Olivier Ramonat --
7 -- This library is free software; you can redistribute it and/or modify --
8 -- it under the terms of the GNU General Public License as published by --
9 -- the Free Software Foundation; either version 2 of the License, or (at --
10 -- your option) any later version. --
12 -- This library is distributed in the hope that it will be useful, but --
13 -- WITHOUT ANY WARRANTY; without even the implied warranty of --
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
15 -- General Public License for more details. --
17 -- You should have received a copy of the GNU General Public License --
18 -- along with this library; if not, write to the Free Software Foundation, --
19 -- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --
20 ------------------------------------------------------------------------------
22 with Ada
.IO_Exceptions
;
24 with Ada
.Strings
.Maps
;
25 with Ada
.Strings
.Unbounded
;
28 package body Morzhol
.Iniparser
is
32 use Ada
.Strings
.Unbounded
;
34 subtype Line_Length
is Positive;
36 Min_Line_Length
: constant Line_Length
:= 1;
37 Max_Line_Length
: constant Line_Length
:= 1_024
;
39 subtype Buffer_String
is String (Min_Line_Length
.. Max_Line_Length
);
41 Parameters
: array (Parameter_Name
) of Unbounded_String
;
45 package Parameter_IO
is
46 new Ada
.Text_IO
.Enumeration_IO
(Enum
=> Parameter_Name
);
49 Config_File
: File_Type
;
64 procedure Open
(Config_File_Name
: in String) is
73 procedure Read_Datas
is
75 procedure Insert_Parameter
(From
: in String);
78 procedure Check_Completness
;
81 Buffer
: Buffer_String
;
83 Blanks
: constant Maps
.Character_Set
:=
84 Maps
.To_Set
(" " & ASCII
.HT
);
86 -----------------------
87 -- Check_Completness --
88 -----------------------
90 procedure Check_Completness
is
92 for P
in Parameter_Name
loop
93 if Length
(Parameters
(P
)) = 0 then
94 raise Uncomplete_Config
95 with "Missing value for " & Parameter_Name
'Image (P
);
99 end Check_Completness
;
101 ----------------------
102 -- Insert_Parameter --
103 ----------------------
105 procedure Insert_Parameter
(From
: in String) is
107 Parameter
: Parameter_Name
;
109 Get
(From
, Parameter
, Last
);
110 Parameters
(Parameter
) :=
111 Trim
(To_Unbounded_String
(From
(Last
+ 1 .. From
'Last)),
115 when Ada
.IO_Exceptions
.Data_Error
=>
116 raise Unknown_Parameter
117 with "Wrong parameter '" & From
& ''';
118 end Insert_Parameter
;
121 while not End_Of_File
(Config_File
) loop
122 Get_Line
(Config_File
, Buffer
, Last
);
123 if Last
/= 0 and then Buffer
(Buffer
'First) /= '#' then
124 Insert_Parameter
(Buffer
(1 .. Last
));
131 Open
(Name
=> Config_File_Name
, File
=> Config_File
, Mode
=> In_File
);
139 procedure Save_Close
is
140 Column
: constant Positive_Count
:=
141 Positive_Count
(Parameter_Name
'Width + 2);
143 Reset
(File
=> Config_File
, Mode
=> Out_File
);
145 for P
in Parameter_Name
loop
146 Put
(Config_File
, P
);
147 Set_Col
(Config_File
, Column
);
148 Put
(Config_File
, To_String
(Parameters
(P
)));
149 New_Line
(Config_File
);
160 function Get_Value
(Parameter
: in Parameter_Name
) return String is
162 return To_String
(Parameters
(Parameter
));
165 function Get_Value
(Parameter
: in Parameter_Name
) return Integer is
167 return Integer'Value (To_String
(Parameters
(Parameter
)));
170 function Get_Value
(Parameter
: in Parameter_Name
) return Float is
172 return Float'Value (To_String
(Parameters
(Parameter
)));
175 function Get_Value
(Parameter
: in Parameter_Name
) return Boolean is
177 return Boolean'Value (To_String
(Parameters
(Parameter
)));
184 procedure Set_Value
(Parameter
: in Parameter_Name
; Value
: in String) is
186 Parameters
(Parameter
) :=
187 Trim
(To_Unbounded_String
(Value
), Side
=> Both
);
190 procedure Set_Value
(Parameter
: in Parameter_Name
; Value
: in Integer) is
192 Set_Value
(Parameter
, Integer'Image (Value
));
195 procedure Set_Value
(Parameter
: in Parameter_Name
; Value
: in Float) is
197 Set_Value
(Parameter
, Float'Image (Value
));
200 procedure Set_Value
(Parameter
: in Parameter_Name
; Value
: in Boolean) is
202 Set_Value
(Parameter
, Boolean'Image (Value
));
209 package body Enum_Values
is
215 function Get_Value
(Parameter
: in Parameter_Name
) return Enum
is
217 return Enum
'Value (To_String
(Parameters
(Parameter
)));
224 procedure Set_Value
(Parameter
: in Parameter_Name
; Value
: in Enum
) is
226 Set_Value
(Parameter
, Enum
'Image (Value
));
235 package body Vector_Values
is
241 function Get_Value
(Parameter
: in Parameter_Name
) return Vector
is
243 Param
: constant String := To_String
(Parameters
(Parameter
));
245 First
: Positive := 1;
248 V
: Vector
(1 .. Max_Vector_Size
);
249 N
: Positive := V
'First;
252 while Last
< Param
'Last loop
253 Get
(Param
(First
.. Param
'Last), V
(N
), Last
);
257 return V
(1 .. N
- 1);
264 procedure Set_Value
(Parameter
: in Parameter_Name
; Value
: in Vector
) is
266 Parameters
(Parameter
) := Null_Unbounded_String
;
267 for I
in Value
'Range loop
268 Append
(Parameters
(Parameter
),
269 Trim
(To_Unbounded_String
(Image
(Value
(I
))),
270 Side
=> Both
) & ' ');
276 end Morzhol
.Iniparser
;