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 #include <sys/types.h>
34 # Print `objects` on the standard output (`stdout`).
35 protected fun printn
(objects
: Object...)
37 stdout
.write
(objects
.to_s
)
40 # Print an `object` on the standard output (`stdout`) and add a newline.
41 protected fun print
(object
: Object)
43 stdout
.write
(object
.to_s
)
47 # Read a character from the standard input (`stdin`).
48 protected fun getc
: Char
50 return stdin
.read_char
.ascii
53 # Read a line from the standard input (`stdin`).
54 protected fun gets
: String
56 return stdin
.read_line
59 # Return the working (current) directory
60 protected fun getcwd
: String do return file_getcwd
.to_s
61 private fun file_getcwd
: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"
64 # File Abstract Stream
65 abstract class FStream
67 # The path of the file.
68 readable var _path
: nullable String = null
71 var _file
: nullable NativeFile = null
73 fun file_stat
: FileStat
74 do return _file
.file_stat
end
83 # Open the same file again.
84 # The original path is reused, therefore the reopened file can be a different file.
88 _file
= new NativeFile.io_open_read
(_path
.to_cstring
)
96 var i
= _file
.io_close
100 redef fun fill_buffer
102 var nb
= _file
.io_read
(_buffer
._items
, _buffer
._capacity
)
112 redef readable var _end_reached
: Bool = false
114 # Open the file at `path` for reading.
115 init open
(path
: String)
119 _file
= new NativeFile.io_open_read
(_path
.to_cstring
)
120 assert cant_open_file
: _file
!= null
124 private init without_file
do end
135 write_native
(s
.to_cstring
, s
.length
)
138 redef fun is_writable
do return _writable
142 var i
= _file
.io_close
146 # Is the file open in write mode
149 # Write `len` bytes from `native`.
150 private fun write_native
(native
: NativeString, len
: Int)
153 var err
= _file
.io_write
(native
, len
)
156 printn
("Problem in writing : ", err
, " ", len
, "\n")
160 # Open the file at `path` for writing.
161 init open
(path
: String)
163 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
164 assert cant_open_file
: _file
!= null
170 private init without_file
do end
173 ###############################################################################
178 _file
= new NativeFile.native_stdin
183 # Is these something to read? (non blocking)
184 # FIXME: should be generalized
185 fun poll_in
: Bool is extern "file_stdin_poll_in"
191 _file
= new NativeFile.native_stdout
192 _path
= "/dev/stdout"
200 _file
= new NativeFile.native_stderr
201 _path
= "/dev/stderr"
206 ###############################################################################
209 # return true if a file with this names exists
210 fun file_exists
: Bool do return to_cstring
.file_exists
212 fun file_stat
: FileStat do return to_cstring
.file_stat
213 fun file_lstat
: FileStat do return to_cstring
.file_lstat
215 # Remove a file, return true if success
216 fun file_delete
: Bool do return to_cstring
.file_delete
218 # remove the trailing extension "ext"
219 fun strip_extension
(ext
: String): String
221 if has_suffix
(ext
) then
222 return substring
(0, length
- ext
.length
)
227 # Extract the basename of a path and remove the extension
228 fun basename
(ext
: String): String
230 var pos
= last_index_of_from
('/', _length
- 1)
233 n
= substring_from
(pos
+1)
235 return n
.strip_extension
(ext
)
238 # Extract the dirname of a path
240 # assert "/path/to/a_file.ext".dirname == "/path/to"
241 # assert "path/to/a_file.ext".dirname == "path/to"
242 # assert "path/to".dirname == "path"
243 # assert "path/to/".dirname == "path"
244 # assert "path".dirname == "."
245 # assert "/path".dirname == "/"
246 # assert "/".dirname == "/"
247 # assert "".dirname == "."
250 var l
= _length
- 1 # Index of the last char
251 if l
> 0 and self.chars
[l
] == '/' then l
-= 1 # remove trailing `/`
252 var pos
= last_index_of_from
('/', l
)
254 return substring
(0, pos
)
255 else if pos
== 0 then
262 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
263 fun realpath
: String do
264 var cs
= to_cstring
.file_realpath
265 var res
= cs
.to_s_with_copy
266 # cs.free_malloc # FIXME memory leak
270 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
271 # ".." are not resolved if they start the path
272 # starting "/" is not removed
273 # trainling "/" is removed
275 # Note that the method only wonrk on the string:
276 # * no I/O access is performed
277 # * the validity of the path is not checked
279 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
280 # assert "../dir/file".simplify_path == "../dir/file"
281 # assert "dir/../../".simplify_path == ".."
282 # assert "dir/..".simplify_path == "."
283 # assert "//absolute//path/".simplify_path == "/absolute/path"
284 fun simplify_path
: String
286 var a
= self.split_with
("/")
287 var a2
= new Array[String]
289 if x
== "." then continue
290 if x
== "" and not a2
.is_empty
then continue
291 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
297 if a2
.is_empty
then return "."
301 # Correctly join two path using the directory separator.
303 # Using a standard "{self}/{path}" does not work when `self` is the empty string.
304 # This method ensure that the join is valid.
306 # assert "hello".join_path("world") == "hello/world"
307 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
308 # assert "".join_path("world") == "world"
309 # assert "/hello".join_path("/world") == "/world"
311 # Note: you may want to use `simplify_path` on the result
313 # Note: I you want to join a great number of path, you can write
315 # [p1, p2, p3, p4].join("/")
316 fun join_path
(path
: String): String
318 if path
.is_empty
then return self
319 if self.is_empty
then return path
320 if path
.chars
[0] == '/' then return path
321 return "{self}/{path}"
324 # Create a directory (and all intermediate directories if needed)
327 var dirs
= self.split_with
("/")
328 var path
= new Buffer
329 if dirs
.is_empty
then return
330 if dirs
[0].is_empty
then
331 # it was a starting /
335 if d
.is_empty
then continue
338 path
.to_s
.to_cstring
.file_mkdir
342 # Change the current working directory
345 # assert getcwd == "/etc"
347 # assert getcwd == "/"
350 fun chdir
do to_cstring
.file_chdir
352 # Return right-most extension (without the dot)
353 fun file_extension
: nullable String
355 var last_slash
= last_index_of
('.')
356 if last_slash
>= 0 then
357 return substring
( last_slash
+1, length
)
363 # returns files contained within the directory represented by self
364 fun files
: Set[ String ] is extern import HashSet[String], HashSet[String].add
, NativeString.to_s
, String.to_cstring
, HashSet[String].as(Set[String]) `{
368 dir_path = String_to_cstring( recv );
369 if ((dir = opendir(dir_path)) == NULL)
376 HashSet_of_String results;
380 results = new_HashSet_of_String();
382 while ( ( de = readdir( dir ) ) != NULL )
383 if ( strcmp( de->d_name, ".." ) != 0 &&
384 strcmp( de->d_name, "." ) != 0 )
386 file_name = NativeString_to_s( strdup( de->d_name ) );
387 HashSet_of_String_add( results, file_name );
391 return HashSet_of_String_as_Set_of_String( results );
396 redef class NativeString
397 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
398 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
399 private fun file_lstat
: FileStat `{
400 struct stat* stat_element;
402 stat_element = malloc(sizeof(struct stat));
403 res = lstat(recv, stat_element);
404 if (res == -1) return NULL;
407 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
408 private fun file_delete
: Bool is extern "string_NativeString_NativeString_file_delete_0"
409 private fun file_chdir
is extern "string_NativeString_NativeString_file_chdir_0"
410 private fun file_realpath
: NativeString is extern "file_NativeString_realpath"
413 extern FileStat `{ struct stat * `}
414 # This class is system dependent ... must reify the vfs
415 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
416 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
417 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
418 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
419 fun size: Int is extern "file_FileStat_FileStat_size_0"
421 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
422 fun is_dir
: Bool `{ return S_ISDIR(recv->st_mode); `}
423 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
424 fun is_blk
: Bool `{ return S_ISBLK(recv->st_mode); `}
425 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
426 fun is_lnk
: Bool `{ return S_ISLNK(recv->st_mode); `}
427 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
430 # Instance of this class are standard FILE * pointers
431 private extern NativeFile
432 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
433 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
434 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
435 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
437 new io_open_read
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
438 new io_open_write
(path
: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
439 new native_stdin
is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
440 new native_stdout
is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
441 new native_stderr
is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
445 fun stdin
: Stdin do return once
new Stdin
448 fun stdout
: OFStream do return once
new Stdout
450 # Standard output for error.
451 fun stderr
: OFStream do return once
new Stderr