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,6c43f45c2ab47c51 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!s50g2000hsb.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Re: Using Ada.Containers.Vector and Limited Private types Date: Sun, 6 Jul 2008 11:09:58 -0700 (PDT) Organization: http://groups.google.com Message-ID: <8ff4c6c2-9892-463e-bdfd-1f7bfd78d607@s50g2000hsb.googlegroups.com> References: NNTP-Posting-Host: 70.101.174.178 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1215367799 19985 127.0.0.1 (6 Jul 2008 18:09:59 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 6 Jul 2008 18:09:59 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s50g2000hsb.googlegroups.com; posting-host=70.101.174.178; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; MS-RTC LM 8),gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:1028 Date: 2008-07-06T11:09:58-07:00 List-Id: On Jul 5, 7:53=A0am, Dale Stanbrough wrote: > I'm trying to sort a collection of Ada.Directories.Directory_Entry (a > limited private type) by storing pointers to them in a Vector but am > having problems with accessibility rules. > > Is it possible to do this? > > Package Ada.Directories only has procedures that have "in out"/out > parameters for the Directory_Entry type, so I can't figure out how to > get a reference to each object as I run through the More_Entries/ > Get_Next_Entry procedures. > > Thanks, > > Dale > > --------------------------------------- > -- Displays directory entries in increasing date/time order > > with Ada.Text_IO; =A0 =A0 =A0 =A0 =A0 =A0use Ada.Text_IO; > with Ada.Integer_Text_IO; =A0 =A0use Ada.Integer_Text_IO; > with Ada.Containers.Vectors; > with Ada.Directories; =A0 =A0 =A0 =A0use Ada.Directories; > with Ada.Calendar; =A0 =A0 =A0 =A0 =A0 use Ada.Calendar; -- for comparing > date/times > > procedure List_By_Date is > > =A0 =A0type Dir_Entry_Ptr is access all Directory_Entry_Type; > > =A0 =A0package Dir_Vectors is new Ada.Containers.Vectors (Positive, > Dir_Entry_Ptr); > =A0 =A0use Dir_Vectors; > > =A0 =A0DV : Vector; > > begin > > =A0 =A0-- collect the entries > =A0 =A0declare > =A0 =A0 =A0 Directory =A0 =A0 : constant Filter_Type :=3D (true, =A0false= , false); > =A0 =A0 =A0 Ordinary_File : constant Filter_Type :=3D (false, true, =A0fa= lse); > > =A0 =A0 =A0 Results : Search_Type; -- used to hold the result of a search > =A0 =A0 =A0 Directory_Entry : aliased Directory_Entry_Type; > > =A0 =A0begin > =A0 =A0 =A0 Start_Search ( Results, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Directory =3D> Current_Directory, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Pattern =A0 =3D> "", -- select all files > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Filter =A0 =A0=3D> Directory or Ordinary_F= ile > =A0 =A0 =A0 =A0 =A0 =A0 =A0); > > =A0 =A0 =A0 while More_Entries (Results) loop > =A0 =A0 =A0 =A0 =A0Get_Next_Entry (Results, Directory_Entry); > =A0 =A0 =A0 =A0 =A0DV.Append (Directory_Entry'access); > =A0 =A0 =A0 =A0 =A0 =A0 -- **********************************************= ******** > =A0 =A0 =A0 =A0 =A0 =A0 -- this is of course incorrect, but how to get ar= ound it? > =A0 =A0 =A0 =A0 =A0 =A0 -- **********************************************= ******** > =A0 =A0 =A0 end loop; > > =A0 =A0end; > > =A0 =A0-- sort the entries > =A0 =A0declare > =A0 =A0 =A0 function My_Sorter (Left, Right : Dir_Entry_Ptr) return boole= an > =A0 =A0 =A0 is > =A0 =A0 =A0 begin > =A0 =A0 =A0 =A0 =A0return Ada.Directories.Modification_Time (Left.all) < > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Ada.Directories.Modification_Time (Right.= all); > =A0 =A0 =A0 end; > > =A0 =A0 =A0 package Dir_Vector_Sorting is new Generic_Sorting (My_Sorter)= ; > > =A0 =A0begin > =A0 =A0 =A0 Dir_Vector_Sorting.Sort (DV); > =A0 =A0end; > > =A0 =A0-- display the entries > =A0 =A0declare > =A0 =A0 =A0 procedure Display_Entry (C : Cursor) > =A0 =A0 =A0 is > =A0 =A0 =A0 =A0 =A0Directory_Entry_Ptr : constant Dir_Entry_Ptr :=3D Elem= ent (C); > =A0 =A0 =A0 begin > =A0 =A0 =A0 =A0 =A0Put (Simple_Name (Directory_Entry_Ptr.all)); > =A0 =A0 =A0 end; > =A0 =A0begin > =A0 =A0 =A0 DV.iterate (Display_Entry'access); > =A0 =A0end; > > end List_By_Date; > > -- > dstan...@spam.o.matic.bigpond.net.au You'll need to allocate a fresh directory entry to "fill in" with each call to Get_Next_Entry, as George suggested. I believe that declaring the pointer type as "access" rather than "access all" will cause the allocated entries to be automatically freed with the storage pool when the procedure exits. (Of course in this 1-procedure program that's irrelevant, but not if used as part of something larger.) This works fine on my system: --------------------------------------- -- Displays directory entries in increasing date/time order with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Containers.Vectors; with Ada.Directories; use Ada.Directories; with Ada.Calendar; use Ada.Calendar; -- for comparing date/ times procedure List_By_Date is type Dir_Entry_Ptr is access Directory_Entry_Type; package Dir_Vectors is new Ada.Containers.Vectors ( Positive, Dir_Entry_Ptr); use Dir_Vectors; DV : Vector; begin -- collect the entries declare Directory : constant Filter_Type :=3D (True, False, False); Ordinary_File : constant Filter_Type :=3D (False, True, False); Results : Search_Type; -- used to hold the result of a search begin Start_Search (Results, Directory =3D> Current_Directory, Pattern =3D> "", -- select all files Filter =3D> Directory or Ordinary_File); while More_Entries (Results) loop declare Directory_Entry : Dir_Entry_Ptr :=3D new Directory_Entry_Type; begin Get_Next_Entry (Results, Directory_Entry.all); DV.Append (Directory_Entry); end; end loop; end; -- sort the entries declare function My_Sorter (Left, Right : Dir_Entry_Ptr) return Boolean is begin return Ada.Directories.Modification_Time (Left.all) < Ada.Directories.Modification_Time (Right.all); end My_Sorter; package Dir_Vector_Sorting is new Generic_Sorting (My_Sorter); begin Dir_Vector_Sorting.Sort (DV); end; -- display the entries declare procedure Display_Entry (C : Cursor) is begin Put_Line (Simple_Name (Element (C).all)); end Display_Entry; begin DV.Iterate (Display_Entry'Access); end; end List_By_Date;