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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f0d9db8126fd91cb X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,GB Path: g2news1.google.com!postnews.google.com!r15g2000prd.googlegroups.com!not-for-mail From: likai3g@gmail.com Newsgroups: comp.lang.ada Subject: Re: Converting pointers to non nul-terminated C "strings" to Ada string Date: Sat, 14 Feb 2009 01:33:21 -0800 (PST) Organization: http://groups.google.com Message-ID: <3178ce8f-2927-40af-9706-1803a4570cf8@r15g2000prd.googlegroups.com> References: NNTP-Posting-Host: 218.18.68.243 Mime-Version: 1.0 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1234604001 6047 127.0.0.1 (14 Feb 2009 09:33:21 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 14 Feb 2009 09:33:21 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r15g2000prd.googlegroups.com; posting-host=218.18.68.243; posting-account=4uMkDwoAAACMyNluw6CdeU7BMSgYHFMk User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:3630 Date: 2009-02-14T01:33:21-08:00 List-Id: On 2=D4=C214=C8=D5, =CF=C2=CE=E72=CA=B158=B7=D6, 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 =3D> data, Length =3D> 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;