
(********************************************************************)
(*                                                                  *)
(*  line.s7i      Filter file which reads the input linewise        *)
(*  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 reads ''baseFile'' linewise.
 *)
const type: line_file is sub null_file struct
    var file: baseFile is STD_NULL;
    var string: line is "";
  end struct;


(**
 *  Open a line_file to filter ''aFile'' linewise.
 *  @return the file opened.
 *)
const func file: openLine (in file: aFile) is func
  result
    var file: newFile is STD_NULL;
  local
    var line_file: new_line_file is line_file.value;
  begin
    new_line_file.baseFile := aFile;
    newFile := toInterface(new_line_file);
  end func;


(**
 *  Write a string to a ''line_file''.
 *)
const proc: write (inout line_file: line_fil, in string: stri) is func
  begin
    line_fil.line &:= stri;
  end func;


(**
 *  Write end-of-line to a ''line_file''.
 *)
const proc: writeln (inout line_file: line_fil) is func
  begin
    writeln(line_fil.baseFile, line_fil.line);
    flush(line_fil.baseFile);
    line_fil.line := "";
  end func;


const proc: flush (inout line_file: line_fil) is func
  begin
    write(line_fil.baseFile, line_fil.line);
    flush(line_fil.baseFile);
    line_fil.line := "";
  end func;


(**
 *  Read a line from a ''line_file''.
 *  The function accepts lines ending with "\n" or [[char#EOF|EOF]].
 *  The line ending characters are not copied into the string. When
 *  the function is left the line_file.bufferChar contains '\n' or
 *  [[char#EOF|EOF]].
 *  @return the line read.
 *)
const func string: getln (inout line_file: line_fil) is func
  result
    var string: stri is "";
  local
    var char: ch is ' ';
  begin
    if line_fil.line = "" then
      ch := getc(line_fil.baseFile);
      while ch <> '\n' and ch <> EOF do
        if ch = '\b' then
          if stri <> "" then
            stri := stri[ .. pred(length(stri))];
          end if;
        else
          stri &:= str(ch);
        end if;
        ch := getc(line_fil.baseFile);
      end while;
      line_fil.bufferChar := ch;
    else
      if line_fil.line[length(line_fil.line)] = '\n' then
        stri := line_fil.line[ .. pred(length(line_fil.line))];
        line_fil.bufferChar := '\n';
      else
        stri := line_fil.line;
        line_fil.bufferChar := EOF;
      end if;
      line_fil.line := "";
    end if;
  end func;


(**
 *  Read a character from a line_file.
 *  @return the character read, or [[char#EOF|EOF]] at the end of the file.
 *)
const func char: getc (inout line_file: line_fil) is func
  result
    var char: charRead is ' ';
  begin
    if line_fil.line = "" then
      line_fil.line := getln(line_fil) & "\n";
    end if;
    if line_fil.line = "\n" and line_fil.bufferChar = EOF then
      charRead := EOF;
    else
      charRead := line_fil.line[1];
      line_fil.line := line_fil.line[2 .. ];
    end if;
  end func;


(**
 *  Read a string with a maximum length from a line_file.
 *  @return the string read.
 *)
const func string: gets (inout line_file: line_fil, in integer: maxLength) is func
  result
    var string: striRead is "";
  begin
    while length(line_fil.line) < maxLength and line_fil.bufferChar <> EOF do
      line_fil.line &:= getln(line_fil) & "\n";
    end while;
    striRead := line_fil.line[ .. maxLength];
    line_fil.line := line_fil.line[succ(maxLength) .. ];
  end func;
