* Stream_Element_Array 2 String
@ 2004-03-26 17:30 Shaddow
2004-03-26 18:04 ` Marius Amado Alves
0 siblings, 1 reply; 6+ messages in thread
From: Shaddow @ 2004-03-26 17:30 UTC (permalink / raw)
Hello
Im using Ada.Streams.Stream_IO for read block of files, and i can convert
a Stream_Element_Array to String type.
how can i do?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Stream_Element_Array 2 String
2004-03-26 17:30 Stream_Element_Array 2 String Shaddow
@ 2004-03-26 18:04 ` Marius Amado Alves
2004-03-27 8:24 ` Simon Wright
0 siblings, 1 reply; 6+ messages in thread
From: Marius Amado Alves @ 2004-03-26 18:04 UTC (permalink / raw)
To: comp.lang.ada
> Im using Ada.Streams.Stream_IO for read block of files and i can convert
> a Stream_Element_Array to String type.
> how can i do?
Ada.Unchecked_Conversion is your friend.
But make sure you need that, instead of basic stream I/O e.g.
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
procedure Strstr is
F : File_Type;
S3 : String (1 .. 3);
begin
Create (F);
String'Write (Stream (F), "abc");
Reset (F);
S3 := "***";
Set_Mode (F, In_File);
String'Read (Stream (F), S3);
pragma Assert (S3 = "abc");
end;
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Stream_Element_Array 2 String
2004-03-26 18:04 ` Marius Amado Alves
@ 2004-03-27 8:24 ` Simon Wright
2004-03-29 7:50 ` Duncan Sands
0 siblings, 1 reply; 6+ messages in thread
From: Simon Wright @ 2004-03-27 8:24 UTC (permalink / raw)
Marius Amado Alves <maa@liacc.up.pt> writes:
> > Im using Ada.Streams.Stream_IO for read block of files and i can convert
> > a Stream_Element_Array to String type.
> > how can i do?
>
> Ada.Unchecked_Conversion is your friend.
>
> But make sure you need that, instead of basic stream I/O e.g.
>
> with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
>
> procedure Strstr is
> F : File_Type;
> S3 : String (1 .. 3);
> begin
> Create (F);
> String'Write (Stream (F), "abc");
> Reset (F);
> S3 := "***";
> Set_Mode (F, In_File);
> String'Read (Stream (F), S3);
> pragma Assert (S3 = "abc");
> end;
Also, it depends whether performance or clarity is what you need!
There is a third way, using address overlays, which is possibly rather
easier to get wrong .. I always feel uncomfortable with it, but here
goes:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;
with Ada.Streams;
with BC.Support.High_Resolution_Time;
procedure String_To_Stream_Element_Array is
function To_Stream_Element_Array_Using_Unchecked_Conversion
(S : String) return Ada.Streams.Stream_Element_Array;
function To_Stream_Element_Array_Using_Overlay
(S : String) return Ada.Streams.Stream_Element_Array;
function To_Stream_Element_Array_Using_Unchecked_Conversion
(S : String) return Ada.Streams.Stream_Element_Array is
subtype Source is String (S'Range);
subtype Result is Ada.Streams.Stream_Element_Array
(Ada.Streams.Stream_Element_Offset (S'First)
.. Ada.Streams.Stream_Element_Offset (S'Last));
function To_Array is new Ada.Unchecked_Conversion (Source, Result);
begin
return To_Array (S);
end To_Stream_Element_Array_Using_Unchecked_Conversion;
function To_Stream_Element_Array_Using_Overlay
(S : String) return Ada.Streams.Stream_Element_Array is
Array_View : Ada.Streams.Stream_Element_Array
(Ada.Streams.Stream_Element_Offset (S'First)
.. Ada.Streams.Stream_Element_Offset (S'Last));
for Array_View'Address use S'Address;
begin
return Array_View;
end To_Stream_Element_Array_Using_Overlay;
Start, Finished : BC.Support.High_Resolution_Time.Time;
Took : Duration;
use type BC.Support.High_Resolution_Time.Time;
begin
Start := BC.Support.High_Resolution_Time.Clock;
declare
A : constant Ada.Streams.Stream_Element_Array :=
To_Stream_Element_Array_Using_Unchecked_Conversion
("now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country");
begin
Finished := BC.Support.High_Resolution_Time.Clock;
Took := Finished - Start;
end;
Put_Line ("unchecked conversion took" & Took'Img);
Start := BC.Support.High_Resolution_Time.Clock;
declare
A : constant Ada.Streams.Stream_Element_Array :=
To_Stream_Element_Array_Using_Overlay
("now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country"
& "now is the time for all good men"
& " to come to the aid of the country");
begin
Finished := BC.Support.High_Resolution_Time.Clock;
Took := Finished - Start;
end;
Put_Line ("overlay took" & Took'Img);
end String_To_Stream_Element_Array;
Runing on this box (which is a bit antiquated now), I get (GNAT 3.15p)
smaug.pushface.org[64]$ ./string_to_stream_element_array
Clock rate is 0.400915179 GHz
unchecked conversion took 0.000008465
overlay took 0.000002641
You can find the Booch Components at http://pushface.org/components/bc/.
The high resolution time stuff is for GNAT and Intel (well, "must
support the rdtsc instruction") only.
Shouldn't that be ".. to the aid of the party"? oh well ..
--
Simon Wright 100% Ada, no bugs.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Stream_Element_Array 2 String
2004-03-27 8:24 ` Simon Wright
@ 2004-03-29 7:50 ` Duncan Sands
2004-03-29 19:00 ` Simon Wright
2004-03-31 9:32 ` Pascal Obry
0 siblings, 2 replies; 6+ messages in thread
From: Duncan Sands @ 2004-03-29 7:50 UTC (permalink / raw)
To: Simon Wright, comp.lang.ada
> There is a third way, using address overlays, which is possibly rather
> easier to get wrong .. I always feel uncomfortable with it, but here
> goes:
What if a Stream_Element is not the same size as a Character?
Duncan.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Stream_Element_Array 2 String
2004-03-29 7:50 ` Duncan Sands
@ 2004-03-29 19:00 ` Simon Wright
2004-03-31 9:32 ` Pascal Obry
1 sibling, 0 replies; 6+ messages in thread
From: Simon Wright @ 2004-03-29 19:00 UTC (permalink / raw)
Duncan Sands <baldrick@free.fr> writes:
> > There is a third way, using address overlays, which is possibly
> > rather easier to get wrong .. I always feel uncomfortable with it,
> > but here goes:
>
> What if a Stream_Element is not the same size as a Character?
You are using bizarre hardware?
Of course my suggestion will only work in the circumstances where any
other unchecked conversion would have worked too.
Since I am in an environment where microseconds count, and the
hardware is not bizarre, I am perfectly happy with assuming that
pragma Assert (Stream_Element'Size = Character'Size);
Is unnecessary ..
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Stream_Element_Array 2 String
2004-03-29 7:50 ` Duncan Sands
2004-03-29 19:00 ` Simon Wright
@ 2004-03-31 9:32 ` Pascal Obry
1 sibling, 0 replies; 6+ messages in thread
From: Pascal Obry @ 2004-03-31 9:32 UTC (permalink / raw)
Duncan Sands <baldrick@free.fr> writes:
> > There is a third way, using address overlays, which is possibly rather
> > easier to get wrong .. I always feel uncomfortable with it, but here
> > goes:
>
> What if a Stream_Element is not the same size as a Character?
You can check that and use the right implementation to convert from/to
Stream_Element_Array. We use this technique in AWS.
Pascal.
--
--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-03-31 9:32 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-26 17:30 Stream_Element_Array 2 String Shaddow
2004-03-26 18:04 ` Marius Amado Alves
2004-03-27 8:24 ` Simon Wright
2004-03-29 7:50 ` Duncan Sands
2004-03-29 19:00 ` Simon Wright
2004-03-31 9:32 ` Pascal Obry
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox