clic_0.3.0_56bbdc00/src/clic-config-edit.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
with Ada.Wide_Wide_Text_IO;
with Ada.Directories;

with AAA.Strings;
with AAA.Directories;

with CLIC.Config.Load;

with Simple_Logging;

with TOML;

package body CLIC.Config.Edit is

   package Trace renames Simple_Logging;

   use TOML;

   procedure Write_Config_File (Table : TOML_Value; Path : String)
     with Pre => Table.Kind = TOML_Table;

   function Remove_From_Table (Table : TOML_Value;
                               Key   : Config_Key;
                               Quiet : Boolean := False)
                               return Boolean
     with Pre => Table.Kind = TOML_Table;
   --  When Quiet, don't print error messages when returning False

   function Add_In_Table (Table : TOML_Value;
                          Key   : Config_Key;
                          Val   : TOML_Value)
                          return Boolean
     with Pre => Table.Kind = TOML_Table;

   -----------------------
   -- Write_Config_File --
   -----------------------

   procedure Write_Config_File (Table : TOML_Value; Path : String) is
      use Ada.Wide_Wide_Text_IO;
      use Ada.Directories;
      File : File_Type;
   begin

      --  Create the directory for the config file, in case it doesn't exists
      Create_Path (Containing_Directory (Path));

      Create (File, Out_File, Path);
      Trace.Debug ("Write config: '" & TOML.Dump_As_String (Table) & "'");
      Put (File, WW (TOML.Dump_As_String (Table)));
      Close (File);
   end Write_Config_File;

   -----------------------
   -- Remove_From_Table --
   -----------------------

   function Remove_From_Table (Table : TOML_Value;
                               Key   : Config_Key;
                               Quiet : Boolean := False)
                               return Boolean
   is
      use AAA.Strings;

      Id   : constant String := Split (Key, '.', Raises => False);
      Leaf : constant Boolean := Id = Key;
   begin
      if not Table.Has (Id) then
         --  The key doesn't exist
         if not Quiet then
            Trace.Error ("Configuration key not defined");
         end if;
         return False;
      end if;

      if Leaf then
         Table.Unset (Id);
         return True;
      else
         declare
            Sub : constant TOML_Value := Table.Get (Id);
         begin
            if Sub.Kind = TOML_Table then
               return Remove_From_Table (Sub, Split (Key, '.', Tail));
            else
               if not Quiet then
                  Trace.Error ("Configuration key not defined");
               end if;
               return False;
            end if;
         end;
      end if;
   end Remove_From_Table;

   ------------------
   -- Add_In_Table --
   ------------------

   function Add_In_Table (Table : TOML_Value;
                          Key   : Config_Key;
                          Val   : TOML_Value)
                          return Boolean
   is
      use AAA.Strings;
      Id   : constant String := Split (Key, '.', Raises => False);
      Leaf : constant Boolean := Id = Key;
   begin
      if Leaf then
         Table.Set (Id, Val);
         return True;
      end if;

      if not Table.Has (Id) then
         --  The subkey doesn't exist, create a table for it
         Table.Set (Id, Create_Table);
      end if;

      declare
         Sub : constant TOML_Value := Table.Get (Id);
      begin
         if Sub.Kind = TOML_Table then
            return Add_In_Table (Sub, Split (Key, '.', Tail), Val);
         else
            Trace.Error ("Configuration key already defined");
            return False;
         end if;
      end;
   end Add_In_Table;

   -----------
   -- Unset --
   -----------

   function Unset (Path  : String;
                   Key   : Config_Key;
                   Quiet : Boolean := False)
                   return Boolean
   is
      use AAA.Directories;

      Tmp : Replacer := New_Replacement (File              => Path,
                                         Backup            => False,
                                         Allow_No_Original => True);

      Table : constant TOML_Value := Load.Load_TOML_File (Tmp.Editable_Name);
   begin

      if Table.Is_Null then
         --  The configuration file doesn't exist or is not valid
         if not Quiet then
            Trace.Error ("configuration file doesn't exist or is not valid");
         end if;
         return False;
      end if;

      if not Remove_From_Table (Table, Key, Quiet) then
         return False;
      end if;

      Write_Config_File (Table, Tmp.Editable_Name);

      Tmp.Replace;

      return True;
   end Unset;

   ---------
   -- Set --
   ---------

   function Set (Path  : String;
                 Key   : Config_Key;
                 Value : String;
                 Check : Check_Import := null)
                 return Boolean
   is
      use AAA.Directories;

      Tmp : Replacer := New_Replacement (File              => Path,
                                         Backup            => False,
                                         Allow_No_Original => True);

      Table : TOML_Value := Load.Load_TOML_File (Tmp.Editable_Name);

      To_Add : constant TOML_Value := To_TOML_Value (Value);
   begin
      if To_Add.Is_Null then
         Trace.Error ("Invalid configuration value: '" & Value & "'");
         return False;
      end if;

      if Check /= null and then not Check (Key, To_Add) then
         return False;
      end if;

      if Table.Is_Null then
         --  The configuration file doesn't exist or is not valid. Create an
         --  empty table.
         Table := TOML.Create_Table;
      end if;

      if not Add_In_Table (Table, Key, To_Add) then
         return False;
      end if;

      Write_Config_File (Table, Tmp.Editable_Name);

      Tmp.Replace;

      return True;
   end Set;

   ---------------
   -- Set_Typed --
   ---------------

   function Set_Typed (Path  : String;
                       Key   : Config_Key;
                       Value : Value_Type;
                       Check : Check_Import := null)
                       return Boolean
   is

      ------------------
      -- Check_Proper --
      ------------------
      --  Check that the type to be stored is actually the one intended, in
      --  addition to the user check
      function Check_Proper (Key : Config_Key; Value : TOML.TOML_Value)
                             return Boolean
      is ((Check = null or else Check (Key, Value))
          and then Value.Kind = TOML_Type);

      -----------------
      -- Set_Boolean --
      -----------------

      function Set_Boolean return Boolean is
      begin
         return Set (Path, Key,
                     (case Boolean'Value (Image (Value)) is
                         when True  => "true",
                         when False => "false"),
                     Check_Proper'Unrestricted_Access);
      end Set_Boolean;

   begin
      if TOML_Type = TOML_Boolean then
         --  Boolean is special because we need to pass exactly "true/false"
         return Set_Boolean;
      else
         return Set (Path, Key, Image (Value),
                     Check_Proper'Unrestricted_Access);
      end if;
   end Set_Typed;

end CLIC.Config.Edit;