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 super NativeFileCapable
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
= 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
= 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
= io_open_write
(path
.to_cstring
)
152 assert cant_open_file
: _file
!= null
158 private init without_file
do end
161 ###############################################################################
175 _file
= native_stdout
176 _path
= "/dev/stdout"
184 _file
= native_stderr
185 _path
= "/dev/stderr"
190 ###############################################################################
193 # return true if a file with this names exists
194 fun file_exists
: Bool do return to_cstring
.file_exists
196 fun file_stat
: FileStat do return to_cstring
.file_stat
198 # Remove a file, return true if success
199 fun file_delete
: Bool do return to_cstring
.file_delete
201 # remove the trailing extension "ext"
202 fun strip_extension
(ext
: String): String
204 if has_suffix
(ext
) then
205 return substring
(0, length
- ext
.length
)
210 # Extract the basename of a path and remove the extension
211 fun 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
)
221 # Extract the dirname of a path
224 var pos
= last_index_of_from
('/', _length
- 1)
226 return substring
(0, pos
)
232 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
233 # ".." are not resolved if they start the path
234 # starting "/" is not removed
235 # trainling "/" is removed
237 # Note that the method only wonrk on the string:
238 # * no I/O access is performed
239 # * the validity of the path is not checked
241 # "some/./complex/../../path/from/../to/a////file//".simplify_path # -> "path/to/a/file"
242 # "../dir/file" # -> "../dir/file"
243 # "dir/../../" # -> ".."
244 # "//absolute//path/" # -> "/absolute/path"
245 fun simplify_path
: String
247 var a
= self.split_with
("/")
248 var a2
= new Array[String]
250 if x
== "." then continue
251 if x
== "" and not a2
.is_empty
then continue
252 if x
== ".." and not a2
.is_empty
then
261 # Create a directory (and all intermediate directories if needed)
264 var dirs
= self.split_with
("/")
265 var path
= new Buffer
266 if dirs
.is_empty
then return
267 if dirs
[0].is_empty
then
268 # it was a starting /
272 if d
.is_empty
then continue
275 path
.to_s
.to_cstring
.file_mkdir
279 # Return right-most extension (without the dot)
280 fun file_extension
: nullable String
282 var last_slash
= last_index_of
('.')
283 if last_slash
>= 0 then
284 return substring
( last_slash
+1, length
)
290 # returns files contained within the directory represented by self
291 fun files
: Set[ String ] is extern import HashSet, HashSet::add
, String::from_cstring
, String::to_cstring
, HashSet[String] as( Set[String] ), String as( Object )
294 redef class NativeString
295 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
296 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
297 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
298 private fun file_delete
: Bool is extern "string_NativeString_NativeString_file_delete_0"
302 # This class is system dependent ... must reify the vfs
303 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
304 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
305 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
306 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
307 fun size
: Int is extern "file_FileStat_FileStat_size_0"
310 # Instance of this class are standard FILE * pointers
311 private extern NativeFile
312 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
313 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
314 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
315 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
318 private interface NativeFileCapable
319 fun io_open_read
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
320 fun io_open_write
(path
: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
321 fun native_stdin
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
322 fun native_stdout
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
323 fun native_stderr
: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
327 fun stdin
: IFStream do return once
new Stdin
330 fun stdout
: OFStream do return once
new Stdout
332 # Standard output for error.
333 fun stderr
: OFStream do return once
new Stderr