ini_files_10.0.0_6f2bab21/config.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
--  Created On      : Fri Apr 26 08:13:44 1996

with Ada.Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Characters.Handling;
with Ada.Unchecked_Deallocation;
with Ada.IO_Exceptions;

package body Config is

   procedure Free is new Ada.Unchecked_Deallocation (String, Str_Ptr);

   procedure Init (Cfg                 : out Configuration;
                   Config_File_Name    :     String;
                   Case_Sensitive      :     Boolean := True;
                   On_Type_Mismatch    :     Type_Mismatch_Action := Raise_Data_Error;
                   Variable_Terminator :     Character := '='
                   )
   is
   begin
      Free (Cfg.Config_File);
      Cfg.Config_File := new String'(Config_File_Name);
      Cfg.Case_Sensitive := Case_Sensitive;
      Cfg.On_Type_Mismatch := On_Type_Mismatch;
      Cfg.Variable_Terminator := Variable_Terminator;
   end Init;

   function Init (Config_File_Name    :  String;
                  Case_Sensitive      :  Boolean := True;
                  On_Type_Mismatch    :  Type_Mismatch_Action := Raise_Data_Error;
                  Variable_Terminator :  Character := '='
                  )
   return Configuration
   is
      Cfg : Configuration;
   begin
      Init (Cfg, Config_File_Name, Case_Sensitive, On_Type_Mismatch, Variable_Terminator);
      return Cfg;
   end Init;

   function Is_Number_Start (c : Character) return Boolean is
   begin
      case c is
         when '0' .. '9' | '+' | '-' =>
            return True;
         when others =>
            return False;
      end case;
   end Is_Number_Start;

   --  Internal
   --
   procedure Get_Value (Cfg         :  in Configuration;
                        Section     :  in String;
                        Mark        :  in String;
                        Line        : out String;
                        Value_Start : out Natural;
                        Value_End   : out Natural;
                        Found_Line  : out Natural
                       )
   is
      use Ada.Text_IO;
      use Ada.Strings.Fixed;
      use Ada.Strings.Maps;
      use Ada.Strings;
      use Ada.Characters.Handling;

      File              : File_Type;

      Line_End          : Natural                       := 0;
      Line_Count        : Natural                       := 0;

      Sect_End          : Natural;
      Comment_Ind       : Natural;
      Equal_Ind         : Natural;

      Found_Section_End : Natural;
      Found_Mark_Start  : Natural;
      Found_Mark_End    : Natural                       := 0;
      In_Found_Section  : Boolean                       := False;

      Value_Start_Try   : Natural;

   begin -- Get_Value
      Value_Start  := Line'First;
      Value_End    := Line'First - 1;
      Found_Line   := 0;
      Open (File, In_File, Cfg.Config_File.all);
      Read_File :
      while not End_Of_File (File) loop
         Get_Line (File, Line, Line_End); -- error if line end > line'Last
         Line_Count := Line_Count + 1;
         if Line_End > 1 then
            case Line (Line'First) is
               when '[' =>
                  Sect_End := Index (Source  => Line (Line'First .. Line_End),
                                    Pattern => "]");
                  --  error if ext_end = 0
                  Found_Section_End := Sect_End - 1;
                  --  pragma Debug(Put_Line("Config: found_section => " &
                  --  Line(2..Found_Section_End)));
                  if Cfg.Case_Sensitive then
                     In_Found_Section := Section = Line (Line'First + 1 .. Found_Section_End);
                  else
                     In_Found_Section :=
                       To_Lower (Section) = To_Lower (Line (Line'First + 1 .. Found_Section_End));
                  end if;

               when ';' | '#' =>
                  null; -- This is a full-line comment
               when others =>
                  if Section = "*" then
                     In_Found_Section := True;
                  end if;
                  if In_Found_Section then
                     Comment_Ind := Index (Source => Line (Line'First .. Line_End),
                                          Set    => To_Set ("#;"));
                     if Comment_Ind >= Line'First then
                        Line_End := Comment_Ind - 1;
                     end if;
                     Equal_Ind := Index (Source  => Line (Line'First .. Line_End),
                                        Pattern => (1 => Cfg.Variable_Terminator));
                     if Equal_Ind >= Line'First then
                        Found_Mark_Start :=
                          Index_Non_Blank (Line (Line'First .. Equal_Ind - 1), Forward);
                        Found_Mark_End :=
                          Index_Non_Blank (Line (Line'First .. Equal_Ind - 1), Backward);
                     else
                        Found_Mark_Start :=
                          Index_Non_Blank (Line (Line'First .. Line_End), Forward);
                        Found_Mark_End :=
                          Index_Non_Blank (Line (Line'First .. Line_End), Backward);
                     end if;
                     --   Put_Line("Config: found_mark    => [" &
                     --   Line(Found_Mark_start..Found_Mark_End) & "] equal ind=" & Equal_Ind'Img &
                     --   " termi=[" & Cfg.Variable_Terminator & ']');
                     if Found_Mark_Start > 0 and then
                       Found_Mark_End > 0
                     then
                        if (Cfg.Case_Sensitive and then
                            (Line (Found_Mark_Start .. Found_Mark_End) = Mark))
                          or else (not Cfg.Case_Sensitive and then
                                   (To_Lower (Line (Found_Mark_Start ..
                                                  Found_Mark_End))
                                    = To_Lower (Mark)))
                        then
                           Found_Line := Line_Count;
                           if Equal_Ind >= Line'First then
                              Value_Start_Try :=
                                Index_Non_Blank (Line (Equal_Ind + 1 .. Line_End),
                                                Forward);
                              if Value_Start_Try >= Line'First then
                                 Value_End :=
                                   Index_Non_Blank (Line (Value_Start_Try .. Line_End),
                                                   Backward);
                                 Value_Start  := Value_Start_Try;
                              end if;
                           end if;
                           exit Read_File;
                        end if;
                     end if;
                  end if;
            end case;
         end if;
      end loop Read_File;
      Close (File);
   end Get_Value;

   Max_Line_Length : constant := 1000;

   function Value_Of (Cfg     : Configuration;
                      Section : String;
                      Mark    : String;
                      Default : String := "")
   return String
   is
      Line              : String (1 .. Max_Line_Length);
      Value_Start       : Natural := Line'First;
      Value_End         : Natural := Line'First - 1;
      Found_Line        : Natural := 0;
   begin
      Get_Value (Cfg, Section, Mark, Line, Value_Start, Value_End, Found_Line);
      if Line (Value_Start .. Value_End) = "" then
         return Default;
      else
         return Line (Value_Start .. Value_End);
      end if;
   end Value_Of;

   procedure Type_Error (Cfg : Configuration; Val, Desc : String) is
      use Ada.Text_IO;
      Err_Msg : constant String := "'" & Val & "' is not " & Desc;
   begin
      case Cfg.On_Type_Mismatch is
         when Raise_Data_Error =>
            raise Ada.Text_IO.Data_Error with "Config (.ini files): " & Err_Msg;

         when Print_Warning    =>
            Put_Line (Standard_Error, "Config: warning: " & Err_Msg);

         when Be_Quiet         =>
            null;
      end case;
   end Type_Error;

   function Value_Of (Cfg     : Configuration;
                      Section : String;
                      Mark    : String;
                      Default : Integer := 0)
   return Integer
   is
      Line              : String (1 .. Max_Line_Length);
      Value_Start       : Natural := Line'First;
      Value_End         : Natural := Line'First - 1;
      Found_Line        : Natural := 0;
   begin
      Get_Value (Cfg, Section, Mark, Line, Value_Start, Value_End, Found_Line);

      if Found_Line = 0 then
         return Default;
      end if;

      declare
         Value_As_String : constant String := Line (Value_Start .. Value_End);
      begin
         if Value_As_String'Length > 2 and then
           Line (Value_Start .. Value_Start + 1) = "0x"
         then
            return Integer'Value ("16#" &
                                   Line (Value_Start + 2 .. Value_End) &
                                   "#");
         elsif Value_As_String'Length > 0  and then
           Is_Number_Start (Line (Value_Start))
         then
            return Integer'Value (Value_As_String);
         else
            Type_Error (Cfg, Value_As_String, "an integer number in line" & Integer'Image (Found_Line));
            return Default;
         end if;
      end;
   exception
      when Constraint_Error =>  --  raised by 'Value on faulty argument
         Type_Error (Cfg, Line (Value_Start .. Value_End), "an integer number in line" & Integer'Image (Found_Line));
         return Default;
   end Value_Of;

   function Value_Of (Cfg     : Configuration;
                      Section : String;
                      Mark    : String;
                      Default : Long_Float := 0.0)
   return Long_Float
   is
      Line              : String (1 .. Max_Line_Length);
      Value_Start       : Natural := Line'First;
      Value_End         : Natural := Line'First - 1;
      Found_Line        : Natural := 0;
   begin
      Get_Value (Cfg, Section, Mark, Line, Value_Start, Value_End, Found_Line);

      if Found_Line = 0 then
         return Default;
      end if;

      declare
         Value_As_String : constant String := Line (Value_Start .. Value_End);
         Val  : Long_Float;
         package LFIO is new Ada.Text_IO.Float_IO (Long_Float);
         Last : Positive;
      begin
         if Value_As_String'Length > 0 and then
           Is_Number_Start (Value_As_String (Value_As_String'First))
         then
            --  Val := Long_Float'Value(Value_As_String);
            --  ^ an old compiler doesn't like some floats repr. through 'Value
            LFIO.Get (Value_As_String, Val, Last);
            return Val;
         else
            Type_Error (Cfg, Value_As_String, "a floating-point number in line" & Integer'Image (Found_Line));
            return Default;
         end if;
      exception
         --  raised by 'Value or Get (respectively) on faulty argument:
         when Constraint_Error | Ada.IO_Exceptions.Data_Error =>
            Type_Error (Cfg, Value_As_String, "a floating-point number in line" & Integer'Image (Found_Line));
            return Default;
      end;
   end Value_Of;

   function Value_Of (Cfg     : Configuration;
                      Section : String;
                      Mark    : String;
                      Default : Boolean := False)
   return Boolean
   is
   begin
      return Boolean'Value (Value_Of (Cfg, Section, Mark, Boolean'Image (Default)));
   end Value_Of;

   --  Return True if one of the following conditions is met:
   --  o the Mark is within the Section, but no equal sign is in that line,
   --  o the Mark is set to either 1, True or Yes.
   --  All other cases return False.
   function Is_Set (Cfg     : Configuration;
                    Section : String;
                    Mark    : String)
   return Boolean is
      use Ada.Characters.Handling;
      Line              : String (1 .. Max_Line_Length);
      Value_Start       : Natural;
      Value_End         : Natural;
      Found_Line        : Natural;
   begin
      Get_Value (Cfg, Section, Mark, Line, Value_Start, Value_End, Found_Line);
      declare
        Value : constant String := To_Lower (Line (Value_Start .. Value_End));
      begin
         return Found_Line > 0 and then
           (Value = ""     or else Value = "1" or else
            Value = "true" or else Value = "yes");
      end;
   end Is_Set;

   function File_Name (Cfg : Configuration) return String is
   begin
     return Cfg.Config_File.all;
   end File_Name;

   --  List of strings, for memorizing a config file.

   type Ini_Line;
   type Ini_Line_Ptr is access Ini_Line;
   type Ini_Line is record
     next : Ini_Line_Ptr := null;
     line : Str_Ptr;
   end record;
   procedure Free is new Ada.Unchecked_Deallocation (Ini_Line, Ini_Line_Ptr);

   procedure Write_and_Free (Cfg          : in     Configuration;
                             new_contents : in out Ini_Line_Ptr)
   is
      curr, to_free : Ini_Line_Ptr := null;
      use Ada.Text_IO;
      File              : File_Type;
   begin
      Create (File, Out_File, Cfg.Config_File.all);
      curr := new_contents;
      while curr /= null loop
         Put_Line (File, curr.line.all);
         to_free := curr;
         curr := curr.next;
         Free (to_free.line);
         Free (to_free);
      end loop;
      Close (File);
      new_contents := null;
   end Write_and_Free;

   procedure Replace_Value (Cfg       : Configuration;
                            Section   : String;
                            Mark      : String;
                            New_Value : String)
   is
      Line              : String (1 .. Max_Line_Length);
      Value_Start       : Natural;
      Value_End         : Natural;
      Found_Line        : Natural;
      Equal_Ind         : Natural;
      Line_End          : Natural    := 0;
      Line_Count        : Natural    := 0;
      use Ada.Text_IO;
      File              : File_Type;
      use Ada.Strings.Fixed;
      --
      root, curr, new_ini_line : Ini_Line_Ptr := null;
   begin
      Get_Value (Cfg, Section, Mark, Line, Value_Start, Value_End, Found_Line);
      if Found_Line = 0 then
         raise Location_Not_Found with "(Config / .ini file)";
      end if;
      Open (File, In_File, Cfg.Config_File.all);
      Read_File :
      while not End_Of_File (File) loop
         Get_Line (File, Line, Line_End);
         Line_Count := Line_Count + 1;
         --
         new_ini_line := new Ini_Line;
         if root = null then
           root := new_ini_line;
         else
           curr.next := new_ini_line;
         end if;
         curr := new_ini_line;
         --
         if Line_Count = Found_Line then -- Change this line
            Equal_Ind := Index (Source  => Line (1 .. Line_End),
                               Pattern => (1 => Cfg.Variable_Terminator));
            if Equal_Ind < 1 then -- No '=' or so yet, will change...
              curr.line := new String'(Line (1 .. Line_End) & Cfg.Variable_Terminator & New_Value);
            else
              curr.line := new String'(Line (1 .. Equal_Ind) & New_Value);
            end if;
         else -- any other line: just copy
            curr.line := new String'(Line (1 .. Line_End));
         end if;
      end loop Read_File;
      Close (File);
      --  Now, write the new file
      Write_and_Free (Cfg, root);
   end Replace_Value;

   procedure Replace_Section (Cfg          : Configuration;
                              Section      : String;
                              New_Contents : String)
   is
      Line              : String (1 .. Max_Line_Length);
      Line_End          : Natural    := 0;
      Line_Count        : Natural    := 0;
      use Ada.Text_IO;
      File              : File_Type;
      --
      root, curr, new_ini_line : Ini_Line_Ptr := null;
      --
      procedure List_progress is
      begin
         new_ini_line := new Ini_Line;
         if root = null then
           root := new_ini_line;
         else
           curr.next := new_ini_line;
         end if;
         curr := new_ini_line;
      end List_progress;
      --
      Matched_section, Found_section : Boolean := False;
      I : Natural := New_Contents'First;
      use Ada.Characters.Handling;
   begin
      Open (File, In_File, Cfg.Config_File.all);
      Read_File :
      while not End_Of_File (File) loop
         Get_Line (File, Line, Line_End);
         Line_Count := Line_Count + 1;
         if Line_End > 0 and then
            Line (1) = '['
         then                     -- It is a section header.
            Matched_section :=
               Line_End >= 2 + Section'Length and then
                 (
                      (Cfg.Case_Sensitive and then
                       Line (2 .. 2 + Section'Length) = Section & ']'
                      )
                    or else
                      ((not Cfg.Case_Sensitive) and then
                        To_Lower (Line (2 .. 2 + Section'Length)) = To_Lower (Section) & ']'
                      )
                 );
            List_progress;
            curr.line := new String'(Line (1 .. Line_End));
            if Matched_section then
               Found_section := True;
               for J in New_Contents'Range loop -- copy new contents
                  if New_Contents (J) = LF then
                     List_progress;
                     curr.line := new String'(New_Contents (I .. J - 1));
                     I := J + 1;
                  end if;
                  if J = New_Contents'Last then
                     List_progress;
                     curr.line := new String'(New_Contents (I .. J));
                  end if;
                  --  NB: we can have have a LF at the end, hence both "if"-s
               end loop;
            end if;
         elsif Matched_section then
            null; -- don't copy old contents
         else
            List_progress;
            curr.line := new String'(Line (1 .. Line_End));
         end if;
      end loop Read_File;
      Close (File);
      --  Now, write the new file
      Write_and_Free (Cfg, root);
      if not Found_section then
         raise Section_Not_Found with "(Config / .ini file)";
      end if;
   end Replace_Section;

   --  Disable the Mark using a semicolon prefix
   --
   procedure Disable (Cfg      : Configuration;
                      Section  : String;
                      Mark     : String)
   is
      Line              : String (1 .. Max_Line_Length);
      Value_Start       : Natural;
      Value_End         : Natural;
      Found_Line        : Natural;
      Line_End          : Natural    := 0;
      Line_Count        : Natural    := 0;
      use Ada.Text_IO;
      File              : File_Type;
      --
      root, curr, new_ini_line : Ini_Line_Ptr := null;
   begin
      Get_Value (Cfg, Section, Mark, Line, Value_Start, Value_End, Found_Line);
      if Found_Line = 0 then
         raise Location_Not_Found with "(Config / .ini file)";
      end if;
      Open (File, In_File, Cfg.Config_File.all);
      Read_File :
      while not End_Of_File (File) loop
         Get_Line (File, Line, Line_End);
         Line_Count := Line_Count + 1;
         --
         new_ini_line := new Ini_Line;
         if root = null then
           root := new_ini_line;
         else
           curr.next := new_ini_line;
         end if;
         curr := new_ini_line;
         --
         if Line_Count = Found_Line then -- Comment out this line
            curr.line := new String'("; " & Line (1 .. Line_End));
         else -- any other line: just copy
            curr.line := new String'(Line (1 .. Line_End));
         end if;
      end loop Read_File;
      Close (File);
      --  Now, write the new file
      Write_and_Free (Cfg, root);
   end Disable;

   function Read_Sections (Cfg : Configuration) return Section_List
   is
      use Ada.Text_IO;
      use Ada.Strings.Fixed;

      File          : File_Type;
      Section_Names : Section_List;
   begin
      Open (File, In_File, Cfg.Config_File.all);

      Read_File :
      while not End_Of_File (File) loop
         declare
            Cfg_Line : constant String := Get_Line (File);
            Sect_End : Natural;
         begin
            if Cfg_Line'Length > 1 and then Cfg_Line (Cfg_Line'First) = '[' then
               Sect_End := Index (Cfg_Line, "]") - 1;
               String_Vector.Append (Section_Names, Cfg_Line (Cfg_Line'First + 1 .. Sect_End));
            end if;
         end;
      end loop Read_File;
      Close (File);
      return Section_Names;
   end Read_Sections;

end Config;