Skip to content

Commit

Permalink
make write() consistently return an Int byte count. fixes #1701
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 20, 2013
1 parent 2d23c57 commit 8dd6d08
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
24 changes: 15 additions & 9 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ write(s::IO, x::Uint8) = error(typeof(s)," does not support byte I/O")

if ENDIAN_BOM == 0x01020304
function write(s::IO, x::Integer)
for n = sizeof(x):-1:1
sz = sizeof(x)
for n = sz:-1:1
write(s, uint8((x>>>((n-1)<<3))))
end
sz
end
else
function write(s::IO, x::Integer)
for n = 1:sizeof(x)
sz = sizeof(x)
for n = 1:sz
write(s, uint8((x>>>((n-1)<<3))))
end
sz
end
end

Expand All @@ -46,9 +50,11 @@ write(s::IO, x::Float32) = write(s, box(Int32,unbox(Float32,x)))
write(s::IO, x::Float64) = write(s, box(Int64,unbox(Float64,x)))

function write(s::IO, a::AbstractArray)
nb = 0
for i = 1:length(a)
write(s, a[i])
nb += write(s, a[i])
end
nb
end

function write(s::IO, c::Char)
Expand Down Expand Up @@ -312,20 +318,19 @@ memio() = memio(0, true)

## low-level calls ##

write(s::IOStream, b::Uint8) = ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, s.ios)
write(s::IOStream, c::Char) = ccall(:jl_pututf8, Int32, (Ptr{Void}, Char), s.ios, c)
write(s::IOStream, b::Uint8) = int(ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, s.ios))

function write{T}(s::IOStream, a::Array{T})
if isa(T,BitsKind)
ccall(:jl_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
ccall(:ios_write, Int, (Ptr{Void}, Ptr{Void}, Uint),
s.ios, a, length(a)*sizeof(T))
else
invoke(write, (IO, Array), s, a)
end
end

function write(s::IOStream, p::Ptr, nb::Integer)
ccall(:jl_write, Uint, (Ptr{Void}, Ptr{Void}, Uint), s.ios, p, nb)
ccall(:ios_write, Int, (Ptr{Void}, Ptr{Void}, Uint), s.ios, p, nb)
end

function write{T,N,A<:Array}(s::IOStream, a::SubArray{T,N,A})
Expand All @@ -334,10 +339,11 @@ function write{T,N,A<:Array}(s::IOStream, a::SubArray{T,N,A})
end
colsz = size(a,1)*sizeof(T)
if N<=1
write(s, pointer(a, 1), colsz)
return write(s, pointer(a, 1), colsz)
else
cartesian_map((idxs...)->write(s, pointer(a, idxs), colsz),
tuple(1, size(a)[2:]...))
return colsz*trailingsize(a,2)
end
end

Expand Down Expand Up @@ -367,7 +373,7 @@ end

## text I/O ##

write(s::IOStream, c::Char) = ccall(:ios_pututf8, Int32, (Ptr{Void}, Char), s.ios, c)
write(s::IOStream, c::Char) = int(ccall(:ios_pututf8, Int32, (Ptr{Void}, Char), s.ios, c))
read(s::IOStream, ::Type{Char}) = ccall(:jl_getutf8, Char, (Ptr{Void},), s.ios)

takebuf_string(s::IOStream) =
Expand Down
12 changes: 6 additions & 6 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,20 +558,20 @@ end
## low-level calls

write(s::AsyncStream, b::ASCIIString) =
ccall(:jl_puts, Int32, (Ptr{Uint8},Ptr{Void}),b.data,handle(s))
int(ccall(:jl_puts, Int32, (Ptr{Uint8},Ptr{Void}),b.data,handle(s)))
write(s::AsyncStream, b::Uint8) =
ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, handle(s))
int(ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, handle(s)))
write(s::AsyncStream, c::Char) =
ccall(:jl_pututf8, Int32, (Ptr{Void},Char), handle(s), c)
int(ccall(:jl_pututf8, Int32, (Ptr{Void},Char), handle(s), c))
function write{T}(s::AsyncStream, a::Array{T})
if(isa(T,BitsKind))
ccall(:jl_write, Uint,(Ptr{Void}, Ptr{Void}, Uint32),handle(s), a, uint(length(a)*sizeof(T)))
ccall(:jl_write, Int, (Ptr{Void}, Ptr{Void}, Uint32), handle(s), a, uint(length(a)*sizeof(T)))
else
invoke(write,(IO,Array),s,a)
end
end
write(s::AsyncStream, p::Ptr, nb::Integer) = ccall(:jl_write, Uint,(Ptr{Void}, Ptr{Void}, Uint),handle(s), p, uint(nb))
_write(s::AsyncStream, p::Ptr{Void}, nb::Integer) = ccall(:jl_write, Uint,(Ptr{Void}, Ptr{Void}, Uint),handle(s),p,uint(nb))
write(s::AsyncStream, p::Ptr, nb::Integer) = ccall(:jl_write, Int, (Ptr{Void}, Ptr{Void}, Uint), handle(s), p, uint(nb))
_write(s::AsyncStream, p::Ptr{Void}, nb::Integer) = ccall(:jl_write, Int, (Ptr{Void}, Ptr{Void}, Uint), handle(s), p, uint(nb))

_jl_connect_raw(sock::TcpSocket,sockaddr::Ptr{Void}) = ccall(:jl_connect_raw,Int32,(Ptr{Void},Ptr{Void}),sock.handle,sockaddr)
_jl_getaddrinfo(loop::Ptr{Void},host::ByteString,service::Ptr{Void},cb::Function) = ccall(:jl_getaddrinfo,Int32,(Ptr{Void},Ptr{Uint8},Ptr{Uint8},Function),loop,host,service,cb)
Expand Down
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ DLLEXPORT void jl_free2(void *p, void *hint);

DLLEXPORT int jl_cpu_cores(void);

DLLEXPORT int jl_write(uv_stream_t *stream, const char *str, size_t n);
DLLEXPORT size_t jl_write(uv_stream_t *stream, const char *str, size_t n);
DLLEXPORT int jl_printf(uv_stream_t *s, const char *format, ...);
DLLEXPORT int jl_vprintf(uv_stream_t *s, const char *format, va_list args);

Expand Down
4 changes: 2 additions & 2 deletions src/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ DLLEXPORT int jl_putc(unsigned char c, uv_stream_t *stream)
}
}

DLLEXPORT int jl_write(uv_stream_t *stream, const char *str, size_t n)
DLLEXPORT size_t jl_write(uv_stream_t *stream, const char *str, size_t n)
{
//TODO: BAD!! Needed because Julia can't yet detect null stdio
//TODO: BAD!! Needed because Julia can't yet detect null stdio
if (stream == 0)
return 0;
if (stream->type<UV_HANDLE_TYPE_MAX) { //is uv handle
Expand Down

1 comment on commit 8dd6d08

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Does read already consistently return number of bytes read?

Please sign in to comment.