agpl_1.0.0_b5da3320/src/agpl-socket_utils.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
with Ada.Streams;
with Ada.Unchecked_Conversion;
with Agpl.Ustrings; use Agpl.Ustrings;

with Agpl.Text_Io; use Agpl.Text_Io;

package body Agpl.Socket_Utils is

   function To_Char is new Ada.Unchecked_Conversion
     (Ada.Streams.Stream_Element, Character);

   function Read_Line (Sock : Gnat.Sockets.Socket_Type) return String is
      Line : Ustring;
      C    : Character;
      E    : Ada.Streams.Stream_Element_Array (1 .. 1);
      Last : Ada.Streams.Stream_Element_Offset;
      use Ada.Streams;
      use Gnat.Sockets;
   begin
      loop
         Receive_Socket (Sock, E, Last);
         if Last < E'First then
            raise Constraint_Error with "socket has been closed";
         else
            C := To_Char (E (E'First));
--              Put_Line ("C: " & C);
            if C = Character'Val (10) then
               return +Line;
            elsif C /= Character'Val (13) then
               Asu.Append (Line, C);
            end if;
         end if;
      end loop;
   end Read_Line;

   procedure Skip_Line (Sock : Gnat.Sockets.Socket_Type) is
      Dummy : constant String := Read_Line (Sock);
      pragma Unreferenced (Dummy);
   begin
      null;
   end Skip_Line;

end Agpl.Socket_Utils;