Debian Codegraph @

gcc/ada/libgnat/a-ststbo.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . S T R E A M S . S T O R A G E . B O U N D E D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2020-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 ------------------------------------------------------------------------------
28
29 package body Ada.Streams.Storage.Bounded is
30
31 ----------
32 -- Read --
33 ----------
34
35 overriding procedure Read
36 (Stream : in out Stream_Type; Item : out Stream_Element_Array;
37 Last : out Stream_Element_Offset)
38 is
39 EA : Stream_Element_Array renames
40 Stream.Elements (1 .. Element_Count (Stream));
41 begin
42 if Item'Length = 0 then
43 Last := Item'First - 1;
44
45 -- If the entire content of the stream fits in Item, then copy it and
46 -- clear the stream. This is likely the usual case.
47
48 elsif Element_Count (Stream) <= Item'Length then
49 Last := Item'First + Element_Count (Stream) - 1;
50 Item (Item'First .. Last) := EA;
51 Clear (Stream);
52
53 -- Otherwise, copy as much into Item as will fit. Then slide the
54 -- remaining part of the stream down, and compute the new Count.
55 -- We expect this to be the unusual case, so the cost of copying
56 -- the remaining part probably doesn't matter.
57
58 else
59 Last := Item'Last;
60
61 declare
62 New_Count : constant Stream_Element_Count :=
63 Element_Count (Stream) - Item'Length;
64 begin
65 Item := EA (1 .. Item'Length);
66 EA (1 .. New_Count) :=
67 EA (Element_Count (Stream) - New_Count + 1 ..
68 Element_Count (Stream));
69 Stream.Count := New_Count;
70 end;
71 end if;
72 end Read;
73
74 -----------
75 -- Write --
76 -----------
77
78 overriding procedure Write
79 (Stream : in out Stream_Type; Item : Stream_Element_Array)
80 is
81 begin
82 if Element_Count (Stream) + Item'Length > Stream.Max_Elements then
83 -- That is a precondition in the RM
84 raise Constraint_Error;
85 end if;
86
87 declare
88 New_Count : constant Stream_Element_Count :=
89 Element_Count (Stream) + Item'Length;
90 begin
91 Stream.Elements (Element_Count (Stream) + 1 .. New_Count) := Item;
92 Stream.Count := New_Count;
93 end;
94 end Write;
95
96 -------------------
97 -- Element_Count --
98 -------------------
99
100 overriding function Element_Count
101 (Stream : Stream_Type) return Stream_Element_Count
102 is
103 begin
104 return Stream.Count;
105 end Element_Count;
106
107 -----------
108 -- Clear --
109 -----------
110
111 overriding procedure Clear (Stream : in out Stream_Type)
112 is
113 begin
114 Stream.Count := 0;
115 end Clear;
116
117 end Ada.Streams.Storage.Bounded;