From: eachus@goldfinger.mitre.org (Robert I. Eachus)
Subject: Re: Can this data be represented in Ada?
Organization: The Mitre Corp., Bedford, MA.
	<4JAN199313495649@robots>
Date: Wed, 6 Jan 1993 19:11:13 GMT

In article <4JAN199313495649@robots> nbssal@robots (Stephe Leake) writes:


  > This is a valid way to PROCESS the data, but it is not a way to
  > REPRESENT the data structure. I tried a couple approaches
  > involving unconstrained arrays, but since the max length of a
  > packet is specified at run time, it cannot be static.  Thus this
  > structure is not REPRESENTABLE in Ada. As an Ada advocate, my
  > reaction to this is twofold; first, there is probably a better
  > data structure that is representable. Second, at least Ada can
  > process the data in a reasonable way; maybe actually representing
  > the structure is not required.  There are certainly other
  > applications (complex cross-linked lists) where the actual
  > structure is only partly represented by Ada types.

    I would guess that Steve picked the problem of reading such data
from the transmission media as the interesting part, the data
structure is easily representable in Ada:

  -- Steve's utility types...

   type BYTE is range 0 .. 255;
   for BYTE'SIZE use 8;
   subtype DATA_BYTE is BYTE;
 
   type LENGTH is range 0 .. 50000;      -- or whatever
   for LENGTH'SIZE use 16;
 
procedure Generate_Packets(Packet_Size: in Length; Message: in String) is 

   No_of_Packets: constant Length :=
           (Message'LENGTH + Packet_Size - 3) / (Packet_Size - 2);
   -- Compute number of packets needed.

   type Byte_Array is array(Length range <>) of Byte;
   
   type Contents(DATA_LENGTH: LENGTH := Packet_Size-2) is record
     DATA: Byte_Array(1..DATA_LENGTH);
     FILLER: Byte_Array(DATA_LENGTH..PACKET_SIZE-3);
   end record;

   type Packet is record
     C: Contents;	
   end record;

   type Packet_Array is array(1..No_of_Packets) of Packet;

   type Transmission is record
     L: Length := Packet_Size;
     P: Packet_Array;
   end record;

   package Packet_IO is new Sequential_IO(Transmission);
   
   The_Message: Transmission;

begin

  -- Fill in the message
  -- open the file (pipe?)
  -- send the messaage
  -- close pipe.
  null;
end Generate_Packets;

    There is no particular reason to have the type Contents and have
the data size as a discriminant in Ada. (It makes accessing the Data
require slightly fewer keystrokes, but...)  So I really recommend:

   type Packet is record
     Data_Length: Length := Packet_Size-2;
     Data: Byte_Array(1..Packet_Size-2);
   end record;

   An Ada compiler is more likely to represent this without any added
dope information, although some compilers (DEC?) do not require
pragmas or representation clauses to represent the original type
without dope.

    There are a few "tricks" used in the code above, but although they
are surprising to most Ada programmers, they are intentional features
of the language.  (In particular, arrays of records CONTAINING records
with different discriminant values.)

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
Newsgroups: comp.lang.ada