io/file/read.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 read
#
# Author: Wael Youssfi (wael.youssfi@tokiwa.software)
#
# -----------------------------------------------------------------------
# read -- effect providing byte reading operations from files
#
public read(ro Read_Handler) : simple_effect is
# how much of a file do we read at a time, in bytes
#
private bufsize u64 => 512
# reads all bytes from the file in the path
# returns outcome array u8, a byte array representing the content of the file if the operation was successful
# returns an error in case the operation fails
#
module read_file(fd i64) outcome (array u8) =>
empty array u8 := []
content := read_file fd empty
replace
content
# helper feature for read_file(fd, tmp)
#
# this just calls the read operation and returns the result in a more useful
# format for the use case of read_file(fd, tmp).
#
private read_proxy(fd i64, n u64) outcome (array u8) =>
a := ro.read fd n
replace
a
# helper feature for read_file to allow recursion
#
private read_file(fd i64, tmp array u8) outcome (array u8) =>
t := read_proxy fd bufsize
t.bind (array u8) (a->
if a.length > 0
read_file fd (tmp ++ a).as_array
else
tmp)
# the default file reading operation reading bytes from files via fuzion.sys.fileio.read
#
type.default_read_handler : io.file.Read_Handler is
read(fd i64, n u64) =>
fuzion.sys.fileio.read fd n
# install default effect io.file.read
type.install_default =>
(io.file.read default_read_handler).default
# short-hand for accessing read effect in current environment
#
public read =>
read.install_default
read.env
# reference to the reading operations that could take place
#
private:public Read_Handler ref is
read(fd i64, n u64) outcome (array u8) => abstract
last changed: 2024-03-07