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-7-bit X-Google-Thread: 103376,38159b1b5557a2e7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-01-26 19:54:22 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!elnk-pas-nf1!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Standard Ada Preprocessor References: <400A9B48.3060100@noplace.com> <400BD4B5.6000307@noplace.com> <400BDB7C.40100@noplace.com> <400D2150.6000705@noplace.com> <400E72F9.8060501@noplace.com> <100upo7ln5e3k59@corp.supernews.com> <400FC8E8.2040100@noplace.com> <_JSdna166JuxFo3dRVn-hg@comcast.com> <401115B7.5020205@noplace.com> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Tue, 27 Jan 2004 03:54:21 GMT NNTP-Posting-Host: 63.184.17.57 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.news.pas.earthlink.net 1075175661 63.184.17.57 (Mon, 26 Jan 2004 19:54:21 PST) NNTP-Posting-Date: Mon, 26 Jan 2004 19:54:21 PST Xref: archiver1.google.com comp.lang.ada:4875 Date: 2004-01-27T03:54:21+00:00 List-Id: Warren W. Gay VE3WWG wrote: > Develop some Open Sourced code in > without gnatprep, and we'll see how portable it is ;-) Here's some Q&D open source, GPLed code to solve the following requirments, assuming that the proposed Ada.Directories is not available: List the names of all the files in the current directory, sorted in String order (that is, the order defined by "<" for String), one per line. Sure, this is a simple problem, but I don't have the time to work a significant problem. If you think a more complex problem should be used, provide the problem and your solution to it. All the OS and compiler dependencies are in the subunit Fill. I have provided a body for fill that works with GNAT 3.15p on any OS GNAT supports (because GNAT has already hidden the details of doing this on different OSes in GNAT.OS_Lib). Provide a different body for Fill for another compiler or OS, and explain why you need a preprocessor to do so. It's possible to push the OS and compiler dependencies down even further in this example, but I don't think it's necessary. Warning: Over 100 LOC follows. I apologize for the long posting, but am unable to respond to the challenge in a timely manner otherwise. Feel free to skip it if you like. I've used an 80-character line length, but some lines may wrap. -- File Lister: a sample open-source project without a preprocessor -- Copyright (C) 2004 by Jeffrey R. Carter -- This is free software; you can redistribute it and/or modify it under -- terms of the GNU General Public License as published by the Free Software -- Foundation; either version 2, or (at your option) any later version. -- This software is distributed in the hope that it will be useful, but WITH -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- for more details. Free Software Foundation, 59 Temple Place - Suite -- 330, Boston, MA 02111-1307, USA. -- The root package for the File Lister software. package File_Lister is pragma Pure; end File_Lister; -- The list must be instantiated at the library level because it uses a -- controlled type. -- Print_One must be declared here so we can pass its 'access to the list's -- Iterate procedure. with Ada.Strings.Unbounded; with PragmARC.Assignment; with PragmARC.List_Unbounded; package File_Lister.Lists is procedure Assign is new PragmARC.Assignment (Element => Ada.Strings.Unbounded.Unbounded_String); package File_List is new PragmARC.List_Unbounded (Element => Ada.Strings.Unbounded.Unbounded_String); procedure Print_One (Item : in out Ada.Strings.Unbounded.Unbounded_String; Context : in out File_List.Context_Data'Class; Pos : in File_List.Position; Continue : out Boolean); -- Print Item to standard output, followed by a line terminator. end File_Lister.Lists; with Ada.Text_IO; package body File_Lister.Lists is procedure Print_One (Item : in out Ada.Strings.Unbounded.Unbounded_String; Context : in out File_List.Context_Data'Class; Pos : in File_List.Position; Continue : out Boolean) is use Ada.Strings.Unbounded; use Ada.Text_IO; begin -- Print_One Put_Line (Item => To_String (Item)); Continue := True; end Print_One; end File_Lister.Lists; -- Main procedure for File Lister with Ada.Strings.Unbounded; with File_Lister.Lists; procedure File_Lister.Program is use File_Lister.Lists; procedure Fill (List : in out File_List.Handle) is separate; -- Fill List with information about the files in the current -- directory. List : File_List.Handle; Dummy : File_List.Context_Data; begin -- File_Lister.Program Fill (List => List); List.Sort (Less_Than => Ada.Strings.Unbounded."<"'access); List.Iterate (Action => Print_One'access, Context => Dummy); end File_Lister.Program; -- File_Lister.Program.Fill: version for GNAT 3.15p on all platforms. -- Replace to accomodate other platforms or compilers. with Ada.Strings.Unbounded; with GNAT.Directory_Operations; separate (File_Lister.Program) procedure Fill (List : in out File_List.Handle) is use Ada.Strings.Unbounded; use GNAT.Directory_Operations; Dir : Dir_Type; Name : String (1 .. 1_000); Last : Natural; Info : Unbounded_String; Pos : File_List.Position; begin -- Fill Open (Dir => Dir, Dir_Name => Get_Current_Dir); Get_All : loop Read (Dir => Dir, Str => Name, Last => Last); exit Get_All when Last <= 0; Info := To_Unbounded_String (Name (Name'First .. Last) ); List.Append (Item => Info, After => List.Last, New_Pos => Pos); end loop Get_All; Close (Dir => Dir); end Fill; I've created lists of Unbounded_Strings exceeding 3,000 strings without any memory problems on a Win98 machine with 64 KB of memory, so I doubt there are many situations where this program will have memory problems. Providing error handling is left as an exercise for the reader. -- Jeff Carter "Beyond 100,000 lines of code you should probably be coding in Ada." P. J. Plauger 26