
(********************************************************************)
(*                                                                  *)
(*  getf.s7i      Read and write whole files into and from strings  *)
(*  Copyright (C) 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.                       *)
(*                                                                  *)
(********************************************************************)


include "stdio.s7i";


(**
 *  Read the file with the given 'file_name' into a string.
 *  @return a string with the file content, or "" when no file exists.
 *)
const func string: getf (in string: file_name) is func
  result
    var string: result is "";
  local
    var file: work_file is STD_NULL;
  begin
    work_file := open(file_name, "r");
    if work_file <> STD_NULL then
      result := gets(work_file, length(work_file));
      close(work_file);
    end if;
  end func;


(**
 *  Write the string 'stri' to the file with the name 'file_name'.
 *  If the file exists already, it is overwritten.
 *)
const proc: putf (in string: file_name, in string: stri) is func
  local
    var file: work_file is STD_NULL;
  begin
    if stri <> "" then
      work_file := open(file_name, "w");
      if work_file <> STD_NULL then
        write(work_file, stri);
        close(work_file);
      end if;
    end if;
  end func;


(**
 *  Read the file with the name 'file_name' into an array of strings.
 *  The function accepts lines ending with LF or with CR LF. The line
 *  endings are not copied into the strings. The last line of the
 *  file needs not to be terminated with a LF.
 *  @return a string array with the file content, or an empty array when no file exists.
 *)
const func array string: readf (in string: file_name) is func
  result
    var array string: result is 0 times "";
  local
    var file: work_file is STD_NULL;
  begin
    work_file := open(file_name, "r");
    if work_file <> STD_NULL then
      while hasNext(work_file) do
        result &:= [] getln(work_file);
      end while;
      close(work_file);
    end if;
  end func;


(**
 *  Write the string array 'data' to the file with the name 'file_name'.
 *  The elements of the array are written as lines to the file. The
 *  function uses LF as line ending and the last line is also
 *  terminated with a LF. If the file exists already, it is
 *  overwritten.
 *)
const proc: writef (in string: file_name, in array string: data) is func
  local
    var file: work_file is STD_NULL;
    var integer: line is 0;
  begin
    if length(data) <> 0 then
      work_file := open(file_name, "w");
      if work_file <> STD_NULL then
        for line range 1 to length(data) do
          writeln(work_file, data[line]);
        end for;
        close(work_file);
      end if;
    end if;
  end func;
