* Pointer
@ 2008-05-19 9:38 Sébastien
2008-05-19 9:53 ` Pointer Dmitry A. Kazakov
2008-05-19 16:17 ` Pointer Matthew Heaney
0 siblings, 2 replies; 17+ messages in thread
From: Sébastien @ 2008-05-19 9:38 UTC (permalink / raw)
Hi,
I'm getting from a C interfaces a char**data in a System.Address. I have
the following hypothesis:
- data is an array of n elements where n is known
- There is no memory issue (meaning data[i] with i < n is allocated)
- I do not have to free the memory (memory is managed by the C library)
How can I retrieve all my values in the C arrays using a loop to create
some Ada String (Interfaces.C.Strings.Value)?
thanks by advance,
Sebastien
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 9:38 Pointer Sébastien
@ 2008-05-19 9:53 ` Dmitry A. Kazakov
2008-05-19 9:54 ` Pointer Dmitry A. Kazakov
2008-05-19 10:13 ` Pointer Sébastien
2008-05-19 16:17 ` Pointer Matthew Heaney
1 sibling, 2 replies; 17+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-19 9:53 UTC (permalink / raw)
On Mon, 19 May 2008 09:38:08 +0000, S�bastien wrote:
> I'm getting from a C interfaces a char**data in a System.Address. I have
> the following hypothesis:
> - data is an array of n elements where n is known
> - There is no memory issue (meaning data[i] with i < n is allocated)
> - I do not have to free the memory (memory is managed by the C library)
>
> How can I retrieve all my values in the C arrays using a loop to create
> some Ada String (Interfaces.C.Strings.Value)?
Suggesting, n is returned from the C program:
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
function Get_It return String is
procedure Internal (N : out size_t; Data : out chars_ptr);
pragma Import (C, Internal, "<C-name>");
Data : chars_ptr;
N : size_t;
begin
Internal (Data);
return Value (N, Data);
end Get_It;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 9:53 ` Pointer Dmitry A. Kazakov
@ 2008-05-19 9:54 ` Dmitry A. Kazakov
2008-05-19 10:13 ` Pointer Sébastien
1 sibling, 0 replies; 17+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-19 9:54 UTC (permalink / raw)
On Mon, 19 May 2008 11:53:11 +0200, Dmitry A. Kazakov wrote:
> On Mon, 19 May 2008 09:38:08 +0000, S�bastien wrote:
>
>> I'm getting from a C interfaces a char**data in a System.Address. I have
>> the following hypothesis:
>> - data is an array of n elements where n is known
>> - There is no memory issue (meaning data[i] with i < n is allocated)
>> - I do not have to free the memory (memory is managed by the C library)
>>
>> How can I retrieve all my values in the C arrays using a loop to create
>> some Ada String (Interfaces.C.Strings.Value)?
>
> Suggesting, n is returned from the C program:
>
> with Interfaces.C; use Interfaces.C;
> with Interfaces.C.Strings; use Interfaces.C.Strings;
>
> function Get_It return String is
> procedure Internal (N : out size_t; Data : out chars_ptr);
> pragma Import (C, Internal, "<C-name>");
> Data : chars_ptr;
> N : size_t;
> begin
> Internal (Data);
Internal (N, Data); -- Of course
> return Value (N, Data);
> end Get_It;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 9:53 ` Pointer Dmitry A. Kazakov
2008-05-19 9:54 ` Pointer Dmitry A. Kazakov
@ 2008-05-19 10:13 ` Sébastien
2008-05-19 10:32 ` Pointer Dmitry A. Kazakov
` (2 more replies)
1 sibling, 3 replies; 17+ messages in thread
From: Sébastien @ 2008-05-19 10:13 UTC (permalink / raw)
> Suggesting, n is returned from the C program:
>
> with Interfaces.C; use Interfaces.C;
> with Interfaces.C.Strings; use Interfaces.C.Strings;
>
> function Get_It return String is
> procedure Internal (N : out size_t; Data : out chars_ptr);
> pragma Import (C, Internal, "<C-name>");
> Data : chars_ptr;
> N : size_t;
> begin
> Internal (Data);
> return Value (N, Data);
> end Get_It;
Yes it's exaclty what I did ... and it's exaclty what I would like to
avoid :-) Actually, I was looking for a pure Ada solution.
I think there is a way using an unchecked type conversion, but how to be
sure of the size of the System.Address?
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 10:13 ` Pointer Sébastien
@ 2008-05-19 10:32 ` Dmitry A. Kazakov
2008-05-19 10:34 ` Pointer Ludovic Brenta
2008-05-19 16:22 ` Pointer Matthew Heaney
2 siblings, 0 replies; 17+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-19 10:32 UTC (permalink / raw)
On Mon, 19 May 2008 10:13:48 +0000, S�bastien wrote:
>> Suggesting, n is returned from the C program:
>>
>> with Interfaces.C; use Interfaces.C;
>> with Interfaces.C.Strings; use Interfaces.C.Strings;
>>
>> function Get_It return String is
>> procedure Internal (N : out size_t; Data : out chars_ptr);
>> pragma Import (C, Internal, "<C-name>");
>> Data : chars_ptr;
>> N : size_t;
>> begin
>> Internal (Data);
>> return Value (N, Data);
>> end Get_It;
>
> Yes it's exaclty what I did ... and it's exaclty what I would like to
> avoid :-) Actually, I was looking for a pure Ada solution.
In what sense pure? Do you want to re-implement Interfaces.C.String?
> I think there is a way using an unchecked type conversion, but how to be
> sure of the size of the System.Address?
(And how to sure about the size of Character vs. char?)
You could convert System.Address to a pointer of an Ada object using an
instance of System.Address_To_Access_Conversions.
(It seems that I don't fully understand your problem)
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 10:13 ` Pointer Sébastien
2008-05-19 10:32 ` Pointer Dmitry A. Kazakov
@ 2008-05-19 10:34 ` Ludovic Brenta
2008-05-19 11:31 ` Pointer Sébastien
2008-05-19 16:22 ` Pointer Matthew Heaney
2 siblings, 1 reply; 17+ messages in thread
From: Ludovic Brenta @ 2008-05-19 10:34 UTC (permalink / raw)
Sébastien wrote:
> > Suggesting, n is returned from the C program:
> >
> > with Interfaces.C; use Interfaces.C;
> > with Interfaces.C.Strings; use Interfaces.C.Strings;
> >
> > function Get_It return String is
> > procedure Internal (N : out size_t; Data : out chars_ptr);
> > pragma Import (C, Internal, "<C-name>");
> > Data : chars_ptr;
> > N : size_t;
> > begin
> > Internal (Data);
> > return Value (N, Data);
> > end Get_It;
>
> Yes it's exaclty what I did ... and it's exaclty what I would like to
> avoid :-) Actually, I was looking for a pure Ada solution.
> I think there is a way using an unchecked type conversion, but how to be
> sure of the size of the System.Address?
procedure To_String (Pointer : in System.Address; Length : in Natural)
return String is
Result : String (1 .. Length);
for Result'Address use Pointer;
pragma Import (Ada, Result); -- suppress default initialization
begin
return Result;
end To_String;
But Dmitryi's solution is safer, of course. HTH
--
Ludovic Brenta.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 10:34 ` Pointer Ludovic Brenta
@ 2008-05-19 11:31 ` Sébastien
2008-05-19 12:09 ` Pointer Ludovic Brenta
` (2 more replies)
0 siblings, 3 replies; 17+ messages in thread
From: Sébastien @ 2008-05-19 11:31 UTC (permalink / raw)
> procedure To_String (Pointer : in System.Address; Length : in Natural)
> return String is
> Result : String (1 .. Length);
> for Result'Address use Pointer;
> pragma Import (Ada, Result); -- suppress default initialization
> begin
> return Result;
> end To_String;
>
> But Dmitryi's solution is safer, of course. HTH
Ok, I was not clear enough. Converting char* to String is not an issue,
I'm using Interfaces.C.String with no problem. My problem is to convert
all the strings in the char** in string one by one.
Something like this:
CharPtrPtr: System.Address := MyCFunctionReturninCharStarStar;
N: Natural := MYCFunctionTellingMeHowManyStrings;
for i in Natural range 0 .. N loop
-- Some ada code to convert CharPtrPtr[i] to String
end loop;
Of course I can do somthing like that:
Compile this using
gcc -O2 -g3 -ansi -pedantic -W -Wall -c mybind.c -o mybind.o:
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 ;-)
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 11:31 ` Pointer Sébastien
@ 2008-05-19 12:09 ` Ludovic Brenta
2008-05-19 12:09 ` Pointer Dmitry A. Kazakov
2008-05-19 16:25 ` Pointer Matthew Heaney
2 siblings, 0 replies; 17+ messages in thread
From: Ludovic Brenta @ 2008-05-19 12:09 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 11:31 ` Pointer Sébastien
2008-05-19 12:09 ` Pointer Ludovic Brenta
@ 2008-05-19 12:09 ` Dmitry A. Kazakov
2008-05-19 12:47 ` Pointer Sébastien
2008-05-19 16:25 ` Pointer Matthew Heaney
2 siblings, 1 reply; 17+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-19 12:09 UTC (permalink / raw)
On Mon, 19 May 2008 11:31:57 +0000, S�bastien wrote:
>> procedure To_String (Pointer : in System.Address; Length : in Natural)
>> return String is
>> Result : String (1 .. Length);
>> for Result'Address use Pointer;
>> pragma Import (Ada, Result); -- suppress default initialization
>> begin
>> return Result;
>> end To_String;
>>
>> But Dmitryi's solution is safer, of course. HTH
>
> Ok, I was not clear enough. Converting char* to String is not an issue,
> I'm using Interfaces.C.String with no problem. My problem is to convert
> all the strings in the char** in string one by one.
Aha, it is const char *[] ...
> Something like this:
> CharPtrPtr: System.Address := MyCFunctionReturninCharStarStar;
> N: Natural := MYCFunctionTellingMeHowManyStrings;
>
> for i in Natural range 0 .. N loop
> -- Some ada code to convert CharPtrPtr[i] to String
> end loop;
with Interfaces.C.Pointers;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Interfaces.C; use Interfaces.C;
type Chars_Ptr_Array is array (size_t range <>) of aliased chars_ptr;
pragma Convention (C, Chars_Ptr_Array);
package Pointers_To_String is
new Interfaces.C.Pointers (size_t, chars_ptr, chars_ptr_array, null);
use Pointers_To_String;
...
Data : Pointers_To_String.Pointer := MyCFunctionReturninCharStarStar;
N: Natural := MYCFunctionTellingMeHowManyStrings;
...
for Index in 1..N loop
declare
Element : String := Value (Data.all);
begin
... -- Use the element
end;
Increment (Data); -- Move to the next one
end loop;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 12:09 ` Pointer Dmitry A. Kazakov
@ 2008-05-19 12:47 ` Sébastien
0 siblings, 0 replies; 17+ messages in thread
From: Sébastien @ 2008-05-19 12:47 UTC (permalink / raw)
> with Interfaces.C.Pointers;
> with Interfaces.C.Strings; use Interfaces.C.Strings;
> with Interfaces.C; use Interfaces.C;
>
> type Chars_Ptr_Array is array (size_t range <>) of aliased chars_ptr;
> pragma Convention (C, Chars_Ptr_Array);
> package Pointers_To_String is
> new Interfaces.C.Pointers (size_t, chars_ptr, chars_ptr_array, null);
> use Pointers_To_String;
> ...
> Data : Pointers_To_String.Pointer := MyCFunctionReturninCharStarStar;
> N: Natural := MYCFunctionTellingMeHowManyStrings;
> ...
> for Index in 1..N loop
> declare
> Element : String := Value (Data.all);
> begin
> ... -- Use the element
> end;
> Increment (Data); -- Move to the next one
> end loop;
Ok I didn't know Interfaces.C.Pointer, it's looks like exaclty what I
need and it's a replacement for pointer arithmetic operations.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 11:31 ` Pointer Sébastien
2008-05-19 12:09 ` Pointer Ludovic Brenta
2008-05-19 12:09 ` Pointer Dmitry A. Kazakov
@ 2008-05-19 16:25 ` Matthew Heaney
2 siblings, 0 replies; 17+ messages in thread
From: Matthew Heaney @ 2008-05-19 16:25 UTC (permalink / raw)
On May 19, 7:31 am, Sébastien <seb.mor...@gmail.com> wrote:
>
> 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?
No, you don't need a function written in C. Just use an address
clause, as I described in my original post.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
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 16:22 ` Matthew Heaney
2 siblings, 0 replies; 17+ messages in thread
From: Matthew Heaney @ 2008-05-19 16:22 UTC (permalink / raw)
On May 19, 6:13 am, Sébastien <seb.mor...@gmail.com> wrote:
>
> Yes it's exaclty what I did ... and it's exaclty what I would like to
> avoid :-) Actually, I was looking for a pure Ada solution.
> I think there is a way using an unchecked type conversion, but how to be
> sure of the size of the System.Address?
If you don't want to use an address clause, you could do something
like:
procedure Op (argv_Address : System.Address) is
type Argv_Type is array (Positive) of chars_ptr;
pragma Convention (C, Argv_Type);
type Argv_Pointer is access all Argv_Type;
for Argv_Pointer'Storage_Size use 0;
pragma Convention (C, Argv_Pointer);
function To_Argv_Pointer is
new Unchecked_Conversion (System.Address, Argv_Pointer);
Argv : constant Argv_Pointer :=
To_Argv_Pointer (argv_Address);
begin
-- same as in my last post
end;
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 9:38 Pointer Sébastien
2008-05-19 9:53 ` Pointer Dmitry A. Kazakov
@ 2008-05-19 16:17 ` Matthew Heaney
2008-05-19 17:23 ` Pointer Sébastien
1 sibling, 1 reply; 17+ messages in thread
From: Matthew Heaney @ 2008-05-19 16:17 UTC (permalink / raw)
On May 19, 5:38 am, Sébastien <seb.mor...@gmail.com> wrote:
>
> I'm getting from a C interfaces a char**data in a System.Address. I have
> the following hypothesis:
> - data is an array of n elements where n is known
> - There is no memory issue (meaning data[i] with i < n is allocated)
> - I do not have to free the memory (memory is managed by the C library)
>
> How can I retrieve all my values in the C arrays using a loop to create
> some Ada String (Interfaces.C.Strings.Value)?
procedure Op (argv_Address : System.Address; N : Natural) is
type Argv_Type is array (Positive) of chars_ptr;
pragma Convention (C, Argv_Type);
Argv : Argv_Type;
for Argv'Address use argv_Address;
begin
for I in 1 .. N loop
Put_Line (Value (Argv (I))); -- or whatever
end loop;
end Op;
Is that what you had in mind?
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Pointer
2008-05-19 16:17 ` Pointer Matthew Heaney
@ 2008-05-19 17:23 ` Sébastien
0 siblings, 0 replies; 17+ messages in thread
From: Sébastien @ 2008-05-19 17:23 UTC (permalink / raw)
> procedure Op (argv_Address : System.Address; N : Natural) is
> type Argv_Type is array (Positive) of chars_ptr;
> pragma Convention (C, Argv_Type);
>
> Argv : Argv_Type;
> for Argv'Address use argv_Address;
>
> begin
> for I in 1 .. N loop
> Put_Line (Value (Argv (I))); -- or whatever
> end loop;
> end Op;
>
> Is that what you had in mind?
Yes, thanks very much for all the codes you provided, it helps me a lot
to understand all aspects of the problem.
Sebastien
^ permalink raw reply [flat|nested] 17+ messages in thread
* pointer
@ 2005-08-31 17:02 TC
2005-08-31 18:40 ` pointer Martin Krischik
2005-08-31 18:52 ` pointer Jeffrey Carter
0 siblings, 2 replies; 17+ messages in thread
From: TC @ 2005-08-31 17:02 UTC (permalink / raw)
How translate this c code?
typedef struct
{
unsigned char sParola [50];
int iFrequenza;
}Parola;
struct Lista
{
Parola P;
struct Lista *next;
};
struct Lista *vett[26];
struct Lista *LF;
please help me!
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: pointer
2005-08-31 17:02 pointer TC
@ 2005-08-31 18:40 ` Martin Krischik
2005-08-31 18:52 ` pointer Jeffrey Carter
1 sibling, 0 replies; 17+ messages in thread
From: Martin Krischik @ 2005-08-31 18:40 UTC (permalink / raw)
TC wrote:
> How translate this c code?
>
> typedef struct
> {
> unsigned char sParola [50];
> int iFrequenza;
> }Parola;
>
>
> struct Lista
> {
> Parola P;
> struct Lista *next;
> };
>
>
> struct Lista *vett[26];
>
>
> struct Lista *LF;
>
> please help me!
you will need:
Strings: http://en.wikibooks.org/wiki/Ada_Programming/Strings
Records: http://en.wikibooks.org/wiki/Ada_Programming/Types/record
Access Types: http://en.wikibooks.org/wiki/Ada_Programming/Types/access
Arrays: http://en.wikibooks.org/wiki/Ada_Programming/Types/array
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: pointer
2005-08-31 17:02 pointer TC
2005-08-31 18:40 ` pointer Martin Krischik
@ 2005-08-31 18:52 ` Jeffrey Carter
1 sibling, 0 replies; 17+ messages in thread
From: Jeffrey Carter @ 2005-08-31 18:52 UTC (permalink / raw)
TC wrote:
> How translate this c code?
It depends on what you mean by "translate". One could write C-in-Ada
that would be just like this, with all the opportunities for error that
implies.
However, it looks as if you're implementing a linked list with a record
as its data. To do that it's probably best to use the list component of
the standard data-structure library that will be part of Ada 0X. If you
can't do that, because you don't have an Ada-0X compiler, you can find
list components in most of the many fine Ada data-structure libraries
that are available on the web. I recommend the PragmAda Reusable
Components, but I may be biased.
Looking more closely, this appears to be part of a word-counting
program, and so probably homework. If you're doing word counting, you
probably want something that allows O(log N) (or better) insertions and
lookup, such as a map component from Ada.Containers (Ada 0X), or the
skip list from the PragmARCs.
--
Jeffrey Carter
"Now go away or I shall taunt you a second time."
Monty Python and the Holy Grail
E-mail: jeffrey_r_carter-nr [commercial-at]
raytheon [period | full stop] com
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2008-05-19 17:23 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` Pointer Ludovic Brenta
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox