comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.tsoh.plus-bug.bauhaus@maps.futureapps.de>
Subject: Re: Trouble writing Ada callback from C
Date: Sat, 12 Sep 2009 10:44:18 +0200
Date: 2009-09-12T10:44:19+02:00	[thread overview]
Message-ID: <4aab5f63$0$30234$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <2db6b2d3-88c6-43ae-b1e8-4bcb44ce2ee3@s21g2000prm.googlegroups.com>

Jerry wrote:

> 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.

The following works---if I understand correctly what you
are trying to achieve.

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

extern void adainit(void);
extern void adafinal(void);
extern int geolocation_labeler(char*, size_t);

int main()
{
  char* buffer = calloc(2048, sizeof(char));

  adainit();

  assert(*(buffer + 112) == '\0');

  if (geolocation_labeler(buffer + 112, 40) >= 0)
    fputs(buffer + 112, stdout);
  else
    fputs("no label", stderr);

  adafinal();
  return 0;
}


with Interfaces.C.Strings; use Interfaces.C;

function Geolocation_Labeler
  (label : in Strings.chars_ptr; length : in size_t) return int;
pragma Export(C, Geolocation_Labeler, "geolocation_labeler");


function Geolocation_Labeler
  (label : in Strings.chars_ptr; length : in size_t) return int
is
   Result : constant String := " N"; -- or whatever
begin
   if Result'Length + 1 > Length then
      return -1;
   end if;

   Strings.Update (Item => label,
     Offset => 0,
     Chars => To_C (Result, Append_Nul => True),
     Check => False);
   return Result'Length;
exception
   when others =>
      return -2;
end geolocation_labeler;



  parent reply	other threads:[~2009-09-12  8:44 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 [this message]
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