# The FILE *.
private var file: nullable NativeFile = null
+ # The status of a file. see POSIX stat(2).
fun file_stat: FileStat do return _file.file_stat
# File descriptor of this file
class IFStream
super FStream
super BufferedIStream
+ super PollableIStream
# Misc
# Open the same file again.
redef fun close
do
- var i = _file.io_close
+ _file.io_close
+ _buffer.clear
end_reached = true
end
end
end
- private init do end
- private init without_file do end
end
# File output stream
redef fun close
do
- var i = _file.io_close
+ _file.io_close
_is_writable = false
end
self.path = path
_is_writable = true
end
-
- private init do end
- private init without_file do end
end
###############################################################################
+# Standard input stream.
class Stdin
super IFStream
- super PollableIStream
- private init do
+ init do
_file = new NativeFile.native_stdin
path = "/dev/stdin"
prepare_buffer(1)
redef fun poll_in: Bool is extern "file_stdin_poll_in"
end
+# Standard output stream.
class Stdout
super OFStream
- private init do
+ init do
_file = new NativeFile.native_stdout
path = "/dev/stdout"
_is_writable = true
end
end
+# Standard error stream.
class Stderr
super OFStream
- private init do
+ init do
_file = new NativeFile.native_stderr
path = "/dev/stderr"
_is_writable = true
# Correctly join two path using the directory separator.
#
- # Using a standard "{self}/{path}" does not work when `self` is the empty string.
- # This method ensure that the join is valid.
+ # Using a standard "{self}/{path}" does not work in the following cases:
+ #
+ # * `self` is empty.
+ # * `path` ends with `'/'`.
+ # * `path` starts with `'/'`.
#
- # assert "hello".join_path("world") == "hello/world"
- # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
- # assert "".join_path("world") == "world"
- # assert "/hello".join_path("/world") == "/world"
+ # This method ensures that the join is valid.
#
- # Note: you may want to use `simplify_path` on the result
+ # assert "hello".join_path("world") == "hello/world"
+ # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
+ # assert "".join_path("world") == "world"
+ # assert "hello".join_path("/world") == "/world"
+ # assert "hello/".join_path("world") == "hello/world"
+ # assert "hello/".join_path("/world") == "/world"
#
- # Note: I you want to join a great number of path, you can write
+ # Note: You may want to use `simplify_path` on the result.
#
- # [p1, p2, p3, p4].join("/")
+ # Note: This method works only with POSIX paths.
fun join_path(path: String): String
do
if path.is_empty then return self
if self.is_empty then return path
if path.chars[0] == '/' then return path
+ if self.last == '/' then return "{self}{path}"
return "{self}/{path}"
end
+ # Convert the path (`self`) to a program name.
+ #
+ # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
+ # used as a program name. In order to do that, prepend `./` if needed.
+ #
+ # assert "foo".to_program_name == "./foo"
+ # assert "/foo".to_program_name == "/foo"
+ # assert "".to_program_name == "./" # At least, your shell will detect the error.
+ fun to_program_name: String do
+ if self.has_prefix("/") then
+ return self
+ else
+ return "./{self}"
+ end
+ end
+
# Alias for `join_path`
#
# assert "hello" / "world" == "hello/world"