Merge: Nitdoc: some cleaning and fixes.
[nit.git] / lib / standard / file.nit
index de0da82..3443105 100644 (file)
@@ -26,6 +26,7 @@ in "C Header" `{
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <unistd.h>
+       #include <stdio.h>
 `}
 
 # File Abstract Stream
@@ -35,10 +36,12 @@ abstract class FStream
        var path: nullable String = null
 
        # The FILE *.
-       var _file: nullable NativeFile = null
+       private var file: nullable NativeFile = null
 
-       fun file_stat: FileStat
-       do return _file.file_stat end
+       fun file_stat: FileStat do return _file.file_stat
+
+       # File descriptor of this file
+       fun fd: Int do return _file.fileno
 end
 
 # File input stream
@@ -100,7 +103,7 @@ class OFStream
        
        redef fun write(s)
        do
-               assert _writable
+               assert _is_writable
                if s isa FlatText then
                        write_native(s.to_cstring, s.length)
                else
@@ -108,21 +111,18 @@ class OFStream
                end
        end
 
-       redef fun is_writable do return _writable
-       
        redef fun close
        do
                var i = _file.io_close
-               _writable = false
+               _is_writable = false
        end
 
-       # Is the file open in write mode
-       var _writable: Bool
+       redef var is_writable = false
        
        # Write `len` bytes from `native`.
        private fun write_native(native: NativeString, len: Int)
        do
-               assert _writable
+               assert _is_writable
                var err = _file.io_write(native, len)
                if err != len then
                        # Big problem
@@ -138,7 +138,7 @@ class OFStream
                        print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
                end
                self.path = path
-               _writable = true
+               _is_writable = true
        end
        
        private init do end
@@ -165,7 +165,7 @@ class Stdout
        private init do
                _file = new NativeFile.native_stdout
                path = "/dev/stdout"
-               _writable = true
+               _is_writable = true
        end
 end
 
@@ -174,7 +174,7 @@ class Stderr
        private init do
                _file = new NativeFile.native_stderr
                path = "/dev/stderr"
-               _writable = true
+               _is_writable = true
        end
 end
 
@@ -306,6 +306,7 @@ redef class String
        #     assert "dir/../../".simplify_path        ==  ".."
        #     assert "dir/..".simplify_path            ==  "."
        #     assert "//absolute//path/".simplify_path ==  "/absolute/path"
+       #     assert "//absolute//../".simplify_path   ==  "/"
        fun simplify_path: String
        do
                var a = self.split_with("/")
@@ -320,6 +321,7 @@ redef class String
                        a2.push(x)
                end
                if a2.is_empty then return "."
+               if a2.length == 1 and a2.first == "" then return "/"
                return a2.join("/")
        end
 
@@ -512,6 +514,7 @@ private extern class NativeFile `{ FILE* `}
        fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
        fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
        fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
+       fun fileno: Int `{ return fileno(recv); `}
 
        new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
        new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"