@@ -211,25 +211,84 @@ function fdio(name::AbstractString, fd::Integer, own::Bool=false)
211
211
end
212
212
fdio (fd:: Integer , own:: Bool = false ) = fdio (string (" <fd " ,fd," >" ), fd, own)
213
213
214
+ """
215
+ open_flags(; keywords...) -> NamedTuple
216
+
217
+ Compute the `read`, `write`, `create`, `truncate`, `append` flag value for
218
+ a given set of keyword arguments to `open` a `NamedTuple`.
219
+ """
220
+ function open_flags (;
221
+ read :: Union{Bool,Nothing} = nothing ,
222
+ write :: Union{Bool,Nothing} = nothing ,
223
+ create :: Union{Bool,Nothing} = nothing ,
224
+ truncate :: Union{Bool,Nothing} = nothing ,
225
+ append :: Union{Bool,Nothing} = nothing ,
226
+ )
227
+ if write === true && read != = true && append != = true
228
+ create === nothing && (create = true )
229
+ truncate === nothing && (truncate = true )
230
+ end
231
+
232
+ if truncate === true || append === true
233
+ write === nothing && (write = true )
234
+ create === nothing && (create = true )
235
+ end
236
+
237
+ write === nothing && (write = false )
238
+ read === nothing && (read = ! write)
239
+ create === nothing && (create = false )
240
+ truncate === nothing && (truncate = false )
241
+ append === nothing && (append = false )
242
+
243
+ return (
244
+ read = read,
245
+ write = write,
246
+ create = create,
247
+ truncate = truncate,
248
+ append = append,
249
+ )
250
+ end
214
251
215
252
"""
216
- open(filename::AbstractString, [read::Bool, write::Bool, create::Bool, truncate::Bool, append::Bool]) -> IOStream
253
+ open(filename::AbstractString; keywords...) -> IOStream
254
+
255
+ Open a file in a mode specified by five boolean keyword arguments:
256
+
257
+ | Keyword | Desciption | Default |
258
+ |:-----------|:-----------------------|:----------------------------------------|
259
+ | `read` | open for reading | `!write` |
260
+ | `write` | open for writing | `truncate \\ | append` |
261
+ | `create` | create if non-existent | `!read & write \\ | truncate \\ | append` |
262
+ | `truncate` | truncate to zero size | `!read & write` |
263
+ | `append` | seek to end | `false` |
217
264
218
- Open a file in a mode specified by five boolean arguments. The default is to open files for
219
- reading only. Return a stream for accessing the file.
265
+ The default when no keywords are passed is to open files for reading only.
266
+ Returns a stream for accessing the opened file.
220
267
"""
221
- function open (fname:: AbstractString , rd:: Bool , wr:: Bool , cr:: Bool , tr:: Bool , ff:: Bool )
268
+ function open (fname:: AbstractString ;
269
+ read :: Union{Bool,Nothing} = nothing ,
270
+ write :: Union{Bool,Nothing} = nothing ,
271
+ create :: Union{Bool,Nothing} = nothing ,
272
+ truncate :: Union{Bool,Nothing} = nothing ,
273
+ append :: Union{Bool,Nothing} = nothing ,
274
+ )
275
+ flags = open_flags (
276
+ read = read,
277
+ write = write,
278
+ create = create,
279
+ truncate = truncate,
280
+ append = append,
281
+ )
222
282
s = IOStream (string (" <file " ,fname," >" ))
223
283
systemerror (" opening file $fname " ,
224
284
ccall (:ios_file , Ptr{Cvoid},
225
285
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
226
- s. ios, fname, rd, wr, cr, tr ) == C_NULL )
227
- if ff
286
+ s. ios, fname, flags . read, flags . write, flags . create, flags . truncate ) == C_NULL )
287
+ if flags . append
228
288
systemerror (" seeking to end of file $fname " , ccall (:ios_seek_end , Int64, (Ptr{Cvoid},), s. ios) != 0 )
229
289
end
230
290
return s
231
291
end
232
- open (fname:: AbstractString ) = open (fname, true , false , false , false , false )
233
292
234
293
"""
235
294
open(filename::AbstractString, [mode::AbstractString]) -> IOStream
@@ -238,14 +297,14 @@ Alternate syntax for open, where a string-based mode specifier is used instead o
238
297
booleans. The values of `mode` correspond to those from `fopen(3)` or Perl `open`, and are
239
298
equivalent to setting the following boolean groups:
240
299
241
- | Mode | Description |
242
- |:-----|:------------------------------|
243
- | r | read |
244
- | r+ | read, write |
245
- | w | write, create, truncate |
246
- | w+ | read, write, create, truncate |
247
- | a | write, create, append |
248
- | a+ | read, write, create, append |
300
+ | Mode | Description | Keywords |
301
+ |:-----|:------------------------------|:------------------------------------|
302
+ | `r` | read | none |
303
+ | `w` | write, create, truncate | ` write = true` |
304
+ | `a` | write, create, append | `append = true` |
305
+ | `r+` | read, write | ` read = true , write = true` |
306
+ | `w+` | read, write, create, truncate | `truncate = true, read = true` |
307
+ | `a+` | read, write, create, append | `append = true, read = true` |
249
308
250
309
# Examples
251
310
```jldoctest
@@ -277,19 +336,19 @@ julia> rm("myfile.txt")
277
336
```
278
337
"""
279
338
function open (fname:: AbstractString , mode:: AbstractString )
280
- mode == " r" ? open (fname, true , false , false , false , false ) :
281
- mode == " r+" ? open (fname, true , true , false , false , false ) :
282
- mode == " w" ? open (fname, false , true , true , true , false ) :
283
- mode == " w+" ? open (fname, true , true , true , true , false ) :
284
- mode == " a" ? open (fname, false , true , true , false , true ) :
285
- mode == " a+" ? open (fname, true , true , true , false , true ) :
339
+ mode == " r" ? open (fname, read = true ) :
340
+ mode == " r+" ? open (fname, read = true , write = true ) :
341
+ mode == " w" ? open (fname, truncate = true ) :
342
+ mode == " w+" ? open (fname, truncate = true , read = true ) :
343
+ mode == " a" ? open (fname, append = true ) :
344
+ mode == " a+" ? open (fname, append = true , read = true ) :
286
345
throw (ArgumentError (" invalid open mode: $mode " ))
287
346
end
288
347
289
348
"""
290
- open(f::Function, args...)
349
+ open(f::Function, args...; kwargs.... )
291
350
292
- Apply the function `f` to the result of `open(args...)` and close the resulting file
351
+ Apply the function `f` to the result of `open(args...; kwargs... )` and close the resulting file
293
352
descriptor upon completion.
294
353
295
354
# Examples
@@ -304,8 +363,8 @@ julia> open(f->read(f, String), "myfile.txt")
304
363
julia> rm("myfile.txt")
305
364
```
306
365
"""
307
- function open (f:: Function , args... )
308
- io = open (args... )
366
+ function open (f:: Function , args... ; kwargs ... )
367
+ io = open (args... ; kwargs ... )
309
368
try
310
369
f (io)
311
370
finally
0 commit comments