gnoga_2.1.2_5f127c56/src/gnoga.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
------------------------------------------------------------------------------
--                                                                          --
--                   GNOGA - The GNU Omnificent GUI for Ada                 --
--                                                                          --
--                                G N O G A                                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                                                                          --
--                     Copyright (C) 2014 David Botton                      --
--                                                                          --
--  This library is free software;  you can redistribute it and/or modify   --
--  it under terms of the  GNU General Public License  as published by the  --
--  Free Software  Foundation;  either version 3,  or (at your  option) any --
--  later version. This library is distributed in the hope that it will be  --
--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file might be   --
--  covered by the  GNU Public License.                                     --
--                                                                          --
--  For more information please go to http://www.gnoga.com                  --
------------------------------------------------------------------------------

with Ada.Calendar;
with Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones;
with Ada.Integer_Text_IO;
with Ada.Task_Termination;

with UXStrings.Text_IO;

package body Gnoga is

   Use_File        : Boolean := False;
   Automatic_Flush : Boolean := False;
   Log_File        : UXStrings.Text_IO.File_Type;

   -------------------
   -- Escape_Quotes --
   -------------------

   function Escape_Quotes
     (S : String)
      return String
   is

      function Translate_Character
        (C : Unicode_Character)
         return String;

      function Translate_Character
        (C : Unicode_Character)
         return String
      is
      begin
         if C = '"' then
            return "\x22";
         elsif C = ''' then
            return "\x27";
         elsif C = '\' then
            return "\x5C";
         elsif C = Unicode_Character'Val (10) then
            return "\x0A";
         elsif C = Unicode_Character'Val (13) then
            return "\x0D";
         else
            return From_Unicode (C);
         end if;
      end Translate_Character;

      R : String;
   begin
      for C in S loop
         Append (R, Translate_Character (S (C)));
      end loop;

      return R;
   end Escape_Quotes;

   ---------------------
   -- Unescape_Quotes --
   ---------------------

   function Unescape_Quotes
     (S : String)
      return String
   is

      C : Integer := S.First;

      function Translate_Character return String;

      function Translate_Character return String is
      begin
         if C < S.Last - 1 then
            if S.Slice (C, C + 1) = "\\" then
               C := C + 2;
               return "\";
            elsif S.Slice (C, C + 1) = "\x" then
               declare
                  H : constant Integer := Value (S.Slice (C + 2, C + 3), 16);
               begin
                  C := C + 4;

                  return From_Unicode (Unicode_Character'Val (H));
               end;
            end if;
         end if;

         declare
            R : constant String := S.Slice (C, C);
         begin
            C := C + 1;
            return R;
         end;
      end Translate_Character;

      R : String;
   begin
      loop
         Append (R, Translate_Character);
         exit when C > S.Last;
      end loop;

      return R;
   end Unescape_Quotes;

   ----------------
   -- URL_Encode --
   ----------------

   function URL_Encode
     (S        : String;
      Encoding : String := "")
      return String
   is

      function Translate_Character
        (C : Character)
         return String;

      function Translate_Character
        (C : Character)
         return String
      is
      begin
         if C in 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '.' | '-' | '*' | '_' then
            return From_ASCII (C);
         elsif C = ' ' then
            return "+";
         else
            declare
               V : ASCII_Character_Array (1 .. 6); -- 16#HH#
            begin
               Ada.Integer_Text_IO.Put (V, Character'Pos (C), 16);
               return From_ASCII ("%" & V (4 .. 5));
            end;
         end if;
      end Translate_Character;

      R : String;
      T : constant Latin_1_Character_Array :=
        (if Encoding = "UTF-8" then Latin_1_Character_Array (To_UTF_8 (S)) else To_Latin_1 (S));
   begin
      for C in 1 .. T'Length loop
         Append (R, Translate_Character (T (C)));
      end loop;

      return R;
   end URL_Encode;

   ----------------
   -- URL_Decode --
   ----------------

   function URL_Decode
     (S        : String;
      Encoding : String := "")
      return String
   is
      C : Integer := S.First;

      function Translate_Character return Unicode_Character;

      function Translate_Character return Unicode_Character is
         R : Unicode_Character := S (C);
      begin
         if R = '+' then
            R := ' ';
         elsif R = '%' and C < S.Last - 1 then
            R := Unicode_Character'Val (Value (S.Slice (C + 1, C + 2), 16));
            C := C + 2;
         end if;
         C := C + 1;
         return R;
      end Translate_Character;

      R : String;
   begin
      while C in S.First .. S.Last loop
         Append (R, Translate_Character);
      end loop;

      if Encoding = "UTF-8" then
         return From_UTF_8 (UTF_8_Character_Array (To_Latin_1 (R)));
      else
         return R;
      end if;
   end URL_Decode;

   ---------------
   -- Left_Trim --
   ---------------

   function Left_Trim
     (S : String)
      return String
   is
      Space : constant Unicode_Character := ' ';
      Tab   : constant Unicode_Character := Unicode_Character'Val (9);
   begin
      if S.Length = 0 then
         return S;
      end if;

      if S (S.First) = Space or S (S.First) = Tab then
         return Left_Trim (S.Slice ((S.First + 1), S.Last));
      else
         return S;
      end if;
   end Left_Trim;

   ----------------
   -- Right_Trim --
   ----------------

   function Right_Trim
     (S : String)
      return String
   is
      Space : constant Unicode_Character := ' ';
      Tab   : constant Unicode_Character := Unicode_Character'Val (9);
   begin
      if S.Length = 0 then
         return S;
      end if;

      if S (S.Last) = Space or S (S.Last) = Tab then
         return Right_Trim (S.Slice (S.First, (S.Last - 1)));
      else
         return S;
      end if;
   end Right_Trim;

   -----------------------
   -- Left_Trim_Slashes --
   -----------------------

   function Left_Trim_Slashes
     (S : String)
      return String
   is
      Space : constant Unicode_Character := ' ';
      Tab   : constant Unicode_Character := Unicode_Character'Val (9);
      Slash : constant Unicode_Character := '/';
   begin
      if S.Length = 0 then
         return S;
      end if;

      if S (S.First) = Space or S (S.First) = Tab or S (S.First) = Slash then
         return Left_Trim_Slashes (S.Slice ((S.First + 1), S.Last));
      else
         return S;
      end if;
   end Left_Trim_Slashes;

   ------------------------
   -- Right_Trim_Slashes --
   ------------------------

   function Right_Trim_Slashes
     (S : String)
      return String
   is
      Space : constant Unicode_Character := ' ';
      Tab   : constant Unicode_Character := Unicode_Character'Val (9);
      Slash : constant Unicode_Character := '/';
   begin
      if S.Length = 0 then
         return S;
      end if;

      if S (S.Last) = Space or S (S.Last) = Tab or S (S.Last) = Slash then
         return Right_Trim_Slashes (S.Slice (S.First, (S.Last - 1)));
      else
         return S;
      end if;
   end Right_Trim_Slashes;

   --------------------
   -- String_Replace --
   --------------------

   procedure String_Replace
     (Source      : in out String;
      Pattern     : in     String;
      Replacement : in     String)
   is

      I : Natural;
   begin
      loop
         I := Index (Source => Source, Pattern => Pattern);

         exit when I = 0;

         Replace_Slice (Source => Source, Low => I, High => I + Pattern.Length - 1, By => Replacement);
      end loop;
   end String_Replace;

   ----------------------
   -- Write_To_Console --
   ----------------------

   procedure Write_To_Console (Message : in String) is
   begin
      UXStrings.Text_IO.Put_Line (Message);
   end Write_To_Console;

   -----------------
   -- Log_To_File --
   -----------------

   procedure Log_To_File
     (File_Name  : in String;
      Flush_Auto : in Boolean := False)
   is
   begin
      UXStrings.Text_IO.Create
        (File   => Log_File, Mode => UXStrings.Text_IO.Append_File, Name => File_Name, Scheme => UTF_8,
         Ending => UXStrings.Text_IO.LF_Ending);

      Use_File        := True;
      Automatic_Flush := Flush_Auto;
   exception
      when E : others =>
         Log ("Error failed to open log file " & File_Name, E);
   end Log_To_File;

   ---------
   -- Log --
   ---------

   procedure Log (Message : in String) is
      T            : constant Ada.Calendar.Time := Ada.Calendar.Clock;
      Date_Message : constant String            :=
        From_ASCII
          (Ada.Calendar.Formatting.Image
             (Date => T, Include_Time_Fraction => True, Time_Zone => Ada.Calendar.Time_Zones.UTC_Time_Offset (T)) &
           " : ") &
        Message;
   begin
      if Use_File then
         UXStrings.Text_IO.Put_Line (Log_File, Date_Message);
         if Automatic_Flush then
            Flush_Log;
         end if;
      else
         Write_To_Console (Date_Message);
      end if;
   end Log;

   procedure Log
     (Message    : in String;
      Occurrence : in Ada.Exceptions.Exception_Occurrence)
   is
   begin
      Log (Message & From_UTF_8 (Ada.Exceptions.Exception_Information (Occurrence)));
   end Log;

   procedure Log (Occurrence : in Ada.Exceptions.Exception_Occurrence) is
   begin
      Log (From_UTF_8 (Ada.Exceptions.Exception_Information (Occurrence)));
   end Log;

   ---------------
   -- Flush_Log --
   ---------------

   procedure Flush_Log is
   begin
      UXStrings.Text_IO.Flush (Log_File);
   end Flush_Log;

   --------------------------------
   -- Activate_Exception_Handler --
   --------------------------------

   protected Exception_Handler is
      procedure Log
        (Cause      : in Ada.Task_Termination.Cause_Of_Termination;
         Id         : in Ada.Task_Identification.Task_Id;
         Occurrence : in Ada.Exceptions.Exception_Occurrence);
   end Exception_Handler;

   protected body Exception_Handler is
      procedure Log
        (Cause      : in Ada.Task_Termination.Cause_Of_Termination;
         Id         : in Ada.Task_Identification.Task_Id;
         Occurrence : in Ada.Exceptions.Exception_Occurrence)
      is
         use all type Ada.Task_Termination.Cause_Of_Termination;
      begin
         case Cause is
            when Normal =>
               Log (From_UTF_8 ("Normal exit of task: " & Ada.Task_Identification.Image (Id)));
            when Abnormal =>
               Log (From_UTF_8 ("Abnormal exit of task: " & Ada.Task_Identification.Image (Id)));
            when Unhandled_Exception =>
               Log (From_UTF_8 ("Unhandled exception in task: " & Ada.Task_Identification.Image (Id)));
               Log (Occurrence);
         end case;
      end Log;
   end Exception_Handler;

   procedure Activate_Exception_Handler (Id : Ada.Task_Identification.Task_Id) is
   begin
      Ada.Task_Termination.Set_Specific_Handler (Id, Exception_Handler.Log'Access);
   end Activate_Exception_Handler;

begin
   --  Change the default to LF and UTF-8
   UXStrings.Text_IO.Ending (UXStrings.Text_IO.Current_Output, UXStrings.Text_IO.LF_Ending);
   UXStrings.Text_IO.Line_Mark (UXStrings.Text_IO.LF_Ending);
   UXStrings.Text_IO.Scheme (UXStrings.Text_IO.Current_Output, UTF_8);
end Gnoga;