lib/core: remove BufferedReader as file superclass
authorLucas Bajolet <lucas.bajolet@gmail.com>
Mon, 7 May 2018 14:48:15 +0000 (10:48 -0400)
committerLucas Bajolet <lucas.bajolet@gmail.com>
Fri, 11 May 2018 18:37:20 +0000 (14:37 -0400)
Since files are already buffered at the libc-level, we do not need to
have a BufferedReader on top of it.

Signed-off-by: Lucas Bajolet <lucas.bajolet@gmail.com>

lib/core/file.nit
tests/emscripten_nodejs.nit
tests/sav/test_peek.res [deleted file]
tests/test_peek.nit [deleted file]

index d1f4939..d8be114 100644 (file)
@@ -101,7 +101,6 @@ end
 # `Stream` that can read from a File
 class FileReader
        super FileStream
-       super BufferedReader
        super PollableReader
        # Misc
 
@@ -114,62 +113,59 @@ class FileReader
        #     assert l == f.read_line
        fun reopen
        do
-               if not eof and not _file.as(not null).address_is_null then close
+               var fl = _file
+               if fl != null and not fl.address_is_null then close
                last_error = null
                _file = new NativeFile.io_open_read(path.as(not null).to_cstring)
                if _file.as(not null).address_is_null then
                        last_error = new IOError("Cannot open `{path.as(not null)}`: {sys.errno.strerror}")
-                       end_reached = true
                        return
                end
-               end_reached = false
-               buffer_reset
        end
 
-       redef fun close
+       redef fun raw_read_byte
        do
-               super
-               buffer_reset
-               end_reached = true
+               var nb = _file.as(not null).io_read(write_buffer, 1)
+               if last_error == null and _file.as(not null).ferror then
+                       last_error = new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
+               end
+               if nb == 0 then return -1
+               return write_buffer[0].to_i
        end
 
-       redef fun fill_buffer
+       redef fun raw_read_bytes(cstr, max)
        do
-               var nb = _file.as(not null).io_read(_buffer, _buffer_capacity)
+               var nb = _file.as(not null).io_read(cstr, max)
                if last_error == null and _file.as(not null).ferror then
                        last_error = new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
-                       end_reached = true
                end
-               if nb <= 0 then
-                       end_reached = true
-                       nb = 0
-               end
-               _buffer_length = nb
-               _buffer_pos = 0
+               return nb
        end
 
-       # End of file?
-       redef var end_reached = false
+       redef fun eof do
+               if lookahead_length != 0 then
+                       return false
+               end
+               return _file.as(not null).feof
+       end
 
        # Open the file at `path` for reading.
        #
        #     var f = new FileReader.open("/etc/issue")
-       #     assert not f.end_reached
+       #     assert not f.eof
        #     f.close
        #
        # In case of error, `last_error` is set
        #
        #     f = new FileReader.open("/fail/does not/exist")
-       #     assert f.end_reached
+       #     assert f.eof
        #     assert f.last_error != null
        init open(path: String)
        do
                self.path = path
-               prepare_buffer(100)
                _file = new NativeFile.io_open_read(path.to_cstring)
                if _file.as(not null).address_is_null then
                        last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
-                       end_reached = true
                end
        end
 
@@ -178,11 +174,9 @@ class FileReader
        # This is a low-level method.
        init from_fd(fd: Int) do
                self.path = ""
-               prepare_buffer(1)
                _file = fd.fd_to_stream(read_only)
                if _file.as(not null).address_is_null then
                        last_error = new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
-                       end_reached = true
                end
        end
 
@@ -330,7 +324,6 @@ class Stdin
        init do
                _file = new NativeFile.native_stdin
                path = "/dev/stdin"
-               prepare_buffer(1)
        end
 end
 
@@ -1544,6 +1537,8 @@ private extern class NativeFile `{ FILE* `}
 
        fun ferror: Bool `{ return ferror(self); `}
 
+       fun feof: Bool `{ return feof(self); `}
+
        fun fileno: Int `{ return fileno(self); `}
 
        # Flushes the buffer, forcing the write operation
index 2add93c..f299f01 100644 (file)
@@ -1,6 +1,6 @@
 import emscripten
 redef class FileReader
-       redef fun fill_buffer
+       redef fun raw_read_byte
        do
                print "NOT YET IMPLEMENTED"
                abort
diff --git a/tests/sav/test_peek.res b/tests/sav/test_peek.res
deleted file mode 100644 (file)
index edc1121..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-# Thi
-# Thi
-s file is pa
-s file is pa
-rt of NIT ( http://www.nitlanguage.org ).
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-var f = new FileReader.open("test_peek.nit")
-
-print f.peek(5)
-print f.read(5)
-
-print f.peek(12)
-print f.read(12)
-
-print f.read_all
-
-print f.peek(2)
-
-f.close
-
-
diff --git a/tests/test_peek.nit b/tests/test_peek.nit
deleted file mode 100644 (file)
index 4c1d433..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-var f = new FileReader.open("test_peek.nit")
-
-print f.peek(5)
-print f.read(5)
-
-print f.peek(12)
-print f.read(12)
-
-print f.read_all
-
-print f.peek(2)
-
-f.close