From: likai3g@gmail.com
Subject: Re: Converting pointers to non nul-terminated C "strings" to Ada string
Date: Sat, 14 Feb 2009 01:33:21 -0800 (PST)
Date: 2009-02-14T01:33:21-08:00 [thread overview]
Message-ID: <3178ce8f-2927-40af-9706-1803a4570cf8@r15g2000prd.googlegroups.com> (raw)
In-Reply-To: df6e5b77-2d9f-4428-9e34-5f63bea39816@u13g2000yqg.googlegroups.com
On 2月14日, 下午2时58分, xorquew...@googlemail.com wrote:
> Hello.
>
> I have some C code that returns a pointer to a buffer of
> "data" (not a string, just raw data) and also returns the
> size of the buffer:
>
> void
> get_data (char **ptr, int *size);
>
> int size;
> char *ptr;
>
> get_data (&ptr, &size);
>
> The code is part of a library that I don't control, so I can't
> change the interface.
>
> I've defined the Ada equivalent as:
>
> package CS renames Interfaces.C.Strings;
> package C renames Interfaces.C;
>
> procedure memory_data
> (data : out CS.chars_ptr;
> size : out C.int);
> pragma Import (C, memory_data, "get_data");
>
> This works correctly, however I'm having trouble converting
> the chars_ptr type to an Ada string given that it's not nul-
> terminated.
>
> Attempts to do the following:
>
> return CS.Value (Item => data, Length => size);
>
> ... results in a String as long as the position of the first nul in
> the
> C string (the position is inevitably less than the length of the
> string).
>
> Is it possible to properly convert unterminated C strings or do I
> need to start messing about with raw System.Address types?
--try this....
declare
subtype Huge is String(Positive);
type PHuge is access all Huge;
type CString is record
Data: PHuge;
Size: Natural;
end record;
pragma Convention(C_Pass_By_Copy, CString);
procedure Get_Data(S: out CString);
pragma Import(C, Get_Data, "get_data");
S: CString;
begin
Get_Data(S);
Ada.Text_IO.Put_Line(S.Data(1..S.Size));
end;
prev parent reply other threads:[~2009-02-14 9:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-02-14 6:58 Converting pointers to non nul-terminated C "strings" to Ada string xorquewasp
2009-02-14 9:26 ` Dmitry A. Kazakov
2009-02-14 10:38 ` xorquewasp
2009-02-14 11:16 ` Dmitry A. Kazakov
2009-02-14 11:37 ` xorquewasp
2009-02-14 9:27 ` Hibou57 (Yannick Duchêne)
2009-02-14 10:41 ` xorquewasp
2009-02-15 17:44 ` Georg Bauhaus
2009-02-14 11:47 ` Jacob Sparre Andersen
2009-02-14 14:25 ` Hibou57 (Yannick Duchêne)
2009-02-14 9:28 ` sjw
2009-02-14 9:33 ` likai3g [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox