comp.lang.ada
 help / color / mirror / Atom feed
From: Jerry <lanceboyle@qwest.net>
Subject: Re: Trouble writing Ada callback from C
Date: Wed, 16 Sep 2009 16:19:14 -0700 (PDT)
Date: 2009-09-16T16:19:14-07:00	[thread overview]
Message-ID: <5a22a705-1fdb-4a70-950f-4d1a1060a17e@e4g2000prn.googlegroups.com> (raw)
In-Reply-To: 241a6745-0848-4bd0-bb1b-1e826e084911@j19g2000yqk.googlegroups.com

On Sep 12, 11:40 am, Vadim Godunko <vgodu...@gmail.com> wrote:
> On Sep 11, 11:57 am, Jerry <lancebo...@qwest.net> wrote:
>
> > I have a problem with writing an Ada procedure, used as a callback
> > from C, in which one of the arguments is an output char * (in the C
> > version). The actual length of the returned "string" isn't known at
> > calling time, even though the caller apparently allocates 40
> > characters (see below). However, the actual number of characters
> > returned by the C version is less than 40.
>
> > My problem is how to pass probably a char_array back out which is the
> > correct length, but which length is not known at the time the Ada
> > callback is called from C.
>
> subtype constrained_char_array is Interfaces.C.char_array (1 .. 40);
>
> procedure Y (X : out constrained_char_array);
> pragma Convention (C, Y);

Sorry for being slow to respond--I had to step away from this project
for a few days.

This works--thanks! After seeing this suggestion, I felt a little
embarrassed because it's so simple, and in hindsight, obvious.

Also, I realized that part of the problem was that to match the C
results, I had to insert my string into the left of the allocated 41
characters, then place a null character, then fill the rest with
spaces, like this:

-- Declared elsewhere is this, with Label_String_Length set to 40:
subtype Label_String_Type is
    Interfaces.C.char_array (0 .. size_t(Label_String_Length));

-- Then:
function Unbounded_To_Weird_C
   (Item     : Unbounded_String;
    C_Length : size_t)
return Label_String_Type is
begin
   return To_C(To_String(Item & ASCII.nul &
        (Label_String_Length - Length(Item)) * " "), False);
end Unbounded_To_Weird_C;


Thanks, everyone, for the help.
Jerry



      reply	other threads:[~2009-09-16 23:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-11  7:57 Trouble writing Ada callback from C Jerry
2009-09-11  8:24 ` Ludovic Brenta
2009-09-11 20:38   ` sjw
2009-09-12  5:28   ` Jerry
2009-09-12  5:46     ` sjw
2009-09-16 23:06       ` Jerry
2009-09-12  8:44     ` Georg Bauhaus
2009-09-12 18:40 ` Vadim Godunko
2009-09-16 23:19   ` Jerry [this message]
replies disabled

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