vss_24.0.0_b4d0be7c/source/streams/implementation/vss-text_streams-standards.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
--
--  Copyright (C) 2023, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

--  Standard input/output streams as text streams.

with Ada.Streams;
with Interfaces.C_Streams;

with VSS.Implementation.Line_Terminator;
with VSS.Stream_Element_Vectors.Internals;
with VSS.Strings.Converters.Encoders;

package body VSS.Text_Streams.Standards is

   use type Interfaces.C_Streams.FILEs;
   use type Interfaces.C_Streams.size_t;

   type Standard_Output_Text_Stream is
     limited new VSS.Text_Streams.Output_Text_Stream with
   record
      Encoder    : VSS.Strings.Converters.Encoders.Virtual_String_Encoder;
      Terminator : VSS.Strings.Line_Terminator := VSS.Strings.LF;
      Stream     : Interfaces.C_Streams.FILEs :=
        Interfaces.C_Streams.NULL_Stream;
      Error      : VSS.Strings.Virtual_String;
   end record;

   overriding procedure Put
     (Self    : in out Standard_Output_Text_Stream;
      Item    : VSS.Characters.Virtual_Character;
      Success : in out Boolean);

   overriding procedure Put
     (Self    : in out Standard_Output_Text_Stream;
      Item    : VSS.Strings.Virtual_String;
      Success : in out Boolean);

   overriding procedure Put_Line
     (Self    : in out Standard_Output_Text_Stream;
      Item    : VSS.Strings.Virtual_String;
      Success : in out Boolean);

   overriding procedure New_Line
     (Self    : in out Standard_Output_Text_Stream;
      Success : in out Boolean);

   overriding function Has_Error
     (Self : Standard_Output_Text_Stream) return Boolean;

   overriding function Error_Message
     (Self : Standard_Output_Text_Stream) return VSS.Strings.Virtual_String;

   -------------------
   -- Error_Message --
   -------------------

   overriding function Error_Message
     (Self : Standard_Output_Text_Stream) return VSS.Strings.Virtual_String is
   begin
      return Self.Error;
   end Error_Message;

   ---------------
   -- Has_Error --
   ---------------

   overriding function Has_Error
     (Self : Standard_Output_Text_Stream) return Boolean is
   begin
      return not Self.Error.Is_Empty;
   end Has_Error;

   --------------
   -- New_Line --
   --------------

   overriding procedure New_Line
     (Self    : in out Standard_Output_Text_Stream;
      Success : in out Boolean) is
   begin
      Self.Put
        (VSS.Implementation.Line_Terminator.Sequence (Self.Terminator),
         Success);
   end New_Line;

   ---------
   -- Put --
   ---------

   overriding procedure Put
     (Self    : in out Standard_Output_Text_Stream;
      Item    : VSS.Characters.Virtual_Character;
      Success : in out Boolean)
   is
      Buffer : VSS.Stream_Element_Vectors.Stream_Element_Vector;
      Length : Ada.Streams.Stream_Element_Count;
      Data   :
        VSS.Stream_Element_Vectors.Internals.Stream_Element_Array_Access;
      Size   : Interfaces.C_Streams.size_t;

   begin
      if not Success or not Self.Error.Is_Empty then
         Success := False;

         return;
      end if;

      if Self.Stream = Interfaces.C_Streams.NULL_Stream then
         Self.Error := "File is not open";
         Success    := False;

         return;
      end if;

      Self.Encoder.Encode (Item, Buffer);

      if not Buffer.Is_Empty then
         VSS.Stream_Element_Vectors.Internals.Data_Constant_Access
           (Buffer, Length, Data);

         Size :=
           Interfaces.C_Streams.fwrite
             (Data (Data'First)'Address,
              1,
              Interfaces.C_Streams.size_t (Length),
              Self.Stream);

         if Size /= Interfaces.C_Streams.size_t (Length) then
            Self.Error := "File IO error";
            Success    := False;
         end if;
      end if;
   end Put;

   ---------
   -- Put --
   ---------

   overriding procedure Put
     (Self    : in out Standard_Output_Text_Stream;
      Item    : VSS.Strings.Virtual_String;
      Success : in out Boolean)
   is
      Buffer : VSS.Stream_Element_Vectors.Stream_Element_Vector;
      Length : Ada.Streams.Stream_Element_Count;
      Data   :
        VSS.Stream_Element_Vectors.Internals.Stream_Element_Array_Access;
      Size   : Interfaces.C_Streams.size_t;

   begin
      if not Success or not Self.Error.Is_Empty then
         Success := False;

         return;
      end if;

      if Self.Stream = Interfaces.C_Streams.NULL_Stream then
         Self.Error := "File is not open";
         Success    := False;

         return;
      end if;

      Self.Encoder.Encode (Item, Buffer);

      if not Buffer.Is_Empty then
         VSS.Stream_Element_Vectors.Internals.Data_Constant_Access
           (Buffer, Length, Data);

         Size :=
           Interfaces.C_Streams.fwrite
             (Data (Data'First)'Address,
              1,
              Interfaces.C_Streams.size_t (Length),
              Self.Stream);

         if Size /= Interfaces.C_Streams.size_t (Length) then
            Self.Error := "File IO error";
            Success    := False;
         end if;
      end if;
   end Put;

   --------------
   -- Put_Line --
   --------------

   overriding procedure Put_Line
     (Self    : in out Standard_Output_Text_Stream;
      Item    : VSS.Strings.Virtual_String;
      Success : in out Boolean) is
   begin
      Self.Put (Item, Success);
      Self.New_Line (Success);
   end Put_Line;
   --------------------
   -- Standard_Error --
   --------------------

   function Standard_Error return VSS.Text_Streams.Output_Text_Stream'Class is
   begin
      return Result : Standard_Output_Text_Stream :=
        (Encoder     => <>,
         Terminator  => VSS.Strings.LF,
         Stream      => Interfaces.C_Streams.stderr,
         Error       => <>)
      do
         Result.Encoder.Initialize ("utf-8");
         --  XXX Encoding must be detected from application's locale.
         --  XXX Line terminator must be detected by platform.
      end return;
   end Standard_Error;

   ---------------------
   -- Standard_Output --
   ---------------------

   function Standard_Output return VSS.Text_Streams.Output_Text_Stream'Class is
   begin
      return Result : Standard_Output_Text_Stream :=
        (Encoder     => <>,
         Terminator  => VSS.Strings.LF,
         Stream      => Interfaces.C_Streams.stdout,
         Error       => <>)
      do
         Result.Encoder.Initialize ("utf-8");
         --  XXX Encoding must be detected from application's locale.
         --  XXX Line terminator must be detected by platform.
      end return;
   end Standard_Output;

end VSS.Text_Streams.Standards;