nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / lib / core / file.nit
index aba34e8..7f1d481 100644 (file)
@@ -187,7 +187,7 @@ class FileReader
        end
 
        private fun native_poll_in(fd: Int): Int `{
-               struct pollfd fds = {fd, POLLIN, 0};
+               struct pollfd fds = {(int)fd, POLLIN, 0};
                return poll(&fds, 1, 0);
        `}
 end
@@ -292,7 +292,7 @@ redef class Int
        #
        # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
        private fun fd_to_stream(mode: NativeString): NativeFile `{
-               return fdopen(self, mode);
+               return fdopen((int)self, mode);
        `}
 end
 
@@ -498,8 +498,8 @@ class Path
                var output = dest.open_wo
 
                while not input.eof do
-                       var buffer = input.read(1024)
-                       output.write buffer
+                       var buffer = input.read_bytes(1024)
+                       output.write_bytes buffer
                end
 
                input.close
@@ -663,6 +663,19 @@ class Path
                return res
        end
 
+       # Is `self` the path to an existing directory ?
+       #
+       # ~~~nit
+       # assert ".".to_path.is_dir
+       # assert not "/etc/issue".to_path.is_dir
+       # assert not "/should/not/exist".to_path.is_dir
+       # ~~~
+       fun is_dir: Bool do
+               var st = stat
+               if st == null then return false
+               return st.is_dir
+       end
+
        # Delete a directory and all of its content
        #
        # Does not go through symbolic links and may get stuck in a cycle if there
@@ -959,10 +972,13 @@ redef class String
        end
 
        # Return the canonicalized absolute pathname (see POSIX function `realpath`)
+       #
+       # Require: `file_exists`
        fun realpath: String do
                var cs = to_cstring.file_realpath
+               assert file_exists
                var res = cs.to_s_with_copy
-               # cs.free_malloc # FIXME memory leak
+               cs.free
                return res
        end
 
@@ -1142,11 +1158,16 @@ redef class String
 
        # Create a directory (and all intermediate directories if needed)
        #
+       # The optional `mode` parameter specifies the permissions of the directory,
+       # the default value is `0o777`.
+       #
        # Return an error object in case of error.
        #
        #    assert "/etc/".mkdir != null
-       fun mkdir: nullable Error
+       fun mkdir(mode: nullable Int): nullable Error
        do
+               mode = mode or else 0o777
+
                var dirs = self.split_with("/")
                var path = new FlatBuffer
                if dirs.is_empty then return null
@@ -1159,7 +1180,7 @@ redef class String
                        if d.is_empty then continue
                        path.append(d)
                        path.add('/')
-                       var res = path.to_s.to_cstring.file_mkdir
+                       var res = path.to_s.to_cstring.file_mkdir(mode)
                        if not res and error == null then
                                error = new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
                        end
@@ -1265,6 +1286,34 @@ redef class FlatString
        do
                s.write_native(items, first_byte, bytelen)
        end
+
+       redef fun file_extension do
+               var its = _items
+               var p = last_byte
+               var c = its[p]
+               var st = _first_byte
+               while p >= st and c != '.'.ascii do
+                       p -= 1
+                       c = its[p]
+               end
+               if p <= st then return null
+               var ls = last_byte
+               return new FlatString.with_infos(its, ls - p, p + 1)
+       end
+
+       redef fun basename(extension) do
+               var l = last_byte
+               var its = _items
+               var min = _first_byte
+               var sl = '/'.ascii
+               while l > min and its[l] == sl do l -= 1
+               if l == min then return "/"
+               var ns = l
+               while ns >= min and its[ns] != sl do ns -= 1
+               var bname = new FlatString.with_infos(its, l - ns, ns + 1)
+
+               return if extension != null then bname.strip_extension(extension) else bname
+       end
 end
 
 redef class NativeString
@@ -1295,7 +1344,7 @@ redef class NativeString
                return stat_element;
        `}
 
-       private fun file_mkdir: Bool `{ return !mkdir(self, 0777); `}
+       private fun file_mkdir(mode: Int): Bool `{ return !mkdir(self, mode); `}
 
        private fun rmdir: Bool `{ return !rmdir(self); `}
 
@@ -1383,8 +1432,8 @@ private extern class NativeFile `{ FILE* `}
        fun flush: Int `{ return fflush(self); `}
 
        # Used to specify how the buffering will be handled for the current stream.
-       fun set_buffering_type(buf_length: Int, mode: Int): Int `{
-               return setvbuf(self, NULL, mode, buf_length);
+       fun set_buffering_type(buf_length, mode: Int): Int `{
+               return setvbuf(self, NULL, (int)mode, buf_length);
        `}
 
        new io_open_read(path: NativeString) `{ return fopen(path, "r"); `}
@@ -1468,15 +1517,14 @@ redef class Sys
                int first_polled_fd = -1;
                int result;
 
-               in_len = Array_of_Int_length( in_fds );
-               out_len = Array_of_Int_length( out_fds );
+               in_len = (int)Array_of_Int_length( in_fds );
+               out_len = (int)Array_of_Int_length( out_fds );
                total_len = in_len + out_len;
                c_fds = malloc( sizeof(struct pollfd) * total_len );
 
                /* input streams */
                for ( i=0; i<in_len; i ++ ) {
-                       int fd;
-                       fd = Array_of_Int__index( in_fds, i );
+                       int fd = (int)Array_of_Int__index( in_fds, i );
 
                        c_fds[i].fd = fd;
                        c_fds[i].events = POLLIN;
@@ -1484,8 +1532,7 @@ redef class Sys
 
                /* output streams */
                for ( i=0; i<out_len; i ++ ) {
-                       int fd;
-                       fd = Array_of_Int__index( out_fds, i );
+                       int fd = (int)Array_of_Int__index( out_fds, i );
 
                        c_fds[i].fd = fd;
                        c_fds[i].events = POLLOUT;