io/stdin.fz
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation 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 General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion standard library feature stdin
#
# Author: Michael Lill (michael.lill@tokiwa.software)
#
# -----------------------------------------------------------------------
# short-hand to return an io.reader with a read provider
# allowing to read from stdin.
#
# usage example:
# ```
# line_or_error := io.stdin.on ()->
# io.buffered.read_line
# ```
#
public stdin io.buffered.reader =>
read_provider : io.Read_Provider is
read(count i32) choice (array u8) io.end_of_file error =>
arr := fuzion.sys.internal_array_init u8 count
v := fuzion.sys.fileio.read fuzion.sys.stdin.stdin0 arr.data count
if v < -1
error "an error occurred while reading stdin"
else if v <= 0
io.end_of_file
else if v < count
array u8 v i->arr[i]
else
arr.as_array
io.buffered.reader read_provider 1024 []