*: update all clients of the `CString::to_s` services
[nit.git] / src / interpreter / primitive_types.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
9 # another product.
10
11 # Encapsulates all primitive data types of nit
12 #
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
16
17 intrude import core::file
18 intrude import core::text::flat
19
20 # Wrapper for `NativeFile`
21 class PrimitiveNativeFile
22
23 var file: Stream
24
25 init native_stdin do
26 init(sys.stdin)
27 end
28
29 init native_stdout do
30 init(sys.stdout)
31 end
32
33 init native_stderr do
34 init(sys.stderr)
35 end
36
37 init io_open_read(path: String) do
38 init(new FileReader.open(path.to_s))
39 end
40
41 init io_open_write(path: String) do
42 init(new FileWriter.open(path.to_s))
43 end
44
45 fun address_is_null: Bool do
46 if file isa FileStream then return file.as(FileStream)._file.address_is_null
47 return false
48 end
49
50 fun io_read(buf: CString, 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)
54 return str.length
55 end
56
57 fun io_write(buf: CString, 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_unsafe(len, copy=false).substring_from(from))
60 return len
61 end
62
63 fun io_close: Int do
64 if file isa FileStream then return file.as(FileStream)._file.io_close
65 file.close
66 return 0
67 end
68
69 fun fileno: Int do
70 if file isa FileStream then return file.as(FileStream)._file.fileno
71 return 0
72 end
73
74 fun flush: Int do
75 if file isa FileStream then return file.as(FileStream)._file.flush
76 return 0
77 end
78
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)
81 return 0
82 end
83 end