comp.lang.ada
 help / color / mirror / Atom feed
* Newbie Questions about Get, Get_Line
@ 2000-12-30  7:33 gressett
  2000-12-30 14:13 ` Marin David Condic
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: gressett @ 2000-12-30  7:33 UTC (permalink / raw)


I am getting back into Ada again after several months of doing other
things. This time I am working on a program that does interactive
terminal IO, and I am finding that the Ada.Text_IO routines I am
working with have some unexpected behavior. (Get, Get_Line,
Get_Immediate) (The compiler is gnat 3.13p on Linux)

What I would really find useful is a routine that could take a string
variable and fill it with terminal input with the following
properties:

If the user input is shorter that the string variable, it will be
padded with blanks.

If the user input is longer than the string variable, the extra
characters should be thrown away, never to be seen again.





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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30  7:33 Newbie Questions about Get, Get_Line gressett
@ 2000-12-30 14:13 ` Marin David Condic
  2000-12-30 17:17   ` Ted Dennison
  2000-12-30 14:31 ` Jeff Creem
  2000-12-30 17:09 ` Ted Dennison
  2 siblings, 1 reply; 16+ messages in thread
From: Marin David Condic @ 2000-12-30 14:13 UTC (permalink / raw)


When do you know that the user input is done? Is it when the user hits
the Enter key terminating the line? If so, it should be pretty
straightforward to use Text_IO.Get_Line. That should discard anything
longer than your string buffer. If the input is shorter than your string,
the Last parameter to Get_Line tells you the last character input. Just
fill the rest of the string with spaces.

MDC

gressett@iglobal.net wrote:

> If the user input is shorter that the string variable, it will be
> padded with blanks.
>
> If the user input is longer than the string variable, the extra
> characters should be thrown away, never to be seen again.

--
======================================================================
Marin David Condic - Quadrus Corporation - http://www.quadruscorp.com/
Send Replies To: m c o n d i c @ q u a d r u s c o r p . c o m
Visit my web site at:  http://www.mcondic.com/

    "Giving money and power to Government is like giving whiskey
    and car keys to teenage boys."

        --   P. J. O'Rourke
======================================================================





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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30  7:33 Newbie Questions about Get, Get_Line gressett
  2000-12-30 14:13 ` Marin David Condic
@ 2000-12-30 14:31 ` Jeff Creem
  2000-12-30 17:09 ` Ted Dennison
  2 siblings, 0 replies; 16+ messages in thread
From: Jeff Creem @ 2000-12-30 14:31 UTC (permalink / raw)



<gressett@iglobal.net> wrote in message
news:k73r4toon9d6e0oi384t6240d32d7leohc@4ax.com...
> I am getting back into Ada again after several months of doing other
> things. This time I am working on a program that does interactive
> terminal IO, and I am finding that the Ada.Text_IO routines I am
> working with have some unexpected behavior. (Get, Get_Line,
> Get_Immediate) (The compiler is gnat 3.13p on Linux)

Well..I also was confused and found Get_Line had unexpected behaviour when I
called it
and it did not paint my house. Then I read the LRM description about what it
is supposed
to do and realized that my expectations were wrong.

I think it reads
< LRM Quote>
Reads successive characters from the specified input file and assigns them
to successive characters of the specified string. Reading stops if the end
of the string is met. Reading also stops if the end of the line is met
before meeting the end of the string; in this case Skip_Line is (in effect)
called with a spacing of 1. The values of characters not assigned are not
specified.
If characters are read, returns in Last the index value such that Item(Last)
is the last character assigned (the index of the first character assigned is
Item'First). If no characters are read, returns in Last an index value that
is one less than Item'First. The exception End_Error is propagated if an
attempt is made to skip a file terminator.
</LRM Quote>


>
> What I would really find useful is a routine that could take a string
> variable and fill it with terminal input with the following
> properties:
>
> If the user input is shorter that the string variable, it will be
> padded with blanks.
>
> If the user input is longer than the string variable, the extra
> characters should be thrown away, never to be seen again.
>


This is very easy to do but the part about padding with spaces is probably a
bad idea.
I have seen a lot of Ada code where local string variables are initialized
to all spaces
and then just parts filled in. This seems ok but it leads to a lot of
wasteful code being generated
messing with spaces especially when someone starts using very long strings.

Having said that if you are really sure you want something like this try


package Slow_Get is

   --If the user input is shorter that the string variable, it will be
   --padded with blanks.

   --If the user input is longer than the string variable, the extra
   --characters should be thrown away, never to be seen again.
  procedure Get_Line(Item : out String);

end Slow_Get;


with Text_Io;

package body Slow_Get is

   procedure Get_Line (
         Item :    out String ) is

      Last : Integer;

   begin
      Text_Io.Get_Line(
         Item => Item,
         Last => Last);

      if Last = Item'Last then
         Text_Io.Skip_Line;
      end if;

      for Pad_Index in Last + 1 .. Item'Last loop
         Item(Pad_Index) := ' ';
      end loop;

   end Get_Line;

end Slow_Get;








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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30  7:33 Newbie Questions about Get, Get_Line gressett
  2000-12-30 14:13 ` Marin David Condic
  2000-12-30 14:31 ` Jeff Creem
@ 2000-12-30 17:09 ` Ted Dennison
  2000-12-31  8:13   ` gressett
  2 siblings, 1 reply; 16+ messages in thread
From: Ted Dennison @ 2000-12-30 17:09 UTC (permalink / raw)


In article <k73r4toon9d6e0oi384t6240d32d7leohc@4ax.com>,
  gressett@iglobal.net wrote:

> What I would really find useful is a routine that could take a string
> variable and fill it with terminal input with the following
> properties:
>
> If the user input is shorter that the string variable, it will be
> padded with blanks.

Ewww. Why would you want that? Its soooo sloppy (not to mention
wasteful). Plus it would significantly complicate dealing with the
string later. If you want to insert that string in the middle of other
strings, how are you going to do it? Just using "&" on a slice won't do
the trick anymore, because you won't know the proper slice to use.

If you really want this behavior, you can always do a:
   Str := (others => ' ');
before the call, then ignore the return length. Better would be to use
the return length to do the fill:
   Str (Last+1..Str'Last) := (others => ' ');

> If the user input is longer than the string variable, the extra
> characters should be thrown away, never to be seen again.

There's nothing stopping you from writing a routine to do this yourself.
It would only be about 3 lines of code (although handling *any* length
would probably be a smidge more complicated, requiring a loop or
recursion). But if I were you, I'd try to work with what the language
gives me for a while. You should get used to Ada string handling before
you go trying to fight it.

Some have expressed a desire for a function that returns a
perfectly-sized string, instead of the string-buffer and length
interface. That would be more in keeping with the language (although you
could only use it in certain circumstances). For an example of how to do
that, see Martin Carlisle's entry on AdaPower at
http://www.adapower.com/lang/recstring.html

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30 14:13 ` Marin David Condic
@ 2000-12-30 17:17   ` Ted Dennison
  2000-12-30 17:25     ` Ted Dennison
  2000-12-30 22:46     ` Marin David Condic
  0 siblings, 2 replies; 16+ messages in thread
From: Ted Dennison @ 2000-12-30 17:17 UTC (permalink / raw)


In article <3A4DED82.C23CDB77@acm.org>,
  Marin David Condic <mcondic.nospam@acm.org> wrote:
> straightforward to use Text_IO.Get_Line. That should discard anything
> longer than your string buffer. If the input is shorter than your

Uhhh....no, I don't think so. If Last = Buffer'last, then there is more
input from the same line, and Get_Line needs to be called again to read
it. (This can mean that a later call to Get_Line will return with a
length of 0.) For a good discussion of this, see
http://www.adapower.com/lang/get_line.html

If you meant that he could just pass in the maximum length buffer to
Get_Line, then call Get_Line again if nessecary in a loop with a trash
buffer to flush the rest of the line, then I think that would work.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30 17:17   ` Ted Dennison
@ 2000-12-30 17:25     ` Ted Dennison
  2000-12-30 22:46     ` Marin David Condic
  1 sibling, 0 replies; 16+ messages in thread
From: Ted Dennison @ 2000-12-30 17:25 UTC (permalink / raw)


In article <92l5bf$i0p$1@nnrp1.deja.com>,
  Ted Dennison <dennison@telepath.com> wrote:
> Get_Line, then call Get_Line again if nessecary in a loop with a trash
> buffer to flush the rest of the line, then I think that would work.

Jeff raises a good point: Skip_Line can be called to do this. (Can you
tell I've never bothered to handle this before? :-)  ).

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30 17:17   ` Ted Dennison
  2000-12-30 17:25     ` Ted Dennison
@ 2000-12-30 22:46     ` Marin David Condic
  2000-12-30 23:12       ` Ted Dennison
  1 sibling, 1 reply; 16+ messages in thread
From: Marin David Condic @ 2000-12-30 22:46 UTC (permalink / raw)


Ted Dennison wrote:

> Uhhh....no, I don't think so. If Last = Buffer'last, then there is more
> input from the same line, and Get_Line needs to be called again to read
> it. (This can mean that a later call to Get_Line will return with a
> length of 0.) For a good discussion of this, see
> http://www.adapower.com/lang/get_line.html
>

Then I guess I stand corrected. :-) Maybe I was confusing the behavior
because of the reference to Skip_Line? As for my use of it - I always use a
string way bigger than what I'd think could be reasonably be used in most
cases. Then of course there are recursive techniques that can guarantee you
don't overflow the buffer.

MDC
--
======================================================================
Marin David Condic - Quadrus Corporation - http://www.quadruscorp.com/
Send Replies To: m c o n d i c @ q u a d r u s c o r p . c o m
Visit my web site at:  http://www.mcondic.com/

    "Giving money and power to Government is like giving whiskey
    and car keys to teenage boys."

        --   P. J. O'Rourke
======================================================================





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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30 22:46     ` Marin David Condic
@ 2000-12-30 23:12       ` Ted Dennison
  0 siblings, 0 replies; 16+ messages in thread
From: Ted Dennison @ 2000-12-30 23:12 UTC (permalink / raw)


In article <3A4E65CD.4A828FB1@acm.org>,
  Marin David Condic <mcondic.nospam@acm.org> wrote:
> because of the reference to Skip_Line? As for my use of it - I always
> use a string way bigger than what I'd think could be reasonably be
> used in most cases. Then of course there are recursive techniques that
> can guarantee you don't overflow the buffer.

Same here. I wouldn't have know any better myself, if I hadn't just
looked it up.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-30 17:09 ` Ted Dennison
@ 2000-12-31  8:13   ` gressett
  2000-12-31 15:50     ` Larry Kilgallen
                       ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: gressett @ 2000-12-31  8:13 UTC (permalink / raw)


Ted Dennison <dennison@telepath.com> wrote:

>In article <k73r4toon9d6e0oi384t6240d32d7leohc@4ax.com>,
>  gressett@iglobal.net wrote:
>
>> What I would really find useful is a routine that could take a string
>> variable and fill it with terminal input with the following
>> properties:
>>
>> If the user input is shorter that the string variable, it will be
>> padded with blanks.
>
>Ewww. Why would you want that? Its soooo sloppy (not to mention
>wasteful). Plus it would significantly complicate dealing with the
>string later.

It's a fixed-length String variable; I have to pad it with something,
and in this case, the fixed-length string is a good match to the
requirements of the problem being solved. 

The real problem here is that the designers of the Ada.Text_IO package
quit too soon. There should have been an Ada.Interactive_IO which
would deal with more of the problems of screen and Keyboard IO in a
standard way.



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-31  8:13   ` gressett
@ 2000-12-31 15:50     ` Larry Kilgallen
  2000-12-31 17:03     ` Robert Dewar
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 16+ messages in thread
From: Larry Kilgallen @ 2000-12-31 15:50 UTC (permalink / raw)


In article <98pt4t477fqavihkgfbjioo7peo9vb7f20@4ax.com>, gressett@iglobal.net writes:
> Ted Dennison <dennison@telepath.com> wrote:
> 
>>In article <k73r4toon9d6e0oi384t6240d32d7leohc@4ax.com>,
>>  gressett@iglobal.net wrote:
>>
>>> What I would really find useful is a routine that could take a string
>>> variable and fill it with terminal input with the following
>>> properties:
>>>
>>> If the user input is shorter that the string variable, it will be
>>> padded with blanks.
>>
>>Ewww. Why would you want that? Its soooo sloppy (not to mention
>>wasteful). Plus it would significantly complicate dealing with the
>>string later.
> 
> It's a fixed-length String variable; I have to pad it with something,
> and in this case, the fixed-length string is a good match to the
> requirements of the problem being solved. 
> 
> The real problem here is that the designers of the Ada.Text_IO package
> quit too soon. There should have been an Ada.Interactive_IO which
> would deal with more of the problems of screen and Keyboard IO in a
> standard way.

While you may view padding with blanks as a "standard way" some of
us look at it (within a program) only as a method to emulate other
programming languages.  When one writes to a fixed record file it
may be of use, at which time I would write:

	record_buffer.street_address := fixed_blank_street_address;
        record_buffer.street_address ( 1 .. street'length ) := street;

of course with some earlier checks to ensure street'length is no greater
than  record_buffer.street_address'length.  Ada will catch such an error
on the second line above, but checking earlier in the program can make it
easier to create a clean error message.

==============================================================================
Great Inventors of our time: Al Gore -> Internet; Sun Microsystems -> Clusters
==============================================================================



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-31  8:13   ` gressett
  2000-12-31 15:50     ` Larry Kilgallen
@ 2000-12-31 17:03     ` Robert Dewar
  2000-12-31 17:47     ` Ted Dennison
  2000-12-31 21:07     ` tmoran
  3 siblings, 0 replies; 16+ messages in thread
From: Robert Dewar @ 2000-12-31 17:03 UTC (permalink / raw)


In article <98pt4t477fqavihkgfbjioo7peo9vb7f20@4ax.com>,
  gressett@iglobal.net wrote:
> The real problem here is that the designers of the
Ada.Text_IO package
> quit too soon. There should have been an Ada.Interactive_IO
which
> would deal with more of the problems of screen and Keyboard
IO in a
> standard way.


Perhaps, but the problem in this thread is trivially solvable
with the current Text_IO facilities, so I don't think this is
a good argument for the point.

Now if you are using Unbounded strings, then there really
are missing routines, which is why GNAT supplies:

with Ada.Text_IO;

package Ada.Strings.Unbounded.Text_IO is

   function Get_Line return Unbounded_String;
   function Get_Line (File : Ada.Text_IO.File_Type)
     return Unbounded_String;
   --  Reads up to the end of the current line, returning the
   --  result as an unbounded string of appropriate length. If
   --  no File parameter is present, input is from
   --  Current_Input.

   procedure Put (U : Unbounded_String);
   procedure Put
    (File : Ada.Text_IO.File_Type;
      U   : Unbounded_String);
   procedure Put_Line U : Unbounded_String);
   procedure Put_Line
     (File : Ada.Text_IO.File_Type;
      U    : Unbounded_String);
   --  These are equivalent to the standard Text_IO routines
   --  passed the value To_String (U), but operate more
   --  efficiently, because the extra copy of the argument is
   --  avoided.

end Ada.Strings.Unbounded.Text_IO;


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-31  8:13   ` gressett
  2000-12-31 15:50     ` Larry Kilgallen
  2000-12-31 17:03     ` Robert Dewar
@ 2000-12-31 17:47     ` Ted Dennison
  2000-12-31 21:07     ` tmoran
  3 siblings, 0 replies; 16+ messages in thread
From: Ted Dennison @ 2000-12-31 17:47 UTC (permalink / raw)


In article <98pt4t477fqavihkgfbjioo7peo9vb7f20@4ax.com>,
  gressett@iglobal.net wrote:

> It's a fixed-length String variable; I have to pad it with something,
> and in this case, the fixed-length string is a good match to the
> requirements of the problem being solved.

Why? Do you have to print it out as a field in a table on the screen? Or
are you just saying this because when you don't pad it and try to print
out the whole string you get garbage?

I'm sorry if you find the second option insulting, but you termed
yourself a newbie, and lots of newbies have that misunderstanding. Of
course, what they *should* be doing is printing out just the valid slice
of the string, not the whole string...


--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-31  8:13   ` gressett
                       ` (2 preceding siblings ...)
  2000-12-31 17:47     ` Ted Dennison
@ 2000-12-31 21:07     ` tmoran
  2001-01-01  6:12       ` gressett
  3 siblings, 1 reply; 16+ messages in thread
From: tmoran @ 2000-12-31 21:07 UTC (permalink / raw)


>The real problem here is that the designers of the Ada.Text_IO package
>quit too soon. There should have been an Ada.Interactive_IO which
>would deal with more of the problems of screen and Keyboard IO in a
>standard way.
  If there was an Ada.Interactive_IO that worked well for DOS it would
be quite unsuited for Windows style programs.  And vice versa.



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

* Re: Newbie Questions about Get, Get_Line
  2000-12-31 21:07     ` tmoran
@ 2001-01-01  6:12       ` gressett
  2001-01-01 17:45         ` Robert Dewar
  2001-01-01 17:46         ` Robert Dewar
  0 siblings, 2 replies; 16+ messages in thread
From: gressett @ 2001-01-01  6:12 UTC (permalink / raw)


tmoran@acm.org wrote:

>>The real problem here is that the designers of the Ada.Text_IO package
>>quit too soon. There should have been an Ada.Interactive_IO which
>>would deal with more of the problems of screen and Keyboard IO in a
>>standard way.
>  If there was an Ada.Interactive_IO that worked well for DOS it would
>be quite unsuited for Windows style programs.  And vice versa.
It doesn't need to suitable for Windows; for that, a binding for the
Windows API would be used. What I have in mind would be suitable for a
dumb terminal, DOS, a console window in Windows, an X terminal, etc.
Something fairly simple.

Simple problems that get repeatedly solved should have standard
solutions, so that they quit being problems.



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

* Re: Newbie Questions about Get, Get_Line
  2001-01-01  6:12       ` gressett
@ 2001-01-01 17:45         ` Robert Dewar
  2001-01-01 17:46         ` Robert Dewar
  1 sibling, 0 replies; 16+ messages in thread
From: Robert Dewar @ 2001-01-01 17:45 UTC (permalink / raw)


In article <ia705t8etakmjhr9v2g26e6haedpeivfvo@4ax.com>,
  gressett@iglobal.net wrote:
> What I have in mind would be suitable for a
> dumb terminal, DOS, a console window in Windows, an X
> terminal, etc. Something fairly simple.

I don't see anything in this thread that would suggest that
Text_IO is unsuitable for "fairly simple" interactive I/O,
and it is used (portably) in that manner, all the time!


Sent via Deja.com
http://www.deja.com/



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

* Re: Newbie Questions about Get, Get_Line
  2001-01-01  6:12       ` gressett
  2001-01-01 17:45         ` Robert Dewar
@ 2001-01-01 17:46         ` Robert Dewar
  1 sibling, 0 replies; 16+ messages in thread
From: Robert Dewar @ 2001-01-01 17:46 UTC (permalink / raw)


In article <ia705t8etakmjhr9v2g26e6haedpeivfvo@4ax.com>,
  gressett@iglobal.net wrote:
> Simple problems that get repeatedly solved should have
> standard solutions, so that they quit being problems.

All the problems discussed in this thread have simple
straightforward solutions using Text_IO. Yes, as is often the
case on CLA, you get a thread with a lot of confusion and
unhelpful suggestions and comments, but I don't see how a
language design can help that (indeed CLA is better that way
than most other language forums :-)


Sent via Deja.com
http://www.deja.com/



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

end of thread, other threads:[~2001-01-01 17:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-30  7:33 Newbie Questions about Get, Get_Line gressett
2000-12-30 14:13 ` Marin David Condic
2000-12-30 17:17   ` Ted Dennison
2000-12-30 17:25     ` Ted Dennison
2000-12-30 22:46     ` Marin David Condic
2000-12-30 23:12       ` Ted Dennison
2000-12-30 14:31 ` Jeff Creem
2000-12-30 17:09 ` Ted Dennison
2000-12-31  8:13   ` gressett
2000-12-31 15:50     ` Larry Kilgallen
2000-12-31 17:03     ` Robert Dewar
2000-12-31 17:47     ` Ted Dennison
2000-12-31 21:07     ` tmoran
2001-01-01  6:12       ` gressett
2001-01-01 17:45         ` Robert Dewar
2001-01-01 17:46         ` Robert Dewar

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