comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeff Creem" <jeff@thecreems.com>
Subject: Re: Newbie Questions about Get, Get_Line
Date: Sat, 30 Dec 2000 09:31:12 -0500
Date: 2000-12-30T09:31:12-05:00	[thread overview]
Message-ID: <t4rsasspcaak67@corp.supernews.com> (raw)
In-Reply-To: k73r4toon9d6e0oi384t6240d32d7leohc@4ax.com


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








  parent reply	other threads:[~2000-12-30 14:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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