
(********************************************************************)
(*                                                                  *)
(*  echo.s7i      Filter file which generates an echo of the input  *)
(*  Copyright (C) 1992, 1993, 1994, 2005  Thomas Mertes             *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library is distributed in the hope that it    *)
(*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
(*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(*  PURPOSE.  See the GNU Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser General       *)
(*  Public License along with this program; if not, write to the    *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


(**
 *  [[file|File]] implementation type which generates an echo of the input.
 *)
const type: echo_file is sub null_file struct
    var file: in_file is STD_NULL;
    var file: out_file is STD_NULL;
    var integer: in_pos is 1;
  end struct;


(**
 *  Open an echo_file.
 *  @return the file opened.
 *)
const func file: openEcho (in file: in_fil, in file: out_fil) is func
  result
    var file: newFile is STD_NULL;
  local
    var echo_file: new_echo_file is echo_file.value;
  begin
    new_echo_file.in_file := in_fil;
    new_echo_file.out_file := out_fil;
    new_echo_file.in_pos := 1;
    newFile := toInterface(new_echo_file);
  end func;


(**
 *  Read a character from an echo_file.
 *  The request is forwarded to ''in_file''. The character read from
 *  ''in_file'' is written (echoed) to ''out_file''. Ctrl-C and ctrl-T
 *  are handled special, as they can be used to terminate the
 *  program. The user is asked for confirmation before the program
 *  is terminated.
 *  @return the character read, or [[char#EOF|EOF]] at the end of the file.
 *)
const func char: getc (inout echo_file: echo_fil) is func
  result
    var char: charRead is ' ';
  local
    var integer: number is 0;
  begin
    repeat
      flush(echo_fil.out_file);
(*    cursor_on(echo_fil.out_file); *)
      charRead := getc(echo_fil.in_file);
(*    cursor_off(echo_fil.out_file); *)
      if charRead >= ' ' and charRead <= '~' then
        incr(echo_fil.in_pos);
        write(echo_fil.out_file, charRead);
      elsif charRead = '\n' then
        echo_fil.in_pos := 1;
        writeln(echo_fil.out_file);
      elsif charRead = '\b' then
        if echo_fil.in_pos > 1 then
          decr(echo_fil.in_pos);
          backSpace(echo_fil.out_file, " ");
        end if;
      elsif charRead = '\C' or charRead = '\T' then
        write(echo_fil.out_file, " terminate (y/n)? ");
        flush(echo_fil.out_file);
        if lower(getc(echo_fil.in_file)) = 'y' then
          writeln(echo_fil.out_file, "yes");
          writeln(echo_fil.out_file);
          writeln(echo_fil.out_file, "*** PROGRAM TERMINATED BY USER");
          exit(PROGRAM);
        else
          backSpace(echo_fil.out_file, " terminate (y/n)? ");
        end if;
      elsif charRead <> EOF then
        incr(echo_fil.in_pos);
        write(echo_fil.out_file, '?');
      end if;
    until charRead <> '\C' and charRead <> '\T';
  end func;


(**
 *  Read a string with a maximum length from an echo_file.
 *  @return the string read.
 *)
const func string: gets (inout echo_file: echo_fil, in integer: maxLength) is func
  result
    var string: striRead is "";
  local
    var integer: number is 1;
    var char: ch is ' ';
  begin
    while number <= maxLength and ch <> EOF do
      ch := getc(echo_fil);
      if ch <> EOF then
        striRead &:= str(ch);
      end if;
    end while;
  end func;
