Skip to content

Commit

Permalink
Merge pull request #3872 from stevengj/readwritable
Browse files Browse the repository at this point in the history
add more isreadable/iswriteable/isreadonly functions
  • Loading branch information
Keno committed Jul 29, 2013
2 parents 4d70ecc + f808a21 commit 1ffed5d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ else
error("seriously? what is this machine?")
end

isreadonly(s) = isreadable(s) && !iswriteable(s)

## binary I/O ##

# all subtypes should implement this
Expand Down Expand Up @@ -243,6 +245,8 @@ close(s::IOStream) = ccall(:ios_close, Void, (Ptr{Void},), s.ios)
isopen(s::IOStream) = bool(ccall(:ios_isopen, Cint, (Ptr{Void},), s.ios))
flush(s::IOStream) = ccall(:ios_flush, Void, (Ptr{Void},), s.ios)
isreadonly(s::IOStream) = bool(ccall(:ios_get_readonly, Cint, (Ptr{Void},), s.ios))
iswriteable(s::IOStream) = !isreadonly(s)
isreadable(s::IOStream) = true

truncate(s::IOStream, n::Integer) =
(ccall(:ios_trunc, Int32, (Ptr{Void}, Uint), s.ios, n)==0 ||
Expand Down
3 changes: 3 additions & 0 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ end

read{T}(from::IOBuffer, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, Uint))

isreadable(io::IOBuffer) = io.readable
iswriteable(io::IOBuffer) = io.writable

# TODO: IOBuffer is not iterable, so doesn't really have a length.
# This should maybe be sizeof() instead.
#length(io::IOBuffer) = (io.seekable ? io.size : nb_available(io))
Expand Down
7 changes: 7 additions & 0 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ function TTY(fd::RawFD; readable::Bool = false)
ret
end

# note that uv_is_readable/writeable work for any subtype of
# uv_stream_t, including uv_tty_t and uv_pipe_t
isreadable(io::Union(NamedPipe,PipeServer,TTY)) =
bool(ccall(:uv_is_readable, Cint, (Ptr{Void},), io.handle))
iswriteable(io::Union(NamedPipe,PipeServer,TTY)) =
bool(ccall(:uv_is_writable, Cint, (Ptr{Void},), io.handle))

show(io::IO,stream::TTY) = print(io,"TTY(",uv_status_string(stream),", ",
nb_available(stream.buffer)," bytes waiting)")

Expand Down

0 comments on commit 1ffed5d

Please sign in to comment.