comp.lang.ada
 help / color / mirror / Atom feed
* printf package/Function/Procedure in Ada ??
@ 1999-03-09  0:00 solo
  1999-03-10  0:00 ` dennison
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: solo @ 1999-03-09  0:00 UTC (permalink / raw)



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

This will be a package that will have a printf procedure that
exactly matches that of the C printf specs.

regards,
Solo




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printf package/Function/Procedure in Ada ??
  1999-03-09  0:00 printf package/Function/Procedure in Ada ?? solo
                   ` (2 preceding siblings ...)
  1999-03-10  0:00 ` Richard D Riehle
@ 1999-03-10  0:00 ` Tom Moran
  1999-03-10  0:00   ` Corey Ashford
  3 siblings, 1 reply; 7+ messages in thread
From: Tom Moran @ 1999-03-10  0:00 UTC (permalink / raw)


Isn't
  Put("I=" & integer'image(i) & name);
simpler than
  printf("I=%i%s", i, name); 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printf package/Function/Procedure in Ada ??
  1999-03-09  0:00 printf package/Function/Procedure in Ada ?? solo
  1999-03-10  0:00 ` dennison
@ 1999-03-10  0:00 ` robert_dewar
  1999-03-10  0:00 ` Richard D Riehle
  1999-03-10  0:00 ` Tom Moran
  3 siblings, 0 replies; 7+ messages in thread
From: robert_dewar @ 1999-03-10  0:00 UTC (permalink / raw)


In article <7c4e75$e5m@drn.newsguy.com>,
  solo <solo@newsguy.com> wrote:
> This will be a package that will have a printf procedure
> that exactly matches that of the C printf specs.

Since the C printf spec is fundametally type unsafe, there
is no way to cleanly emulate this capability in Ada, and
the more significant question is why you would want to do
this. How about you tell us what the real problem you are
trying to solve is, instead of asking about a particular
(impractical) solution?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printf package/Function/Procedure in Ada ??
  1999-03-10  0:00 ` Tom Moran
@ 1999-03-10  0:00   ` Corey Ashford
  1999-03-10  0:00     ` Ehud Lamm
  0 siblings, 1 reply; 7+ messages in thread
From: Corey Ashford @ 1999-03-10  0:00 UTC (permalink / raw)


Tom Moran wrote:
> 
> Isn't
>   Put("I=" & integer'image(i) & name);
> simpler than
>   printf("I=%i%s", i, name);

Except that printf allows you to more easily get fancy with formatting of
the variables, e.g. %08x, or %7.3f.  You can do this kind of thing with
text_io, but it's a lot more keystrokes.

In general, I think formatted I/O is a little more difficult in Ada, but Ada provides
many things that C can't do easily or at all (e.g. string slices, 'image for enumerated
types).

- Corey




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printf package/Function/Procedure in Ada ??
  1999-03-10  0:00   ` Corey Ashford
@ 1999-03-10  0:00     ` Ehud Lamm
  0 siblings, 0 replies; 7+ messages in thread
From: Ehud Lamm @ 1999-03-10  0:00 UTC (permalink / raw)


On Wed, 10 Mar 1999, Corey Ashford wrote:

> Tom Moran wrote:
> > 
> > Isn't
> >   Put("I=" & integer'image(i) & name);
> > simpler than
> >   printf("I=%i%s", i, name);
> 
> Except that printf allows you to more easily get fancy with formatting of
> the variables, e.g. %08x, or %7.3f.  You can do this kindof thing with
> text_io, but it's a lot more keystrokes.
> 
> In general, I think formatted I/O is a little more difficult in Ada, but Ada provides
> many things that C can't do easily or at all (e.g. string slices, 'image for enumerated
> types).
> 

I think the real problem is input not output. In Ada you have to "parse"
fileds in the input semi manually, where as scanf does it for you. I know
scanf sucks in many respects, but sometimes its behind the scenes work is
very valuable.

Writing scanf in Ada is conceptually problematic - since as opposed to
output, in input you really don't have any clues as to the types that may
exist in the input stream.

My best conceptual solution (this means I haven't really coded it yet) is
to have something like this:

Int1:=To_Integer(Get_Field(<field characteristics>))

Where Get_Field is overloaded by type but all other params (like width)
are standard. It always returns String (or something like it). The
conversion stays the responsibility of the caller.

Better ideas are more than welcome.

Ehud Lamm     mslamm@pluto.mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm     <== Use PURL to find me!





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printf package/Function/Procedure in Ada ??
  1999-03-09  0:00 printf package/Function/Procedure in Ada ?? solo
@ 1999-03-10  0:00 ` dennison
  1999-03-10  0:00 ` robert_dewar
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: dennison @ 1999-03-10  0:00 UTC (permalink / raw)


In article <7c4e75$e5m@drn.newsguy.com>,
  solo <solo@newsguy.com> 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).
>
> This will be a package that will have a printf procedure that
> exactly matches that of the C printf specs.

printf() takes an indeterminate amount of arguments. You can't do that in Ada.
However you can simulate it with a single argument that is an unconstrained
array.

Using that trick, along with some of the tricks and the facilities in gnat's
Spitbol.* packages, I think it'd be doable. Probably not worth the effort, but
doable.

For those of you who haven't taken a look at it, I highly reccomend playing
with the gnat Spitbol packages. There are some rather nifty coding tricks in
there that I had never thought of before. I'm certainly the richer for having
figgured out how to use it.

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printf package/Function/Procedure in Ada ??
  1999-03-09  0:00 printf package/Function/Procedure in Ada ?? solo
  1999-03-10  0:00 ` dennison
  1999-03-10  0:00 ` robert_dewar
@ 1999-03-10  0:00 ` Richard D Riehle
  1999-03-10  0:00 ` Tom Moran
  3 siblings, 0 replies; 7+ messages in thread
From: Richard D Riehle @ 1999-03-10  0:00 UTC (permalink / raw)


In article <7c4e75$e5m@drn.newsguy.com>,
	solo <solo@newsguy.com> 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




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1999-03-10  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-09  0:00 printf package/Function/Procedure in Ada ?? solo
1999-03-10  0:00 ` dennison
1999-03-10  0:00 ` robert_dewar
1999-03-10  0:00 ` Richard D Riehle
1999-03-10  0:00 ` Tom Moran
1999-03-10  0:00   ` Corey Ashford
1999-03-10  0:00     ` Ehud Lamm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox