From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d54559bde05f5bb8 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news.glorb.com!wn14feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Weird string I/O problem Reply-To: no to spamers (No@email.given.org) References: <184a2bfd-4d95-434b-91e0-64c8f869e8b7@r15g2000prh.googlegroups.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: <_7sZk.171628$Mh5.91283@bgtnsc04-news.ops.worldnet.att.net> Date: Wed, 03 Dec 2008 09:16:42 GMT NNTP-Posting-Host: 12.65.222.87 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1228295802 12.65.222.87 (Wed, 03 Dec 2008 09:16:42 GMT) NNTP-Posting-Date: Wed, 03 Dec 2008 09:16:42 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:2855 Date: 2008-12-03T09:16:42+00:00 List-Id: -- Adam. -- -- The reason I used the "exit when char = CR or char = LF ;" -- statement was to allow the program to work in both Linux and -- Windows environment without modification. -- Also, A.10 (8). states the creator of this implementation -- can define the terminators or EOL. So, I just used the OS -- defined in two standard OS. -- And the reason I forgot the ";", well it was a typo. And those who -- says they do not make typos is a person that is not trust worthy! -- -- For christoph.grein -- Better re-read RM A.10.7. It does not say what you think it does. -- -- Now A.9(8) should be A.10.8. And it says that it is up to the -- author's implementation to define the terminators. Normally -- that is defined by the OS and hardware, but it can be redefined -- by the author code. -- -- Full Working Example: -- -- This version contains the orginal core code that I posted with some -- extra code on the outside of the core, plus some extra comments. -- -- -- Build your own input routine. Example: -- with Ada.Characters.Latin_1 ; -- Needed for control chars. use Ada.Characters.Latin_1 ; with Ada.Text_IO ; use Ada.Text_IO ; procedure t is -- -- Is control character codes in a String illegal or is it legal? That -- is up to the user of this routine. But the code is easy to adapt to -- the users needs. -- -- This routine does not handle other control codes except EOL which -- is defined by either LF or CR/LF. This allows one to insert the -- BEL control character into the inputed string, then later it can be -- printed or removed by other routines. -- Exceptions: 1. Control C is handled by OS. -- 2. Some characters like BS (8) may be stored in the -- string while others characters like TAB (9) may -- be expanded, depending on OS or BIOS. -- -- Returns String: On EOL or -- the number of non-expanded keystokes > 79 -- function Get ( P : String ) return String is char : character ; input_string : String ( 1..80 ) ; last : natural ; procedure Get ( C : out character ) is begin loop Get_Immediate ( C ) ; exit when C /= NUL ; end loop ; Put ( C ) ; end Get ; begin Put ( P ) ; input_string := ( others => nul ) ; for index in input_string'Range loop -- limits data size Get ( char ) ; -- Traps both LF or CR/LF type of EOL exit when char = CR or char = LF ; input_string ( index ) := char ; Last := Index ; end loop ; if char = CR then -- for CR/LF pairing type of OSs Get ( char ) ; end if ; if char = LF or Last = input_string'Last then -- process EOL on screen New_Line ; end if ; -- valid data is input_string ( 1 .. last ) ; -- with no term control characters like LF or CR return ( input_string ( 1 .. last ) ) ; end get ; begin Put_Line ( Get ( ">> " ) ) ; end t ; In <184a2bfd-4d95-434b-91e0-64c8f869e8b7@r15g2000prh.googlegroups.com>, Adam Beneschan writes: >On Dec 1, 11:53 pm, christoph.gr...@eurocopter.com wrote: >> On 2 Dez., 07:55, a...@anon.org (anon) wrote: >> >> > Have you ever learn, you do not give the complete code, because too many >> > students like to steal code for there grade. >> >> > So, you ONLY give enough code for the person to use the code to >> > build his own. And that's what i did! >> >> anon pretending educational intentions presents wrong code. What a >> nice fellow! >> >> for index in input_string'Range loop >> ... Here get the next char from the input stream. >> ... Be careful, there might be control characters. >> ... See Ada.Text_IO for appropriate subprograms. >> exit when char = CR or char = LF ; -- not all OSes treat end of >> lines like this >> this is why there are >> End_of_Line queries >> input_string ( index ) := char ; >> Last := Index ; >> end loop; >> >> This would be the way, I guess. > >Also make sure it works when the user enters an empty string. Aside >from the error of trying to test characters for CR and LF, and the >issue that it wouldn't even work on an OS that returns *both* CR and >LF in sequence, when the above code is executed multiple times to >input multiple lines, there's a nasty bug (in anon's original code) >that will show up when the input is an empty string. > >In other words, even from an educational standpoint, the original code >seems more useful as a "Can you spot all the errors in this?" exercise >than as something to help a student get started building his or her >own. (And yes, I did catch the missing semicolon after "end loop".) >Kind of reminds me of an old MAD Magazine puzzle where they showed you >a picture that had everything possible wrong with it (including an >upside-down sign that said MAD IS NOT FUNNY), and asked "What's wrong >with this picture"? On the answer page, it simply said "Better you >should ask, what's RIGHT with this picture"? > > -- Adam >