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.)
26 # Print `objects` on the standard output (`stdout`).
27 protected fun printn
(objects
: Object...)
29 stdout
.write
(objects
.to_s
)
32 # Print an `object` on the standard output (`stdout`) and add a newline.
33 protected fun print
(object
: Object)
35 stdout
.write
(object
.to_s
)
39 # Read a character from the standard input (`stdin`).
40 protected fun getc
: Char
42 return stdin
.read_char
.ascii
45 # Read a line from the standard input (`stdin`).
46 protected fun gets
: String
48 return stdin
.read_line
52 # File Abstract Stream
53 abstract class FStream
55 # The path of the file.
56 readable var _path
: nullable String = null
59 var _file
: nullable NativeFile = null
61 fun file_stat
: FileStat
62 do return _file
.file_stat
end
71 # Open the same file again.
72 # The original path is reused, therefore the reopened file can be a different file.
76 _file
= new NativeFile.io_open_read
(_path
.to_cstring
)
84 var i
= _file
.io_close
90 var nb
= _file
.io_read
(_buffer
._items
, _buffer
._capacity
)
100 redef readable var _end_reached
: Bool = false
102 # Open the file at `path` for reading.
103 init open
(path
: String)
107 _file
= new NativeFile.io_open_read
(_path
.to_cstring
)
108 assert cant_open_file
: _file
!= null
112 private init without_file
do end
123 write_native
(s
.to_cstring
, s
.length
)
126 redef fun is_writable
do return _writable
130 var i
= _file
.io_close
134 # Is the file open in write mode
137 # Write `len` bytes from `native`.
138 private fun write_native
(native
: NativeString, len
: Int)
141 var err
= _file
.io_write
(native
, len
)
144 printn
("Problem in writing : ", err
, " ", len
, "\n")
148 # Open the file at `path` for writing.
149 init open
(path
: String)
151 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
152 assert cant_open_file
: _file
!= null
158 private init without_file
do end
161 ###############################################################################
166 _file
= new NativeFile.native_stdin
171 # Is these something to read? (non blocking)
172 # FIXME: should be generalized
173 fun poll_in
: Bool is extern "file_stdin_poll_in"
179 _file
= new NativeFile.native_stdout
180 _path
= "/dev/stdout"
188 _file
= new NativeFile.native_stderr
189 _path
= "/dev/stderr"
194 ###############################################################################
197 # return true if a file with this names exists
198 fun file_exists
: Bool do return to_cstring
.file_exists
200 fun file_stat
: FileStat do return to_cstring
.file_stat
201 fun file_lstat
: FileStat do return to_cstring
.file_lstat
203 # Remove a file, return true if success
204 fun file_delete
: Bool do return to_cstring
.file_delete
206 # remove the trailing extension "ext"
207 fun strip_extension
(ext
: String): String
209 if has_suffix
(ext
) then
210 return substring
(0, length
- ext
.length
)
215 # Extract the basename of a path and remove the extension
216 fun basename
(ext
: String): String
218 var pos
= last_index_of_from
('/', _length
- 1)
221 n
= substring_from
(pos
+1)
223 return n
.strip_extension
(ext
)
226 # Extract the dirname of a path
229 var pos
= last_index_of_from
('/', _length
- 1)
231 return substring
(0, pos
)
237 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
238 # ".." are not resolved if they start the path
239 # starting "/" is not removed
240 # trainling "/" is removed
242 # Note that the method only wonrk on the string:
243 # * no I/O access is performed
244 # * the validity of the path is not checked
246 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
247 # assert "../dir/file".simplify_path == "../dir/file"
248 # assert "dir/../../".simplify_path == ".."
249 # assert "//absolute//path/".simplify_path == "/absolute/path"
250 fun simplify_path
: String
252 var a
= self.split_with
("/")
253 var a2
= new Array[String]
255 if x
== "." then continue
256 if x
== "" and not a2
.is_empty
then continue
257 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
266 # Correctly join two path using the directory separator.
268 # Using a standard "{self}/{path}" does not work when `self` is the empty string.
269 # This method ensure that the join is valid.
271 # assert "hello".join_path("world") == "hello/world"
272 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
273 # assert "".join_path("world") == "world"
274 # assert "/hello".join_path("/world") == "/world"
276 # Note: you may want to use `simplify_path` on the result
278 # Note: I you want to join a great number of path, you can write
280 # [p1, p2, p3, p4].join("/")
281 fun join_path
(path
: String): String
283 if path
.is_empty
then return self
284 if self.is_empty
then return path
285 if path
[0] == '/' then return path
286 return "{self}/{path}"
289 # Create a directory (and all intermediate directories if needed)
292 var dirs
= self.split_with
("/")
293 var path
= new Buffer
294 if dirs
.is_empty
then return
295 if dirs
[0].is_empty
then
296 # it was a starting /
300 if d
.is_empty
then continue
303 path
.to_s
.to_cstring
.file_mkdir
307 # Return right-most extension (without the dot)
308 fun file_extension
: nullable String
310 var last_slash
= last_index_of
('.')
311 if last_slash
>= 0 then
312 return substring
( last_slash
+1, length
)
318 # returns files contained within the directory represented by self
319 fun files
: Set[ String ] is extern import HashSet, HashSet::add
, String::from_cstring
, String::to_cstring
, HashSet[String] as( Set[String] ), String as( Object )
322 redef class NativeString
323 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
324 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
325 private fun file_lstat
: FileStat `{
326 struct stat* stat_element;
328 stat_element = malloc(sizeof(struct stat));
329 res = lstat(recv, stat_element);
330 if (res == -1) return NULL;
333 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
334 private fun file_delete
: Bool is extern "string_NativeString_NativeString_file_delete_0"
337 extern FileStat `{ struct stat * `}
338 # This class is system dependent ... must reify the vfs
339 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
340 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
341 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
342 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
343 fun size: Int is extern "file_FileStat_FileStat_size_0"
345 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
346 fun is_dir
: Bool `{ return S_ISDIR(recv->st_mode); `}
347 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
348 fun is_blk
: Bool `{ return S_ISBLK(recv->st_mode); `}
349 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
350 fun is_lnk
: Bool `{ return S_ISLNK(recv->st_mode); `}
351 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
354 # Instance of this class are standard FILE * pointers
355 private extern NativeFile
356 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
357 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
358 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
359 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
361 new io_open_read
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
362 new io_open_write
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
363 new native_stdin
is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
364 new native_stdout
is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
365 new native_stderr
is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
369 fun stdin
: Stdin do return once
new Stdin
372 fun stdout
: OFStream do return once
new Stdout
374 # Standard output for error.
375 fun stderr
: OFStream do return once
new Stderr