comp.lang.ada
 help / color / mirror / Atom feed
From: Jerry <lanceboyle@qwest.net>
Subject: Re: Trouble writing Ada callback from C
Date: Fri, 11 Sep 2009 22:28:05 -0700 (PDT)
Date: 2009-09-11T22:28:05-07:00	[thread overview]
Message-ID: <2db6b2d3-88c6-43ae-b1e8-4bcb44ce2ee3@s21g2000prm.googlegroups.com> (raw)
In-Reply-To: f637f1e2-f78b-4d3d-8afb-03376fbd27cd@j39g2000yqh.googlegroups.com

On Sep 11, 1:24 am, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
>
> I take it you mean "not known at compile time" because it is indeed
> "known at call time".
>
> C doesn't have proper strings; it has pointers to null-terminated
> arrays of characters, which do not carry bounds information. So, the
> specification of your callback is not "return a string in an array"
> but "write N characters starting where label points, where N+1 <
> a_length, followed by a null character (ASCII.NUL)".  Here is a
> possible implementation (not compiled or tested either):
>
> with Interfaces.C.Strings; use Interfaces.C.Strings;
> procedure geolocation_labeler (label : in chars_ptr; length : in
> size_t) is
>    Result : constant String := " N"; -- or whatever
> begin
>    Update (Item => label, Offset => 0, Str => To_C (Result), Check =>
> False);
>    -- See AARM B.3.1(43, 50.a/2)
> exception
>    when others =>
>       -- Important: callbacks must handle all exceptions because the C
> layer
>       -- calling us cannot propagate or handle them.
>       null; -- or report it.
> end geolocation_labeler;
>
> HTH
>
> --
> Ludovic Brenta.

Thanks, Ludovic.

Unfortunately, the C overlords are giving me a string of length 0--the
first character is the null character. So Update quits with

raised INTERFACES.C.STRINGS.UPDATE_ERROR

as is expected since Update can't write before the null character.

I'm now (desperately) studying the GNAT code in i-cstrin.adb and
attempting to make something like its Poke command, and then using it
in a modified Update which I'm calling Unsafe_Update which is just
like Update but with these lines deleted:

      if Check and then Offset + Chars'Length  > Strlen (Item) then
         raise Update_Error;
      end if;

I'm getting several errors which I'm trying to work through.

Jerry



  parent reply	other threads:[~2009-09-12  5:28 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 [this message]
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
replies disabled

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