Skip to content

Commit

Permalink
Merge pull request #5058 from timholy/readable
Browse files Browse the repository at this point in the history
Fix #5056
  • Loading branch information
timholy committed Dec 7, 2013
2 parents c852b4b + 3d27678 commit 249a2d4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
11 changes: 5 additions & 6 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ fd(s::IOStream) = int(ccall(:jl_ios_fd, Clong, (Ptr{Void},), s.ios))
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))
iswritable(s::IOStream) = !isreadonly(s)
isreadable(s::IOStream) = true
iswritable(s::IOStream) = bool(ccall(:ios_get_writable, Cint, (Ptr{Void},), s.ios))
isreadable(s::IOStream) = bool(ccall(:ios_get_readable, Cint, (Ptr{Void},), s.ios))

function truncate(s::IOStream, n::Integer)
ccall(:ios_trunc, Int32, (Ptr{Void}, Uint), s.ios, n) == 0 ||
Expand Down Expand Up @@ -351,7 +350,7 @@ write(s::IOStream, b::Uint8) = int(ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b,

function write{T}(s::IOStream, a::Array{T})
if isbits(T)
if isreadonly(s)
if !iswritable(s)
error("attempt to write to a read-only IOStream")
end
int(ccall(:ios_write, Uint, (Ptr{Void}, Ptr{Void}, Uint),
Expand All @@ -362,7 +361,7 @@ function write{T}(s::IOStream, a::Array{T})
end

function write(s::IOStream, p::Ptr, nb::Integer)
if isreadonly(s)
if !iswritable(s)
error("attempt to write to a read-only IOStream")
end
int(ccall(:ios_write, Uint, (Ptr{Void}, Ptr{Void}, Uint), s.ios, p, nb))
Expand Down Expand Up @@ -409,7 +408,7 @@ end
## text I/O ##

function write(s::IOStream, c::Char)
if isreadonly(s)
if !iswritable(s)
error("attempt to write to a read-only IOStream")
end
int(ccall(:ios_pututf8, Int32, (Ptr{Void}, Char), s.ios, c))
Expand Down
3 changes: 2 additions & 1 deletion src/julia.expmap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
ios_fd;
ios_file;
ios_flush;
ios_get_readonly;
ios_get_readable;
ios_get_writable;
ios_getc;
ios_getutf8;
ios_mem;
Expand Down
22 changes: 15 additions & 7 deletions src/support/ios.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ DLLEXPORT size_t ios_write_direct(ios_t *dest, ios_t *src)

size_t ios_write(ios_t *s, const char *data, size_t n)
{
if (s->readonly) return 0;
if (!s->writable) return 0;
if (n == 0) return 0;
size_t space;
size_t wrote = 0;
Expand Down Expand Up @@ -687,17 +687,22 @@ int ios_bufmode(ios_t *s, bufmode_t mode)
return 0;
}

int ios_get_readonly(ios_t *s)
int ios_get_readable(ios_t *s)
{
return s->readonly;
return s->readable;
}

int ios_get_writable(ios_t *s)
{
return s->writable;
}

void ios_set_readonly(ios_t *s)
{
if (s->readonly) return;
if (!s->writable) return;
ios_flush(s);
s->state = bst_none;
s->readonly = 1;
s->writable = 0;
}

static size_t ios_copy_(ios_t *to, ios_t *from, size_t nbytes, bool_t all)
Expand Down Expand Up @@ -786,8 +791,9 @@ static void _ios_init(ios_t *s)
s->ownbuf = 1;
s->ownfd = 0;
s->_eof = 0;
s->readable = 1;
s->writable = 1;
s->rereadable = 0;
s->readonly = 0;
}

/* stream object initializers. we do no allocation. */
Expand All @@ -809,8 +815,10 @@ ios_t *ios_file(ios_t *s, char *fname, int rd, int wr, int create, int trunc)
if (fd == -1)
goto open_file_err;
s = ios_fd(s, fd, 1, 1);
if (!rd)
s->readable = 0;
if (!wr)
s->readonly = 1;
s->writable = 0;
return s;
open_file_err:
s->fd = -1;
Expand Down
6 changes: 4 additions & 2 deletions src/support/ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ typedef struct {
// to be a pointer
long fd;

unsigned char readonly:1;
unsigned char readable:1;
unsigned char writable:1;
unsigned char ownbuf:1;
unsigned char ownfd:1;
unsigned char _eof:1;
Expand Down Expand Up @@ -77,7 +78,8 @@ DLLEXPORT char *ios_takebuf(ios_t *s, size_t *psize); // release buffer to call
// set buffer space to use
DLLEXPORT int ios_setbuf(ios_t *s, char *buf, size_t size, int own);
DLLEXPORT int ios_bufmode(ios_t *s, bufmode_t mode);
DLLEXPORT int ios_get_readonly(ios_t *s);
DLLEXPORT int ios_get_readable(ios_t *s);
DLLEXPORT int ios_get_writable(ios_t *s);
DLLEXPORT void ios_set_readonly(ios_t *s);
DLLEXPORT size_t ios_copy(ios_t *to, ios_t *from, size_t nbytes);
DLLEXPORT size_t ios_copyall(ios_t *to, ios_t *from);
Expand Down

0 comments on commit 249a2d4

Please sign in to comment.