comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: Weird string I/O problem
Date: Wed, 03 Dec 2008 09:16:42 GMT
Date: 2008-12-03T09:16:42+00:00	[thread overview]
Message-ID: <_7sZk.171628$Mh5.91283@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 184a2bfd-4d95-434b-91e0-64c8f869e8b7@r15g2000prh.googlegroups.com

--  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 <adam@irvine.com> 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
>




  reply	other threads:[~2008-12-03  9:16 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-26  5:52 Weird string I/O problem Jerry
2008-11-26  7:24 ` christoph.grein
2008-11-26  7:38   ` christoph.grein
2008-11-26  8:25     ` Dmitry A. Kazakov
2008-11-26  9:07   ` Jean-Pierre Rosen
2008-11-26 15:21     ` John McCormick
2008-11-26 15:56     ` Adam Beneschan
2008-11-27 10:13       ` Jean-Pierre Rosen
2008-12-01 16:17         ` Adam Beneschan
2008-11-27  4:46 ` Jerry
2008-11-27 10:27   ` Jean-Pierre Rosen
2008-12-01 19:47 ` anon
2008-12-02  5:44   ` christoph.grein
2008-12-02  6:55     ` anon
2008-12-02  7:53       ` christoph.grein
2008-12-02 16:39         ` Adam Beneschan
2008-12-03  9:16           ` anon [this message]
2008-12-03 10:42             ` christoph.grein
2008-12-03 12:21               ` John B. Matthews
2008-12-04  0:15               ` anon
2008-12-04  7:31                 ` christoph.grein
2008-12-04  7:56                   ` Ludovic Brenta
2008-12-04  8:46                     ` Georg Bauhaus
2008-12-03 11:35             ` stefan-lucks
2008-12-04  0:27               ` anon
2008-12-04  8:58                 ` stefan-lucks
2008-12-04 22:54                   ` anon
2008-12-05  9:06                     ` Georg Bauhaus
replies disabled

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