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: 103376,f0d9db8126fd91cb X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!feeder.news-service.com!newsfeed-fusi2.netcologne.de!195.14.215.230.MISMATCH!news.netcologne.de!newsfeed-hp2.netcologne.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Converting pointers to non nul-terminated C "strings" to Ada string Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <9o91prwivma0$.1e4dd833ypxy1.dlg@40tude.net> <6948ef0c-ce70-4330-ba2f-313d9b6ff6d5@l16g2000yqo.googlegroups.com> Date: Sat, 14 Feb 2009 12:16:02 +0100 Message-ID: <1tbswd6ooo8u1.1jl2rgy3nq0lq$.dlg@40tude.net> NNTP-Posting-Date: 14 Feb 2009 12:16:03 CET NNTP-Posting-Host: d7da565d.newsspool4.arcor-online.net X-Trace: DXC=4g`3M1_T:ni[F<50eo:0kn4IUK On Sat, 14 Feb 2009 02:38:35 -0800 (PST), xorquewasp@googlemail.com wrote: > Dmitry A. Kazakov wrote: >> >> I would suggest to use Interfaces.C.Pointers instead of >> Interfaces.C.Strings: > > That does mean going outside of the RM, which is a bit unfortunate. > I'll keep it in mind, however. What do you mean by that? Interfaces.C.Pointers is a part of the standard Ada library. >> Are you sure that get_data is char **, int * rather than void **, size_t *? >> In the latter case I would consider Storage_Array instead of String on the >> Ada side. > > Well, the C function parameters are defined as char **, int * but do have > a meaning closer to void **, size_t *. Storage_Array is probably more > appropriate, as you state. That still leaves the question of > converting from an address or access type to an unconstrained array (can't pass an > unconstrained array or access to an unconstrained array to C code). One technique is to declare a huge flat array: type Never_Instantiate_Me is array (size_t'range) of char; pragma Convention (C, Never_Instantiate_Me); or else simply: subtype Never_Instantiate_Me is char_array (size_t'range); Never_Instantiate_Me will have no dope. type Never_Instantiate_Me_Ptr is access all Never_Instantiate_Me; pragma Convention (C, Never_Instantiate_Me_Ptr);; Then you go: function Get return String is procedure memory_data ( data : out Never_Instantiate_Me_Ptr; size : out size_t ); pragma Import (C, memory_data, "get_data"); Data : Never_Instantiate_Me_Ptr; Size : size_t; begin memory_data (Data, Size); if Size = 0 then return ""; else return To_Ada ( Data (size_t'First..size_t'First + Size - 1), False ); end if; end Get; Note Size = 0 check. When Size is zero, there is no way to construct an empty char_array since size_t is modular. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de