1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Encapsulates all primitive data types of nit
13 # Ensures that the use in the interpreter is independant of the
14 # underlying implementation and that the services are semantically correct.
15 module primitive_types
17 intrude import core
::file
18 intrude import core
::text
::flat
20 # Wrapper for `NativeFile`
21 class PrimitiveNativeFile
37 init io_open_read
(path
: String) do
38 init(new FileReader.open
(path
.to_s
))
41 init io_open_write
(path
: String) do
42 init(new FileWriter.open
(path
.to_s
))
45 fun address_is_null
: Bool do
46 if file
isa FileStream then return file
.as(FileStream)._file
.address_is_null
50 fun io_read
(buf
: NativeString, len
: Int): Int do
51 if file
isa FileStream then return file
.as(FileStream)._file
.io_read
(buf
, len
)
52 var str
= file
.as(Reader).read
(len
)
53 str
.to_cstring
.copy_to
(buf
, str
.length
, 0, 0)
57 fun io_write
(buf
: NativeString, from
, len
: Int): Int do
58 if file
isa FileStream then return file
.as(FileStream)._file
.io_write
(buf
, from
, len
)
59 file
.as(Writer).write
(buf
.to_s_with_length
(len
).substring_from
(from
))
64 if file
isa FileStream then return file
.as(FileStream)._file
.io_close
70 if file
isa FileStream then return file
.as(FileStream)._file
.fileno
75 if file
isa FileStream then return file
.as(FileStream)._file
.flush
79 fun set_buffering_type
(size
, mode
: Int): Int do
80 if file
isa FileStream then return file
.as(FileStream)._file
.set_buffering_type
(size
, mode
)