comp.lang.ada
 help / color / mirror / Atom feed
* Does ada have a sscanf equivalent?
@ 2010-02-22  9:08 J.s
  2010-02-22 10:24 ` Georg Bauhaus
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: J.s @ 2010-02-22  9:08 UTC (permalink / raw)


I am trying to translate some C code that parses a string into numbers
(both Integers and Floats) from an input file. Would it be easiest to
just import the C function or is there an Ada equivalent? (I found a
package but I don't think I understand how it works:
http://www.web-port.net/Christfried.Webers/sources/flex/s-valrea__ads.htm)

Input file:
        . . .
	vert 7 0.4453125 0.416015625 9 1
	vert 8 0.453125 0.517578125 10 1
	vert 9 0.39453125 0.513671875 11 1
        . . .
C code:
        . . .
   if (sscanf (buff, "%s %d ( %f %f %f ) ( %f %f %f )",
     joint->name, &joint->parent, &joint->pos(0),
     &joint->pos(1), &joint->pos(2), &joint->orient(0),
     &joint->orient(1), &joint->orient(2)) == 8){
        . . .



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

* Re: Does ada have a sscanf equivalent?
  2010-02-22  9:08 Does ada have a sscanf equivalent? J.s
@ 2010-02-22 10:24 ` Georg Bauhaus
  2010-02-22 10:42 ` Jacob Sparre Andersen
  2010-02-22 19:48 ` jpwoodruff
  2 siblings, 0 replies; 10+ messages in thread
From: Georg Bauhaus @ 2010-02-22 10:24 UTC (permalink / raw)


On 2/22/10 10:08 AM, J.s wrote:
> I am trying to translate some C code that parses a string into numbers
> (both Integers and Floats) from an input file. Would it be easiest to
> just import the C function or is there an Ada equivalent? (I found a
> package but I don't think I understand how it works:
> http://www.web-port.net/Christfried.Webers/sources/flex/s-valrea__ads.htm)

Don't use this package, it is internal to this compiler,
like most packages from the System.* hierchy are not
intended for general use such as scanning text.

Instead, have a look at the Ada.Text_IO hierachy,
the generic packages for integer types and floating
point types should provide the necessary subprograms.
(Also note that with each basic type you get its attributes,
including "attribute functions" like 'Image and 'Value.)

One way I would write this is
   read one Line,
   find the index K of the first space,
   use IIO.Get (with string parameter Line(K .. Line'Last)
    to read an integer from the line starting at character
    index K, where IIO is an instance of Integer_IO;
    this moves K
   do the same for the following floating point numbers
    calling subs from an instance of Float_IO
   and the again employing IIO

For more extensive parsing needs, for example using pattern
matching, there are a number of libraries, including
standard Ada ones. But they are not needed here, I think.

Do you have a good text book? It should explain this
in some I/O chapter.

> Input file:
>          . . .
> 	vert 7 0.4453125 0.416015625 9 1
> 	vert 8 0.453125 0.517578125 10 1
> 	vert 9 0.39453125 0.513671875 11 1
>          . . .
> C code:
>          . . .
>     if (sscanf (buff, "%s %d ( %f %f %f ) ( %f %f %f )",
>       joint->name,&joint->parent,&joint->pos(0),
>       &joint->pos(1),&joint->pos(2),&joint->orient(0),
>       &joint->orient(1),&joint->orient(2)) == 8){
>          . . .




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

* Re: Does ada have a sscanf equivalent?
  2010-02-22  9:08 Does ada have a sscanf equivalent? J.s
  2010-02-22 10:24 ` Georg Bauhaus
@ 2010-02-22 10:42 ` Jacob Sparre Andersen
  2010-02-22 15:38   ` J.s
  2010-02-22 19:48 ` jpwoodruff
  2 siblings, 1 reply; 10+ messages in thread
From: Jacob Sparre Andersen @ 2010-02-22 10:42 UTC (permalink / raw)


"J.s" <justin.squirek@gmail.com> writes:

> I am trying to translate some C code that parses a string into numbers
> (both Integers and Floats) from an input file.

Ada.Text_IO.Integer_IO and Ada.Text_IO.Float_IO contain routines for
this purpose.

Greetings,

Jacob
-- 
"The current state of knowledge can be summarised thus:
 In the beginning, there was nothing, which exploded."



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

* Re: Does ada have a sscanf equivalent?
  2010-02-22 10:42 ` Jacob Sparre Andersen
@ 2010-02-22 15:38   ` J.s
  0 siblings, 0 replies; 10+ messages in thread
From: J.s @ 2010-02-22 15:38 UTC (permalink / raw)


On Feb 22, 5:42 am, Jacob Sparre Andersen <spa...@nbi.dk> wrote:
> "J.s" <justin.squi...@gmail.com> writes:
> > I am trying to translate some C code that parses a string into numbers
> > (both Integers and Floats) from an input file.
>
> Ada.Text_IO.Integer_IO and Ada.Text_IO.Float_IO contain routines for
> this purpose.
>
> Greetings,
>
> Jacob
> --
> "The current state of knowledge can be summarised thus:
>  In the beginning, there was nothing, which exploded."

Thanks, I can't believe I overlooked those packages



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

* Re: Does ada have a sscanf equivalent?
  2010-02-22  9:08 Does ada have a sscanf equivalent? J.s
  2010-02-22 10:24 ` Georg Bauhaus
  2010-02-22 10:42 ` Jacob Sparre Andersen
@ 2010-02-22 19:48 ` jpwoodruff
  2010-02-22 23:11   ` Jerry
  2 siblings, 1 reply; 10+ messages in thread
From: jpwoodruff @ 2010-02-22 19:48 UTC (permalink / raw)


On Feb 22, 1:08 am, "J.s" <justin.squi...@gmail.com> wrote:
> I am trying to translate some C code that parses a string into numbers
> (both Integers and Floats) from an input file. Would it be easiest to
> just import the C function or is there an Ada equivalent?

Let me offer a product of my own devising.

"The packages Numeric_IO and Name_IO, together with their children and
support, assist a program to read a user’s input.  The packages are
intended to support numerical computation by providing Get and Put
procedures for floating numbers and for vectors and matrices of
floating numbers.

"The procedures ease an end-user’s burden in preparing inputs for
computational programs.  The rules for input of floating numbers are
relaxed so that program inputs need not conform to the strict Ada
syntax for floating numbers. Facilities allow input either from files
or interactively. Consistent policies throughout all the services
allow programs to address input errors, to prompt the end-user
interactively or to specify optional default values."

Dmitry has been kind enough to make it available.

http://www.dmitry-kazakov.de/ada/Numeric-Name-IO

--
John



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

* Re: Does ada have a sscanf equivalent?
  2010-02-22 19:48 ` jpwoodruff
@ 2010-02-22 23:11   ` Jerry
  2010-02-23  2:11     ` jpwoodruff
  0 siblings, 1 reply; 10+ messages in thread
From: Jerry @ 2010-02-22 23:11 UTC (permalink / raw)


On Feb 22, 12:48 pm, jpwoodruff <jpwoodr...@gmail.com> wrote:
> On Feb 22, 1:08 am, "J.s" <justin.squi...@gmail.com> wrote:
>
> > I am trying to translate some C code that parses a string into numbers
> > (both Integers and Floats) from an input file. Would it be easiest to
> > just import the C function or is there an Ada equivalent?
>
> Let me offer a product of my own devising.
>
> "The packages Numeric_IO and Name_IO, together with their children and
> support, assist a program to read a user’s input.  The packages are
> intended to support numerical computation by providing Get and Put
> procedures for floating numbers and for vectors and matrices of
> floating numbers.
>
> "The procedures ease an end-user’s burden in preparing inputs for
> computational programs.  The rules for input of floating numbers are
> relaxed so that program inputs need not conform to the strict Ada
> syntax for floating numbers. Facilities allow input either from files
> or interactively. Consistent policies throughout all the services
> allow programs to address input errors, to prompt the end-user
> interactively or to specify optional default values."
>
> Dmitry has been kind enough to make it available.
>
> http://www.dmitry-kazakov.de/ada/Numeric-Name-IO
>
> --
> John

This looks pretty cool. But it doesn't seem to use the Ada 2005
definitions for matrices and vectors. I haven't looked at the code but
I wonder how hard it would be to create such a version. I would think
it should be pretty easy.

Jerry



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

* Re: Does ada have a sscanf equivalent?
  2010-02-22 23:11   ` Jerry
@ 2010-02-23  2:11     ` jpwoodruff
  2010-02-23  9:36       ` Conversion from Ada95 to Ada05 (Was: Does ada have a sscanf equivalent?) stefan-lucks
  0 siblings, 1 reply; 10+ messages in thread
From: jpwoodruff @ 2010-02-23  2:11 UTC (permalink / raw)


On Feb 22, 3:11 pm, Jerry <lancebo...@qwest.net> wrote:
> On Feb 22, 12:48 pm, jpwoodruff <jpwoodr...@gmail.com> wrote:
>
>
>
> > On Feb 22, 1:08 am, "J.s" <justin.squi...@gmail.com> wrote:
>
> > > I am trying to translate some C code that parses a string into numbers
> > > (both Integers and Floats) from an input file. Would it be easiest to
> > > just import the C function or is there an Ada equivalent?
>
> > Let me offer a product of my own devising.
>
> > "The packages Numeric_IO and Name_IO, together with their children and
> > support, assist a program to read a user’s input.  The packages are
> > intended to support numerical computation by providing Get and Put
> > procedures for floating numbers and for vectors and matrices of
> > floating numbers.
>
> > "The procedures ease an end-user’s burden in preparing inputs for
> > computational programs.  The rules for input of floating numbers are
> > relaxed so that program inputs need not conform to the strict Ada
> > syntax for floating numbers. Facilities allow input either from files
> > or interactively. Consistent policies throughout all the services
> > allow programs to address input errors, to prompt the end-user
> > interactively or to specify optional default values."
>
> > Dmitry has been kind enough to make it available.
>
> >http://www.dmitry-kazakov.de/ada/Numeric-Name-IO
>
> > --
> > John
>
> This looks pretty cool. But it doesn't seem to use the Ada 2005
> definitions for matrices and vectors. I haven't looked at the code but
> I wonder how hard it would be to create such a version. I would think
> it should be pretty easy.
>
> Jerry

There's a file in the distribution called "Guide-to-Examples" that
will shed some light on how to roll this forward to Ada 05 standards.

"As of November 2004 I am aware of six different publicly available
libraries that provide some operations on vectors and matrices.  In
the interest of offering the numeric_IO.matrix_IO services to users
who might be using one of these libraries, I have build small
demonstrations of the usage of the IO functions that conform with each
of them."

The same library worked for all cases.

One of the examples was called Ada0y as signed by Martin Dowie. I
can't claim what version that was, but if not too much has changed
since then, maybe this will still fly:


   type Real is digits 6 ;

   package Ada_Matrix is new Ada.Numerics.Generic_Real_Arrays (Real =>
Real) ;

   -- Numeric_IO services will read and write the same matrix
representation
   package Scalar_IO is new Numeric_IO (Real, Integer) ;

   package Matrix_IO is new Scalar_IO.Matrix_IO
     (Vector_Ix    => Integer,
      Vector_Type  => Ada_Matrix.Real_Vector,
      Matrix_Ix1   => Integer,
      Matrix_Ix2   => Integer,
      Matrix_Type  => Ada_Matrix.Real_Matrix) ;



I'm a retired software engineer who has decided not to follow the new
standard.  I decided that I don't need a new programming paradigm
(except maybe for prolog).  And some of my old favorites didn't work
with the '05 compiler I looked at.

This whole business dates to 1986, with an update to Ada95's generic
child packages.  Besides, I wrote in "A Family of Numeric ..." a
paragraph headed Anachronism Alert.  "... So don't read this package
for esthetics, but it seems to work OK"

John



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

* Conversion from Ada95 to Ada05 (Was: Does ada have a sscanf equivalent?)
  2010-02-23  2:11     ` jpwoodruff
@ 2010-02-23  9:36       ` stefan-lucks
  2010-02-23 19:48         ` Randy Brukardt
  0 siblings, 1 reply; 10+ messages in thread
From: stefan-lucks @ 2010-02-23  9:36 UTC (permalink / raw)


On Mon, 22 Feb 2010, jpwoodruff wrote:

> I'm a retired software engineer who has decided not to follow the new
> standard.  I decided that I don't need a new programming paradigm
> (except maybe for prolog).  And some of my old favorites didn't work
> with the '05 compiler I looked at.

I am curios: Which of your old favorites didn't work anymore? Which Ada95 
patterns occur in practice, which are incompatible to Ada05?

The only issue I had when converting Ada my own software from Ada95 to 
Ada05 was a very trivial one related to identifiers which had become 
keywords. (Namely, I happened to use the identifier "Interface".) And one 
or two compiler bugs which have gone now. So my impression was that the 
Ada05 guys did an excellent job at preserving compatibility to Ada95.

So long

Stefan

-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




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

* Re: Conversion from Ada95 to Ada05 (Was: Does ada have a sscanf equivalent?)
  2010-02-23  9:36       ` Conversion from Ada95 to Ada05 (Was: Does ada have a sscanf equivalent?) stefan-lucks
@ 2010-02-23 19:48         ` Randy Brukardt
  2010-02-23 21:12           ` Conversion from Ada95 to Ada05 Robert A Duff
  0 siblings, 1 reply; 10+ messages in thread
From: Randy Brukardt @ 2010-02-23 19:48 UTC (permalink / raw)


<stefan-lucks@see-the.signature> wrote in message 
news:Pine.LNX.4.64.1002231026050.9207@medsec1.medien.uni-weimar.de...
...
> I am curios: Which of your old favorites didn't work anymore? Which Ada95
> patterns occur in practice, which are incompatible to Ada05?

I can't speak for John, but the main incompatibility that we hear about was 
the elimination of return-by-reference. While it didn't make much sense, 
people managed to make it work for various things and those patterns don't 
work anymore.

We're trying to come up with a sane way to have the same effect (returning 
an access value with a limited lifetime and without the need for explicit 
dereferencing) for Ada 2012. Hopefully, that capability will allow the 
holdouts to come along...

                          Randy.





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

* Re: Conversion from Ada95 to Ada05
  2010-02-23 19:48         ` Randy Brukardt
@ 2010-02-23 21:12           ` Robert A Duff
  0 siblings, 0 replies; 10+ messages in thread
From: Robert A Duff @ 2010-02-23 21:12 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> <stefan-lucks@see-the.signature> wrote in message 
> news:Pine.LNX.4.64.1002231026050.9207@medsec1.medien.uni-weimar.de...
> ...
>> I am curios: Which of your old favorites didn't work anymore? Which Ada95
>> patterns occur in practice, which are incompatible to Ada05?
>
> I can't speak for John, but the main incompatibility that we hear about was 
> the elimination of return-by-reference. While it didn't make much sense, 
> people managed to make it work for various things and those patterns don't 
> work anymore.

Right, but it's not just return-by-reference.  If you return a local limited
object, that's legal in Ada 95, but illegal in Ada 2005.
If the limited type is immutably limited, that's the
return-by-ref case.  (Immutably limited means it's really
limited deep down -- like a limited private completed
with a limited record.)  Otherwise (like a limited private
completed with an integer type) it's return-by-copy.

In AdaCore's test suite, there are something like a couple dozen tests
that run into this incompatibility, out of some 15,000 tests.
It turns out that the return-by-ref case is something like 1/3 of
those, and the return-by-copy case is 2/3.
(I measured this when I was involved in implementing the new Ada 2005
build-in-place return feature, but I don't remember the exact numbers).

I think it was a language design mistake to apply this rule to the
return-by-copy case.  If it applied only to the return-by-ref
case, it would be less incompatible.  Of course, that would
break privacy, which is evil.  But incompatibilities are even
more evil, in my opinion.  It would work, because the
limited-but-not-inherently-limited case is not build-in-place
in Ada 2005 -- it's still return-by-copy.

On the other hand, the workarounds for the return-by-copy case
are fairly painless -- for example, you can make the type
nonlimited.  That sort of makes sense anyway, since the
limitedness is a lie -- "limited" is supposed to mean
"cannot copy", but in fact clients CAN copy in this case
(and there's a famous Henry Baker paper about that
from Ada 83 days).

> We're trying to come up with a sane way to have the same effect (returning 
> an access value with a limited lifetime and without the need for explicit 
> dereferencing) for Ada 2012. Hopefully, that capability will allow the 
> holdouts to come along...

Yup.

- Bob



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

end of thread, other threads:[~2010-02-23 21:12 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-22  9:08 Does ada have a sscanf equivalent? J.s
2010-02-22 10:24 ` Georg Bauhaus
2010-02-22 10:42 ` Jacob Sparre Andersen
2010-02-22 15:38   ` J.s
2010-02-22 19:48 ` jpwoodruff
2010-02-22 23:11   ` Jerry
2010-02-23  2:11     ` jpwoodruff
2010-02-23  9:36       ` Conversion from Ada95 to Ada05 (Was: Does ada have a sscanf equivalent?) stefan-lucks
2010-02-23 19:48         ` Randy Brukardt
2010-02-23 21:12           ` Conversion from Ada95 to Ada05 Robert A Duff

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