syntax: 'meth' -> 'fun', 'attr' -> 'var'
[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 # This module handle file input and output
16 package file
17
18 intrude import stream
19 intrude import string
20 import string_search
21
22 redef class Object
23 # Simple I/O
24
25 # Print `objects' on the standard output (`stdout').
26 protected fun printn(objects: Object...)
27 do
28 stdout.write(objects.to_s)
29 end
30
31 # Print an `object' on the standard output (`stdout') and add a newline.
32 protected fun print(object: Object)
33 do
34 stdout.write(object.to_s)
35 stdout.write("\n")
36 end
37
38 # Read a character from the standard input (`stdin').
39 protected fun getc: Char
40 do
41 return stdin.read_char.ascii
42 end
43
44 # Read a line from the standard input (`stdin').
45 protected fun gets: String
46 do
47 return stdin.read_line
48 end
49 end
50
51 # File Abstract Stream
52 class FStream
53 special IOS
54 special NativeFileCapable
55 # The path of the file.
56 readable var _path: nullable String = null
57
58 # The FILE *.
59 var _file: nullable NativeFile = null
60
61 fun file_stat: FileStat
62 do return _file.file_stat end
63 end
64
65 # File input stream
66 class IFStream
67 special FStream
68 special BufferedIStream
69 # Misc
70
71 fun reopen
72 do
73 if not eof then close
74 _file = io_open_read(_path.to_cstring)
75 _end_reached = false
76 _buffer_pos = 0
77 _buffer.clear
78 end
79
80 redef fun close
81 do
82 var i = _file.io_close
83 _end_reached = true
84 end
85
86 # Fill the internal read buffer. Needed by read operations.
87 redef fun fill_buffer
88 do
89 var nb = _file.io_read(_buffer._items, _buffer._capacity)
90 if nb <= 0 then
91 _end_reached = true
92 nb = 0
93 end
94 _buffer._length = nb
95 _buffer_pos = 0
96 end
97
98 # End of file?
99 redef readable var _end_reached: Bool = false
100
101 # Open the file at `path' for reading.
102 init open(path: String)
103 do
104 _path = path
105 prepare_buffer(10)
106 _file = io_open_read(_path.to_cstring)
107 assert cant_open_file: _file != null
108 end
109
110 private init do end
111 private init without_file do end
112 end
113
114 # File output stream
115 class OFStream
116 special FStream
117 special OStream
118
119 # Write a string.
120 redef fun write(s)
121 do
122 assert _writable
123 write_native(s.to_cstring, s.length)
124 end
125
126 redef fun is_writable do return _writable
127
128 redef fun close
129 do
130 var i = _file.io_close
131 _writable = false
132 end
133
134 # Is the file open in write mode
135 var _writable: Bool
136
137 # Write `len' bytes from `native'.
138 private fun write_native(native: NativeString, len: Int)
139 do
140 assert _writable
141 var err = _file.io_write(native, len)
142 if err != len then
143 # Big problem
144 printn("Problem in writing : ", err, " ", len, "\n")
145 end
146 end
147
148 # Open the file at `path' for writing.
149 init open(path: String)
150 do
151 _file = io_open_write(path.to_cstring)
152 assert cant_open_file: _file != null
153 _path = path
154 _writable = true
155 end
156
157 private init do end
158 private init without_file do end
159 end
160
161 ###############################################################################
162
163 class Stdin
164 special IFStream
165 private init do
166 _file = native_stdin
167 _path = "/dev/stdin"
168 prepare_buffer(1)
169 end
170 end
171
172 class Stdout
173 special OFStream
174 private init do
175 _file = native_stdout
176 _path = "/dev/stdout"
177 _writable = true
178 end
179 end
180
181 class Stderr
182 special OFStream
183 private init do
184 _file = native_stderr
185 _path = "/dev/stderr"
186 _writable = true
187 end
188 end
189
190 ###############################################################################
191
192 redef class String
193 # return true if a file with this names exists
194 fun file_exists: Bool do return to_cstring.file_exists
195
196 fun file_stat: FileStat do return to_cstring.file_stat
197
198 fun file_delete: Bool do return to_cstring.file_delete
199
200 fun strip_extension(ext: String): String
201 do
202 if has_suffix(ext) then
203 return substring(0, length - ext.length)
204 end
205 return self
206 end
207
208 fun basename(ext: String): String
209 do
210 var pos = last_index_of_from('/', _length - 1)
211 var n = self
212 if pos >= 0 then
213 n = substring_from(pos+1)
214 end
215 return n.strip_extension(ext)
216 end
217
218 fun dirname: String
219 do
220 var pos = last_index_of_from('/', _length - 1)
221 if pos >= 0 then
222 return substring(0, pos)
223 else
224 return "."
225 end
226 end
227
228 fun file_path: String
229 do
230 var l = _length
231 var pos = last_index_of_from('/', l - 1)
232 if pos >= 0 then
233 return substring(0, pos)
234 end
235 return "."
236 end
237
238 # Create a directory (and all intermediate directories if needed)
239 fun mkdir
240 do
241 var dirs = self.split_with("/")
242 var path = new Buffer
243 if dirs.is_empty then return
244 if dirs[0].is_empty then
245 # it was a starting /
246 path.add('/')
247 end
248 for d in dirs do
249 if d.is_empty then continue
250 path.append(d)
251 path.add('/')
252 path.to_s.to_cstring.file_mkdir
253 end
254 end
255 end
256
257 redef class NativeString
258 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
259 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
260 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
261 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
262 end
263
264 universal FileStat
265 special Pointer
266 # This class is system dependent ... must reify the vfs
267 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
268 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
269 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
270 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
271 fun size: Int is extern "file_FileStat_FileStat_size_0"
272 end
273
274 # Instance of this class are standard FILE * pointers
275 private universal NativeFile
276 special Pointer
277 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
278 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
279 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
280 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
281 end
282
283 private interface NativeFileCapable
284 fun io_open_read(path: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
285 fun io_open_write(path: NativeString): NativeFile is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
286 fun native_stdin: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
287 fun native_stdout: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
288 fun native_stderr: NativeFile is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
289 end
290
291 # Standard input.
292 fun stdin: IFStream do return once new Stdin
293
294 # Standard output.
295 fun stdout: OFStream do return once new Stdout
296
297 # Standard output for error.
298 fun stderr: OFStream do return once new Stderr