comp.lang.ada
 help / color / mirror / Atom feed
From: "SteveD" <nospam_steved94@attbi.com>
Subject: Re: Reading a C record from socket - chicken / egg problem - now complete
Date: Sat, 23 Nov 2002 18:55:01 GMT
Date: 2002-11-23T18:55:01+00:00	[thread overview]
Message-ID: <9aQD9.114216$NH2.8094@sccrnsc01> (raw)
In-Reply-To: 5ad0dd8a.0211230443.4b476252@posting.google.com

"Wojtek Narczynski" <wojtek@power.com.pl> wrote in message
news:5ad0dd8a.0211230443.4b476252@posting.google.com...
> Hello,
>
> (sorry for my previous "post", hand slipped...)
>
> I want to read a record like this:
>
>    typedef struct {
>       unsigned char version;
>       unsigned char type;
>       unsigned char requestIdB1;
>       unsigned char requestIdB0;
>       unsigned char contentLengthB1;
>       unsigned char contentLengthB0;
>       unsigned char paddingLength;
>       unsigned char reserved;
>       unsigned char contentData[contentLength];
>       unsigned char paddingData[paddingLength];
>    } FCGI_Record;
>
> from socket. The 'type' field indicates different record type, so I
> could declare different record types in Ada, but when I know 'type'
> the record is already in memory. How should I approach this: 1. use
> Unchecked_Conversion to convert to appropriate type? 2. Copy data into
> a new record of appropriate type? 3. Just live with the structure C
> imposes?

If you're using streaming sockets the data appears as a stream of bytes.  In
this case there is no reason you must read the entire record with one recv.
Use one recv to learn the record type and the second recv to read the record
content.

>
> Another question is: can I declare non-contiguous enumeration
> subtypes, or only range x..y? I've been unable to figure out how to
> declare non-contiguous subtype, so I guess this isn't allowable, but
> why? How am I supposed to write a strong typing program if I am not
> able to declare a type that clearly "exists" (in math sense)? For
> example:
>
> -- Doestn't work
>
> type Animal is ( Pig, Dog, Fish );
> subtype Nice_Animal is Animal ( Pig, Dog );
> subtype Home_Animail is Animal ( Dog, Fish );
> subtype Eatable_Animal is Animal ( Pig, Fish );
>
> In reality I wanted to declare subtypes of request type that don't
> span a contiguous range.
>

While the enumeration values must be a contiguous range, there
representations do not.  The following example illustrates this.  You'll
notice that Unchecked_Conversion is used to get the underlying
representation of the enumeration ('Pos) doesn't work for this.

with Interfaces;
 use Interfaces;
with Ada.Text_Io;
with Ada.Unchecked_Conversion;

procedure Enum_Reps is

  package Integer_8_Io is
    new Ada.Text_Io.Integer_Io( Integer_8 );

  type Request_Type is ( req_type1, req_type2, req_type3 );
  for Request_Type use ( req_type1 => 3, req_type2 => 14, req_type3 => 25 );
  for Request_Type'Size use 8;

  function Conv is
    new Ada.Unchecked_Conversion( Integer_8, Request_Type );

 type_code  : Integer_8;
 enum_value : Request_Type;

begin
  loop
    Ada.Text_Io.Put( "Enter code (3,14,25) > " );
    Integer_8_Io.Get( type_code );
    Ada.Text_Io.Skip_Line;
    exit when type_code /= 3 and type_Code /= 14 and type_code /= 25;
    enum_value := Conv( type_code );
    Ada.Text_Io.Put_Line( "Enum is: " & Request_Type'Image( enum_value ) );
  end loop;
end Enum_Reps;

I hope this helps,
SteveD





  parent reply	other threads:[~2002-11-23 18:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-23 12:43 Reading a C record from socket - chicken / egg problem - now complete Wojtek Narczynski
2002-11-23 14:54 ` Robert A Duff
2002-11-23 17:31 ` Simon Wright
2002-12-05 16:54   ` Reading a C record from socket - chicken / egg problem Wojtek Narczynski
2002-11-23 18:55 ` SteveD [this message]
2002-11-28 21:33 ` Reading a C record from socket - chicken / egg problem - now complete Craig Carey
replies disabled

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