lib/standard: fixes DocUnits parsed as list elements
[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 ropes
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 #include <stdio.h>
30 `}
31
32 # File Abstract Stream
33 abstract class FStream
34 super IOS
35 # The path of the file.
36 var path: nullable String = null
37
38 # The FILE *.
39 private var file: nullable NativeFile = null
40
41 # The status of a file. see POSIX stat(2).
42 fun file_stat: FileStat do return _file.file_stat
43
44 # File descriptor of this file
45 fun fd: Int do return _file.fileno
46 end
47
48 # File input stream
49 class IFStream
50 super FStream
51 super BufferedIStream
52 super PollableIStream
53 # Misc
54
55 # Open the same file again.
56 # The original path is reused, therefore the reopened file can be a different file.
57 fun reopen
58 do
59 if not eof then close
60 _file = new NativeFile.io_open_read(path.to_cstring)
61 end_reached = false
62 _buffer_pos = 0
63 _buffer.clear
64 end
65
66 redef fun close
67 do
68 _file.io_close
69 _buffer.clear
70 end_reached = true
71 end
72
73 redef fun fill_buffer
74 do
75 var nb = _file.io_read(_buffer.items, _buffer.capacity)
76 if nb <= 0 then
77 end_reached = true
78 nb = 0
79 end
80 _buffer.length = nb
81 _buffer_pos = 0
82 end
83
84 # End of file?
85 redef var end_reached: Bool = false
86
87 # Open the file at `path` for reading.
88 init open(path: String)
89 do
90 self.path = path
91 prepare_buffer(10)
92 _file = new NativeFile.io_open_read(path.to_cstring)
93 assert not _file.address_is_null else
94 print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
95 end
96 end
97
98 end
99
100 # File output stream
101 class OFStream
102 super FStream
103 super OStream
104
105 redef fun write(s)
106 do
107 assert _is_writable
108 if s isa FlatText then
109 write_native(s.to_cstring, s.length)
110 else
111 for i in s.substrings do write_native(i.to_cstring, i.length)
112 end
113 end
114
115 redef fun close
116 do
117 _file.io_close
118 _is_writable = false
119 end
120
121 redef var is_writable = false
122
123 # Write `len` bytes from `native`.
124 private fun write_native(native: NativeString, len: Int)
125 do
126 assert _is_writable
127 var err = _file.io_write(native, len)
128 if err != len then
129 # Big problem
130 printn("Problem in writing : ", err, " ", len, "\n")
131 end
132 end
133
134 # Open the file at `path` for writing.
135 init open(path: String)
136 do
137 _file = new NativeFile.io_open_write(path.to_cstring)
138 assert not _file.address_is_null else
139 print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
140 end
141 self.path = path
142 _is_writable = true
143 end
144 end
145
146 ###############################################################################
147
148 # Standard input stream.
149 class Stdin
150 super IFStream
151
152 init do
153 _file = new NativeFile.native_stdin
154 path = "/dev/stdin"
155 prepare_buffer(1)
156 end
157
158 redef fun poll_in: Bool is extern "file_stdin_poll_in"
159 end
160
161 # Standard output stream.
162 class Stdout
163 super OFStream
164 init do
165 _file = new NativeFile.native_stdout
166 path = "/dev/stdout"
167 _is_writable = true
168 end
169 end
170
171 # Standard error stream.
172 class Stderr
173 super OFStream
174 init do
175 _file = new NativeFile.native_stderr
176 path = "/dev/stderr"
177 _is_writable = true
178 end
179 end
180
181 ###############################################################################
182
183 redef class Streamable
184 # Like `write_to` but take care of creating the file
185 fun write_to_file(filepath: String)
186 do
187 var stream = new OFStream.open(filepath)
188 write_to(stream)
189 stream.close
190 end
191 end
192
193 redef class String
194 # return true if a file with this names exists
195 fun file_exists: Bool do return to_cstring.file_exists
196
197 # The status of a file. see POSIX stat(2).
198 fun file_stat: FileStat do return to_cstring.file_stat
199
200 # The status of a file or of a symlink. see POSIX lstat(2).
201 fun file_lstat: FileStat do return to_cstring.file_lstat
202
203 # Remove a file, return true if success
204 fun file_delete: Bool do return to_cstring.file_delete
205
206 # Copy content of file at `self` to `dest`
207 fun file_copy_to(dest: String)
208 do
209 var input = new IFStream.open(self)
210 var output = new OFStream.open(dest)
211
212 while not input.eof do
213 var buffer = input.read(1024)
214 output.write buffer
215 end
216
217 input.close
218 output.close
219 end
220
221 # Remove the trailing extension `ext`.
222 #
223 # `ext` usually starts with a dot but could be anything.
224 #
225 # assert "file.txt".strip_extension(".txt") == "file"
226 # assert "file.txt".strip_extension("le.txt") == "fi"
227 # assert "file.txt".strip_extension("xt") == "file.t"
228 #
229 # if `ext` is not present, `self` is returned unmodified.
230 #
231 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
232 fun strip_extension(ext: String): String
233 do
234 if has_suffix(ext) then
235 return substring(0, length - ext.length)
236 end
237 return self
238 end
239
240 # Extract the basename of a path and remove the extension
241 #
242 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
243 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
244 # assert "path/to".basename(".ext") == "to"
245 # assert "path/to/".basename(".ext") == "to"
246 # assert "path".basename("") == "path"
247 # assert "/path".basename("") == "path"
248 # assert "/".basename("") == "/"
249 # assert "".basename("") == ""
250 fun basename(ext: String): String
251 do
252 var l = length - 1 # Index of the last char
253 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
254 if l == 0 then return "/"
255 var pos = chars.last_index_of_from('/', l)
256 var n = self
257 if pos >= 0 then
258 n = substring(pos+1, l-pos)
259 end
260 return n.strip_extension(ext)
261 end
262
263 # Extract the dirname of a path
264 #
265 # assert "/path/to/a_file.ext".dirname == "/path/to"
266 # assert "path/to/a_file.ext".dirname == "path/to"
267 # assert "path/to".dirname == "path"
268 # assert "path/to/".dirname == "path"
269 # assert "path".dirname == "."
270 # assert "/path".dirname == "/"
271 # assert "/".dirname == "/"
272 # assert "".dirname == "."
273 fun dirname: String
274 do
275 var l = length - 1 # Index of the last char
276 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
277 var pos = chars.last_index_of_from('/', l)
278 if pos > 0 then
279 return substring(0, pos)
280 else if pos == 0 then
281 return "/"
282 else
283 return "."
284 end
285 end
286
287 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
288 fun realpath: String do
289 var cs = to_cstring.file_realpath
290 var res = cs.to_s_with_copy
291 # cs.free_malloc # FIXME memory leak
292 return res
293 end
294
295 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
296 # ".." are not resolved if they start the path
297 # starting "/" is not removed
298 # trainling "/" is removed
299 #
300 # Note that the method only wonrk on the string:
301 # * no I/O access is performed
302 # * the validity of the path is not checked
303 #
304 # ~~~
305 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
306 # assert "../dir/file".simplify_path == "../dir/file"
307 # assert "dir/../../".simplify_path == ".."
308 # assert "dir/..".simplify_path == "."
309 # assert "//absolute//path/".simplify_path == "/absolute/path"
310 # assert "//absolute//../".simplify_path == "/"
311 # ~~~
312 fun simplify_path: String
313 do
314 var a = self.split_with("/")
315 var a2 = new Array[String]
316 for x in a do
317 if x == "." then continue
318 if x == "" and not a2.is_empty then continue
319 if x == ".." and not a2.is_empty and a2.last != ".." then
320 a2.pop
321 continue
322 end
323 a2.push(x)
324 end
325 if a2.is_empty then return "."
326 if a2.length == 1 and a2.first == "" then return "/"
327 return a2.join("/")
328 end
329
330 # Correctly join two path using the directory separator.
331 #
332 # Using a standard "{self}/{path}" does not work in the following cases:
333 #
334 # * `self` is empty.
335 # * `path` ends with `'/'`.
336 # * `path` starts with `'/'`.
337 #
338 # This method ensures that the join is valid.
339 #
340 # assert "hello".join_path("world") == "hello/world"
341 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
342 # assert "".join_path("world") == "world"
343 # assert "hello".join_path("/world") == "/world"
344 # assert "hello/".join_path("world") == "hello/world"
345 # assert "hello/".join_path("/world") == "/world"
346 #
347 # Note: You may want to use `simplify_path` on the result.
348 #
349 # Note: This method works only with POSIX paths.
350 fun join_path(path: String): String
351 do
352 if path.is_empty then return self
353 if self.is_empty then return path
354 if path.chars[0] == '/' then return path
355 if self.last == '/' then return "{self}{path}"
356 return "{self}/{path}"
357 end
358
359 # Convert the path (`self`) to a program name.
360 #
361 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
362 # used as a program name. In order to do that, prepend `./` if needed.
363 #
364 # assert "foo".to_program_name == "./foo"
365 # assert "/foo".to_program_name == "/foo"
366 # assert "".to_program_name == "./" # At least, your shell will detect the error.
367 fun to_program_name: String do
368 if self.has_prefix("/") then
369 return self
370 else
371 return "./{self}"
372 end
373 end
374
375 # Alias for `join_path`
376 #
377 # assert "hello" / "world" == "hello/world"
378 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
379 # assert "" / "world" == "world"
380 # assert "/hello" / "/world" == "/world"
381 #
382 # This operator is quite useful for chaining changes of path.
383 # The next one being relative to the previous one.
384 #
385 # var a = "foo"
386 # var b = "/bar"
387 # var c = "baz/foobar"
388 # assert a/b/c == "/bar/baz/foobar"
389 fun /(path: String): String do return join_path(path)
390
391 # Returns the relative path needed to go from `self` to `dest`.
392 #
393 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
394 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
395 #
396 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
397 #
398 # In some cases, the result is still independent of the current directory:
399 #
400 # assert "foo/bar".relpath("..") == "../../.."
401 #
402 # In other cases, parts of the current directory may be exhibited:
403 #
404 # var p = "../foo/bar".relpath("baz")
405 # var c = getcwd.basename("")
406 # assert p == "../../{c}/baz"
407 #
408 # For path resolution independent of the current directory (eg. for paths in URL),
409 # or to use an other starting directory than the current directory,
410 # just force absolute paths:
411 #
412 # var start = "/a/b/c/d"
413 # var p2 = (start/"../foo/bar").relpath(start/"baz")
414 # assert p2 == "../../d/baz"
415 #
416 #
417 # Neither `self` or `dest` has to be real paths or to exist in directories since
418 # the resolution is only done with string manipulations and without any access to
419 # the underlying file system.
420 #
421 # If `self` and `dest` are the same directory, the empty string is returned:
422 #
423 # assert "foo".relpath("foo") == ""
424 # assert "foo/../bar".relpath("bar") == ""
425 #
426 # The empty string and "." designate both the current directory:
427 #
428 # assert "".relpath("foo/bar") == "foo/bar"
429 # assert ".".relpath("foo/bar") == "foo/bar"
430 # assert "foo/bar".relpath("") == "../.."
431 # assert "/" + "/".relpath(".") == getcwd
432 fun relpath(dest: String): String
433 do
434 var cwd = getcwd
435 var from = (cwd/self).simplify_path.split("/")
436 if from.last.is_empty then from.pop # case for the root directory
437 var to = (cwd/dest).simplify_path.split("/")
438 if to.last.is_empty then to.pop # case for the root directory
439
440 # Remove common prefixes
441 while not from.is_empty and not to.is_empty and from.first == to.first do
442 from.shift
443 to.shift
444 end
445
446 # Result is going up in `from` with ".." then going down following `to`
447 var from_len = from.length
448 if from_len == 0 then return to.join("/")
449 var up = "../"*(from_len-1) + ".."
450 if to.is_empty then return up
451 var res = up + "/" + to.join("/")
452 return res
453 end
454
455 # Create a directory (and all intermediate directories if needed)
456 fun mkdir
457 do
458 var dirs = self.split_with("/")
459 var path = new FlatBuffer
460 if dirs.is_empty then return
461 if dirs[0].is_empty then
462 # it was a starting /
463 path.add('/')
464 end
465 for d in dirs do
466 if d.is_empty then continue
467 path.append(d)
468 path.add('/')
469 path.to_s.to_cstring.file_mkdir
470 end
471 end
472
473 # Delete a directory and all of its content, return `true` on success
474 #
475 # Does not go through symbolic links and may get stuck in a cycle if there
476 # is a cycle in the filesystem.
477 fun rmdir: Bool
478 do
479 var ok = true
480 for file in self.files do
481 var file_path = self.join_path(file)
482 var stat = file_path.file_lstat
483 if stat.is_dir then
484 ok = file_path.rmdir and ok
485 else
486 ok = file_path.file_delete and ok
487 end
488 stat.free
489 end
490
491 # Delete the directory itself
492 if ok then to_cstring.rmdir
493
494 return ok
495 end
496
497 # Change the current working directory
498 #
499 # "/etc".chdir
500 # assert getcwd == "/etc"
501 # "..".chdir
502 # assert getcwd == "/"
503 #
504 # TODO: errno
505 fun chdir do to_cstring.file_chdir
506
507 # Return right-most extension (without the dot)
508 #
509 # Only the last extension is returned.
510 # There is no special case for combined extensions.
511 #
512 # assert "file.txt".file_extension == "txt"
513 # assert "file.tar.gz".file_extension == "gz"
514 #
515 # For file without extension, `null` is returned.
516 # Hoever, for trailing dot, `""` is returned.
517 #
518 # assert "file".file_extension == null
519 # assert "file.".file_extension == ""
520 #
521 # The starting dot of hidden files is never considered.
522 #
523 # assert ".file.txt".file_extension == "txt"
524 # assert ".file".file_extension == null
525 fun file_extension: nullable String
526 do
527 var last_slash = chars.last_index_of('.')
528 if last_slash > 0 then
529 return substring( last_slash+1, length )
530 else
531 return null
532 end
533 end
534
535 # returns files contained within the directory represented by self
536 fun files: Array[String] is extern import Array[String], Array[String].add, NativeString.to_s, String.to_cstring `{
537 char *dir_path;
538 DIR *dir;
539
540 dir_path = String_to_cstring( recv );
541 if ((dir = opendir(dir_path)) == NULL)
542 {
543 perror( dir_path );
544 exit( 1 );
545 }
546 else
547 {
548 Array_of_String results;
549 String file_name;
550 struct dirent *de;
551
552 results = new_Array_of_String();
553
554 while ( ( de = readdir( dir ) ) != NULL )
555 if ( strcmp( de->d_name, ".." ) != 0 &&
556 strcmp( de->d_name, "." ) != 0 )
557 {
558 file_name = NativeString_to_s( strdup( de->d_name ) );
559 Array_of_String_add( results, file_name );
560 }
561
562 closedir( dir );
563 return results;
564 }
565 `}
566 end
567
568 redef class NativeString
569 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
570 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
571 private fun file_lstat: FileStat `{
572 struct stat* stat_element;
573 int res;
574 stat_element = malloc(sizeof(struct stat));
575 res = lstat(recv, stat_element);
576 if (res == -1) return NULL;
577 return stat_element;
578 `}
579 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
580 private fun rmdir: Bool `{ return rmdir(recv); `}
581 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
582 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
583 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
584 end
585
586 # This class is system dependent ... must reify the vfs
587 extern class FileStat `{ struct stat * `}
588 # Returns the permission bits of file
589 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
590 # Returns the last access time
591 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
592 # Returns the last status change time
593 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
594 # Returns the last modification time
595 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
596 # Returns the size
597 fun size: Int is extern "file_FileStat_FileStat_size_0"
598
599 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
600 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
601 # Returns true if it is a directory
602 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
603 # Returns true if it is a character device
604 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
605 # Returns true if it is a block device
606 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
607 # Returns true if the type is fifo
608 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
609 # Returns true if the type is a link
610 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
611 # Returns true if the type is a socket
612 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
613 end
614
615 # Instance of this class are standard FILE * pointers
616 private extern class NativeFile `{ FILE* `}
617 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
618 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
619 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
620 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
621 fun fileno: Int `{ return fileno(recv); `}
622
623 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
624 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
625 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
626 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
627 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
628 end
629
630 redef class Sys
631
632 # Standard input
633 var stdin: PollableIStream = new Stdin is protected writable
634
635 # Standard output
636 var stdout: OStream = new Stdout is protected writable
637
638 # Standard output for errors
639 var stderr: OStream = new Stderr is protected writable
640
641 end
642
643 # Print `objects` on the standard output (`stdout`).
644 protected fun printn(objects: Object...)
645 do
646 sys.stdout.write(objects.to_s)
647 end
648
649 # Print an `object` on the standard output (`stdout`) and add a newline.
650 protected fun print(object: Object)
651 do
652 sys.stdout.write(object.to_s)
653 sys.stdout.write("\n")
654 end
655
656 # Read a character from the standard input (`stdin`).
657 protected fun getc: Char
658 do
659 return sys.stdin.read_char.ascii
660 end
661
662 # Read a line from the standard input (`stdin`).
663 protected fun gets: String
664 do
665 return sys.stdin.read_line
666 end
667
668 # Return the working (current) directory
669 protected fun getcwd: String do return file_getcwd.to_s
670 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"