TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
"Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Mike Bates 5598 Computer_Systems <[log in to unmask]>
Wed, 3 Mar 1999 09:35:26 -0800
AdaWorks <[log in to unmask]>
AdaWorks <[log in to unmask]>
TEXT/PLAIN (119 lines)
Mike asks a good question below.

On Wed, 3 Mar 1999, Mike Bates 5598 Computer_Systems wrote:

> If you want to make the contrast more dramatic, add some plain text, a
> variable-length string, and an integer in hexadecimal.
> How many lines does it take to do this in Ada, C++, or Java:
>       printf("The result for %-30s is %10.2f (%08x)\n", x, y, z);
> My question: Has anyone written good Ada equivalents to printf and
> scanf?

  I have a package I use for teaching that comes close. It
  turns out to be useful for scientists and others who are not
  concerned with any types other than those in package Standard.

  with Basic_IO;
  procedure Demonstrate is
    use Basic_IO.Catenators;
    X : Float;
    X := Basic_IO.Get("Enter a Value");
    Basic_IO.Put_Line("X = " & X & ",a floating point value ");
  end Demonstrate

  All the scalar input is in the form of a function,

      function Get(Prompt : String :="") return Scalar_Data_Type

  There is a nested package, Catenators, which catenates float to string,
  string to float, integer to string, string to integer, as well as the
  option for catenatating long float and long integer.

  The catenators package is always made directly visible through a "use"
  clause.  All other services require dot notation.

  It turns out that, for routine coding, the kind done by researchers,
  scientists, mathematicians, that strong typing is not always required.
  The standard types are perfectly fine.  For anything in package standard,
  the Basic_IO package does the job.

  I admit that the kernal of this idea was published in various other works
  long before this one.  Here is a package specification for the Basic_IO.
  If anyone wants me to also post the body, I will do so, or send it to
  you as an attachment.  Also, I do not presume that this is the ideal
  design.  Perhaps we could collaborate on a better design that would be
  part of the standard libraries someday.  Maybe it would be called,

               package Standard_IO is ... end Standard_IO;

  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. 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;

    function  Is_Yes (Value : Answer) return Boolean; -- test Answer

    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;
      type Answer is (N, No, Y, Yes); -- Use for dialogue management


Richard Riehle
[log in to unmask]