1
+ -- SPDX-License-Identifier: GPL-3.0-or-later
2
+ -- SPDX-FileCopyrightText: Copyright 2023 Stephen Merrony
3
+
4
+ with Ada.Text_IO ; use Ada.Text_IO;
5
+
6
+ with TOML ;
7
+ with TOML.File_IO ;
8
+
9
+ package body Config is
10
+
11
+ procedure Load_Config_File (Filename : String; Verbose : Boolean := False) is
12
+ Toml_Parse_Result : TOML.Read_Result;
13
+ begin
14
+ Toml_Parse_Result := TOML.File_IO.Load_File (Filename);
15
+ if not Toml_Parse_Result.Success then
16
+ raise Could_Not_Parse with To_String (Toml_Parse_Result.Message);
17
+ end if ;
18
+
19
+ declare
20
+ Top_Keys : constant TOML.Key_Array := Toml_Parse_Result.Value.Keys;
21
+ Tab_Ix : Natural := 0 ;
22
+ begin
23
+ for TK of Top_Keys loop
24
+ if Verbose then
25
+ Put_Line (" Configuration for " & To_String (TK) & " is..." );
26
+ end if ;
27
+
28
+ if To_String (TK) = " keypadder" then
29
+ declare
30
+ Keypadder_Table : constant TOML.TOML_Value := TOML.Get (Toml_Parse_Result.Value, " keypadder" );
31
+ begin
32
+ Keypadder_Conf.Port := Port_T (TOML.As_Integer (TOML.Get (Keypadder_Table, " port" )));
33
+ if Verbose then
34
+ Put_Line (" Port:" & Keypadder_Conf.Port'Image);
35
+ end if ;
36
+ end ;
37
+ elsif To_String (TK) = " tab" then
38
+ declare
39
+ Tabs_Array : constant TOML.TOML_Value := TOML.Get (Toml_Parse_Result.Value, " tab" );
40
+ Tabs_Count : constant Natural := TOML.Length (Tabs_Array);
41
+ Tab : TOML.TOML_Value;
42
+
43
+ begin
44
+ if Tabs_Count = 0 then
45
+ raise Incomplete_Configuration with " you must configure at least one Tab" ;
46
+ end if ;
47
+ for T in 1 .. Tabs_Count loop
48
+ Tab_Ix := Tab_Ix + 1 ;
49
+ Tab := TOML.Item (Tabs_Array, T);
50
+ Tabs (T).Label := TOML.As_Unbounded_String (TOML.Get (Tab, " label" ));
51
+ Tabs (T).Columns := Natural (TOML.As_Integer (TOML.Get (Tab, " cols" )));
52
+ -- Put_Line ("Tab defined:" & TOML.Dump_As_String (Tab));
53
+ if Verbose then
54
+ Put_Line (" Tab: " & To_String (Tabs (T).Label) &
55
+ " with:" & Tabs (T).Columns'Image & " columns" );
56
+ end if ;
57
+ declare
58
+ Keys_Array : constant TOML.TOML_Value := TOML.Get (Tab, " keys" );
59
+ Key_Table : TOML.TOML_Value;
60
+ begin
61
+ Put_Line (" Keys defined:" & TOML.Length (Keys_Array)'Image);
62
+ Tabs (Tab_Ix).Keys_Count := TOML.Length (Keys_Array);
63
+ for K in 1 .. TOML.Length (Keys_Array) loop
64
+ Key_Table := TOML.Item (Keys_Array, K);
65
+ Tabs (Tab_Ix).Keys (K).Label := TOML.As_Unbounded_String (TOML.Get (Key_Table, " label" ));
66
+ if TOML.Has (Key_Table, " send" ) then
67
+ Tabs (Tab_Ix).Keys (K).Send := TOML.As_Unbounded_String (TOML.Get (Key_Table, " send" ));
68
+ end if ;
69
+ if TOML.Has (Key_Table, " colspan" ) then
70
+ Tabs (Tab_Ix).Keys (K).Colspan := Natural (TOML.As_Integer (TOML.Get (Key_Table, " colspan" )));
71
+ end if ;
72
+ if TOML.Has (Key_Table, " rowspan" ) then
73
+ Tabs (Tab_Ix).Keys (K).Rowspan := Natural (TOML.As_Integer (TOML.Get (Key_Table, " rowspan" )));
74
+ end if ;
75
+ if Verbose then
76
+ Put_Line (" Key No. " & K'Image & " is: " & To_String (Tabs (Tab_Ix).Keys (K).Label));
77
+ end if ;
78
+ end loop ;
79
+ end ;
80
+ end loop ;
81
+ end ;
82
+ else
83
+ raise Unknown_Configuration_Item with To_String (TK);
84
+ end if ;
85
+
86
+ end loop ;
87
+ end ;
88
+ end Load_Config_File ;
89
+
90
+ end Config ;
0 commit comments