Skip to content

Commit

Permalink
fix AsyncStream eof()
Browse files Browse the repository at this point in the history
closes #2090 and #2051
  • Loading branch information
JeffBezanson committed Jan 26, 2013
1 parent 0859a9f commit f0e4fcd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
1 change: 0 additions & 1 deletion base/git.jl
Expand Up @@ -33,7 +33,6 @@ each_tagged_version(dir::String) = cd(each_tagged_version,dir)
function each_submodule(f::Function, recursive::Bool, dir::ByteString)
cmd = `git submodule foreach --quiet 'echo "$name $path $sha1"'`
for line in each_line(cmd)
isempty(line) && break # FIXME: temporary work-around #2089
name, path, sha1 = match(r"^(.*) (.*) ([0-9a-f]{40})$", line).captures
cd(dir) do
f(name, path, sha1)
Expand Down
35 changes: 23 additions & 12 deletions base/io.jl
Expand Up @@ -181,16 +181,16 @@ type EachLine
end
each_line(stream::IO) = EachLine(stream)

start(itr::EachLine) = readline(itr.stream)
function done(itr::EachLine, line)
if !isempty(line)
start(itr::EachLine) = nothing
function done(itr::EachLine, nada)
if !eof(itr.stream)
return false
end
close(itr.stream)
itr.ondone()
true
end
next(itr::EachLine, this_line) = (this_line, readline(itr.stream))
next(itr::EachLine, nada) = (readline(itr.stream), nothing)

function readlines(s, fx::Function...)
a = {}
Expand Down Expand Up @@ -416,24 +416,35 @@ end
readall(filename::String) = open(readall, filename)

## Character streams ##
const _wstmp = Array(Char, 1)
const _chtmp = Array(Char, 1)
function peekchar(s::IOStream)
if ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s, _chtmp) < 0
return char(-1)
end
return _chtmp[1]
end

function peek(s::IOStream)
ccall(:ios_peekc, Int32, (Ptr{Void},), s)
end

function eatwspace(s::IOStream)
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
while status > 0 && iswspace(_wstmp[1])
ch = peekchar(s); status = int(ch)
while status >= 0 && iswspace(ch)
read(s, Char) # advance one character
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
ch = peekchar(s); status = int(ch)
end
end

function eatwspace_comment(s::IOStream, cmt::Char)
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
while status > 0 && (iswspace(_wstmp[1]) || _wstmp[1] == cmt)
if _wstmp[1] == cmt
ch = peekchar(s); status = int(ch)
while status >= 0 && (iswspace(ch) || ch == cmt)
if ch == cmt
readline(s)
else
read(s, Char) # advance one character
end
status = ccall(:ios_peekutf8, Int32, (Ptr{Void}, Ptr{Uint32}), s.ios, _wstmp)
ch = peekchar(s); status = int(ch)
end
end

Expand Down
6 changes: 5 additions & 1 deletion base/stream.jl
Expand Up @@ -20,7 +20,11 @@ typealias UVStream AsyncStream

const _sizeof_uv_pipe = ccall(:jl_sizeof_uv_pipe_t,Int32,())

eof(s::AsyncStream) = !s.open && nb_available(s.buffer)<=0
function eof(s::AsyncStream)
start_reading(s)
wait_readnb(s,1)
!s.open && nb_available(s.buffer)<=0
end

type NamedPipe <: AsyncStream
handle::Ptr{Void}
Expand Down
1 change: 1 addition & 0 deletions src/julia.expmap
Expand Up @@ -22,6 +22,7 @@
ios_getc;
ios_getutf8;
ios_mem;
ios_peekc;
ios_peekutf8;
ios_pos;
ios_printf;
Expand Down
2 changes: 1 addition & 1 deletion src/support/ios.h
Expand Up @@ -129,7 +129,7 @@ int ios_prevutf8(ios_t *s);
DLLEXPORT int ios_putc(int c, ios_t *s);
//wint_t ios_putwc(ios_t *s, wchar_t wc);
DLLEXPORT int ios_getc(ios_t *s);
int ios_peekc(ios_t *s);
DLLEXPORT int ios_peekc(ios_t *s);
//wint_t ios_getwc(ios_t *s);
int ios_ungetc(int c, ios_t *s);
//wint_t ios_ungetwc(ios_t *s, wint_t wc);
Expand Down

1 comment on commit f0e4fcd

@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.

Please sign in to comment.