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 # File manipulations (create, read, write, etc.)
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
52 abstract class FStream
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
170 # Is these something to read? (non blocking)
171 # FIXME: should be generalized
172 fun poll_in
: Bool is extern "file_stdin_poll_in"
178 _file
= new NativeFile.native_stdout
179 _path
= "/dev/stdout"
187 _file
= new NativeFile.native_stderr
188 _path
= "/dev/stderr"
193 ###############################################################################
196 # return true if a file with this names exists
197 fun file_exists
: Bool do return to_cstring
.file_exists
199 fun file_stat
: FileStat do return to_cstring
.file_stat
201 # Remove a file, return true if success
202 fun file_delete
: Bool do return to_cstring
.file_delete
204 # remove the trailing extension "ext"
205 fun strip_extension
(ext
: String): String
207 if has_suffix
(ext
) then
208 return substring
(0, length
- ext
.length
)
213 # Extract the basename of a path and remove the extension
214 fun basename
(ext
: String): String
216 var pos
= last_index_of_from
('/', _length
- 1)
219 n
= substring_from
(pos
+1)
221 return n
.strip_extension
(ext
)
224 # Extract the dirname of a path
227 var pos
= last_index_of_from
('/', _length
- 1)
229 return substring
(0, pos
)
235 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
236 # ".." are not resolved if they start the path
237 # starting "/" is not removed
238 # trainling "/" is removed
240 # Note that the method only wonrk on the string:
241 # * no I/O access is performed
242 # * the validity of the path is not checked
244 # "some/./complex/../../path/from/../to/a////file//".simplify_path # -> "path/to/a/file"
245 # "../dir/file" # -> "../dir/file"
246 # "dir/../../" # -> ".."
247 # "//absolute//path/" # -> "/absolute/path"
248 fun simplify_path
: String
250 var a
= self.split_with
("/")
251 var a2
= new Array[String]
253 if x
== "." then continue
254 if x
== "" and not a2
.is_empty
then continue
255 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
264 # Correctly join two path using the directory separator.
266 # Using a standard "{self}/{path}" does not work when `self' is the empty string.
267 # This method ensure that the join is valid.
269 # "hello".join_path("world") # -> "hello/world"
270 # "hel/lo".join_path("wor/ld") # -> "hel/lo/wor/ld"
271 # "".join_path("world") # -> "world"
272 # "/hello".join_path("/world") # -> "/world"
274 # Note: you may want to use `simplify_path' on the result
276 # Note: I you want to join a great number of path, you can write
278 # [p1, p2, p3, p4].join("/")
279 fun join_path
(path
: String): String
281 if path
.is_empty
then return self
282 if self.is_empty
then return path
283 if path
[0] == '/' then return path
284 return "{self}/{path}"
287 # Create a directory (and all intermediate directories if needed)
290 var dirs
= self.split_with
("/")
291 var path
= new Buffer
292 if dirs
.is_empty
then return
293 if dirs
[0].is_empty
then
294 # it was a starting /
298 if d
.is_empty
then continue
301 path
.to_s
.to_cstring
.file_mkdir
305 # Return right-most extension (without the dot)
306 fun file_extension
: nullable String
308 var last_slash
= last_index_of
('.')
309 if last_slash
>= 0 then
310 return substring
( last_slash
+1, length
)
316 # returns files contained within the directory represented by self
317 fun files
: Set[ String ] is extern import HashSet, HashSet::add
, String::from_cstring
, String::to_cstring
, HashSet[String] as( Set[String] ), String as( Object )
320 redef class NativeString
321 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
322 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
323 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
324 private fun file_delete
: Bool is extern "string_NativeString_NativeString_file_delete_0"
328 # This class is system dependent ... must reify the vfs
329 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
330 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
331 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
332 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
333 fun size
: Int is extern "file_FileStat_FileStat_size_0"
336 # Instance of this class are standard FILE * pointers
337 private extern NativeFile
338 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
339 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
340 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
341 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
343 new io_open_read
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
344 new io_open_write
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
345 new native_stdin
is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
346 new native_stdout
is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
347 new native_stderr
is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
351 fun stdin
: Stdin do return once
new Stdin
354 fun stdout
: OFStream do return once
new Stdout
356 # Standard output for error.
357 fun stderr
: OFStream do return once
new Stderr