Skip to content

Commit

Permalink
Use error constants
Browse files Browse the repository at this point in the history
  • Loading branch information
emboss committed Nov 29, 2012
1 parent 906ba00 commit 90d9469
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 57 deletions.
8 changes: 3 additions & 5 deletions ext/binyo/binyo-error.h
Expand Up @@ -12,15 +12,13 @@
#define _BINYO_ERROR_H_

#define BINYO_OK 1
#define BINYO_ERR 0
#define BINYO_ERR -1

#define BINYO_IO_READ_ERR -2
#define BINYO_IO_READ_EOF -1

#define BINYO_IO_WRITE_ERR -1
#define BINYO_IO_EOF -2

int binyo_has_errors(void);
int binyo_error_message(char *buf, int buf_len);
void binyo_error_clear(void);

#endif /* BINYO_ERROR_H */

40 changes: 19 additions & 21 deletions ext/binyo/io.c
Expand Up @@ -41,15 +41,14 @@ int_read_all(binyo_instream *in, VALUE vbuf, VALUE *out)
}

xfree(buf);
if (r < -1) return 0;
if (r == BINYO_ERR) return BINYO_ERR;
*out = vbuf;
return 1;
return BINYO_OK;
}

static int
int_rb_read_generic(binyo_instream *in, VALUE vlen, VALUE vbuf, VALUE *out)
{

long len;
size_t tlen;
ssize_t r;
Expand All @@ -66,39 +65,39 @@ int_rb_read_generic(binyo_instream *in, VALUE vlen, VALUE vbuf, VALUE *out)
len = NUM2LONG(vlen);
if (len < 0) {
binyo_error_add("Negative length given");
return 0;
return BINYO_ERR;
}
if ((size_t) len > SIZE_MAX) {
binyo_error_add("Size too large: %ld", len);
return 0;
return BINYO_ERR;
}

tlen = (size_t) len;
if (len == 0) {
rb_str_resize(vbuf, 0);
*out = vbuf;
return 1;
return BINYO_OK;
}

buf = ALLOC_N(uint8_t, tlen);
r = binyo_instream_read(in, buf, tlen);

if (r == 0) {
if (r == BINYO_ERR) {
binyo_error_add("Error while reading from stream");
xfree(buf);
return 0;
return BINYO_ERR;
}
else if (r == -1) {
else if (r == BINYO_IO_EOF) {
xfree(buf);
rb_str_resize(vbuf, 0);
*out = Qnil;
return 1;
return BINYO_OK;
}
else {
rb_str_buf_cat(vbuf, (const char *)buf, r);
xfree(buf);
*out = vbuf;
return 1;
return BINYO_OK;
}
}

Expand All @@ -122,7 +121,7 @@ binyo_instream_read(binyo_instream *in, uint8_t *buf, size_t len)

if (len > SSIZE_MAX) {
binyo_error_add("Size too large: %ld", len);
return -2;
return BINYO_ERR;
}
return in->methods->read(in, buf, len);
}
Expand All @@ -134,7 +133,7 @@ int_gets_generic(binyo_instream *in, char *line, size_t len)
char *p = line;
char *end = line + len;

if (!line) return -2;
if (!line) return BINYO_ERR;

while (p < end) {
if ((r = in->methods->read(in, (uint8_t *) p, 1)) < 0)
Expand All @@ -147,9 +146,8 @@ int_gets_generic(binyo_instream *in, char *line, size_t len)
}
}

if (r < -1) return -2;
if (ret == 0 && r == -1)
return -1;
if (r == BINYO_ERR) return BINYO_ERR;
if (ret == 0 && r == BINYO_IO_EOF) return BINYO_IO_EOF;

/* normalize CRLF */
if (*p == '\n' && *(p - 1) == '\r')
Expand All @@ -164,7 +162,7 @@ binyo_instream_gets(binyo_instream *in, char *line, size_t len)
int_check_stream(in);
if (len > SSIZE_MAX) {
binyo_error_add("Size too large: %ld", len);
return -2;
return BINYO_ERR;
}
if (in->methods->gets) {
return in->methods->gets(in, line, len);
Expand Down Expand Up @@ -235,7 +233,7 @@ binyo_outstream_write(binyo_outstream *out, uint8_t *buf, size_t len)
int_check_stream_has(out, write);
if (len > SSIZE_MAX) {
binyo_error_add("Size too large: %ld", len);
return -1;
return BINYO_ERR;
}
return out->methods->write(out, buf, len);
}
Expand All @@ -251,12 +249,12 @@ binyo_outstream_rb_write(binyo_outstream *out, VALUE vbuf, VALUE *ret)
else {
ssize_t w;
w = binyo_outstream_write(out, (uint8_t *) RSTRING_PTR(vbuf), RSTRING_LEN(vbuf));
if (w < 0) {
if (w == BINYO_ERR) {
binyo_error_add("Error while writing to stream");
return 0;
return BINYO_ERR;
}
*ret = LONG2NUM(w);
return 1;
return BINYO_OK;
}
}

Expand Down
8 changes: 4 additions & 4 deletions ext/binyo/io_buffer.c
Expand Up @@ -79,13 +79,13 @@ int_buffer_grow(binyo_byte_buffer *buffer, size_t cur_len)
ssize_t
binyo_buffer_write(binyo_byte_buffer *buffer, uint8_t *b, size_t len)
{
if (!b) return BINYO_IO_WRITE_ERR;
if (!b) return BINYO_ERR;
if (len == 0) return 0;
if (len > SSIZE_MAX) return BINYO_IO_WRITE_ERR;
if (len > SSIZE_MAX) return BINYO_ERR;

if (buffer->limit - buffer->size < len) {
if (!int_buffer_grow(buffer, len))
return BINYO_IO_WRITE_ERR;
if (int_buffer_grow(buffer, len) == BINYO_ERR)
return BINYO_ERR;
}

memcpy(buffer->data + buffer->size, b, len);
Expand Down
10 changes: 6 additions & 4 deletions ext/binyo/io_in_bytes.c
Expand Up @@ -72,18 +72,19 @@ int_bytes_read(binyo_instream *instream, uint8_t *buf, size_t len)

int_safe_cast(in, instream);

if (!buf) return BINYO_IO_READ_ERR;
if (!buf) return BINYO_ERR;

src = in->src;

if (in->num_read == src->len)
return BINYO_IO_READ_EOF;
return BINYO_IO_EOF;

/* Check for SSIZE_MAX already done in io.c */
to_read = src->len - in->num_read < len ? src->len - in->num_read : len;
memcpy(buf, src->p, to_read);
src->p += to_read;
in->num_read += to_read;
return to_read;
return (ssize_t) to_read;
}

static ssize_t
Expand All @@ -100,12 +101,13 @@ int_bytes_gets(binyo_instream *instream, char *line, size_t len)
src = in->src;

if (in->num_read == src->len)
return BINYO_IO_READ_EOF;
return BINYO_IO_EOF;

d = line;
to_read = src->len - in->num_read < len ? src->len - in->num_read : len;
end = d + to_read;

/* ret cannot exceed SSIZE_MAX, check for len already done in io.c */
while (d < end) {
*d = *(src->p);
src->p++;
Expand Down
2 changes: 1 addition & 1 deletion ext/binyo/io_in_cache.c
Expand Up @@ -75,7 +75,7 @@ int_cache_read(binyo_instream *instream, uint8_t *buf, size_t len)

int_safe_cast(in, instream);

if (!buf) return BINYO_IO_READ_ERR;
if (!buf) return BINYO_ERR;

read = binyo_instream_read(in->inner, buf, len);
if (read > 0)
Expand Down
12 changes: 6 additions & 6 deletions ext/binyo/io_in_fd.c
Expand Up @@ -72,18 +72,18 @@ int_fd_read(binyo_instream *instream, uint8_t *buf, size_t len)
binyo_instream_fd *in;

int_safe_cast(in, instream);
if (!buf) return BINYO_IO_READ_ERR;
if (!buf) return BINYO_ERR;

fd = in->fd;
binyo_clear_sys_error();
r = read(fd, buf, len);

if (r == -1) {
binyo_add_io_error();
return BINYO_IO_READ_ERR;
return BINYO_ERR;
}
else if (r == 0) {
return BINYO_IO_READ_EOF;
return BINYO_IO_EOF;
}
else {
return r;
Expand All @@ -100,7 +100,7 @@ int_fd_gets(binyo_instream *instream, char *line, size_t len)
char *end = line + len;

int_safe_cast(in, instream);
if (!line) return BINYO_IO_READ_ERR;
if (!line) return BINYO_ERR;

fd = in->fd;
binyo_clear_sys_error();
Expand All @@ -113,11 +113,11 @@ int_fd_gets(binyo_instream *instream, char *line, size_t len)
}

if (r == -1) {
return BINYO_IO_READ_ERR;
return BINYO_ERR;
}

if (ret == 0 && r == 0)
return BINYO_IO_READ_EOF;
return BINYO_IO_EOF;

if (*p == '\n' && *(p - 1) == '\r')
ret--;
Expand Down
6 changes: 3 additions & 3 deletions ext/binyo/io_in_generic.c
Expand Up @@ -84,19 +84,19 @@ int_io_read(binyo_instream *instream, uint8_t *buf, size_t len)

int_safe_cast(in, instream);

if (!buf) return BINYO_IO_READ_ERR;
if (!buf) return BINYO_ERR;

vlen = LONG2NUM(len);
vbuf = rb_str_new2("");
rb_enc_associate(vbuf, rb_ascii8bit_encoding());

if (!int_io_rb_read_impl(in, vlen, vbuf, &read)) {
binyo_error_add("Error while reading from IO");
return BINYO_IO_READ_ERR;
return BINYO_ERR;
}

if (NIL_P(read)) {
return BINYO_IO_READ_EOF;
return BINYO_IO_EOF;
}
else {
ssize_t r = (ssize_t) RSTRING_LEN(read);
Expand Down
12 changes: 6 additions & 6 deletions ext/binyo/io_in_seq.c
Expand Up @@ -93,8 +93,8 @@ int_do_read(binyo_instream_seq *in, uint8_t *buf, size_t len)
buf += read;
}

if (read < -1) return BINYO_IO_READ_ERR;
if (total == 0) return BINYO_IO_READ_EOF;
if (read == BINYO_ERR) return BINYO_ERR;
if (total == 0 && read == BINYO_IO_EOF) return BINYO_IO_EOF;
return total;
}

Expand All @@ -107,15 +107,15 @@ int_seq_read(binyo_instream *instream, uint8_t *buf, size_t len)

int_safe_cast(in, instream);

if (!buf) return BINYO_IO_READ_ERR;
if (!buf) return BINYO_ERR;

while (total < len) {
read = int_do_read(in, buf, len - total);
if (read < -1) return BINYO_IO_READ_ERR;
if (read == -1) {
if (read == BINYO_ERR) return BINYO_ERR;
if (read == BINYO_IO_EOF) {
in->i++;
if (in->i == in->num) {
return BINYO_IO_READ_EOF;
return BINYO_IO_EOF;
}
else {
in->active = in->streams[in->i];
Expand Down
4 changes: 2 additions & 2 deletions ext/binyo/io_out_fd.c
Expand Up @@ -69,7 +69,7 @@ int_fd_write(binyo_outstream *outstream, uint8_t *buf, size_t len)

int_safe_cast(out, outstream);

if (!buf) return BINYO_IO_WRITE_ERR;
if (!buf) return BINYO_ERR;

fd = out->fd;
binyo_clear_sys_error();
Expand All @@ -78,7 +78,7 @@ int_fd_write(binyo_outstream *outstream, uint8_t *buf, size_t len)

if (w < 0) {
binyo_add_io_error();
return BINYO_IO_WRITE_ERR;
return BINYO_ERR;
}
else {
return w;
Expand Down
8 changes: 3 additions & 5 deletions ext/binyo/io_out_generic.c
Expand Up @@ -55,15 +55,13 @@ static ssize_t
int_io_write(binyo_outstream *outstream, uint8_t *buf, size_t len)
{
VALUE vbuf, ret;
int w;

if (!buf) return BINYO_IO_WRITE_ERR;
if (!buf) return BINYO_ERR;

vbuf = rb_str_new((const char *)buf, len);
w = int_io_rb_write(outstream, vbuf, &ret);
if (!w) {
if(int_io_rb_write(outstream, vbuf, &ret) == BINYO_ERR) {
binyo_error_add("Error while writing to IO");
return BINYO_IO_WRITE_ERR;
return BINYO_ERR;
}
return NUM2LONG(ret);
}
Expand Down

0 comments on commit 90d9469

Please sign in to comment.