Skip to content

Commit

Permalink
fixing EOF case when reading arrays. closes #17.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed May 13, 2011
1 parent f9a6b8b commit 64bc3c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 32 deletions.
7 changes: 4 additions & 3 deletions j/io.j
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ end
function read{T}(s::IOStream, a::Array{T})
if isa(T,BitsKind)
nb = numel(a)*sizeof(T)
ccall(:ios_readall, Ulong,
(Ptr{Void}, Ptr{Void}, Ulong), s.ios, a, ulong(nb))
# TODO: detect eof
if ccall(:ios_readall, Ulong,
(Ptr{Void}, Ptr{Void}, Ulong), s.ios, a, ulong(nb)) < nb
throw(EOFError())
end
a
else
invoke(read, (Any, Array), s, a)
Expand Down
33 changes: 4 additions & 29 deletions src/support/ios.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,6 @@ static int _fd_available(long fd)
}
*/

// poll for read, unless forwrite!=0
static void _fd_poll(long fd, int forwrite)
{
#ifndef WIN32
fd_set set;

FD_ZERO(&set);
FD_SET(fd, &set);
if (forwrite)
select(fd+1, NULL, &set, NULL, NULL);
else
select(fd+1, &set, NULL, NULL, NULL);
#else
#endif
}

static int _enonfatal(int err)
{
return (err == EAGAIN || err == EINPROGRESS || err == EINTR ||
Expand Down Expand Up @@ -117,10 +101,8 @@ static int _os_read_all(long fd, void *buf, size_t n, size_t *nread)
n -= got;
*nread += got;
buf += got;
if (err)
if (err || got==0)
return err;
if (got == 0)
_fd_poll(fd, 0);
}
return 0;
}
Expand Down Expand Up @@ -157,8 +139,6 @@ int _os_write_all(long fd, void *buf, size_t n, size_t *nwritten)
buf += wrote;
if (err)
return err;
if (wrote == 0)
_fd_poll(fd, 1);
}
return 0;
}
Expand Down Expand Up @@ -283,7 +263,7 @@ static size_t _ios_read(ios_t *s, char *dest, size_t n, int all)
else
result = _os_read(s->fd, dest, n, &got);
tot += got;
if (got < n)
if (got == 0)
s->_eof = 1;
return tot;
}
Expand All @@ -294,13 +274,8 @@ static size_t _ios_read(ios_t *s, char *dest, size_t n, int all)
return tot;
}
if (got == 0) {
if (all) {
_fd_poll(s->fd, 0);
}
else {
s->_eof = 1;
return tot;
}
s->_eof = 1;
return tot;
}
s->size = got;
}
Expand Down

0 comments on commit 64bc3c0

Please sign in to comment.