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,496108d9dbc25896 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: printf package/Function/Procedure in Ada ?? Date: 1999/03/10 Message-ID: <7c6fuk$eug@sjx-ixn8.ix.netcom.com> X-Deja-AN: 453489243 References: <7c4e75$e5m@drn.newsguy.com> Organization: Netcom X-NETCOM-Date: Wed Mar 10 11:08:04 AM PST 1999 Newsgroups: comp.lang.ada Date: 1999-03-10T11:08:04-08:00 List-Id: In article <7c4e75$e5m@drn.newsguy.com>, solo wrote: > >Hello, > >Any one knows where one can obtain a printf package/Function written all >in Ada? >(i.e. without Ada interfacing to the C printf() function to do its thing). I find that the need for this sort of thing is usually for someone who is using the types defined in package Standard. I have a package that has been useful (for both Ada 83 and Ada 95) that you could use. it is not in the PAL, but I should probably put it there since several people in the Ada community have asked for it. Here is the package specification. -- ================================================================ -- BasicIO.ADS [by Richard Riehle, AdaWorks Software Engineering -- -- This IO package gets and puts data on a terminal. It has -- no random cursor positioning. I/O for Integer and Float is -- pre-instantiated. Use type Answer for dialogue management. A "sloppy" -- option simplifies Float input. Prompt option is provided for Get -- operations. Nested catenators can be made visible with the "use" clause. -- ================================================================ package BASICIO is type Answer is limited private; -- Values are (N, No, Y, Yes) procedure New_Line(Number : Positive := 1); procedure Set_Col(Number : Positive); -- must be > current column procedure Flush_Input_Line; procedure Put (Data : in Integer); procedure Put (Data : in Long_Integer); procedure Put (Data : in Character); procedure Put (Data : in String); procedure Put_Line(Data : in String); -- carriage return/line feed procedure Put (Data : in Float; Fore, Aft : in Positive); procedure Get(Data : out String; Last : out Natural); procedure Get(Prompt : in String; Data : out String; Last : out Natural); -- BasicIO.Get(Prompt => "Enter: ", Data => STR, Last => Len); procedure Get(Data : in out Answer); -- for limited private type procedure Get(Prompt : in String; Data : in out Answer); function Get return Character; -- X : Character := Get; function Get return Integer; function Get return Long_Integer; function Get (Sloppy : in Boolean := False) return Float; -- (Sloppy => True) permits sloppy input of floating point function Get (Prompt : in String) return Character; function Get (Prompt : in String) return Integer; function Get (Prompt : in String; Sloppy : in Boolean := False) return Float; -- The prompt has turned out to be especially useful for students function Is_Yes (Value : Answer) return Boolean; -- test Answer -- It has been suggested that this be, Is_No, or one of each package Catenators is -- use BasicIO.Catenators; for infix visibility function "&" (L : Integer; R : String) return String; function "&" (L : String; R : Integer) return String; function "&" (L : Long_Integer; R : String) return String; function "&" (L : String; R : Long_Integer) return String; function "&" (L : Float; R : String) return String; function "&" (L : String; R : Float) return String; end Catenators; Device_Error : exception; Invalid_Data : exception; private type Answer is (N, No, Y, Yes); -- Use for dialogue management end BASICIO; The package has been modified by some users to add more formatting capabilities. As it is, the floating point is always printed with FORE and AFT but never with EXP. The nested package, Catenators, is always "used". That is, in the declarative part of your own unit, use BasicIO.Catenators. The sloppy float is left over from Ada 83, but it still has more forms of sloppiness than the Ada 95 standard permits. Everything in this package is built on top of Text_IO so it is completely portable. This package has proven useful for a wide variety of purposes. Some users of Ada who do not care about strong typing (reseachers, scientists) are perfectly content with Standard.Integer and Standard.Float. It also demonstrates some ideas about providing the right abstraction to the right audience. C++ users are quick to notice that overloading in Ada is also controlled by the return type of a function. The limited private type, Answer, has proven useful for simple text-based dialogue management. A side benefit has been as a pedagogical device for introducing limited private early in a course. I will send the package body to anyone who wants it. There is a new version in process that uses Jerry Van Dijk's NT_Console package to enable simple screen positioning. It has seemed to me, for quite a long time, that Annex A should include a simple package like this, maybe called package Standard_IO. If there is enough interest I will create a version of this package for the PAL. Richard Riehle richard@adaworks.com http://www.adaworks.com