From 0e734d60ac8d78d9204b4ccd2cc636efe182cfcb Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 30 Mar 2015 19:46:44 +0700 Subject: [PATCH] lib/standard: factorize File*::close Signed-off-by: Jean Privat --- lib/standard/file.nit | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/standard/file.nit b/lib/standard/file.nit index a2856ae..7052c15 100644 --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@ -47,6 +47,21 @@ abstract class FileStream # File descriptor of this file fun fd: Int do return _file.fileno + redef fun close + do + if _file == null then return + if _file.address_is_null then + if last_error != null then return + last_error = new IOError("Cannot close unopened file") + return + end + var i = _file.io_close + if i != 0 then + last_error = new IOError("Close failed due to error {sys.errno.strerror}") + end + _file = null + end + # Sets the buffering mode for the current FileStream # # If the buf_size is <= 0, its value will be 512 by default @@ -89,11 +104,9 @@ class FileReader redef fun close do - if _file == null or _file.address_is_null then return - var i = _file.io_close + super _buffer.clear end_reached = true - _file = null end redef fun fill_buffer @@ -154,19 +167,8 @@ class FileWriter redef fun close do - if _file == null then return - if _file.address_is_null then - if last_error != null then return - last_error = new IOError("Cannot close unopened write stream") - _is_writable = false - return - end - var i = _file.io_close - if i != 0 then - last_error = new IOError("Close failed due to error {sys.errno.strerror}") - end + super _is_writable = false - _file = null end redef var is_writable = false -- 1.7.9.5