comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Translate from C
Date: Sat, 09 Apr 2005 15:54:42 -0500
Date: 2005-04-09T15:54:42-05:00	[thread overview]
Message-ID: <HfGdnV-7ze-P3MXfRVn-tw@comcast.com> (raw)
In-Reply-To: pan.2005.04.09.19.30.30.254343@a2lf.org

>I've this code in C : http://www.a2lf.org/util.c
>and i want to translate it in Ada;
  In Ada one would usually have "Destination := Source;" rather than
doing a procedure call.  But if it needs to be a procedure,
  procedure Copy(Source : in Interfaces.C.char_array;
                 Destination : out Interfaces.C.char_array;
                 N : in Natural) is
  begin
    Destination(Destination'first .. Destination'first+N-1)
      := Source(Source'first .. Source'first+N-1);
  end Copy;
assuming you want to deal with C arrays rather than Ada Strings.
Of course if you wanted to handle mixed types, eg, copy a 32 bit
float to 4 8-bit chars,  your friend's solution comes close.  However,
  S : String (1 .. Bytes);
may include bounds information as well as the data.  You can avoid that
possibility by making the S and D declarations as large, fixed size,
strings, where you are careful to only touch the first Bytes bytes, eg
  S : String (Positive);  -- fixed size array needs no dynamic bound info
  D : String (Positive);
To avoid possible initialization (ie, destruction) of S and D, you should
  pragma Import(C, S);
  pragma Import(C, D);
Also, in Ada you could replace the loop
      for I in S'Range loop
         D (I) := S (I);
      end loop;
by
      D(1 .. Bytes) := S(1 .. Bytes);



  reply	other threads:[~2005-04-09 20:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-09 19:30 Translate from C Jean-Baptiste CAMPESATO
2005-04-09 20:54 ` tmoran [this message]
     [not found] ` <gkvg51de5t83scllh0sp7go8nmcfe3ede9@4ax.com>
2005-04-10  7:47   ` Jean-Baptiste CAMPESATO
2005-04-11 13:39 ` Steve
replies disabled

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