Skip to content

Commit

Permalink
Implement Read for Streams.
Browse files Browse the repository at this point in the history
Fix size issue in Write for Streams.
Add streams to test program.
  • Loading branch information
Blady-Com committed Feb 7, 2021
1 parent c35aef8 commit 37b5645
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/uxstrings-text_io-text_streams.adb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,34 @@ with Ada.Unchecked_Conversion;

package body UXStrings.Text_IO.Text_Streams is

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

overriding procedure Read
(Stream : in out Stream_File; Item : out Stream_Element_Array; Last : out Stream_Element_Offset)
is
subtype SEA is Stream_Element_Array (Item'Range);
subtype UCA is UTF_8_Character_Array (1 .. Natural (Item'Last));
function Convert is new Ada.Unchecked_Conversion (UCA, SEA);
Read_Buffer : UCA;
Read_Last : Natural;
begin
pragma Compile_Time_Warning (Standard.True, "Read unimplemented");
raise Program_Error with "Unimplemented procedure Read";
Read_Stream (Stream.File.all, Read_Buffer, Read_Last);
Item := Convert (Read_Buffer);
Last := Stream_Element_Offset (Read_Last);
end Read;

-----------
-- Write --
-----------

overriding procedure Write (Stream : in out Stream_File; Item : Stream_Element_Array) is
subtype SEA is Stream_Element_Array (Item'Range);
subtype UCA is UTF_8_Character_Array (1 .. Natural (Item'Last));
function Convert is new Ada.Unchecked_Conversion (SEA, UCA);
Str : constant UXString := From_UTF_8 (UTF_8_Character_Array (Convert (Item)));
begin
Put (Stream.File.all, Str);
Write_Stream (Stream.File.all, Convert (Item));
end Write;

------------
Expand Down
30 changes: 30 additions & 0 deletions src/uxstrings-text_io1.adb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ package body UXStrings.Text_IO is

LM : UXString := From_Latin_1 (Character'Val (13) & Character'Val (10)); -- Default is CRLF for Line_Mark function

---------------
-- Read_More --
---------------

procedure Read_More (File : in out File_Type) is
Buffer_Size : constant := 200;
subtype Buffer_Type is String (1 .. Buffer_Size);
Expand All @@ -42,6 +46,32 @@ package body UXStrings.Text_IO is
end if;
end Read_More;

-----------------
-- Read_Stream --
-----------------

procedure Read_Stream (File : in out File_Type; Item : out UTF_8_Character_Array; Last : out Natural) is
Read_Buffer : UXString;
begin
if File.Buffer.Length < Item'Length then
Read_More (File);
end if;
Bounded_Move (File.Buffer, Read_Buffer, Item'Length, Last);
if Last > 0 then
Item (Item'First .. Item'First + Last - 1) := Read_Buffer.Chars.all;
end if;
end Read_Stream;

-----------------
-- Write_Stream --
-----------------

procedure Write_Stream (File : in out File_Type; Item : UTF_8_Character_Array) is
Dummy_Result : Integer;
begin
Dummy_Result := Write (File.FD, Item'Address, Item'Length);
end Write_Stream;

------------
-- Create --
------------
Expand Down
3 changes: 3 additions & 0 deletions src/uxstrings-text_io1.ads
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@ private
EOF : Boolean := False;
end record;

procedure Read_Stream (File : in out File_Type; Item : out UTF_8_Character_Array; Last : out Natural);
procedure Write_Stream (File : in out File_Type; Item : UTF_8_Character_Array);

end UXStrings.Text_IO;
18 changes: 18 additions & 0 deletions src/uxstrings1.adb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ package body UXStrings is
UTF_8_Character_Array'Write (Stream, Item.Chars.all);
end UXString_Write;

------------------
-- Bounded_Move --
------------------

procedure Bounded_Move (Source : in out UXString; Target : out UXString; Max : Natural; Last : out Natural) is
Item : UTF8_Code_Point;
Pointer : Integer := Source.Chars'First;
Count : Natural := 0;
begin
while Pointer <= Source.Chars'First + Max - 1 and Pointer <= Source.Chars'last loop
Get (Source.Chars.all, Pointer, Item);
Count := Count + 1;
end loop;
Target := Source.Slice (1, Count);
Delete (Source, 1, Count);
Last := Target.Chars.all'Length;
end Bounded_Move;

-- UXStrings API implementation

------------
Expand Down
1 change: 1 addition & 0 deletions src/uxstrings1.ads
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ private
procedure Adjust (Object : in out UXString);
procedure Finalize (Object : in out UXString);

procedure Bounded_Move (Source : in out UXString; Target : out UXString; Max : Natural; Last : out Natural);

procedure UXString_Read (Stream : not null access Ada.Streams.Root_Stream_Type'Class; Item : out UXString);
for UXString'Read use UXString_Read;
Expand Down
41 changes: 37 additions & 4 deletions tests/test_uxstrings_text_io.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
with UXStrings; use UXStrings;
with UXStrings.Text_IO; use UXStrings.Text_IO;
with UXStrings.Text_IO.Text_Streams; use UXStrings.Text_IO.Text_Streams;
with UXStrings.Conversions;

procedure Test_UXStrings_Text_IO is
Expand Down Expand Up @@ -30,6 +31,32 @@ procedure Test_UXStrings_Text_IO is
Put_Line ("File read.");
end;

procedure Write_Stream is
F : File_Type;
S : Stream_Access;
begin
Create (F,Out_File, "test_stream.txt", Latin_1);
S := Stream (F);
for C of To_Latin_1 ("une soirée passée à étudier la physique ω=Δθ/Δt...") loop
Character'Write (S, C);
end loop;
Close (F);
Put_Line ("File witten.");
end;

procedure Read_Stream is
F : File_Type;
T : UTF_8_Character_Array (1..40);
S : Stream_Access;
begin
Open (F, In_File, "test_stream.txt", Latin_1);
S := Stream (F);
UTF_8_Character_Array'Read (S, T);
Put_Line (From_UTF_8 (T));
Close (F);
Put_Line ("File read.");
end;

S1 : UXString;

begin
Expand All @@ -44,11 +71,17 @@ begin
Get_Line (S1);
Put_Line (S1);
exit when S1 = "exit";
if S1.Index( "write") = S1.First then
Write (if S1.index ("utf_") > 0 then Value(S1.Slice (6, S1.Length)) else Latin_1);
if S1.Index ("fwrite") = S1.First then
Write (if S1.index ("utf_") > 0 then Value (S1.Slice (8, S1.Length)) else Latin_1);
end if;
if S1.Index ("fread")= S1.First then
Read(if S1.index ("utf_") > 0 then Value (S1.Slice (7, S1.Length)) else Latin_1);
end if;
if S1.Index ("swrite")= S1.First then
Write_Stream;
end if;
if S1.Index( "read")= S1.First then
Read(if S1.index ("utf_") > 0 then Value(S1.Slice (6, S1.Length)) else Latin_1);
if S1.Index ("sread")= S1.First then
Read_Stream;
end if;
end loop;
Put_Line ("<-->");
Expand Down

0 comments on commit 37b5645

Please sign in to comment.