lib/file: fix border-use-cases of String::basename
[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 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 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 stdout.write(object.to_s)
44 stdout.write("\n")
45 end
46
47 # Read a character from the standard input (`stdin`).
48 protected fun getc: Char
49 do
50 return 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 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 readable 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 readable var _end_reached: Bool = false
113
114 # Open the file at `path` for reading.
115 init open(path: String)
116 do
117 _path = path
118 prepare_buffer(10)
119 _file = new NativeFile.io_open_read(_path.to_cstring)
120 assert cant_open_file: _file != null
121 end
122
123 private init do end
124 private init without_file do end
125 end
126
127 # File output stream
128 class OFStream
129 super FStream
130 super OStream
131
132 redef fun write(s)
133 do
134 assert _writable
135 write_native(s.to_cstring, s.length)
136 end
137
138 redef fun is_writable do return _writable
139
140 redef fun close
141 do
142 var i = _file.io_close
143 _writable = false
144 end
145
146 # Is the file open in write mode
147 var _writable: Bool
148
149 # Write `len` bytes from `native`.
150 private fun write_native(native: NativeString, len: Int)
151 do
152 assert _writable
153 var err = _file.io_write(native, len)
154 if err != len then
155 # Big problem
156 printn("Problem in writing : ", err, " ", len, "\n")
157 end
158 end
159
160 # Open the file at `path` for writing.
161 init open(path: String)
162 do
163 _file = new NativeFile.io_open_write(path.to_cstring)
164 assert cant_open_file: _file != null
165 _path = path
166 _writable = true
167 end
168
169 private init do end
170 private init without_file do end
171 end
172
173 ###############################################################################
174
175 class Stdin
176 super IFStream
177 private init do
178 _file = new NativeFile.native_stdin
179 _path = "/dev/stdin"
180 prepare_buffer(1)
181 end
182
183 # Is these something to read? (non blocking)
184 # FIXME: should be generalized
185 fun poll_in: Bool is extern "file_stdin_poll_in"
186 end
187
188 class Stdout
189 super OFStream
190 private init do
191 _file = new NativeFile.native_stdout
192 _path = "/dev/stdout"
193 _writable = true
194 end
195 end
196
197 class Stderr
198 super OFStream
199 private init do
200 _file = new NativeFile.native_stderr
201 _path = "/dev/stderr"
202 _writable = true
203 end
204 end
205
206 ###############################################################################
207
208 redef class String
209 # return true if a file with this names exists
210 fun file_exists: Bool do return to_cstring.file_exists
211
212 fun file_stat: FileStat do return to_cstring.file_stat
213 fun file_lstat: FileStat do return to_cstring.file_lstat
214
215 # Remove a file, return true if success
216 fun file_delete: Bool do return to_cstring.file_delete
217
218 # Copy content of file at `self` to `dest`
219 fun file_copy_to(dest: String)
220 do
221 var input = new IFStream.open(self)
222 var output = new OFStream.open(dest)
223
224 while not input.eof do
225 var buffer = input.read(1024)
226 output.write buffer
227 end
228
229 input.close
230 output.close
231 end
232
233 # remove the trailing extension "ext"
234 fun strip_extension(ext: String): String
235 do
236 if has_suffix(ext) then
237 return substring(0, length - ext.length)
238 end
239 return self
240 end
241
242 # Extract the basename of a path and remove the extension
243 #
244 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
245 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
246 # assert "path/to".basename(".ext") == "to"
247 # assert "path/to/".basename(".ext") == "to"
248 # assert "path".basename("") == "path"
249 # assert "/path".basename("") == "path"
250 # assert "/".basename("") == "/"
251 # assert "".basename("") == ""
252 fun basename(ext: String): String
253 do
254 var l = _length - 1 # Index of the last char
255 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
256 if l == 0 then return "/"
257 var pos = last_index_of_from('/', l)
258 var n = self
259 if pos >= 0 then
260 n = substring(pos+1, l-pos)
261 end
262 return n.strip_extension(ext)
263 end
264
265 # Extract the dirname of a path
266 #
267 # assert "/path/to/a_file.ext".dirname == "/path/to"
268 # assert "path/to/a_file.ext".dirname == "path/to"
269 # assert "path/to".dirname == "path"
270 # assert "path/to/".dirname == "path"
271 # assert "path".dirname == "."
272 # assert "/path".dirname == "/"
273 # assert "/".dirname == "/"
274 # assert "".dirname == "."
275 fun dirname: String
276 do
277 var l = _length - 1 # Index of the last char
278 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
279 var pos = last_index_of_from('/', l)
280 if pos > 0 then
281 return substring(0, pos)
282 else if pos == 0 then
283 return "/"
284 else
285 return "."
286 end
287 end
288
289 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
290 fun realpath: String do
291 var cs = to_cstring.file_realpath
292 var res = cs.to_s_with_copy
293 # cs.free_malloc # FIXME memory leak
294 return res
295 end
296
297 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
298 # ".." are not resolved if they start the path
299 # starting "/" is not removed
300 # trainling "/" is removed
301 #
302 # Note that the method only wonrk on the string:
303 # * no I/O access is performed
304 # * the validity of the path is not checked
305 #
306 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
307 # assert "../dir/file".simplify_path == "../dir/file"
308 # assert "dir/../../".simplify_path == ".."
309 # assert "dir/..".simplify_path == "."
310 # assert "//absolute//path/".simplify_path == "/absolute/path"
311 fun simplify_path: String
312 do
313 var a = self.split_with("/")
314 var a2 = new Array[String]
315 for x in a do
316 if x == "." then continue
317 if x == "" and not a2.is_empty then continue
318 if x == ".." and not a2.is_empty and a2.last != ".." then
319 a2.pop
320 continue
321 end
322 a2.push(x)
323 end
324 if a2.is_empty then return "."
325 return a2.join("/")
326 end
327
328 # Correctly join two path using the directory separator.
329 #
330 # Using a standard "{self}/{path}" does not work when `self` is the empty string.
331 # This method ensure that the join is valid.
332 #
333 # assert "hello".join_path("world") == "hello/world"
334 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
335 # assert "".join_path("world") == "world"
336 # assert "/hello".join_path("/world") == "/world"
337 #
338 # Note: you may want to use `simplify_path` on the result
339 #
340 # Note: I you want to join a great number of path, you can write
341 #
342 # [p1, p2, p3, p4].join("/")
343 fun join_path(path: String): String
344 do
345 if path.is_empty then return self
346 if self.is_empty then return path
347 if path.chars[0] == '/' then return path
348 return "{self}/{path}"
349 end
350
351 # Create a directory (and all intermediate directories if needed)
352 fun mkdir
353 do
354 var dirs = self.split_with("/")
355 var path = new Buffer
356 if dirs.is_empty then return
357 if dirs[0].is_empty then
358 # it was a starting /
359 path.add('/')
360 end
361 for d in dirs do
362 if d.is_empty then continue
363 path.append(d)
364 path.add('/')
365 path.to_s.to_cstring.file_mkdir
366 end
367 end
368
369 # Change the current working directory
370 #
371 # "/etc".chdir
372 # assert getcwd == "/etc"
373 # "..".chdir
374 # assert getcwd == "/"
375 #
376 # TODO: errno
377 fun chdir do to_cstring.file_chdir
378
379 # Return right-most extension (without the dot)
380 fun file_extension : nullable String
381 do
382 var last_slash = last_index_of('.')
383 if last_slash >= 0 then
384 return substring( last_slash+1, length )
385 else
386 return null
387 end
388 end
389
390 # returns files contained within the directory represented by self
391 fun files : Set[ String ] is extern import HashSet[String], HashSet[String].add, NativeString.to_s, String.to_cstring, HashSet[String].as(Set[String]) `{
392 char *dir_path;
393 DIR *dir;
394
395 dir_path = String_to_cstring( recv );
396 if ((dir = opendir(dir_path)) == NULL)
397 {
398 perror( dir_path );
399 exit( 1 );
400 }
401 else
402 {
403 HashSet_of_String results;
404 String file_name;
405 struct dirent *de;
406
407 results = new_HashSet_of_String();
408
409 while ( ( de = readdir( dir ) ) != NULL )
410 if ( strcmp( de->d_name, ".." ) != 0 &&
411 strcmp( de->d_name, "." ) != 0 )
412 {
413 file_name = NativeString_to_s( strdup( de->d_name ) );
414 HashSet_of_String_add( results, file_name );
415 }
416
417 closedir( dir );
418 return HashSet_of_String_as_Set_of_String( results );
419 }
420 `}
421 end
422
423 redef class NativeString
424 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
425 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
426 private fun file_lstat: FileStat `{
427 struct stat* stat_element;
428 int res;
429 stat_element = malloc(sizeof(struct stat));
430 res = lstat(recv, stat_element);
431 if (res == -1) return NULL;
432 return stat_element;
433 `}
434 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
435 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
436 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
437 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
438 end
439
440 extern FileStat `{ struct stat * `}
441 # This class is system dependent ... must reify the vfs
442 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
443 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
444 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
445 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
446 fun size: Int is extern "file_FileStat_FileStat_size_0"
447
448 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
449 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
450 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
451 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
452 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
453 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
454 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
455 end
456
457 # Instance of this class are standard FILE * pointers
458 private extern NativeFile `{ FILE* `}
459 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
460 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
461 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
462 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
463
464 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
465 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
466 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
467 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
468 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
469 end
470
471 # Standard input.
472 fun stdin: Stdin do return once new Stdin
473
474 # Standard output.
475 fun stdout: OFStream do return once new Stdout
476
477 # Standard output for error.
478 fun stderr: OFStream do return once new Stderr