comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Pointer
Date: Mon, 19 May 2008 05:09:46 -0700 (PDT)
Date: 2008-05-19T05:09:46-07:00	[thread overview]
Message-ID: <17836d5d-179f-4867-a4cf-1caacdc6581f@b64g2000hsa.googlegroups.com> (raw)
In-Reply-To: 4831652D.3020507@gmail.com

Sébastien wrote:
> char* get_nth(char** data, unsigned int n) {
>    return data[i];
> }
>
> Ada binding:
> function get_nth(data: System.Address; n: Int) return Char_Ptr;
> pragma Import(C, get_nth, "get_nth");
>
> Then:
> for i in Natural range 0 .. N loop
>     MyString := Interfaces.C.Strings.Value(get_nth(CharPtrPtr, i));
> end loop;
>
> I know it's working, but I have to code a C function in order to do
> that, is there a way to replace get_nth by an ada code function?
>
> Hope it's a better explanation ;-)

OK, then how about:

type Array_Of_C_Strings is array (Positive range <>) of
Interfaces.C.Strings.chars_ptr;
pragma Convention (C, Array_Of_C_Strings);

type Array_Of_Strings is array (Positive range <>) of access String;

function Get return Array_Of_C_Strings is
   function How_Many_Strings return Natural;
   pragma Import (C, How_Many_Strings,
"MYCFunctionTellingMeHowManyStrings");
   Length : constant Natural := How_Many_Strings;

   function Get_Array return Array_Of_C_Strings;
   pragma Import (C, Get_Array, "MyCFunctionReturninCharStarStar");

   Raw_Result : Array_Of_C_Strings (1 .. Length) := Get_Array;
   Result : Array_Of_Strings (1 .. Length);
begin
   for K in Result'Length loop
      Result (K) := new String'(Interfaces.C.Strings.Value (Raw_Result
(K)));
   end loop;
   return Result;
end Get;

This duplicates the strings and creates an array of access values;
each access value remembers the length of the string it points to.
There is probably another solution that avoids this duplication.

--
Ludovic Brenta.



  reply	other threads:[~2008-05-19 12:09 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-19  9:38 Pointer Sébastien
2008-05-19  9:53 ` Pointer Dmitry A. Kazakov
2008-05-19  9:54   ` Pointer Dmitry A. Kazakov
2008-05-19 10:13   ` Pointer Sébastien
2008-05-19 10:32     ` Pointer Dmitry A. Kazakov
2008-05-19 10:34     ` Pointer Ludovic Brenta
2008-05-19 11:31       ` Pointer Sébastien
2008-05-19 12:09         ` Ludovic Brenta [this message]
2008-05-19 12:09         ` Pointer Dmitry A. Kazakov
2008-05-19 12:47           ` Pointer Sébastien
2008-05-19 16:25         ` Pointer Matthew Heaney
2008-05-19 16:22     ` Pointer Matthew Heaney
2008-05-19 16:17 ` Pointer Matthew Heaney
2008-05-19 17:23   ` Pointer Sébastien
  -- strict thread matches above, loose matches on Subject: below --
2005-08-31 17:02 pointer TC
2005-08-31 18:40 ` pointer Martin Krischik
2005-08-31 18:52 ` pointer Jeffrey Carter
replies disabled

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