utilada_aws_2.5.0_f65f9ba9/src/base/properties/util-properties.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
-----------------------------------------------------------------------
--  util-properties -- Generic name/value property management
--  Copyright (C) 2001 - 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Ada.Strings.Fixed;
with Ada.Strings.Unbounded.Text_IO;
with Ada.Strings.Maps;
with Ada.Unchecked_Deallocation;
with Util.Files;
with Util.Beans.Objects.Maps;
package body Util.Properties is

   use Ada.Text_IO;
   use Ada.Strings.Unbounded.Text_IO;
   use Implementation;
   use Util.Beans.Objects;

   procedure Free is
     new Ada.Unchecked_Deallocation (Object => Implementation.Shared_Manager'Class,
                                     Name   => Implementation.Shared_Manager_Access);

   Trim_Chars : constant Ada.Strings.Maps.Character_Set
     := Ada.Strings.Maps.To_Set (" " & ASCII.HT);

   package body Implementation is

      procedure Create (Self : in out Util.Properties.Manager'Class) is
      begin
         if Self.Impl = null then
            Self.Impl := Allocator;
         elsif Self.Impl.Is_Shared then
            declare
               Old     : Implementation.Shared_Manager_Access := Self.Impl;
               Is_Zero : Boolean;
               Impl    : constant Implementation.Manager_Access := Create_Copy (Self.Impl.all);
            begin
               Self.Impl := Implementation.Shared_Manager'Class (Impl.all)'Access;
               Old.Finalize (Is_Zero);
               if Is_Zero then
                  Free (Old);
               end if;
            end;
         end if;
      end Create;

      procedure Initialize (Self : in out Util.Properties.Manager'Class) is
         Release : Boolean;
      begin
         if Self.Impl /= null then
            Self.Impl.Finalize (Release);
            if Release then
               Free (Self.Impl);
            end if;
         end if;
         Self.Impl := Allocator;
      end Initialize;

      package body Shared_Implementation is

         overriding
         function Is_Shared (Self : in Manager) return Boolean is
         begin
            return not Self.Shared and then Util.Concurrent.Counters.Value (Self.Count) > 1;
         end Is_Shared;

         overriding
         procedure Set_Shared (Self   : in out Manager;
                               Shared : in Boolean) is
         begin
            Self.Shared := Shared;
         end Set_Shared;

         overriding
         procedure Adjust (Self : in out Manager) is
         begin
            Util.Concurrent.Counters.Increment (Self.Count);
         end Adjust;

         overriding
         procedure Finalize (Self    : in out Manager;
                             Release : out Boolean) is
         begin
            Util.Concurrent.Counters.Decrement (Self.Count, Release);
         end Finalize;

      end Shared_Implementation;

   end Implementation;

   type Property_Map is limited new Implementation.Manager with record
      Props  : Util.Beans.Objects.Maps.Map_Bean;
   end record;

   --  Get the value identified by the name.
   --  If the name cannot be found, the method should return the Null object.
   overriding
   function Get_Value (From : in Property_Map;
                       Name : in String) return Util.Beans.Objects.Object;

   --  Set the value identified by the name.
   --  If the map contains the given name, the value changed.
   --  Otherwise name is added to the map and the value associated with it.
   overriding
   procedure Set_Value (From  : in out Property_Map;
                        Name  : in String;
                        Value : in Util.Beans.Objects.Object);

   --  Returns TRUE if the property exists.
   overriding
   function Exists (Self : in Property_Map;
                    Name : in String)
                    return Boolean;

   --  Remove the property given its name.
   overriding
   procedure Remove (Self : in out Property_Map;
                     Name : in String);

   --  Iterate over the properties and execute the given procedure passing the
   --  property name and its value.
   overriding
   procedure Iterate (Self    : in Property_Map;
                      Process : access procedure (Name : in String;
                                                  Item : in Util.Beans.Objects.Object));

   --  Deep copy of properties stored in 'From' to 'To'.
   overriding
   function Create_Copy (Self : in Property_Map)
                         return Implementation.Manager_Access;

   package Shared_Property_Implementation is
      new Implementation.Shared_Implementation (Property_Map);

   subtype Property_Manager is Shared_Property_Implementation.Manager;
   type Property_Manager_Access is access all Property_Manager'Class;

   --  ------------------------------
   --  Get the value identified by the name.
   --  If the name cannot be found, the method should return the Null object.
   --  ------------------------------
   overriding
   function Get_Value (From : in Property_Map;
                       Name : in String) return Util.Beans.Objects.Object is
   begin
      return From.Props.Get_Value (Name);
   end Get_Value;

   --  ------------------------------
   --  Set the value identified by the name.
   --  If the map contains the given name, the value changed.
   --  Otherwise name is added to the map and the value associated with it.
   --  ------------------------------
   overriding
   procedure Set_Value (From  : in out Property_Map;
                        Name  : in String;
                        Value : in Util.Beans.Objects.Object) is
   begin
      From.Props.Set_Value (Name, Value);
   end Set_Value;

   --  ------------------------------
   --  Returns TRUE if the property exists.
   --  ------------------------------
   overriding
   function Exists (Self : in Property_Map;
                    Name : in String)
                    return Boolean is
   begin
      return Self.Props.Contains (Name);
   end Exists;

   --  ------------------------------
   --  Remove the property given its name.
   --  ------------------------------
   overriding
   procedure Remove (Self : in out Property_Map;
                     Name : in String) is
   begin
      Self.Props.Delete (Name);
   end Remove;

   --  ------------------------------
   --  Iterate over the properties and execute the given procedure passing the
   --  property name and its value.
   --  ------------------------------
   overriding
   procedure Iterate (Self    : in Property_Map;
                      Process : access procedure (Name : in String;
                                                  Item : in Util.Beans.Objects.Object)) is
      Iter : Util.Beans.Objects.Maps.Cursor := Self.Props.First;
   begin
      while Util.Beans.Objects.Maps.Has_Element (Iter) loop
         Util.Beans.Objects.Maps.Query_Element (Iter, Process);
         Util.Beans.Objects.Maps.Next (Iter);
      end loop;
   end Iterate;

   --  ------------------------------
   --  Deep copy of properties stored in 'From' to 'To'.
   --  ------------------------------
   overriding
   function Create_Copy (Self : in Property_Map)
                         return Implementation.Manager_Access is
      Result : constant Property_Manager_Access := new Property_Manager;
   begin
      --  SCz 2017-07-20: the map assignment is buggy on GNAT 2016 because the copy of the
      --  object also copies the internal Lock and Busy flags which makes the target copy
      --  unusable because the Lock and Busy flag prevents modifications.  Instead of the
      --  Ada assignment, we use the Assign procedure which makes the deep copy of the map.
      --  Result.Props := Self.Props;
      Result.Props.Assign (Self.Props);
      return Result.all'Access;
   end Create_Copy;

   function Allocate_Property return Implementation.Shared_Manager_Access is
     (new Property_Manager);

   --  Create a property implementation if there is none yet.
   procedure Check_And_Create_Impl is
      new Implementation.Create (Allocator => Allocate_Property);

   --  ------------------------------
   --  Get the value identified by the name.
   --  If the name cannot be found, the method should return the Null object.
   --  ------------------------------
   overriding
   function Get_Value (From : in Manager;
                       Name : in String) return Util.Beans.Objects.Object is
   begin
      if From.Impl = null then
         return Util.Beans.Objects.Null_Object;
      else
         return From.Impl.Get_Value (Name);
      end if;
   end Get_Value;

   --  ------------------------------
   --  Set the value identified by the name.
   --  If the map contains the given name, the value changed.
   --  Otherwise name is added to the map and the value associated with it.
   --  ------------------------------
   overriding
   procedure Set_Value (From  : in out Manager;
                        Name  : in String;
                        Value : in Util.Beans.Objects.Object) is
   begin
      Check_And_Create_Impl (From);
      From.Impl.Set_Value (Name, Value);
   end Set_Value;

   --  ------------------------------
   --  Returns TRUE if the property manager is empty.
   --  ------------------------------
   function Is_Empty (Self : in Manager'Class) return Boolean is
   begin
      if Self.Impl = null then
         return True;
      else
         return False;
      end if;
   end Is_Empty;

   function Exists (Self : in Manager'Class;
                    Name : in String) return Boolean is
   begin
      --  There is not yet an implementation, no property
      return Self.Impl /= null and then Self.Impl.Exists (Name);
   end Exists;

   function Exists (Self : in Manager'Class;
                    Name : in Unbounded_String) return Boolean is
   begin
      --  There is not yet an implementation, no property
      return Self.Impl /= null and then Self.Impl.Exists (-Name);
   end Exists;

   function Get (Self : in Manager'Class;
                 Name : in String) return Unbounded_String is
      Value : constant Util.Beans.Objects.Object := Self.Get_Value (Name);
   begin
      if Util.Beans.Objects.Is_Null (Value) then
         raise NO_PROPERTY with "No property: '" & Name & "'";
      end if;

      return To_Unbounded_String (Value);
   end Get;

   function Get (Self : in Manager'Class;
                 Name : in Unbounded_String) return Unbounded_String is
   begin
      return Self.Get (-Name);
   end Get;

   function Get (Self : in Manager'Class;
                 Name : in String) return String is
      Value : constant Util.Beans.Objects.Object := Self.Get_Value (Name);
   begin
      if Util.Beans.Objects.Is_Null (Value) then
         raise NO_PROPERTY with "No property: '" & Name & "'";
      end if;

      return To_String (Value);
   end Get;

   function Get (Self : in Manager'Class;
                 Name : in Unbounded_String) return String is
   begin
      return Self.Get (-Name);
   end Get;

   function Get (Self : in Manager'Class;
                 Name : in String;
                 Default : in String) return String is
   begin
      if Exists (Self, Name) then
         return Get (Self, Name);
      else
         return Default;
      end if;
   end Get;

   --  ------------------------------
   --  Returns a property manager that is associated with the given name.
   --  Raises NO_PROPERTY if there is no such property manager or if a property exists
   --  but is not a property manager.
   --  ------------------------------
   function Get (Self : in Manager'Class;
                 Name : in String) return Manager is
      Value : constant Util.Beans.Objects.Object := Self.Get_Value (Name);
      Bean  : constant access Util.Beans.Basic.Readonly_Bean'Class
        := Util.Beans.Objects.To_Bean (Value);
   begin
      if Bean = null or else not (Bean.all in Manager'Class) then
         raise NO_PROPERTY with "No property '" & Name & "'";
      end if;
      return Manager (Bean.all);
   end Get;

   --  ------------------------------
   --  Create a property manager and associated it with the given name.
   --  ------------------------------
   function Create (Self : in out Manager'Class;
                    Name : in String) return Manager is
      Result : Manager_Access;
   begin
      Check_And_Create_Impl (Self);

      --  Create the property manager and make it shared so that it can be populated by
      --  the caller.
      Result := new Manager;
      Check_And_Create_Impl (Result.all);
      Result.Impl.Set_Shared (True);
      Self.Impl.Set_Value (Name, Util.Beans.Objects.To_Object (Result.all'Access));
      return Manager (Result.all);
   end Create;

   --  ------------------------------
   --  Set the value of the property.  The property is created if it
   --  does not exists.
   --  ------------------------------
   procedure Set (Self : in out Manager'Class;
                  Name : in String;
                  Item : in String) is
   begin
      Self.Set_Value (Name, To_Object (Item));
   end Set;

   --  ------------------------------
   --  Set the value of the property.  The property is created if it
   --  does not exists.
   --  ------------------------------
   procedure Set (Self : in out Manager'Class;
                  Name : in String;
                  Item : in Unbounded_String) is
   begin
      Self.Set_Value (Name, To_Object (Item));
   end Set;

   --  ------------------------------
   --  Set the value of the property.  The property is created if it
   --  does not exists.
   --  ------------------------------
   procedure Set (Self : in out Manager'Class;
                  Name : in Unbounded_String;
                  Item : in Unbounded_String) is
   begin
      Self.Set_Value (-Name, To_Object (Item));
   end Set;

   --  ------------------------------
   --  Remove the property given its name.
   --  ------------------------------
   procedure Remove (Self : in out Manager'Class;
                     Name : in String) is
   begin
      if Self.Impl = null then
         raise NO_PROPERTY with "No property '" & Name & "'";
      end if;
      Check_And_Create_Impl (Self);
      Remove (Self.Impl.all, Name);
   end Remove;

   --  ------------------------------
   --  Remove the property given its name.
   --  ------------------------------
   procedure Remove (Self : in out Manager'Class;
                     Name : in Unbounded_String) is
   begin
      Self.Remove (-Name);
   end Remove;

   --  ------------------------------
   --  Iterate over the properties and execute the given procedure passing the
   --  property name and its value.
   --  ------------------------------
   procedure Iterate (Self    : in Manager'Class;
                      Process : access procedure (Name : in String;
                                                  Item : in Util.Beans.Objects.Object)) is
   begin
      if Self.Impl /= null then
         Self.Impl.Iterate (Process);
      end if;
   end Iterate;

   --  ------------------------------
   --  Get the property manager represented by the item value.
   --  Raise the Conversion_Error exception if the value is not a property manager.
   --  ------------------------------
   function To_Manager (Item : in Value) return Manager is
      Bean  : constant access Util.Beans.Basic.Readonly_Bean'Class
        := Util.Beans.Objects.To_Bean (Item);
   begin
      if Bean = null then
         raise Util.Beans.Objects.Conversion_Error;
      end if;
      if Bean.all in Util.Beans.Objects.Maps.Map_Bean'Class then
         declare
            Props  : Manager;
            Impl   : Property_Manager_Access;
         begin
            Check_And_Create_Impl (Props);
            Impl := Property_Manager'Class (Props.Impl.all)'Access;
            Impl.Props.Assign (Util.Beans.Objects.Maps.Map_Bean (Bean.all));
            return Props;
         end;
      end if;
      if not (Bean.all in Manager'Class) then
         raise Util.Beans.Objects.Conversion_Error;
      end if;
      return Manager (Bean.all);
   end To_Manager;

   --  ------------------------------
   --  Returns True if the item value represents a property manager.
   --  ------------------------------
   function Is_Manager (Item : in Value) return Boolean is
      Bean  : constant access Util.Beans.Basic.Readonly_Bean'Class
        := Util.Beans.Objects.To_Bean (Item);
   begin
      return Bean /= null and then (Bean.all in Manager'Class);
   end Is_Manager;

   --  ------------------------------
   --  Collect the name of the properties defined in the manager.
   --  When a prefix is specified, only the properties starting with the prefix are
   --  returned.
   --  ------------------------------
   procedure Get_Names (Self   : in Manager;
                        Into   : in out Util.Strings.Vectors.Vector;
                        Prefix : in String := "") is
      procedure Process (Name : in String;
                         Item : in Util.Beans.Objects.Object);

      procedure Process (Name : in String;
                         Item : in Util.Beans.Objects.Object) is
         pragma Unreferenced (Item);
      begin
         if Prefix'Length = 0 or else Ada.Strings.Fixed.Index (Name, Prefix) = 1 then
            Into.Append (Name);
         end if;
      end Process;
   begin
      if Self.Impl /= null then
         Self.Impl.Iterate (Process'Access);
      end if;
   end Get_Names;

   overriding
   procedure Adjust (Object : in out Manager) is
   begin
      if Object.Impl /= null then
         Object.Impl.Adjust;
      end if;
   end Adjust;

   overriding
   procedure Finalize (Object : in out Manager) is
      Is_Zero : Boolean;
   begin
      if Object.Impl /= null then
         Object.Impl.Finalize (Is_Zero);
         if Is_Zero then
            Free (Object.Impl);
         end if;
      end if;
   end Finalize;

   procedure Load_Properties (Self   : in out Manager'Class;
                              File   : in File_Type;
                              Prefix : in String := "";
                              Strip  : in Boolean := False) is
      pragma Unreferenced (Strip);
      Line    : Unbounded_String;
      Name    : Unbounded_String;
      Value   : Unbounded_String;
      Current : Manager;
      Pos     : Natural;
      Len     : Natural;
   begin
      Check_And_Create_Impl (Self);
      Current := Manager (Self);
      Current.Impl.Set_Shared (True);
      while not End_Of_File (File) loop
         Line := Get_Line (File);
         Len  := Length (Line);
         if Len /= 0 then
            case Element (Line, 1) is
               when '!' | '#' =>
                  null;

               when '[' =>
                  Pos := Index (Line, "]");
                  if Pos > 0 then
                     Current := Self.Create (To_String (Unbounded_Slice (Line, 2, Pos - 1)));
                  end if;

               when others =>
                  Pos := Index (Line, "=");
                  if Pos > 0 and then Prefix'Length > 0 and then Index (Line, Prefix) = 1 then
                     Name  := Unbounded_Slice (Line, Prefix'Length + 1, Pos - 1);
                     Value := Tail (Line, Len - Pos);
                     Trim (Name, Trim_Chars, Trim_Chars);
                     Trim (Value, Trim_Chars, Trim_Chars);
                     Current.Set (Name, Value);

                  elsif Pos > 0 and then Prefix'Length = 0 then
                     Name  := Head (Line, Pos - 1);
                     Value := Tail (Line, Len - Pos);
                     Trim (Name, Trim_Chars, Trim_Chars);
                     Trim (Value, Trim_Chars, Trim_Chars);
                     Current.Set (Name, Value);

                  end if;

            end case;
         end if;
      end loop;
      Self.Impl.Set_Shared (False);

   exception
      when End_Error =>
         return;
   end Load_Properties;

   procedure Load_Properties (Self   : in out Manager'Class;
                              Path   : in String;
                              Prefix : in String := "";
                              Strip  : in Boolean := False) is
      F : File_Type;
   begin
      Open (F, In_File, Path);
      Load_Properties (Self, F, Prefix, Strip);
      Close (F);
   end Load_Properties;

   --  ------------------------------
   --  Save the properties in the given file path.
   --  ------------------------------
   procedure Save_Properties (Self   : in out Manager'Class;
                              Path   : in String;
                              Prefix : in String := "") is
      procedure Save_Property (Name : in String;
                               Item : in Util.Beans.Objects.Object);

      Tmp      : constant String := Path & ".tmp";
      F        : File_Type;
      Recurse  : Boolean := False;
      Level    : Natural := 0;
      Ini_Mode : Boolean := False;

      procedure Save_Property (Name : in String;
                               Item : in Util.Beans.Objects.Object) is
         Bean  : constant access Util.Beans.Basic.Readonly_Bean'Class
           := Util.Beans.Objects.To_Bean (Item);
      begin
         if Bean /= null then
            if Recurse then
               New_Line (F);
               Put (F, "[");
               Put (F, Name);
               Put (F, "]");
               New_Line (F);
               Level := Level + 1;
               To_Manager (Item).Iterate (Save_Property'Access);
               Level := Level - 1;
            else
               Ini_Mode := True;
            end if;
         elsif not Recurse or else Level > 0 then
            if Prefix'Length > 0 then
               if Name'Length < Prefix'Length then
                  return;
               end if;
               if Name (Name'First .. Prefix'Length - 1) /= Prefix then
                  return;
               end if;
            end if;
            Put (F, Name);
            Put (F, "=");
            Put (F, Util.Beans.Objects.To_String (Item));
            New_Line (F);
         end if;
      end Save_Property;

   begin
      Create (File => F, Name => Tmp);
      Self.Iterate (Save_Property'Access);
      if Ini_Mode then
         Recurse := True;
         Self.Iterate (Save_Property'Access);
      end if;
      Close (File => F);

      --  Do a system atomic rename of old file in the new file.
      --  Ada.Directories.Rename does not allow this.
      Util.Files.Rename (Tmp, Path);
   end Save_Properties;

   --  ------------------------------
   --  Copy the properties from FROM which start with a given prefix.
   --  If the prefix is empty, all properties are copied.  When <b>Strip</b> is True,
   --  the prefix part is removed from the property name.
   --  ------------------------------
   procedure Copy (Self   : in out Manager'Class;
                   From   : in Manager'Class;
                   Prefix : in String := "";
                   Strip  : in Boolean := False) is
      procedure Process (Name : in String;
                         Value : in Util.Beans.Objects.Object);

      procedure Process (Name  : in String;
                         Value : in Util.Beans.Objects.Object) is
      begin
         if Prefix'Length > 0 then
            if Name'Length < Prefix'Length then
               return;
            end if;
            if Name (Name'First .. Name'First + Prefix'Length - 1) /= Prefix then
               return;
            end if;
            if Strip then
               Self.Set_Value (Name (Name'First + Prefix'Length .. Name'Last), Value);
               return;
            end if;
         end if;
         Self.Set_Value (Name, Value);
      end Process;

   begin
      From.Iterate (Process'Access);
   end Copy;

end Util.Properties;