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