
(********************************************************************)
(*                                                                  *)
(*  reverse.s7i   Filter file which writes right to left on output  *)
(*  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.                       *)
(*                                                                  *)
(********************************************************************)


const type: reverse_file is sub null_file struct
    var file: out_file is STD_NULL;
    var integer: line_size is 0;
  end struct;


const func file: openReverse (in file: out_fil, in integer: lin_size) is func
  result
    var file: newFile is STD_NULL;
  local
    var reverse_file: new_reverse_file is reverse_file.value;
  begin
    new_reverse_file.out_file := out_fil;
    new_reverse_file.line_size := lin_size;
    newFile := toInterface(new_reverse_file);
  end func;


(*
const proc: page (ref reverse_file: reverse_fil) is func
  begin
    page(reverse_fil.out_file);
  end func;
*)


const proc: write (inout reverse_file: reverse_fil, in string: stri) is func
  local
    var integer: number is 0;
  begin
    for number range 1 to length(stri) do
      if stri[number] <> '\b' then
        write(reverse_fil.out_file, stri[number]);
        write(reverse_fil.out_file, "\b");
        write(reverse_fil.out_file, "\b");
      else
        write(reverse_fil.out_file, "  ");
        write(reverse_fil.out_file, "\b");
      end if;
    end for;
  end func;


const proc: writeln (inout reverse_file: reverse_fil) is func
  begin
    writeln(reverse_fil.out_file);
    write(reverse_fil.out_file, "" lpad pred(reverse_fil.line_size));
  end func;


const proc: moveLeft (inout reverse_file: reverse_fil, in string: stri) is func
  local
    var integer: index is 0;
  begin
    for index range length(stri) downto 1 do
      write(reverse_fil.out_file, stri[index]);
    end for;
  end func;


const proc: erase (inout reverse_file: reverse_fil, in string: stri) is func
  begin
    moveLeft(reverse_fil.out_file, stri);
    write(reverse_fil.out_file, "\b");
  end func;
