From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,fb17569fccf2604b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Sat, 12 Sep 2009 10:44:18 +0200 From: Georg Bauhaus Reply-To: rm.tsoh+bauhaus@maps.futureapps.de User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Trouble writing Ada callback from C References: <2db6b2d3-88c6-43ae-b1e8-4bcb44ce2ee3@s21g2000prm.googlegroups.com> In-Reply-To: <2db6b2d3-88c6-43ae-b1e8-4bcb44ce2ee3@s21g2000prm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Message-ID: <4aab5f63$0$30234$9b4e6d93@newsspool1.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 12 Sep 2009 10:44:19 CEST NNTP-Posting-Host: 29d12a9d.newsspool1.arcor-online.net X-Trace: DXC=[a\93L9OGVmIkjb;<8iR=aic==]BZ:afn4Fo<]lROoRa^YC2XCjHcbio[ 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 #include #include 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;