lib/standard: Move top-level methods in `file` really to the top level.
[nit.git] / lib / standard / file.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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>
6 #
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
13 # another product.
14
15 # File manipulations (create, read, write, etc.)
16 module file
17
18 intrude import stream
19 intrude import string
20 import string_search
21 import time
22
23 in "C Header" `{
24 #include <dirent.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 `}
30
31 # File Abstract Stream
32 abstract class FStream
33 super IOS
34 # The path of the file.
35 var path: nullable String = null
36
37 # The FILE *.
38 var _file: nullable NativeFile = null
39
40 fun file_stat: FileStat
41 do return _file.file_stat end
42 end
43
44 # File input stream
45 class IFStream
46 super FStream
47 super BufferedIStream
48 # Misc
49
50 # Open the same file again.
51 # The original path is reused, therefore the reopened file can be a different file.
52 fun reopen
53 do
54 if not eof then close
55 _file = new NativeFile.io_open_read(path.to_cstring)
56 end_reached = false
57 _buffer_pos = 0
58 _buffer.clear
59 end
60
61 redef fun close
62 do
63 var i = _file.io_close
64 end_reached = true
65 end
66
67 redef fun fill_buffer
68 do
69 var nb = _file.io_read(_buffer.items, _buffer.capacity)
70 if nb <= 0 then
71 end_reached = true
72 nb = 0
73 end
74 _buffer.length = nb
75 _buffer_pos = 0
76 end
77
78 # End of file?
79 redef var end_reached: Bool = false
80
81 # Open the file at `path` for reading.
82 init open(path: String)
83 do
84 self.path = path
85 prepare_buffer(10)
86 _file = new NativeFile.io_open_read(path.to_cstring)
87 assert not _file.address_is_null else
88 print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
89 end
90 end
91
92 private init do end
93 private init without_file do end
94 end
95
96 # File output stream
97 class OFStream
98 super FStream
99 super OStream
100
101 redef fun write(s)
102 do
103 assert _writable
104 write_native(s.to_cstring, s.length)
105 end
106
107 redef fun is_writable do return _writable
108
109 redef fun close
110 do
111 var i = _file.io_close
112 _writable = false
113 end
114
115 # Is the file open in write mode
116 var _writable: Bool
117
118 # Write `len` bytes from `native`.
119 private fun write_native(native: NativeString, len: Int)
120 do
121 assert _writable
122 var err = _file.io_write(native, len)
123 if err != len then
124 # Big problem
125 printn("Problem in writing : ", err, " ", len, "\n")
126 end
127 end
128
129 # Open the file at `path` for writing.
130 init open(path: String)
131 do
132 _file = new NativeFile.io_open_write(path.to_cstring)
133 assert not _file.address_is_null else
134 print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
135 end
136 self.path = path
137 _writable = true
138 end
139
140 private init do end
141 private init without_file do end
142 end
143
144 ###############################################################################
145
146 class Stdin
147 super IFStream
148 super PollableIStream
149
150 private init do
151 _file = new NativeFile.native_stdin
152 path = "/dev/stdin"
153 prepare_buffer(1)
154 end
155
156 redef fun poll_in: Bool is extern "file_stdin_poll_in"
157 end
158
159 class Stdout
160 super OFStream
161 private init do
162 _file = new NativeFile.native_stdout
163 path = "/dev/stdout"
164 _writable = true
165 end
166 end
167
168 class Stderr
169 super OFStream
170 private init do
171 _file = new NativeFile.native_stderr
172 path = "/dev/stderr"
173 _writable = true
174 end
175 end
176
177 ###############################################################################
178
179 redef class Streamable
180 # Like `write_to` but take care of creating the file
181 fun write_to_file(filepath: String)
182 do
183 var stream = new OFStream.open(filepath)
184 write_to(stream)
185 stream.close
186 end
187 end
188
189 redef class String
190 # return true if a file with this names exists
191 fun file_exists: Bool do return to_cstring.file_exists
192
193 # The status of a file. see POSIX stat(2).
194 fun file_stat: FileStat do return to_cstring.file_stat
195
196 # The status of a file or of a symlink. see POSIX lstat(2).
197 fun file_lstat: FileStat do return to_cstring.file_lstat
198
199 # Remove a file, return true if success
200 fun file_delete: Bool do return to_cstring.file_delete
201
202 # Copy content of file at `self` to `dest`
203 fun file_copy_to(dest: String)
204 do
205 var input = new IFStream.open(self)
206 var output = new OFStream.open(dest)
207
208 while not input.eof do
209 var buffer = input.read(1024)
210 output.write buffer
211 end
212
213 input.close
214 output.close
215 end
216
217 # Remove the trailing extension `ext`.
218 #
219 # `ext` usually starts with a dot but could be anything.
220 #
221 # assert "file.txt".strip_extension(".txt") == "file"
222 # assert "file.txt".strip_extension("le.txt") == "fi"
223 # assert "file.txt".strip_extension("xt") == "file.t"
224 #
225 # if `ext` is not present, `self` is returned unmodified.
226 #
227 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
228 fun strip_extension(ext: String): String
229 do
230 if has_suffix(ext) then
231 return substring(0, length - ext.length)
232 end
233 return self
234 end
235
236 # Extract the basename of a path and remove the extension
237 #
238 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
239 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
240 # assert "path/to".basename(".ext") == "to"
241 # assert "path/to/".basename(".ext") == "to"
242 # assert "path".basename("") == "path"
243 # assert "/path".basename("") == "path"
244 # assert "/".basename("") == "/"
245 # assert "".basename("") == ""
246 fun basename(ext: String): String
247 do
248 var l = length - 1 # Index of the last char
249 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
250 if l == 0 then return "/"
251 var pos = chars.last_index_of_from('/', l)
252 var n = self
253 if pos >= 0 then
254 n = substring(pos+1, l-pos)
255 end
256 return n.strip_extension(ext)
257 end
258
259 # Extract the dirname of a path
260 #
261 # assert "/path/to/a_file.ext".dirname == "/path/to"
262 # assert "path/to/a_file.ext".dirname == "path/to"
263 # assert "path/to".dirname == "path"
264 # assert "path/to/".dirname == "path"
265 # assert "path".dirname == "."
266 # assert "/path".dirname == "/"
267 # assert "/".dirname == "/"
268 # assert "".dirname == "."
269 fun dirname: String
270 do
271 var l = length - 1 # Index of the last char
272 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
273 var pos = chars.last_index_of_from('/', l)
274 if pos > 0 then
275 return substring(0, pos)
276 else if pos == 0 then
277 return "/"
278 else
279 return "."
280 end
281 end
282
283 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
284 fun realpath: String do
285 var cs = to_cstring.file_realpath
286 var res = cs.to_s_with_copy
287 # cs.free_malloc # FIXME memory leak
288 return res
289 end
290
291 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
292 # ".." are not resolved if they start the path
293 # starting "/" is not removed
294 # trainling "/" is removed
295 #
296 # Note that the method only wonrk on the string:
297 # * no I/O access is performed
298 # * the validity of the path is not checked
299 #
300 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
301 # assert "../dir/file".simplify_path == "../dir/file"
302 # assert "dir/../../".simplify_path == ".."
303 # assert "dir/..".simplify_path == "."
304 # assert "//absolute//path/".simplify_path == "/absolute/path"
305 fun simplify_path: String
306 do
307 var a = self.split_with("/")
308 var a2 = new Array[String]
309 for x in a do
310 if x == "." then continue
311 if x == "" and not a2.is_empty then continue
312 if x == ".." and not a2.is_empty and a2.last != ".." then
313 a2.pop
314 continue
315 end
316 a2.push(x)
317 end
318 if a2.is_empty then return "."
319 return a2.join("/")
320 end
321
322 # Correctly join two path using the directory separator.
323 #
324 # Using a standard "{self}/{path}" does not work when `self` is the empty string.
325 # This method ensure that the join is valid.
326 #
327 # assert "hello".join_path("world") == "hello/world"
328 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
329 # assert "".join_path("world") == "world"
330 # assert "/hello".join_path("/world") == "/world"
331 #
332 # Note: you may want to use `simplify_path` on the result
333 #
334 # Note: I you want to join a great number of path, you can write
335 #
336 # [p1, p2, p3, p4].join("/")
337 fun join_path(path: String): String
338 do
339 if path.is_empty then return self
340 if self.is_empty then return path
341 if path.chars[0] == '/' then return path
342 return "{self}/{path}"
343 end
344
345 # Create a directory (and all intermediate directories if needed)
346 fun mkdir
347 do
348 var dirs = self.split_with("/")
349 var path = new FlatBuffer
350 if dirs.is_empty then return
351 if dirs[0].is_empty then
352 # it was a starting /
353 path.add('/')
354 end
355 for d in dirs do
356 if d.is_empty then continue
357 path.append(d)
358 path.add('/')
359 path.to_s.to_cstring.file_mkdir
360 end
361 end
362
363 # Change the current working directory
364 #
365 # "/etc".chdir
366 # assert getcwd == "/etc"
367 # "..".chdir
368 # assert getcwd == "/"
369 #
370 # TODO: errno
371 fun chdir do to_cstring.file_chdir
372
373 # Return right-most extension (without the dot)
374 #
375 # Only the last extension is returned.
376 # There is no special case for combined extensions.
377 #
378 # assert "file.txt".file_extension == "txt"
379 # assert "file.tar.gz".file_extension == "gz"
380 #
381 # For file without extension, `null` is returned.
382 # Hoever, for trailing dot, `""` is returned.
383 #
384 # assert "file".file_extension == null
385 # assert "file.".file_extension == ""
386 #
387 # The starting dot of hidden files is never considered.
388 #
389 # assert ".file.txt".file_extension == "txt"
390 # assert ".file".file_extension == null
391 fun file_extension: nullable String
392 do
393 var last_slash = chars.last_index_of('.')
394 if last_slash > 0 then
395 return substring( last_slash+1, length )
396 else
397 return null
398 end
399 end
400
401 # returns files contained within the directory represented by self
402 fun files : Set[ String ] is extern import HashSet[String], HashSet[String].add, NativeString.to_s, String.to_cstring, HashSet[String].as(Set[String]) `{
403 char *dir_path;
404 DIR *dir;
405
406 dir_path = String_to_cstring( recv );
407 if ((dir = opendir(dir_path)) == NULL)
408 {
409 perror( dir_path );
410 exit( 1 );
411 }
412 else
413 {
414 HashSet_of_String results;
415 String file_name;
416 struct dirent *de;
417
418 results = new_HashSet_of_String();
419
420 while ( ( de = readdir( dir ) ) != NULL )
421 if ( strcmp( de->d_name, ".." ) != 0 &&
422 strcmp( de->d_name, "." ) != 0 )
423 {
424 file_name = NativeString_to_s( strdup( de->d_name ) );
425 HashSet_of_String_add( results, file_name );
426 }
427
428 closedir( dir );
429 return HashSet_of_String_as_Set_of_String( results );
430 }
431 `}
432 end
433
434 redef class NativeString
435 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
436 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
437 private fun file_lstat: FileStat `{
438 struct stat* stat_element;
439 int res;
440 stat_element = malloc(sizeof(struct stat));
441 res = lstat(recv, stat_element);
442 if (res == -1) return NULL;
443 return stat_element;
444 `}
445 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
446 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
447 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
448 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
449 end
450
451 # This class is system dependent ... must reify the vfs
452 extern class FileStat `{ struct stat * `}
453 # Returns the permission bits of file
454 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
455 # Returns the last access time
456 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
457 # Returns the last status change time
458 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
459 # Returns the last modification time
460 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
461 # Returns the size
462 fun size: Int is extern "file_FileStat_FileStat_size_0"
463
464 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
465 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
466 # Returns true if it is a directory
467 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
468 # Returns true if it is a character device
469 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
470 # Returns true if it is a block device
471 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
472 # Returns true if the type is fifo
473 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
474 # Returns true if the type is a link
475 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
476 # Returns true if the type is a socket
477 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
478 end
479
480 # Instance of this class are standard FILE * pointers
481 private extern class NativeFile `{ FILE* `}
482 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
483 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
484 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
485 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
486
487 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
488 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
489 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
490 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
491 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
492 end
493
494 redef class Sys
495
496 # Standard input
497 var stdin: PollableIStream protected writable = new Stdin
498
499 # Standard output
500 var stdout: OStream protected writable = new Stdout
501
502 # Standard output for errors
503 var stderr: OStream protected writable = new Stderr
504
505 end
506
507 # Print `objects` on the standard output (`stdout`).
508 protected fun printn(objects: Object...)
509 do
510 sys.stdout.write(objects.to_s)
511 end
512
513 # Print an `object` on the standard output (`stdout`) and add a newline.
514 protected fun print(object: Object)
515 do
516 sys.stdout.write(object.to_s)
517 sys.stdout.write("\n")
518 end
519
520 # Read a character from the standard input (`stdin`).
521 protected fun getc: Char
522 do
523 return sys.stdin.read_char.ascii
524 end
525
526 # Read a line from the standard input (`stdin`).
527 protected fun gets: String
528 do
529 return sys.stdin.read_line
530 end
531
532 # Return the working (current) directory
533 protected fun getcwd: String do return file_getcwd.to_s
534 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"