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>
5 # Copyright 2008 Jean-Sébastien Gélinas <calestar@gmail.com>
7 # This file is free software, which comes along with NIT. This software is
8 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
11 # is kept unaltered, and a notification of the changes is added.
12 # You are allowed to redistribute it and sell it, alone or is a part of
15 # This module handle file input and output
25 # Print `objects' on the standard output (`stdout').
26 protected meth printn
(objects
: Object...)
28 stdout
.write
(objects
.to_s
)
31 # Print an `object' on the standard output (`stdout') and add a newline.
32 protected meth print
(object
: Object)
34 if object
!= null then
35 stdout
.write
(object
.to_s
)
40 # Read a character from the standard input (`stdin').
41 protected meth getc
: Char
43 return stdin
.read_char
.ascii
46 # Read a line from the standard input (`stdin').
47 protected meth gets
: String
49 return stdin
.read_line
53 # File Abstract Stream
56 special NativeFileCapable
58 # The path of the file.
59 readable attr _path
: String = null
62 attr _file
: NativeFile = null
64 meth file_stat
: FileStat
65 do return _file
.file_stat
end
71 special BufferedIStream
77 _file
= io_open_read
(_path
.to_cstring
)
85 var i
= _file
.io_close
89 # Fill the internal read buffer. Needed by read operations.
90 redef meth fill_buffer
92 var nb
= _file
.io_read
(_buffer
._items
, _buffer
._capacity
)
102 redef readable attr _end_reached
: Bool = false
104 # Open the file at `path' for reading.
105 init open
(path
: String)
109 _file
= io_open_read
(_path
.to_cstring
)
110 assert cant_open_file
: _file
!= null
114 private init without_file
do end
126 write_native
(s
.to_cstring
, s
.length
)
129 redef meth is_writable
do return _writable
133 var i
= _file
.io_close
137 # Is the file open in write mode
140 # Write `len' bytes from `native'.
141 private meth write_native
(native
: NativeString, len
: Int)
144 var err
= _file
.io_write
(native
, len
)
147 printn
("Problem in writing : ", err
, " ", len
, "\n")
151 # Open the file at `path' for writing.
152 init open
(path
: String)
154 _file
= io_open_write
(path
.to_cstring
)
155 assert cant_open_file
: _file
!= null
161 private init without_file
do end
164 ###############################################################################
178 _file
= native_stdout
179 _path
= "/dev/stdout"
187 _file
= native_stderr
188 _path
= "/dev/stderr"
193 ###############################################################################
196 # return true if a file with this names exists
197 meth file_exists
: Bool do return to_cstring
.file_exists
199 meth file_stat
: FileStat do return to_cstring
.file_stat
201 meth file_delete
: Bool do return to_cstring
.file_delete
203 meth strip_extension
(ext
: String): String
205 if has_suffix
(ext
) then
206 return substring
(0, length
- ext
.length
)
211 meth basename
(ext
: String): String
213 var pos
= last_index_of_from
('/', _length
- 1)
216 n
= substring_from
(pos
+1)
218 return n
.strip_extension
(ext
)
223 var pos
= last_index_of_from
('/', _length
- 1)
225 return substring
(0, pos
)
231 meth file_path
: String
234 var pos
= last_index_of_from
('/', l
- 1)
236 return substring
(0, pos
)
241 # Create a directory (and all intermediate directories if needed)
244 var dirs
= self.split_with
("/")
245 var path
= new String
246 if dirs
.is_empty
then return
247 if dirs
[0].is_empty
then
248 # it was a starting /
252 if d
.is_empty
then continue
255 path
.to_cstring
.file_mkdir
260 redef class NativeString
261 private meth file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
262 private meth file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
263 private meth file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
264 private meth file_delete
: Bool is extern "string_NativeString_NativeString_file_delete_0"
269 # This class is system dependent ... must reify the vfs
270 meth mode
: Int is extern "file_FileStat_FileStat_mode_0"
271 meth atime
: Int is extern "file_FileStat_FileStat_atime_0"
272 meth ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
273 meth mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
274 meth size
: Int is extern "file_FileStat_FileStat_size_0"
277 # Instance of this class are standard FILE * pointers
278 private universal NativeFile
280 meth io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
281 meth io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
282 meth io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
283 meth file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
286 private interface NativeFileCapable
287 meth io_open_read
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
288 meth io_open_write
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
289 meth native_stdin
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
290 meth native_stdout
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
291 meth native_stderr
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
295 meth stdin
: IFStream do return once
new Stdin
298 meth stdout
: OFStream do return once
new Stdout
300 # Standard output for error.
301 meth stderr
: OFStream do return once
new Stderr