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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,9fa5063fbdc75ae4 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-16 11:13:38 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!logbridge.uoregon.edu!newshub.sdsu.edu!elnk-nf2-pas!elnk-pas-nf1!newsfeed.earthlink.net!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!intern1.nntp.aus1.giganews.com!nntp.giganews.com!nntp.clear.net.nz!news.clear.net.nz.POSTED!not-for-mail NNTP-Posting-Date: Thu, 16 Oct 2003 13:13:37 -0500 From: Craig Carey Newsgroups: comp.lang.ada Subject: Re: array of strings in a function Date: Fri, 17 Oct 2003 07:13:42 +1300 Message-ID: References: <8unsov8a3r9p02lm0ftkmmget77esc7fv7@4ax.com> X-Newsreader: Forte Agent 1.92/32.572 MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Organization: Customer of Mercury Telecommunications Ltd Cache-Post-Path: drone5.qsi.net.nz!unknown@tnt2-243.quicksilver.net.nz X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/) X-Original-NNTP-Posting-Host: drone5-svc-skyt.qsi.net.nz X-Original-Trace: 17 Oct 2003 07:13:45 +1300, drone5-svc-skyt.qsi.net.nz NNTP-Posting-Host: 203.97.37.6 X-Trace: sv3-fLTx5yRQn45sNUpFykcWTEepSwe62dHBx7DucEN4Tw1GXTpHtYoL8juJIDsaDVXv+pIza9KPvrWGvDD!BQl2tdrWz45ObCuhsCO/Uy2iimXuP4yPsQcu8czmoKHA6nP8SnJXxCn1rwWn5QlpyzXepi1/afzW!Kz1hcpY= X-Complaints-To: Complaints to abuse@clear.net.nz X-DMCA-Complaints-To: Complaints to abuse@clear.net.nz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.1 Xref: archiver1.google.com comp.lang.ada:1009 Date: 2003-10-17T07:13:42+13:00 List-Id: On Thu, 16 Oct 2003 22:31:23 +1300, Craig Carey wrote: >Functions with varying numbers of arguments are handled by Ada 95: Here a package that allows an array of pointers to Ada 95 Strings, to be constructed and read, with no use of the heap. The pointers now point to the first characters of the Strings. --------- with Ada.Text_IO, Ada.Unchecked_Conversion, System; -- pragma Style_Checks ("3abcefhiklM79noprst"); procedure Main is package Varargs is type Char_Const_Ptr is access constant Character; type R_Rec is record Len : Natural; Char1 : Char_Const_Ptr; end record; type SPA is array (Positive range <>) of R_Rec; function "+" (S : String) return R_Rec; function Value (R : R_Rec) return String; end Varargs; package body Varargs is Nil : aliased Character; function Addr_To_Char_Ptr is new Ada.Unchecked_Conversion ( Source => System.Address, Target => Char_Const_Ptr); function "+" (S : String) return R_Rec is begin if S = "" then return R_Rec'(Len => 0, Char1 => Nil'Access); else return R_Rec'(Len => S'Length, Char1 => Addr_To_Char_Ptr (S (1)'Address)); end if; end "+"; function Value (R : R_Rec) return String is Overwrite : String (1 .. 100); -- <- Has no effect begin if R.Len <= 0 then return ""; end if; declare subtype String_1 is String (1 .. R.Len); type String_1_Ptr is access String_1; function To_S1_Ptr is new Ada.Unchecked_Conversion ( Source => Char_Const_Ptr, -- 32 bits Target => String_1_Ptr); begin return To_S1_Ptr (R.Char1).all; end; end Value; end Varargs; use Varargs; procedure Demo (U : SPA) is package Io renames Ada.Text_IO; begin Io.Put ("Got : "); for K in U'Range loop Io.Put ('"' & Value (U (K)) & '"'); if K /= U'Last then Io.Put (", "); end if; end loop; Io.Put_Line ("."); end Demo; G : String := "00275"; begin Demo ((1 => +"PQR", 2 => +G, 3 => +"")); end Main; --------- The output produced in GNAT 3.15 and ObjectAda 7.2.2, shows that the pointers to the strings were packaged and unpackaged correctly: | Got : "PQR", "00275", "". Some of the other solutions seem worse. On Thu, 16 Oct 2003 14:30:51 GMT, "Robert I. Eachus" wrote: ... >Of course, this would mean that users couldn't have "+" in their name >strings. >On Thu, 16 Oct 2003 06:40:26 GMT, tmoran@acm.org wrote: >... >> But you can certainly have a set of subprograms with the same name ... > > > >At Wed, 15 Oct 2003 13:49:42 +0200, Antonio Mart�nez wrote: > Subject: Re: array of strings in a function > [from comp.lang.ada-@-ada-france.org ] > >>What I want to do is a procedure (sorry, not a function) to do something >>like this: >> >>my_AND("Entrada_1", "P2", "Output"); ... >> In general, I don't know the number of string arguments. ... -- Craig Carey