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
61 attr _file
: NativeFile
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
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
112 private init without_file
do end
124 write_native
(s
.to_cstring
, s
.length
)
127 redef meth is_writable
do return _writable
131 var i
= _file
.io_close
135 # Is the file open in write mode
138 # Write `len' bytes from `native'.
139 private meth write_native
(native
: NativeString, len
: Int)
142 var err
= _file
.io_write
(native
, len
)
145 printn
("Problem in writing : ", err
, " ", len
, "\n")
149 # Open the file at `path' for writing.
150 init open
(path
: String)
152 _file
= io_open_write
(path
.to_cstring
)
153 assert cant_open_file
: _file
!= null
158 private init without_file
do end
161 ###############################################################################
175 _file
= native_stdout
176 _path
= "/dev/stdout"
184 _file
= native_stderr
185 _path
= "/dev/stderr"
190 ###############################################################################
193 # return true if a file with this names exists
194 meth file_exists
: Bool do return to_cstring
.file_exists
196 meth file_stat
: FileStat do return to_cstring
.file_stat
198 meth strip_extension
(ext
: String): String
200 if has_suffix
(ext
) then
201 return substring
(0, length
- ext
.length
)
206 meth basename
(ext
: String): String
208 var pos
= last_index_of_from
('/', _length
- 1)
211 n
= substring_from
(pos
+1)
213 return n
.strip_extension
(ext
)
218 var pos
= last_index_of_from
('/', _length
- 1)
220 return substring
(0, pos
)
226 meth file_path
: String
229 var pos
= last_index_of_from
('/', l
- 1)
231 return substring
(0, pos
)
236 # Create a directory (and all intermediate directories if needed)
239 var dirs
= self.split_with
("/")
240 var path
= new String
241 if dirs
.is_empty
then return
242 if dirs
[0].is_empty
then
243 # it was a starting /
247 if d
.is_empty
then continue
250 path
.to_cstring
.file_mkdir
255 redef class NativeString
256 private meth file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
257 private meth file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
258 private meth file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
263 # This class is system dependent ... must reify the vfs
264 meth mode
: Int is extern "file_FileStat_FileStat_mode_0"
265 meth atime
: Int is extern "file_FileStat_FileStat_atime_0"
266 meth ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
267 meth mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
268 meth size
: Int is extern "file_FileStat_FileStat_size_0"
271 # Instance of this class are standard FILE * pointers
272 private universal NativeFile
274 meth io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
275 meth io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
276 meth io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
277 meth file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
280 private class NativeFileCapable
281 meth io_open_read
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
282 meth io_open_write
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
283 meth native_stdin
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
284 meth native_stdout
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
285 meth native_stderr
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
289 meth stdin
: IFStream do return once
new Stdin
292 meth stdout
: OFStream do return once
new Stdout
294 # Standard output for error.
295 meth stderr
: OFStream do return once
new Stderr