1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
14 # This module handle file input and output
24 # Print `objects' on the standard output (`stdout').
25 protected meth printn
(objects
: Object...)
27 stdout
.write
(objects
.to_s
)
30 # Print an `object' on the standard output (`stdout') and add a newline.
31 protected meth print
(object
: Object)
33 if object
!= null then
34 stdout
.write
(object
.to_s
)
39 # Read a character from the standard input (`stdin').
40 protected meth getc
: Char
42 return stdin
.read_char
.ascii
45 # Read a line from the standard input (`stdin').
46 protected meth gets
: String
48 return stdin
.read_line
52 # File Abstract Stream
55 special NativeFileCapable
57 # The path of the file.
58 readable attr _path
: String = null
61 attr _file
: NativeFile = null
63 meth file_stat
: FileStat
64 do return _file
.file_stat
end
70 special BufferedIStream
76 _file
= io_open_read
(_path
.to_cstring
)
84 var i
= _file
.io_close
88 # Fill the internal read buffer. Needed by read operations.
89 redef meth fill_buffer
91 var nb
= _file
.io_read
(_buffer
._items
, _buffer
._capacity
)
101 redef readable attr _end_reached
: Bool = false
103 # Open the file at `path' for reading.
104 init open
(path
: String)
108 _file
= io_open_read
(_path
.to_cstring
)
109 assert cant_open_file
: _file
!= null
113 private init without_file
do end
125 write_native
(s
.to_cstring
, s
.length
)
128 redef meth is_writable
do return _writable
132 var i
= _file
.io_close
136 # Is the file open in write mode
139 # Write `len' bytes from `native'.
140 private meth write_native
(native
: NativeString, len
: Int)
143 var err
= _file
.io_write
(native
, len
)
146 printn
("Problem in writing : ", err
, " ", len
, "\n")
150 # Open the file at `path' for writing.
151 init open
(path
: String)
153 _file
= io_open_write
(path
.to_cstring
)
154 assert cant_open_file
: _file
!= null
160 private init without_file
do end
163 ###############################################################################
177 _file
= native_stdout
178 _path
= "/dev/stdout"
186 _file
= native_stderr
187 _path
= "/dev/stderr"
192 ###############################################################################
195 # return true if a file with this names exists
196 meth file_exists
: Bool do return to_cstring
.file_exists
198 meth file_stat
: FileStat do return to_cstring
.file_stat
200 meth strip_extension
(ext
: String): String
202 if has_suffix
(ext
) then
203 return substring
(0, length
- ext
.length
)
208 meth basename
(ext
: String): String
210 var pos
= last_index_of_from
('/', _length
- 1)
213 n
= substring_from
(pos
+1)
215 return n
.strip_extension
(ext
)
220 var pos
= last_index_of_from
('/', _length
- 1)
222 return substring
(0, pos
)
228 meth file_path
: String
231 var pos
= last_index_of_from
('/', l
- 1)
233 return substring
(0, pos
)
238 # Create a directory (and all intermediate directories if needed)
241 var dirs
= self.split_with
("/")
242 var path
= new String
243 if dirs
.is_empty
then return
244 if dirs
[0].is_empty
then
245 # it was a starting /
249 if d
.is_empty
then continue
252 path
.to_cstring
.file_mkdir
257 redef class NativeString
258 private meth file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
259 private meth file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
260 private meth file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
265 # This class is system dependent ... must reify the vfs
266 meth mode
: Int is extern "file_FileStat_FileStat_mode_0"
267 meth atime
: Int is extern "file_FileStat_FileStat_atime_0"
268 meth ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
269 meth mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
270 meth size
: Int is extern "file_FileStat_FileStat_size_0"
273 # Instance of this class are standard FILE * pointers
274 private universal NativeFile
276 meth io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
277 meth io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
278 meth io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
279 meth file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
282 private interface NativeFileCapable
283 meth io_open_read
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
284 meth io_open_write
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
285 meth native_stdin
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
286 meth native_stdout
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
287 meth native_stderr
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
291 meth stdin
: IFStream do return once
new Stdin
294 meth stdout
: OFStream do return once
new Stdout
296 # Standard output for error.
297 meth stderr
: OFStream do return once
new Stderr