
(********************************************************************)
(*                                                                  *)
(*  enable_input.s7i  Templates to enable file output for a type    *)
(*  Copyright (C) 1989 - 2012  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 "file.s7i";


var file: IN is forward;


(**
 *  Template function to define input functions for ''aType''.
 *  It defines the functions [[#read(inout_aType)|read]] and
 *  [[#readln(inout_aType)|readln]]. The functions read a
 *  whitespace terminated word respectively a line as
 *  [[string]]. Afterwards the ''parse'' operator is used to
 *  convert the ''string'' to an ''aType'' value. The functions
 *  [[#read(inout_file,inout_aType)|read]] and
 *  [[#readln(inout_file,inout_aType)|readln]] are defined with
 *  [[file]] argument and without [[file]] argument. The
 *  functions without [[file]] argument use the standard
 *  input file [[stdio#IN|IN]].
 *)
const proc: enable_input (in type: aType) is func
  begin

    (**
     *  Read ''aVar'' from a word read from ''inFile''.
     *  Before reading the word it skips spaces and tabs. The function
     *  accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left inFile.bufferChar contains the
     *  word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the word to the type ''aType''.
     *)
    const proc: read (inout file: inFile, inout aType: aVar) is func
      begin
         aVar := aType parse getwd(inFile);
      end func;

   (**
     *  Read ''aVar'' from a word read from ''inFile'' or use ''defaultValue''.
     *  Before reading the word it skips spaces and tabs. The function
     *  accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
     *  When the word is empty ''defaultValue'' is assigned to ''aVar''.
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left inFile.bufferChar contains the
     *  word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the word to the type ''aType''.
     *)
    const proc: read (inout file: inFile, inout aType: aVar,
        in aType: defaultValue) is func
      local
        var string: stri is "";
      begin
        stri := getwd(inFile);
        if stri = "" then
          aVar := defaultValue;
        else
          aVar := aType parse stri;
        end if;
      end func;

    (**
     *  Read ''aVar'' from a line read from ''inFile''.
     *  The function reads a string up to end-of-line or [[char#EOF|EOF]].
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left inFile.bufferChar contains the
     *  line ending character ('\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the line to the type ''aType''.
     *)
    const proc: readln (inout file: inFile, inout aType: aVar) is func
      begin
        aVar := aType parse trimValue(aType, getln(inFile));
      end func;

    (**
     *  Read ''aVar'' from a line read from ''inFile'' or use ''defaultValue''.
     *  The function reads a string up to end-of-line or [[char#EOF|EOF]].
     *  When the line is empty ''defaultValue'' is assigned to ''aVar''.
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left inFile.bufferChar contains the
     *  line ending character ('\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the line to the type ''aType''.
     *)
    const proc: readln (inout file: inFile, inout aType: aVar,
        in aType: defaultValue) is func
      local
        var string: stri is "";
      begin
        stri := getln(inFile);
        if stri = "" then
          aVar := defaultValue;
        else
          aVar := aType parse trimValue(aType, stri);
        end if;
      end func;

    (**
     *  Read ''aVar'' from a word read from the standard input file [[stdio#IN|IN]].
     *  Before reading the word it skips spaces and tabs. The function
     *  accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
     *  word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the word to the type ''aType''.
     *)
    const proc: read (inout aType: aVar) is func
      begin
        read(IN, aVar);
      end func;

    (**
     *  Read ''aVar'' from a word read from standard input ([[stdio#IN|IN]]) or use ''defaultValue''.
     *  Before reading the word it skips spaces and tabs. The function
     *  accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
     *  When the word is empty ''defaultValue'' is assigned to ''aVar''.
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
     *  word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the word to the type ''aType''.
     *)
    const proc: read (inout aType: aVar, in aType: defaultValue) is func
      begin
        read(IN, aVar, defaultValue);
      end func;

    (**
     *  Read ''aVar'' from a line read from the standard input file [[stdio#IN|IN]].
     *  The function reads a string up to end-of-line or [[char#EOF|EOF]].
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
     *  line ending character ('\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the line to the type ''aType''.
     *)
    const proc: readln (inout aType: aVar) is func
      begin
        readln(IN, aVar);
      end func;

    (**
     *  Read ''aVar'' from a line read from standard input ([[stdio#IN|IN]]) or use ''defaultValue''.
     *  The function reads a string up to end-of-line or [[char#EOF|EOF]].
     *  When the line is empty ''defaultValue'' is assigned to ''aVar''.
     *  The conversion to the type ''aType'' is done with the ''parse''
     *  operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
     *  line ending character ('\n' or [[char#EOF|EOF]]).
     *  @exception RANGE_ERROR When the ''parse'' operator cannot convert
     *             the line to the type ''aType''.
     *)
    const proc: readln (inout aType: aVar, in aType: defaultValue) is func
      begin
        readln(IN, aVar, defaultValue);
      end func;

  end func;
