File: memory_streams.adb

package info (click to toggle)
libaws 20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: ada: 95,505; python: 2,270; ansic: 1,017; makefile: 829; xml: 235; javascript: 202; java: 112; sh: 106
file content (494 lines) | stat: -rw-r--r-- 14,508 bytes parent folder | download
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
------------------------------------------------------------------------------
--                       Generic memory stream                              --
--                                                                          --
--                Copyright (C) 2003-2015, Dmitriy Anisimkov                --
--                                                                          --
--  This library is free software; you can redistribute it and/or modify    --
--  it under the terms of the GNU General Public License as published by    --
--  the Free Software Foundation; either version 2 of the License, 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.  See the GNU       --
--  General Public License for more details.                                --
--                                                                          --
--  You should have received a copy of the GNU General Public License       --
--  along with this library; if not, write to the Free Software Foundation, --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
--                                                                          --
--  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.                                     --
------------------------------------------------------------------------------

pragma Ada_2012;

with Ada.Unchecked_Deallocation;

package body Memory_Streams is

   procedure Free (Item : in out Buffer_Access);

   procedure Free is
      new Ada.Unchecked_Deallocation (Element_Array, Element_Access);

   procedure Trim_Last_Block (Stream : in out Stream_Type);
   --  Remove unused space from last buffer

   function First (Item : in Buffer_Access) return Element_Index with Inline;

   function Last (Item : in Buffer_Access) return Element_Index with Inline;

   ------------
   -- Append --
   ------------

   procedure Append
     (Stream : in out Stream_Type;
      Value  : in     Element_Array;
      Trim   : in     Boolean := False) is
   begin
      if Value'Length = 0 then
         if Trim then
            Trim_Last_Block (Stream);
         end if;

         return;
      end if;

      if Stream.First = null then
         Stream.First := new Buffer_Type (False);

         if Value'Length >= First_Block_Length or else Trim then
            Stream.First.Data := new Element_Array (1 .. Value'Length);
         else
            Stream.First.Data := new Element_Array (1 .. First_Block_Length);
         end if;

         Stream.First.Data (1 .. Value'Length) := Value;

         Stream.Current     := Stream.First;
         Stream.Last        := Stream.First;
         Stream.Last_Length := Value'Length;

      elsif Stream.Last.Steady
        or else Stream.Last_Length = Stream.Last.Data'Length
      then
         Stream.Last.Next := new Buffer_Type (False);
         Stream.Last      := Stream.Last.Next;

         if Value'Length >= Next_Block_Length or else Trim then
            Stream.Last.Data := new Element_Array (1 .. Value'Length);
         else
            Stream.Last.Data := new Element_Array (1 .. Next_Block_Length);
         end if;

         Stream.Last.Data (1 .. Value'Length) := Value;

         Stream.Last_Length := Value'Length;

      else
         declare
            Block_Length : constant Element_Offset :=
                             Stream.Last_Length + Value'Length;
         begin
            if Block_Length <= Stream.Last.Data'Length then
               Stream.Last.Data
                 (Stream.Last_Length + 1 .. Block_Length) := Value;
               Stream.Last_Length := Block_Length;

               if Trim then
                  Trim_Last_Block (Stream);
               end if;

            else
               declare
                  Split_Value : constant Element_Index :=
                                  Value'First
                                    + Stream.Last.Data'Length
                                    - Stream.Last_Length;
                  Next_Length : constant Element_Index :=
                                  Value'Last - Split_Value + 1;
               begin
                  Stream.Last.Data
                    (Stream.Last_Length + 1 .. Stream.Last.Data'Last) :=
                       Value (Value'First .. Split_Value - 1);

                  Stream.Last.Next := new Buffer_Type (False);
                  Stream.Last      := Stream.Last.Next;

                  if Next_Length >= Next_Block_Length or else Trim then
                     Stream.Last.Data := new Element_Array (1 .. Next_Length);
                  else
                     Stream.Last.Data :=
                       new Element_Array (1 .. Next_Block_Length);
                  end if;

                  Stream.Last.Data (1 .. Next_Length) :=
                    Value (Split_Value .. Value'Last);

                  Stream.Last_Length := Next_Length;
               end;
            end if;
         end;
      end if;

      Stream.Length := Stream.Length + Value'Length;
   end Append;

   ------------
   -- Append --
   ------------

   procedure Append
     (Stream : in out Stream_Type;
      Data   : in     Constant_Access) is
   begin
      if Data'Length = 0 then
         return;
      end if;

      if Stream.First = null then
         Stream.First          := new Buffer_Type (True);
         Stream.First.Const    := Data;
         Stream.Current        := Stream.First;
         Stream.Last           := Stream.First;
         Stream.Last_Length    := Data'Length;
         Stream.Current_Offset := Data'First;

      else
         Trim_Last_Block (Stream);

         Stream.Last.Next       := new Buffer_Type (True);
         Stream.Last            := Stream.Last.Next;
         Stream.Last.Const      := Data;
         Stream.Last_Length     := Data'Length;
      end if;

      Stream.Length  := Stream.Length + Data'Length;
   end Append;

   procedure Append
     (Stream : in out Stream_Type;
      Data   : in     Element_Access) is
   begin
      if Data'Length = 0 then
         return;
      end if;

      if Stream.First = null then
         Stream.First          := new Buffer_Type (False);
         Stream.First.Data     := Data;
         Stream.Current        := Stream.First;
         Stream.Last           := Stream.First;
         Stream.Last_Length    := Data'Length;
         Stream.Current_Offset := Data'First;

      else
         Trim_Last_Block (Stream);

         Stream.Last.Next       := new Buffer_Type (False);
         Stream.Last            := Stream.Last.Next;
         Stream.Last.Data       := Data;
         Stream.Last_Length     := Data'Length;
      end if;

      Stream.Length  := Stream.Length + Data'Length;
   end Append;

   -----------
   -- Close --
   -----------

   procedure Close (Stream : in out Stream_Type) is
      First  : Buffer_Access;
      Length : Element_Offset := 0;
   begin
      while Stream.First /= null loop
         First := Stream.First;

         if First.Next = null then
            Length := Length + Stream.Last_Length;

         else
            if First.Steady then
               Length := Length + First.Const'Length;
            else
               Length := Length + First.Data'Length;
            end if;
         end if;

         Stream.First := First.Next;
         Free (First);
      end loop;

      Stream.Current := null;
      Stream.Last    := null;

      Reset (Stream);

      if Stream.Length /= Length then
         raise Program_Error;
      end if;

      Stream.Length := 0;
   end Close;

   -----------------
   -- End_Of_File --
   -----------------

   function End_Of_File (Stream : in Stream_Type) return Boolean is
   begin
      return Stream.Current = null
        or else (Stream.Current.Next = null
                 and then Stream.Current_Offset > Last (Stream.Current));
   end End_Of_File;

   -----------
   -- First --
   -----------

   function First (Item : in Buffer_Access) return Element_Index is
   begin
      if Item.Steady then
         return Item.Const'First;
      else
         return Item.Data'First;
      end if;
   end First;

   ----------
   -- Free --
   ----------

   procedure Free (Item : in out Buffer_Access) is
      procedure Deallocate is
         new Ada.Unchecked_Deallocation (Buffer_Type, Buffer_Access);
   begin
      if not Item.Steady then
         Free (Item.Data);
      end if;

      Deallocate (Item);
   end Free;

   ----------
   -- Last --
   ----------

   function Last (Item : in Buffer_Access) return Element_Index is
   begin
      if Item.Steady then
         return Item.Const'Last;
      else
         return Item.Data'Last;
      end if;
   end Last;

   -------------
   -- Pending --
   -------------

   function Pending (Stream : in Stream_Type) return Element_Offset is
   begin
      if Stream.Current = null then
         return 0;

      else
         declare
            B : Buffer_Access := Stream.Current;
            S : Element_Offset := 0;
         begin
            while B /= null loop
               if B.Next = null then
                  if B.Steady then
                     S := S + Last (B);
                  else
                     S := S + Stream.Last_Length - Stream.Current_Offset + 1;
                  end if;

               else
                  S := S + Last (B);
               end if;

               B := B.Next;
            end loop;

            return S;
         end;
      end if;
   end Pending;

   ----------
   -- Read --
   ----------

   procedure Read
     (Stream : in out Stream_Type;
      Buffer :    out Element_Array;
      Last   :    out Element_Offset)
   is
      Buffer_Offset : Element_Offset := Buffer'First;
      Block_Over    : Boolean;

      procedure Append (Data : in Element_Array);
      --  Add the Data to the Buffer till necessary from the
      --  Resource.Current_Offset and move it.

      ------------
      -- Append --
      ------------

      procedure Append (Data : in Element_Array) is
         Buffer_Len_1  : constant Element_Offset :=
                           Buffer'Last - Buffer_Offset;
         --  Buffer remain length minus 1

         Current_Len_1 : constant Element_Offset :=
                           Data'Last - Stream.Current_Offset;
         --  Data remain length minus 1

         Current_Last  : Element_Index;
      begin
         Block_Over := Buffer_Len_1 >= Current_Len_1;

         if Block_Over then
            Last := Buffer_Offset + Current_Len_1;

            Buffer (Buffer_Offset .. Last) :=
              Data (Stream.Current_Offset .. Data'Last);

            Buffer_Offset := Last + 1;

            Stream.Current_Offset := Data'Last + 1;

         else
            Last := Buffer'Last;

            Current_Last := Stream.Current_Offset + Buffer_Len_1;

            Buffer (Buffer_Offset .. Last) :=
              Data (Stream.Current_Offset .. Current_Last);

            Stream.Current_Offset := Current_Last + 1;
         end if;
      end Append;

   begin
      Last := Buffer'First - 1;

      if Stream.Current = null then
         return;
      end if;

      loop
         if Stream.Current.Next = null then
            --  Last block

            if Stream.Current.Steady then
               Append (Stream.Current.Const.all);
            else
               Append (Stream.Current.Data
                         (Stream.Current.Data'First
                          .. Stream.Last_Length + Stream.Current.Data'First
                             - 1));
            end if;

            if Block_Over then
               Stream.Current := null;
               exit;
            end if;

         else
            if Stream.Current.Steady then
               Append (Stream.Current.Const.all);
            else
               Append (Stream.Current.Data.all);
            end if;

            if Block_Over then
               Stream.Current := Stream.Current.Next;

               Stream.Current_Offset := First (Stream.Current);
            end if;
         end if;

         exit when Last = Buffer'Last;
      end loop;
   end Read;

   -----------
   -- Reset --
   -----------

   procedure Reset (Stream : in out Stream_Type) is
   begin
      Stream.Current        := Stream.First;
      Stream.Current_Offset := 1;
   end Reset;

   ---------------
   -- Set_Index --
   ---------------

   procedure Set_Index
     (Stream : in out Stream_Type;
      To     : in     Element_Offset)
   is
      Idx : Element_Offset := Last (Stream.First);
   begin
      if To < 1 or else To > Size (Stream) then
         Stream.Current        := Stream.Last;
         Stream.Current_Offset := Last (Stream.Current) + 1;

      else
         Stream.Current        := Stream.First;

         while Idx < To loop
            Stream.Current := Stream.Current.Next;
            Idx := Idx + Last (Stream.Current);
         end loop;

         Stream.Current_Offset := Last (Stream.Current) - Idx + To;
      end if;
   end Set_Index;

   ----------
   -- Size --
   ----------

   function Size (Stream : in Stream_Type) return Element_Offset is
   begin
      return Stream.Length;
   end Size;

   ----------------------
   -- Trim_Last_Buffer --
   ----------------------

   procedure Trim_Last_Block (Stream : in out Stream_Type) is
   begin
      if Stream.Last = null
        or else Stream.Last.Steady
        or else Stream.Last.Data'Length = Stream.Last_Length
      then
         return;
      end if;

      declare
         Ptr : constant Element_Access :=
                 new Element_Array'
                   (Stream.Last.Data (1 .. Stream.Last_Length));
      begin
         Free (Stream.Last.Data);
         Stream.Last.Data   := Ptr;
         Stream.Last_Length := Ptr'Length;
      end;
   end Trim_Last_Block;

end Memory_Streams;