home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / ada / 3902 < prev    next >
Encoding:
Text File  |  1993-01-06  |  3.6 KB  |  106 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!gatech!usenet.ins.cwru.edu!agate!linus!linus.mitre.org!linus!mbunix!eachus
  3. From: eachus@goldfinger.mitre.org (Robert I. Eachus)
  4. Subject: Re: Can this data be represented in Ada?
  5. In-Reply-To: nbssal@robots's message of 4 Jan 93 18:49:00 GMT
  6. Message-ID: <EACHUS.93Jan6141113@goldfinger.mitre.org>
  7. Sender: news@linus.mitre.org (NONUSER)
  8. Nntp-Posting-Host: goldfinger.mitre.org
  9. Organization: The Mitre Corp., Bedford, MA.
  10. References: <1992Dec31.014719.22174@nosc.mil> <SDL.92Dec31182221@rigel.linus.mitre.org>
  11.     <4JAN199313495649@robots>
  12. Date: Wed, 6 Jan 1993 19:11:13 GMT
  13. Lines: 91
  14.  
  15. In article <4JAN199313495649@robots> nbssal@robots (Stephe Leake) writes:
  16.  
  17.  
  18.   > This is a valid way to PROCESS the data, but it is not a way to
  19.   > REPRESENT the data structure. I tried a couple approaches
  20.   > involving unconstrained arrays, but since the max length of a
  21.   > packet is specified at run time, it cannot be static.  Thus this
  22.   > structure is not REPRESENTABLE in Ada. As an Ada advocate, my
  23.   > reaction to this is twofold; first, there is probably a better
  24.   > data structure that is representable. Second, at least Ada can
  25.   > process the data in a reasonable way; maybe actually representing
  26.   > the structure is not required.  There are certainly other
  27.   > applications (complex cross-linked lists) where the actual
  28.   > structure is only partly represented by Ada types.
  29.  
  30.     I would guess that Steve picked the problem of reading such data
  31. from the transmission media as the interesting part, the data
  32. structure is easily representable in Ada:
  33.  
  34.   -- Steve's utility types...
  35.  
  36.    type BYTE is range 0 .. 255;
  37.    for BYTE'SIZE use 8;
  38.    subtype DATA_BYTE is BYTE;
  39.  
  40.    type LENGTH is range 0 .. 50000;      -- or whatever
  41.    for LENGTH'SIZE use 16;
  42.  
  43. procedure Generate_Packets(Packet_Size: in Length; Message: in String) is 
  44.  
  45.    No_of_Packets: constant Length :=
  46.            (Message'LENGTH + Packet_Size - 3) / (Packet_Size - 2);
  47.    -- Compute number of packets needed.
  48.  
  49.    type Byte_Array is array(Length range <>) of Byte;
  50.    
  51.    type Contents(DATA_LENGTH: LENGTH := Packet_Size-2) is record
  52.      DATA: Byte_Array(1..DATA_LENGTH);
  53.      FILLER: Byte_Array(DATA_LENGTH..PACKET_SIZE-3);
  54.    end record;
  55.  
  56.    type Packet is record
  57.      C: Contents;    
  58.    end record;
  59.  
  60.    type Packet_Array is array(1..No_of_Packets) of Packet;
  61.  
  62.    type Transmission is record
  63.      L: Length := Packet_Size;
  64.      P: Packet_Array;
  65.    end record;
  66.  
  67.    package Packet_IO is new Sequential_IO(Transmission);
  68.    
  69.    The_Message: Transmission;
  70.  
  71. begin
  72.  
  73.   -- Fill in the message
  74.   -- open the file (pipe?)
  75.   -- send the messaage
  76.   -- close pipe.
  77.   null;
  78. end Generate_Packets;
  79.  
  80.     There is no particular reason to have the type Contents and have
  81. the data size as a discriminant in Ada. (It makes accessing the Data
  82. require slightly fewer keystrokes, but...)  So I really recommend:
  83.  
  84.    type Packet is record
  85.      Data_Length: Length := Packet_Size-2;
  86.      Data: Byte_Array(1..Packet_Size-2);
  87.    end record;
  88.  
  89.    An Ada compiler is more likely to represent this without any added
  90. dope information, although some compilers (DEC?) do not require
  91. pragmas or representation clauses to represent the original type
  92. without dope.
  93.  
  94.     There are a few "tricks" used in the code above, but although they
  95. are surprising to most Ada programmers, they are intentional features
  96. of the language.  (In particular, arrays of records CONTAINING records
  97. with different discriminant values.)
  98.  
  99. --
  100.  
  101.                     Robert I. Eachus
  102.  
  103. with Standard_Disclaimer;
  104. use  Standard_Disclaimer;
  105. function Message (Text: in Clever_Ideas) return Better_Ideas is...
  106.