Merge: File and Bytes
authorJean Privat <jean@pryen.org>
Tue, 26 May 2015 10:09:33 +0000 (06:09 -0400)
committerJean Privat <jean@pryen.org>
Tue, 26 May 2015 10:09:33 +0000 (06:09 -0400)
A bit of a fix in the context of issues #1267 and #1262.

The bytes module has a `ByteBuffer` class, while it is still working with `Int` at the moment, when Nit correctly supports `Bytes`, we can change it to a real `ByteBuffer`

This class is highly recommended when working with byte streams and collections.

As such, file operations are related to this module since most of the operations are working on bytes, what they mean is up to the end-user to figure.

Pull-Request: #1309
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Romain Chanoir <chanoir.romain@courrier.uqam.ca>
Reviewed-by: Ait younes Mehdi Adel <overpex@gmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

1  2 
lib/standard/file.nit

diff --combined lib/standard/file.nit
@@@ -113,25 -113,24 +113,24 @@@ class FileReade
                        return
                end
                end_reached = false
-               _buffer_pos = 0
-               _buffer.clear
+               buffer_reset
        end
  
        redef fun close
        do
                super
-               _buffer.clear
+               buffer_reset
                end_reached = true
        end
  
        redef fun fill_buffer
        do
-               var nb = _file.io_read(_buffer.items, _buffer.capacity)
+               var nb = _file.io_read(_buffer, _buffer_capacity)
                if nb <= 0 then
                        end_reached = true
                        nb = 0
                end
-               _buffer.length = nb
+               _buffer_length = nb
                _buffer_pos = 0
        end
  
@@@ -179,18 -178,23 +178,23 @@@ class FileWrite
        super FileStream
        super Writer
  
+       redef fun write_bytes(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
+               write_native(s.items, s.length)
+       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")
+                       last_error = new IOError("cannot write to non-writable stream")
                        return
                end
-               if s isa FlatText then
-                       write_native(s.to_cstring, s.length)
-               else
-                       for i in s.substrings do write_native(i.to_cstring, i.length)
-               end
+               for i in s.substrings do write_native(i.to_cstring, i.length)
        end
  
        redef fun write_byte(value)
@@@ -435,10 -439,12 +439,12 @@@ class Pat
        # ~~~
        #
        # See `Reader::read_all` for details.
-       fun read_all: String
+       fun read_all: String do return read_all_bytes.to_s
+       fun read_all_bytes: Bytes
        do
                var s = open_ro
-               var res = s.read_all
+               var res = s.read_all_bytes
                s.close
                return res
        end
@@@ -1024,23 -1030,39 +1030,23 @@@ redef class Strin
        #     assert files.is_empty
        #
        # TODO find a better way to handle errors and to give them back to the user.
 -      fun files: Array[String] is extern import Array[String], Array[String].add, NativeString.to_s, String.to_cstring `{
 -              char *dir_path;
 -              DIR *dir;
 -
 -              dir_path = String_to_cstring( recv );
 -              if ((dir = opendir(dir_path)) == NULL)
 -              {
 -                      //perror( dir_path );
 -                      //exit( 1 );
 -                      Array_of_String results;
 -                      results = new_Array_of_String();
 -                      return results;
 -              }
 -              else
 -              {
 -                      Array_of_String results;
 -                      String file_name;
 -                      struct dirent *de;
 -
 -                      results = new_Array_of_String();
 -
 -                      while ( ( de = readdir( dir ) ) != NULL )
 -                              if ( strcmp( de->d_name, ".." ) != 0 &&
 -                                      strcmp( de->d_name, "." ) != 0 )
 -                              {
 -                                      file_name = NativeString_to_s( strdup( de->d_name ) );
 -                                      Array_of_String_add( results, file_name );
 -                              }
 -
 -                      closedir( dir );
 -                      return results;
 -              }
 -      `}
 +      fun files: Array[String]
 +      do
 +              var res = new Array[String]
 +              var d = new NativeDir.opendir(to_cstring)
 +              if d.address_is_null then return res
 +
 +              loop
 +                      var de = d.readdir
 +                      if de.address_is_null then break
 +                      var name = de.to_s_with_copy
 +                      if name == "." or name == ".." then continue
 +                      res.add name
 +              end
 +              d.closedir
 +
 +              return res
 +      end
  end
  
  redef class NativeString
@@@ -1113,24 -1135,6 +1119,24 @@@ private extern class NativeFile `{ FILE
        new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
  end
  
 +# Standard `DIR*` pointer
 +private extern class NativeDir `{ DIR* `}
 +
 +      # Open a directory
 +      new opendir(path: NativeString) `{ return opendir(path); `}
 +
 +      # Close a directory
 +      fun closedir `{ closedir(recv); `}
 +
 +      # Read the next directory entry
 +      fun readdir: NativeString `{
 +              struct dirent *de;
 +              de = readdir(recv);
 +              if (!de) return NULL;
 +              return de->d_name;
 +      `}
 +end
 +
  redef class Sys
  
        # Standard input