From: Alexis Laferrière Date: Tue, 16 Jun 2015 20:25:04 +0000 (-0400) Subject: lib/file: move macros to the light FFI X-Git-Tag: v0.7.6~19^2~10 X-Git-Url: http://nitlanguage.org lib/file: move macros to the light FFI Signed-off-by: Alexis Laferrière --- diff --git a/lib/standard/file.nit b/lib/standard/file.nit index f786f6d..efdf46e 100644 --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@ -272,7 +272,9 @@ redef class Int # Creates a file stream from a file descriptor `fd` using the file access `mode`. # # NOTE: The `mode` specified must be compatible with the one used in the file descriptor. - private fun fd_to_stream(mode: NativeString): NativeFile is extern "file_int_fdtostream" + private fun fd_to_stream(mode: NativeString): NativeFile `{ + return fdopen(self, mode); + `} end # Constant for read-only file streams @@ -1097,25 +1099,37 @@ redef class NativeString if (res == -1) return NULL; return stat_element; `} - private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0" + + private fun file_mkdir: Bool `{ return !mkdir(self, 0777); `} + private fun rmdir: Bool `{ return !rmdir(self); `} - private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0" - private fun file_chdir: Bool is extern "string_NativeString_NativeString_file_chdir_0" - private fun file_realpath: NativeString is extern "file_NativeString_realpath" + + private fun file_delete: Bool `{ + return remove(self) == 0; + `} + + private fun file_chdir: Bool `{ return !chdir(self); `} + + private fun file_realpath: NativeString `{ return realpath(self, NULL); `} end # This class is system dependent ... must reify the vfs private extern class NativeFileStat `{ struct stat * `} + # Returns the permission bits of file - fun mode: Int is extern "file_FileStat_FileStat_mode_0" + fun mode: Int `{ return self->st_mode; `} + # Returns the last access time - fun atime: Int is extern "file_FileStat_FileStat_atime_0" + fun atime: Int `{ return self->st_atime; `} + # Returns the last status change time - fun ctime: Int is extern "file_FileStat_FileStat_ctime_0" + fun ctime: Int `{ return self->st_ctime; `} + # Returns the last modification time - fun mtime: Int is extern "file_FileStat_FileStat_mtime_0" + fun mtime: Int `{ return self->st_mtime; `} + # Returns the size - fun size: Int is extern "file_FileStat_FileStat_size_0" + fun size: Int `{ return self->st_size; `} # Returns true if it is a regular file (not a device file, pipe, sockect, ...) fun is_reg: Bool `{ return S_ISREG(self->st_mode); `} @@ -1141,13 +1155,21 @@ end # Instance of this class are standard FILE * pointers private extern class NativeFile `{ FILE* `} - fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2" - fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2" + fun io_read(buf: NativeString, len: Int): Int `{ + return fread(buf, 1, len, self); + `} + + fun io_write(buf: NativeString, len: Int): Int `{ + return fwrite(buf, 1, len, self); + `} + fun write_byte(value: Int): Int `{ unsigned char b = (unsigned char)value; return fwrite(&b, 1, 1, self); `} - fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0" + + fun io_close: Int `{ return fclose(self); `} + fun file_stat: NativeFileStat `{ struct stat buff; if(fstat(fileno(self), &buff) != -1) { @@ -1161,15 +1183,22 @@ private extern class NativeFile `{ FILE* `} fun fileno: Int `{ return fileno(self); `} # Flushes the buffer, forcing the write operation - fun flush: Int is extern "fflush" + 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 is extern "file_NativeFile_NativeFile_set_buffering_type_0" + fun set_buffering_type(buf_length: Int, mode: Int): Int `{ + return setvbuf(self, NULL, mode, buf_length); + `} + + new io_open_read(path: NativeString) `{ return fopen(path, "r"); `} - 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" - new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0" - new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0" - new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0" + new io_open_write(path: NativeString) `{ return fopen(path, "w"); `} + + new native_stdin `{ return stdin; `} + + new native_stdout `{ return stdout; `} + + new native_stderr `{ return stderr; `} end # Standard `DIR*` pointer @@ -1202,11 +1231,13 @@ redef class Sys var stderr: Writer = new Stderr is protected writable, lazy # Enumeration for buffer mode full (flushes when buffer is full) - fun buffer_mode_full: Int is extern "file_Sys_Sys_buffer_mode_full_0" + fun buffer_mode_full: Int `{ return _IOFBF; `} + # Enumeration for buffer mode line (flushes when a `\n` is encountered) - fun buffer_mode_line: Int is extern "file_Sys_Sys_buffer_mode_line_0" + fun buffer_mode_line: Int `{ return _IONBF; `} + # Enumeration for buffer mode none (flushes ASAP when something is written) - fun buffer_mode_none: Int is extern "file_Sys_Sys_buffer_mode_none_0" + fun buffer_mode_none: Int `{ return _IOLBF; `} # returns first available stream to read or write to # return null on interruption (possibly a signal) @@ -1321,5 +1352,6 @@ do end # Return the working (current) directory -fun getcwd: String do return file_getcwd.to_s -private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0" +fun getcwd: String do return native_getcwd.to_s + +private fun native_getcwd: NativeString `{ return getcwd(NULL, 0); `} diff --git a/lib/standard/file_nit.h b/lib/standard/file_nit.h deleted file mode 100644 index d86b2df..0000000 --- a/lib/standard/file_nit.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef __FILE_H -#define __FILE_H -/* This file is part of NIT ( http://www.nitlanguage.org ). - * - * Copyright 2004-2008 Jean Privat - * Copyright 2008 Floréal Morandat - * Copyright 2008 Jean-Sébastien Gélinas - * - * This file is free software, which comes along with NIT. This software is - * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A - * PARTICULAR PURPOSE. You can modify it is you want, provided this header - * is kept unaltered, and a notification of the changes is added. - * You are allowed to redistribute it and sell it, alone or is a part of - * another product. - */ - -#include -#include -#include -#include - -extern int string_NativeString_NativeString_file_exists_0(char *f); -extern void *string_NativeString_NativeString_file_stat_0(char *f); -extern void *file_NativeFile_NativeFile_file_stat_0(FILE *f); -extern int string_NativeString_NativeString_file_delete_0(char *f); -FILE* file_int_fdtostream(int fd, char* mode); -int file_NativeFile_NativeFile_set_buffering_type_0(FILE* f, int buf_sz, int mode); - -#define file_NativeFile_NativeFile_io_read_2(p, b, l) fread((b), 1, (l), (FILE*)(p)) -#define file_NativeFile_NativeFile_io_write_2(p, b, l) fwrite((b), 1, (l), (FILE*)(p)) -#define file_NativeFile_NativeFile_io_close_0(self) fclose((FILE*)(self)) - -#define file_NativeFileCapable_NativeFileCapable_io_open_read_1(p0) fopen((p0), "r") - -#define file_NativeFileCapable_NativeFileCapable_io_open_write_1(p0) fopen((p0), "w") -#define file_NativeFileCapable_NativeFileCapable_native_stdin_0() stdin -#define file_NativeFileCapable_NativeFileCapable_native_stdout_0() stdout -#define file_NativeFileCapable_NativeFileCapable_native_stderr_0() stderr -#define file_FileStat_FileStat_mode_0(self) (((struct stat*)self)->st_mode) -#define file_FileStat_FileStat_atime_0(self) (((struct stat*)self)->st_atime) -#define file_FileStat_FileStat_ctime_0(self) (((struct stat*)self)->st_ctime) -#define file_FileStat_FileStat_mtime_0(self) (((struct stat*)self)->st_mtime) -#define file_FileStat_FileStat_size_0(self) (((struct stat*)self)->st_size) -#define file_Sys_Sys_buffer_mode_full_0(self) _IOFBF -#define file_Sys_Sys_buffer_mode_line_0(self) _IOLBF -#define file_Sys_Sys_buffer_mode_none_0(self) _IONBF - -#define string_NativeString_NativeString_file_mkdir_0(p) (!mkdir(p, 0777)) -#define string_NativeString_NativeString_file_getcwd_0(p) (getcwd(NULL, 0)) -#define string_NativeString_NativeString_file_chdir_0(p) (!chdir(p)) -#define file_NativeString_realpath(p) (realpath(p, NULL)) - -#define file_stdin_poll_in(self) file_stdin_poll_in_() -int file_stdin_poll_in_(void); -#endif -