Property definitions

core $ FileWriter :: defaultinit
# `Stream` that can write to a File
class FileWriter
	super FileStream
	super Writer

	redef fun write_bytes_from_cstring(cs, len) do
		if last_error != null then return
		if not _is_writable then
			last_error = new IOError("cannot write to non-writable stream")
			return
		end
		write_native(cs, 0, len)
	end

	redef fun write(s)
	do
		if last_error != null then return
		if not _is_writable then
			last_error = new IOError("cannot write to non-writable stream")
			return
		end
		s.write_native_to(self)
	end

	redef fun write_byte(value)
	do
		if last_error != null then return
		if not _is_writable then
			last_error = new IOError("Cannot write to non-writable stream")
			return
		end
		if _file.as(not null).address_is_null then
			last_error = new IOError("Writing on a null stream")
			_is_writable = false
			return
		end

		var err = _file.as(not null).write_byte(value)
		if err != 1 then
			# Big problem
			last_error = new IOError("Problem writing a byte: {err}")
		end
	end

	redef fun close
	do
		super
		_is_writable = false
	end
	redef var is_writable = false

	# Write `len` bytes from `native`.
	private fun write_native(native: CString, from, len: Int)
	do
		if last_error != null then return
		if not _is_writable then
			last_error = new IOError("Cannot write to non-writable stream")
			return
		end
		if _file.as(not null).address_is_null then
			last_error = new IOError("Writing on a null stream")
			_is_writable = false
			return
		end
		var err = _file.as(not null).io_write(native, from, len)
		if err != len then
			# Big problem
			last_error = new IOError("Problem in writing : {err} {len} \n")
		end
	end

	# Open the file at `path` for writing.
	init open(path: String)
	do
		_file = new NativeFile.io_open_write(path.to_cstring)
		self.path = path
		_is_writable = true
		if _file.as(not null).address_is_null then
			last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
			is_writable = false
		end
	end

	# Creates a new File stream from a file descriptor
	init from_fd(fd: Int) do
		self.path = ""
		_file = fd.fd_to_stream(wipe_write)
		_is_writable = true
		 if _file.as(not null).address_is_null then
			 last_error = new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
			_is_writable = false
		end
	end
end
lib/core/file.nit:207,1--300,3