comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Get_Line problem (GNAT bug?)
Date: 7 Dec 2006 09:42:13 -0800
Date: 2006-12-07T09:42:13-08:00	[thread overview]
Message-ID: <1165513333.321151.74320@73g2000cwn.googlegroups.com> (raw)
In-Reply-To: C-6dnavmyanjFurYnZ2dnUVZ_rSdnZ2d@comcast.com

Steve wrote:
> "Maciej Sobczak" <no.spam@no.spam.com> wrote in message
> news:el6jss$268$1@cernne03.cern.ch...
> [snip]
> >
> > Any thoughts?
> >
> You have run across what I consider to be a weakness in the standard Text_IO
> library, it is one of the few things I find really annoying about Ada, which
> I really like otherwise.
>
> In Ada you can't write a simple program to read and process lines in a text
> file that looks something like:
>
> while not End_Of_File( inFile ) loop
>     Get_Line( inFile, Input_Buffer, Count );
>     Process_Line( Input_Buffer( 1 .. Count ) );
> end loop;
>
> The problem is that Get_Line reads a line of text into a string buffer and
> returns the count of characters read.

Nitpick: Get_Line returns the *index* of the last character it has read
into the buffer (or Input_Buffer'First-1 if none), not the count.  The
two are equivalent if the 'First of the buffer is 1.  But it doesn't
have to be.  You can say something like

   Get_Line (InFile, Some_Buffer(10..50), Last);

and if it reads three characters, the three characters will be read
into Some_Buffer(10..12) and Last will be set to 12.

 The "line of text" is defined by the
> next sequential characters from the current position of the file up to the
> next line terminator.  If the last character in the file doesn't happen to
> be a line termnator an End_Error exception is raised.

This, I think, is implementation-dependent.  Ada makes it clear that a
"line terminator" is a logical concept, not a character.  There is
nothing stopping a Unix-type implementation from saying that a file
does not end with a LF charcter, then the implementation will consider
the end-of-file to be accompanied by an *implicit* line terminator that
is not represented by any bits in the file, but is conceptually
considered to be there anyway.  Ada allows this interpretation.

It's a mistake to think of a line terminator as a "character"; this is
probably a common mistake among those used to using C and Unix (or
Linux or other OS's ending in "x") (or Solaris), but it's not even
correct to say that a line terminator is a character on other OS's.  On
Windows, it's two characters (CR-LF).  VMS, as I recall, doesn't use
any character as a line terminator, but keeps information on where
lines start and how long they are in an index or somewhere.  Ada is a
portable language that needs to be implementable on all these systems
and others, so therefore it left the definition of "line terminator" up
to the implementation and did not tie us to the concept that there has
to be a "character" involved.

                              -- Adam




  reply	other threads:[~2006-12-07 17:42 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-06 14:25 Get_Line problem (GNAT bug?) Maciej Sobczak
2006-12-06 18:06 ` Adam Beneschan
2006-12-06 20:34 ` Gautier
2006-12-06 21:47 ` Dmitry A. Kazakov
2006-12-06 23:40   ` Adam Beneschan
2006-12-07  0:02     ` Björn Persson
2006-12-07  1:09       ` Adam Beneschan
2006-12-07  1:28         ` Björn Persson
2006-12-07  5:00         ` Jeffrey R. Carter
2006-12-07  8:26   ` Maciej Sobczak
2006-12-07  9:21     ` Jean-Pierre Rosen
2006-12-07 13:35       ` Ludovic Brenta
2006-12-07 22:23       ` Robert A Duff
2006-12-07 10:22     ` Dmitry A. Kazakov
2006-12-07 14:51       ` Maciej Sobczak
2006-12-07 16:29         ` Dmitry A. Kazakov
2006-12-08  8:22           ` Maciej Sobczak
2006-12-07 22:50       ` Robert A Duff
2006-12-08  0:13         ` Randy Brukardt
2006-12-08  4:04         ` Larry Kilgallen
2006-12-08  9:11         ` Dmitry A. Kazakov
2006-12-07  9:14   ` Jean-Pierre Rosen
2006-12-07  3:34 ` Steve
2006-12-07 17:42   ` Adam Beneschan [this message]
2006-12-07 22:35     ` Robert A Duff
replies disabled

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