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
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)
# ~~~
#
# 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
# 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
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