comp.lang.ada
 help / color / mirror / Atom feed
From: Thomas Locke <thomas@kenshi.dk>
Subject: Re: C getchar() functionality in Ada
Date: Tue, 17 Feb 2009 08:19:01 +0100
Date: 2009-02-17T08:19:01+01:00	[thread overview]
Message-ID: <499a64e6$0$90264$14726298@news.sunsite.dk> (raw)
In-Reply-To: <f189b0f2-1e0a-43bc-af88-394374723278@r41g2000prr.googlegroups.com>

Adam Beneschan wrote:
> I'd be surprised if the program actually ignores *all* linefeeds---
> that would indicate something is wrong with your compiler.  But you're
> probably  seeing that blank lines get ignored---is that correct?


Yes, that is correct.

Just as you say, I've found that the Get and Get_Line functions ignore 
linefeeds.

Get_Immediate on the other hand, appears to be "aware" of linefeeds.


> 
> Ada doesn't think of Text_IO files just as streams of characters (with
> linefeed being one of the characters); files are structured in terms
> of lines and pages, without specifying just what form the line
> separators and page separators look like.  Here, End_Of_Line tests to
> see if the input file is at the end of the line, but it does *not*
> "eat" the line separator.  You'll have to use Skip_Line for that.
> Also, Get will skip over any line separators that it's facing.  So if
> there are two or more line separators in a row, End_Of_Line will not
> swallow any of them, and then Get will swallow all of them.
> Furthermore, if there are multiple line separators followed by the end-
> of-file, End_Of_File will not return True because there are additional
> "empty" lines, but then Get will eat all the line separators and then
> raise End_Error because there is no character to read.


That is *exactly* what I'm seeing.


> I think that with this information you can probably figure out how to
> make it work, as close as possible.  But keep in mind that you are
> *still* not going to be reading and writing individual characters the
> same as in C.  On Windows, the line separator is often CR LF (two
> characters), and there's really no way in Ada (using Text_IO) to treat
> that as two separate characters.  If you need that, you'll have to go
> outside the standard Ada library.  Plus there's no telling what your
> implementation will do if it sees CR or LF by itself, or LF CR, or
> other mutant combinations of those control characters.


Very good point. I wonder how the C getchar() program would handle 
itself with a file of CRLF terminated lines on a *nix machine.

Am I right in assuming that Ada handles itself uniformly across all 
supported platforms?


> In any event, the program you're writing probably has no practical
> use.  If you really want to read individual characters and treat
> control characters just like all other characters, you probably don't
> want to use Text_IO.  If that isn't what you want, then you likely
> don't want to read individual characters.  I realize that this is not
> a practical program that you're trying to mimic, but rather a example
> to demonstrate some point.


Yes, that was what Jeffrey also said. But now that I better understand 
why Text_IO behaves as it does, I think I'd rather build 2-3 small Ada 
programs to mimick the one C program, each program focusing on a 
specific area of behavior.

I'm really only doing this to learn Ada, so doing a bit more code is 
probably not the worst idea.  :o)

For now, I will leave Streams for the future. I can easily see myself 
failing miserably with Streams, as they are probably a bit more 
complicated than Text_IO, and understanding Text_IO is, currently, a big 
enough challenge.


> Hope this helps,
> 
>                               -- Adam


It did, very much. Thank you.

:o)
/Thomas



  reply	other threads:[~2009-02-17  7:19 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-16 20:36 C getchar() functionality in Ada Thomas Locke
2009-02-16 21:43 ` Jeffrey R. Carter
2009-02-17  7:23   ` Thomas Locke
2009-02-17  8:16     ` Niklas Holsti
2009-02-17 13:01       ` Georg Bauhaus
2009-02-17 22:03         ` Thomas Locke
2009-03-03  8:57         ` David Thompson
2009-02-17 20:48     ` Jeffrey R. Carter
2009-02-17 21:59       ` Thomas Locke
2009-02-16 23:46 ` Adam Beneschan
2009-02-17  7:19   ` Thomas Locke [this message]
2009-02-17  8:37     ` Jacob Sparre Andersen
2009-02-17 17:31       ` Keith Thompson
2009-02-17 14:57     ` Hibou57 (Yannick Duchêne)
2009-02-17 14:55   ` Hibou57 (Yannick Duchêne)
2009-02-17 10:37 ` m.collado
2009-02-17 22:07   ` Thomas Locke
2009-02-17 16:40 ` Jean-Pierre Rosen
2009-02-17 19:46   ` Thomas Locke
2009-02-17 20:24     ` Hyman Rosen
2009-02-17 23:44       ` Robert A Duff
2009-02-18  5:44         ` anon
2009-02-18  0:39       ` Adam Beneschan
2009-02-18  7:43     ` Jean-Pierre Rosen
2009-02-18 11:42       ` christoph.grein
2009-02-17 20:38   ` Jeffrey R. Carter
2009-02-18  7:46     ` Jean-Pierre Rosen
2009-02-18 10:41       ` christoph.grein
replies disabled

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