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 fun printn
(objects
: Object...)
28 stdout
.write
(objects
.to_s
)
31 # Print an `object' on the standard output (`stdout') and add a newline.
32 protected fun print
(object
: Object)
34 stdout
.write
(object
.to_s
)
38 # Read a character from the standard input (`stdin').
39 protected fun getc
: Char
41 return stdin
.read_char
.ascii
44 # Read a line from the standard input (`stdin').
45 protected fun gets
: String
47 return stdin
.read_line
51 # File Abstract Stream
54 # The path of the file.
55 readable var _path
: nullable String = null
58 var _file
: nullable NativeFile = null
60 fun file_stat
: FileStat
61 do return _file
.file_stat
end
70 # Open the same file again.
71 # The original path is reused, therefore the reopened file can be a different file.
75 _file
= new NativeFile.io_open_read
(_path
.to_cstring
)
83 var i
= _file
.io_close
89 var nb
= _file
.io_read
(_buffer
._items
, _buffer
._capacity
)
99 redef readable var _end_reached
: Bool = false
101 # Open the file at `path' for reading.
102 init open
(path
: String)
106 _file
= new NativeFile.io_open_read
(_path
.to_cstring
)
107 assert cant_open_file
: _file
!= null
111 private init without_file
do end
122 write_native
(s
.to_cstring
, s
.length
)
125 redef fun is_writable
do return _writable
129 var i
= _file
.io_close
133 # Is the file open in write mode
136 # Write `len' bytes from `native'.
137 private fun write_native
(native
: NativeString, len
: Int)
140 var err
= _file
.io_write
(native
, len
)
143 printn
("Problem in writing : ", err
, " ", len
, "\n")
147 # Open the file at `path' for writing.
148 init open
(path
: String)
150 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
151 assert cant_open_file
: _file
!= null
157 private init without_file
do end
160 ###############################################################################
165 _file
= new NativeFile.native_stdin
174 _file
= new NativeFile.native_stdout
175 _path
= "/dev/stdout"
183 _file
= new NativeFile.native_stderr
184 _path
= "/dev/stderr"
189 ###############################################################################
192 # return true if a file with this names exists
193 fun file_exists
: Bool do return to_cstring
.file_exists
195 fun file_stat
: FileStat do return to_cstring
.file_stat
197 # Remove a file, return true if success
198 fun file_delete
: Bool do return to_cstring
.file_delete
200 # remove the trailing extension "ext"
201 fun strip_extension
(ext
: String): String
203 if has_suffix
(ext
) then
204 return substring
(0, length
- ext
.length
)
209 # Extract the basename of a path and remove the extension
210 fun basename
(ext
: String): String
212 var pos
= last_index_of_from
('/', _length
- 1)
215 n
= substring_from
(pos
+1)
217 return n
.strip_extension
(ext
)
220 # Extract the dirname of a path
223 var pos
= last_index_of_from
('/', _length
- 1)
225 return substring
(0, pos
)
231 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
232 # ".." are not resolved if they start the path
233 # starting "/" is not removed
234 # trainling "/" is removed
236 # Note that the method only wonrk on the string:
237 # * no I/O access is performed
238 # * the validity of the path is not checked
240 # "some/./complex/../../path/from/../to/a////file//".simplify_path # -> "path/to/a/file"
241 # "../dir/file" # -> "../dir/file"
242 # "dir/../../" # -> ".."
243 # "//absolute//path/" # -> "/absolute/path"
244 fun simplify_path
: String
246 var a
= self.split_with
("/")
247 var a2
= new Array[String]
249 if x
== "." then continue
250 if x
== "" and not a2
.is_empty
then continue
251 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
260 # Correctly join two path using the directory separator.
262 # Using a standard "{self}/{path}" does not work when `self' is the empty string.
263 # This method ensure that the join is valid.
265 # "hello".join_path("world") # -> "hello/world"
266 # "hel/lo".join_path("wor/ld") # -> "hel/lo/wor/ld"
267 # "".join_path("world") # -> "world"
268 # "/hello".join_path("/world") # -> "/world"
270 # Note: you may want to use `simplify_path' on the result
272 # Note: I you want to join a great number of path, you can write
274 # [p1, p2, p3, p4].join("/")
275 fun join_path
(path
: String): String
277 if path
.is_empty
then return self
278 if self.is_empty
then return path
279 if path
[0] == '/' then return path
280 return "{self}/{path}"
283 # Create a directory (and all intermediate directories if needed)
286 var dirs
= self.split_with
("/")
287 var path
= new Buffer
288 if dirs
.is_empty
then return
289 if dirs
[0].is_empty
then
290 # it was a starting /
294 if d
.is_empty
then continue
297 path
.to_s
.to_cstring
.file_mkdir
301 # Return right-most extension (without the dot)
302 fun file_extension
: nullable String
304 var last_slash
= last_index_of
('.')
305 if last_slash
>= 0 then
306 return substring
( last_slash
+1, length
)
312 # returns files contained within the directory represented by self
313 fun files
: Set[ String ] is extern import HashSet, HashSet::add
, String::from_cstring
, String::to_cstring
, HashSet[String] as( Set[String] ), String as( Object )
316 redef class NativeString
317 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
318 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
319 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
320 private fun file_delete
: Bool is extern "string_NativeString_NativeString_file_delete_0"
324 # This class is system dependent ... must reify the vfs
325 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
326 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
327 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
328 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
329 fun size
: Int is extern "file_FileStat_FileStat_size_0"
332 # Instance of this class are standard FILE * pointers
333 private extern NativeFile
334 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
335 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
336 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
337 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
339 new io_open_read
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
340 new io_open_write
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
341 new native_stdin
is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
342 new native_stdout
is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
343 new native_stderr
is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
347 fun stdin
: IFStream do return once
new Stdin
350 fun stdout
: OFStream do return once
new Stdout
352 # Standard output for error.
353 fun stderr
: OFStream do return once
new Stderr