fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

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)
#
# -----------------------------------------------------------------------


# shorthand to return an io.reader with a read handler
# allowing to read from stdin.
#
# usage example using a local mutate instance:
#
#     lm : mutate is
#     lm ! ()->
#       io.buffered lm .Reader.instate (io.stdin.reader lm) ()->
#         for l := (io.buffered lm).read_line
#             i in 1..
#         while l.ok
#           say "$i: $l"
#
# usage example using read_lines:
#
#     for l in io.stdin.read_lines
#         i in 1..
#     do
#       say "$i: $l"
#
# NYI: UNDER DEVELOPMENT: It would be good to move features reader, read_fully,
# etc. to a parent feature and have other features, e.g., for reading files, inherit
# from that feature as well.
#
public stdin is

  # create a reader for stdin using the given local mutate instance
  #
  public reader (LM type : mutate) : (buffered LM).Reader is
    # implementation of read for reading from stdin
    #
    public redef read(max_count i64) choice (Sequence u8) io.end_of_file error =>
      arr := fuzion.sys.internal_array_init u8 max_count
      v := fzE_file_read fzE_file_stdin arr.data max_count.as_i32
      if v = -1
        error "an error occurred while reading stdin"
      else if v = 0
        io.end_of_file
      else
        arr.as_array.take v

  # read all of stdin in an array of u8 bytes using
  # io.buffered.read_fully.
  #
  public read_fully Sequence u8 =>
    lm : mutate is
    lm ! ()->
      (buffered lm).Reader.instate (reader lm) ()->
        (buffered lm).read_fully.or_panic

  # read all of stdin in an array of String using
  # io.buffered.read_lines.
  #
  public read_lines array String =>
    lm : mutate is
    lm ! ()->
      (buffered lm).Reader.instate (reader lm) ()->
        (buffered lm).read_lines.or_panic

  # read all of stdin in a single String using
  # String.from_bytes on the result of read_fully.
  #
  public read_string String =>
    String.from_bytes read_fully

last changed: 2026-10-07