"Fossies" - the Fresh Open Source Software Archive

Member "seed7/lib/echo.s7i" (27 Aug 2015, 4645 Bytes) of package /linux/misc/seed7_05_20210223.tgz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 (********************************************************************)
    3 (*                                                                  *)
    4 (*  echo.s7i      Filter file which generates an echo of the input  *)
    5 (*  Copyright (C) 1992, 1993, 1994, 2005  Thomas Mertes             *)
    6 (*                                                                  *)
    7 (*  This file is part of the Seed7 Runtime Library.                 *)
    8 (*                                                                  *)
    9 (*  The Seed7 Runtime Library is free software; you can             *)
   10 (*  redistribute it and/or modify it under the terms of the GNU     *)
   11 (*  Lesser General Public License as published by the Free Software *)
   12 (*  Foundation; either version 2.1 of the License, or (at your      *)
   13 (*  option) any later version.                                      *)
   14 (*                                                                  *)
   15 (*  The Seed7 Runtime Library is distributed in the hope that it    *)
   16 (*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
   17 (*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
   18 (*  PURPOSE.  See the GNU Lesser General Public License for more    *)
   19 (*  details.                                                        *)
   20 (*                                                                  *)
   21 (*  You should have received a copy of the GNU Lesser General       *)
   22 (*  Public License along with this program; if not, write to the    *)
   23 (*  Free Software Foundation, Inc., 51 Franklin Street,             *)
   24 (*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
   25 (*                                                                  *)
   26 (********************************************************************)
   27 
   28 
   29 (**
   30  *  [[file|File]] implementation type which generates an echo of the input.
   31  *)
   32 const type: echo_file is sub null_file struct
   33     var file: in_file is STD_NULL;
   34     var file: out_file is STD_NULL;
   35     var integer: in_pos is 1;
   36   end struct;
   37 
   38 
   39 (**
   40  *  Open an echo_file.
   41  *  @return the file opened.
   42  *)
   43 const func file: openEcho (in file: in_fil, in file: out_fil) is func
   44   result
   45     var file: newFile is STD_NULL;
   46   local
   47     var echo_file: new_echo_file is echo_file.value;
   48   begin
   49     new_echo_file.in_file := in_fil;
   50     new_echo_file.out_file := out_fil;
   51     new_echo_file.in_pos := 1;
   52     newFile := toInterface(new_echo_file);
   53   end func;
   54 
   55 
   56 (**
   57  *  Read a character from an echo_file.
   58  *  The request is forwarded to ''in_file''. The character read from
   59  *  ''in_file'' is written (echoed) to ''out_file''. Ctrl-C and ctrl-T
   60  *  are handled special, as they can be used to terminate the
   61  *  program. The user is asked for confirmation before the program
   62  *  is terminated.
   63  *  @return the character read, or [[char#EOF|EOF]] at the end of the file.
   64  *)
   65 const func char: getc (inout echo_file: echo_fil) is func
   66   result
   67     var char: charRead is ' ';
   68   local
   69     var integer: number is 0;
   70   begin
   71     repeat
   72       flush(echo_fil.out_file);
   73 (*    cursor_on(echo_fil.out_file); *)
   74       charRead := getc(echo_fil.in_file);
   75 (*    cursor_off(echo_fil.out_file); *)
   76       if charRead >= ' ' and charRead <= '~' then
   77         incr(echo_fil.in_pos);
   78         write(echo_fil.out_file, charRead);
   79       elsif charRead = '\n' then
   80         echo_fil.in_pos := 1;
   81         writeln(echo_fil.out_file);
   82       elsif charRead = '\b' then
   83         if echo_fil.in_pos > 1 then
   84           decr(echo_fil.in_pos);
   85           backSpace(echo_fil.out_file, " ");
   86         end if;
   87       elsif charRead = '\C' or charRead = '\T' then
   88         write(echo_fil.out_file, " terminate (y/n)? ");
   89         flush(echo_fil.out_file);
   90         if lower(getc(echo_fil.in_file)) = 'y' then
   91           writeln(echo_fil.out_file, "yes");
   92           writeln(echo_fil.out_file);
   93           writeln(echo_fil.out_file, "*** PROGRAM TERMINATED BY USER");
   94           exit(PROGRAM);
   95         else
   96           backSpace(echo_fil.out_file, " terminate (y/n)? ");
   97         end if;
   98       elsif charRead <> EOF then
   99         incr(echo_fil.in_pos);
  100         write(echo_fil.out_file, '?');
  101       end if;
  102     until charRead <> '\C' and charRead <> '\T';
  103   end func;
  104 
  105 
  106 (**
  107  *  Read a string with a maximum length from an echo_file.
  108  *  @return the string read.
  109  *)
  110 const func string: gets (inout echo_file: echo_fil, in integer: maxLength) is func
  111   result
  112     var string: striRead is "";
  113   local
  114     var integer: number is 1;
  115     var char: ch is ' ';
  116   begin
  117     while number <= maxLength and ch <> EOF do
  118       ch := getc(echo_fil);
  119       if ch <> EOF then
  120         striRead &:= str(ch);
  121       end if;
  122     end while;
  123   end func;