+ last_error = input.last_error or else output.last_error
+ end
+
+ # Open this file for reading.
+ #
+ # ~~~
+ # var file = "/etc/issue".to_path.open_ro
+ # print file.read_line
+ # file.close
+ # ~~~
+ #
+ # Note that it is the user's responsibility to close the stream.
+ # Therefore, for simple use case, look at `read_all` or `each_line`.
+ #
+ # ENSURE `last_error == result.last_error`
+ fun open_ro: FileReader
+ do
+ var res = new FileReader.open(path)
+ last_error = res.last_error
+ return res
+ end
+
+ # Open this file for writing
+ #
+ # ~~~
+ # var file = "bla.log".to_path.open_wo
+ # file.write "Blabla\n"
+ # file.close
+ # ~~~
+ #
+ # Note that it is the user's responsibility to close the stream.
+ # Therefore, for simple use case, look at `Writable::write_to_file`.
+ #
+ # ENSURE `last_error == result.last_error`
+ fun open_wo: FileWriter
+ do
+ var res = new FileWriter.open(path)
+ last_error = res.last_error
+ return res
+ end
+
+ # Read all the content of the file as a string.
+ #
+ # ~~~
+ # var content = "/etc/issue".to_path.read_all
+ # print content
+ # ~~~
+ #
+ # `last_error` is updated to contains the error information on error, and null on success.
+ # In case of error, the result might be empty or truncated.
+ #
+ # See `Reader::read_all` for details.
+ fun read_all: String do return read_all_bytes.to_s
+
+ # Read all the content on the file as a raw sequence of bytes.
+ #
+ # ~~~
+ # var content = "/etc/issue".to_path.read_all_bytes
+ # print content.to_s
+ # ~~~
+ #
+ # `last_error` is updated to contains the error information on error, and null on success.
+ # In case of error, the result might be empty or truncated.
+ fun read_all_bytes: Bytes
+ do
+ var s = open_ro
+ var res = s.read_all_bytes
+ s.close
+ last_error = s.last_error
+ return res
+ end
+
+ # Read all the lines of the file
+ #
+ # ~~~
+ # var lines = "/etc/passwd".to_path.read_lines
+ #
+ # print "{lines.length} users"
+ #
+ # for l in lines do
+ # var fields = l.split(":")
+ # print "name={fields[0]} uid={fields[2]}"
+ # end
+ # ~~~
+ #
+ # `last_error` is updated to contains the error information on error, and null on success.
+ # In case of error, the result might be empty or truncated.
+ #
+ # See `Reader::read_lines` for details.
+ fun read_lines: Array[String]
+ do
+ var s = open_ro
+ var res = s.read_lines
+ s.close
+ last_error = s.last_error
+ return res
+ end
+
+ # Return an iterator on each line of the file
+ #
+ # ~~~
+ # for l in "/etc/passwd".to_path.each_line do
+ # var fields = l.split(":")
+ # print "name={fields[0]} uid={fields[2]}"
+ # end
+ # ~~~
+ #
+ # Note: the stream is automatically closed at the end of the file (see `LineIterator::close_on_finish`)
+ #
+ # `last_error` is updated to contains the error information on error, and null on success.
+ #
+ # See `Reader::each_line` for details.
+ fun each_line: LineIterator
+ do
+ var s = open_ro
+ var res = s.each_line
+ res.close_on_finish = true
+ last_error = s.last_error
+ return res
+ end
+
+
+ # Lists the files contained within the directory at `path`.
+ #
+ # var files = "/etc".to_path.files
+ # assert files.has("/etc/issue".to_path)
+ #
+ # `last_error` is updated to contains the error information on error, and null on success.
+ # In case of error, the result might be empty or truncated.
+ #
+ # var path = "/etc/issue".to_path
+ # files = path.files
+ # assert files.is_empty
+ # assert path.last_error != null
+ fun files: Array[Path]
+ do
+ last_error = null
+ var res = new Array[Path]
+ var d = new NativeDir.opendir(path.to_cstring)
+ if d.address_is_null then
+ last_error = new IOError("Cannot list directory `{path}`: {sys.errno.strerror}")
+ return res
+ end
+
+ loop
+ var de = d.readdir
+ if de.address_is_null then
+ # readdir cannot fail, so null means end of list
+ break
+ end
+ var name = de.to_s_with_copy
+ if name == "." or name == ".." then continue
+ res.add new Path(path / name)
+ end
+ d.closedir
+
+ return res