comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Unchecked_Conversion vs Address-overlaying
Date: Sun, 26 May 2013 16:34:54 -0700 (PDT)
Date: 2013-05-26T16:34:54-07:00	[thread overview]
Message-ID: <6bbc35cb-18ed-4efc-b1ec-6c41dcc0256c@googlegroups.com> (raw)

I recently had to make a dump-function for a collection of bytes and was wondering if the method I chose (address-overlaying) was better than using Unchecked_Conversion or not.

------------------------------

    -- The private type BLOB designates some collection of memory-data;
    -- it is actually an access to a storage-array [of Storage-Elements].
    -- It's sized to the Cell_Size that all the other types are given so
    -- that they can be wrapped in a record and that pushed onto a stack.
    Type BLOB is Not Null Access All System.Storage_Elements.Storage_Array
    with
	Size => Cell_Size;


    -- Returns a hex-dump of the given BLOB.
    Function Image( Item : BLOB ) Return String is
	Use System, System.Storage_Elements;

        -- Our undervalued friend, the Nybble and the Byte, as well
        -- as a type that serves as a collection of bytes.
	Subtype Nybble	is Interfaces.Unsigned_8 Range 16#0#..16#F#;
	Subtype Byte	is Interfaces.Unsigned_8;
	Type Byte_String is Array(Positive Range <>) of Byte;

        -- Return a Hex Character for any given Nybble.
	Function Nybble_to_Hex( Input : Nybble ) Return Character is
	    subtype Decimal is Nybble range 0..9;
	    Base : Constant Interfaces.Unsigned_8:= 
	      			( if Input in Decimal then Character'Pos('0')
				  else Character'Pos('A') - 10 );
	begin
	    Return Character'Val( Base + Input );
	End Nybble_To_Hex;

        -- Return the string that is the hex-values for
        -- the high- and low-nybbles of the given byte.
	Function Image( Item : Byte ) Return String is
	begin
	    Return ( 1 => Nybble_to_Hex(Item / 16),     -- High
	             2 => Nybble_to_Hex(Item mod 16) ); -- Low
	End Image;

        -- Given a Byte_String return the string containing its Nybbles.
	Function Image( Item : Byte_String ) return String is
	begin
	    Case Item'Length is
	    when 0	=> Return "";
	    when 1	=> Return Image(Item(Item'First));
	    when others	=>
		declare
		    Head : String Renames Image(Item(Item'First));
		    subtype Tail_Range is Positive Range
		      Positive'Succ(Item'First)..Item'Last;

		    Tail : String Renames Image(Item(Tail_Range));
		begin
		    Return Head & Tail;
		end;
	    end case;
	End Image;

        -- Calculate the length of the BLOB, in bytes.
	Length : Constant Natural:= (Item'Length*Storage_Array'Component_Size)
					  / Byte'Object_Size;
	Pragma Warnings(Off);
        -- Create an overlayed, non-initializing variable of the proper size.
	Image_String : Byte_String(1..Length)
	  with Import, Convention => Ada, Address => Item.All'Address;
	Pragma Warnings(On);
    begin
        -- Return the image of that overlay.
	Return Image(Image_String);
    end Image;


             reply	other threads:[~2013-05-26 23:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-26 23:34 Shark8 [this message]
2013-05-27  2:33 ` Unchecked_Conversion vs Address-overlaying Jeffrey Carter
2013-05-27  4:37 ` Per Sandberg
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox