diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2009-11-20 10:01:57 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2009-11-20 10:01:57 -0800 |
commit | aaed1aa81d33693a28d6bd49117e4130b00bfa56 (patch) | |
tree | 6c1767cffb8ff8eeeb802d658f68a951a83ab697 /stream.c | |
parent | e1bf988db6f77edc3da566b8f0dc768d3f887080 (diff) | |
download | txr-aaed1aa81d33693a28d6bd49117e4130b00bfa56.tar.gz txr-aaed1aa81d33693a28d6bd49117e4130b00bfa56.tar.bz2 txr-aaed1aa81d33693a28d6bd49117e4130b00bfa56.zip |
Changing ``obj_t *'' occurences to a ``val'' typedef. (Ideally,
we wouldn't have to declare object variables at all, so why
use an obtuse syntax to do so?)
Diffstat (limited to 'stream.c')
-rw-r--r-- | stream.c | 172 |
1 files changed, 86 insertions, 86 deletions
@@ -41,54 +41,54 @@ #include "stream.h" #include "utf8.h" -obj_t *std_input, *std_output, *std_error; +val std_input, std_output, std_error; struct strm_ops { struct cobj_ops cobj_ops; - obj_t *(*put_string)(obj_t *, obj_t *); - obj_t *(*put_char)(obj_t *, obj_t *); - obj_t *(*get_line)(obj_t *); - obj_t *(*get_char)(obj_t *); - obj_t *(*get_byte)(obj_t *); - obj_t *(*close)(obj_t *, obj_t *); + val (*put_string)(val, val); + val (*put_char)(val, val); + val (*get_line)(val); + val (*get_char)(val); + val (*get_byte)(val); + val (*close)(val, val); }; -static obj_t *common_equal(obj_t *self, obj_t *other) +static val common_equal(val self, val other) { return self == other ? t : nil; } -static void common_destroy(obj_t *obj) +static void common_destroy(val obj) { (void) close_stream(obj, nil); } struct stdio_handle { FILE *f; - obj_t *descr; + val descr; struct utf8_decoder ud; }; -void stdio_stream_print(obj_t *stream, obj_t *out) +void stdio_stream_print(val stream, val out) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; format(out, lit("#<~s ~s>"), stream->co.cls, h->descr, nao); } -void stdio_stream_destroy(obj_t *stream) +void stdio_stream_destroy(val stream) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; common_destroy(stream); free(h); } -void stdio_stream_mark(obj_t *stream) +void stdio_stream_mark(val stream) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; gc_mark(h->descr); } -static obj_t *stdio_maybe_read_error(obj_t *stream) +static val stdio_maybe_read_error(val stream) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; if (h->f == 0) @@ -101,7 +101,7 @@ static obj_t *stdio_maybe_read_error(obj_t *stream) return nil; } -static obj_t *stdio_maybe_write_error(obj_t *stream) +static val stdio_maybe_write_error(val stream) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; if (h->f == 0) @@ -121,7 +121,7 @@ static int stdio_get_char_callback(void *f) return getc((FILE *) f); } -static obj_t *stdio_put_string(obj_t *stream, obj_t *str) +static val stdio_put_string(val stream, val str) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; @@ -136,7 +136,7 @@ static obj_t *stdio_put_string(obj_t *stream, obj_t *str) return stdio_maybe_write_error(stream); } -static obj_t *stdio_put_char(obj_t *stream, obj_t *ch) +static val stdio_put_char(val stream, val ch) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; return h->f != 0 && utf8_encode(c_chr(ch), stdio_put_char_callback, h->f) @@ -175,7 +175,7 @@ static wchar_t *snarf_line(struct stdio_handle *h) return buf; } -static obj_t *stdio_get_line(obj_t *stream) +static val stdio_get_line(val stream) { if (stream->co.handle == 0) { return stdio_maybe_read_error(stream); @@ -188,7 +188,7 @@ static obj_t *stdio_get_line(obj_t *stream) } } -obj_t *stdio_get_char(obj_t *stream) +val stdio_get_char(val stream) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; if (h->f) { @@ -198,7 +198,7 @@ obj_t *stdio_get_char(obj_t *stream) return stdio_maybe_read_error(stream); } -obj_t *stdio_get_byte(obj_t *stream) +val stdio_get_byte(val stream) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; if (h->f) { @@ -208,7 +208,7 @@ obj_t *stdio_get_byte(obj_t *stream) return stdio_maybe_read_error(stream); } -static obj_t *stdio_close(obj_t *stream, obj_t *throw_on_error) +static val stdio_close(val stream, val throw_on_error) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; @@ -238,7 +238,7 @@ static struct strm_ops stdio_ops = { stdio_close }; -static obj_t *pipe_close(obj_t *stream, obj_t *throw_on_error) +static val pipe_close(val stream, val throw_on_error) { struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; @@ -289,21 +289,21 @@ static struct strm_ops pipe_ops = { pipe_close }; -void string_in_stream_mark(obj_t *stream) +void string_in_stream_mark(val stream) { - obj_t *stuff = (obj_t *) stream->co.handle; + val stuff = (val) stream->co.handle; gc_mark(stuff); } -static obj_t *string_in_get_line(obj_t *stream) +static val string_in_get_line(val stream) { - obj_t *pair = (obj_t *) stream->co.handle; - obj_t *string = car(pair); - obj_t *pos = cdr(pair); + val pair = (val) stream->co.handle; + val string = car(pair); + val pos = cdr(pair); /* TODO: broken, should only scan to newline */ if (lt(pos, length(string))) { - obj_t *result = sub_str(string, pos, nil); + val result = sub_str(string, pos, nil); *cdr_l(pair) = length_str(string); return result; } @@ -311,11 +311,11 @@ static obj_t *string_in_get_line(obj_t *stream) return nil; } -static obj_t *string_in_get_char(obj_t *stream) +static val string_in_get_char(val stream) { - obj_t *pair = (obj_t *) stream->co.handle; - obj_t *string = car(pair); - obj_t *pos = cdr(pair); + val pair = (val) stream->co.handle; + val string = car(pair); + val pos = cdr(pair); if (lt(pos, length_str(string))) { *cdr_l(pair) = plus(pos, one); @@ -345,7 +345,7 @@ struct byte_input { size_t index; }; -static obj_t *byte_in_get_byte(obj_t *stream) +static val byte_in_get_byte(val stream) { struct byte_input *bi = (struct byte_input *) stream->co.handle; @@ -375,7 +375,7 @@ struct string_output { size_t fill; }; -static void string_out_stream_destroy(obj_t *stream) +static void string_out_stream_destroy(val stream) { struct string_output *so = (struct string_output *) stream->co.handle; @@ -387,7 +387,7 @@ static void string_out_stream_destroy(obj_t *stream) } } -static obj_t *string_out_put_string(obj_t *stream, obj_t *str) +static val string_out_put_string(val stream, val str) { struct string_output *so = (struct string_output *) stream->co.handle; @@ -416,7 +416,7 @@ static obj_t *string_out_put_string(obj_t *stream, obj_t *str) } } -static obj_t *string_out_put_char(obj_t *stream, obj_t *ch) +static val string_out_put_char(val stream, val ch) { wchar_t mini[2]; mini[0] = c_chr(ch); @@ -438,7 +438,7 @@ static struct strm_ops string_out_ops = { 0, }; -static obj_t *dir_get_line(obj_t *stream) +static val dir_get_line(val stream) { DIR *handle = (DIR *) stream->co.handle; @@ -456,7 +456,7 @@ static obj_t *dir_get_line(obj_t *stream) } } -static obj_t *dir_close(obj_t *stream, obj_t *throw_on_error) +static val dir_close(val stream, val throw_on_error) { if (stream->co.handle != 0) { closedir((DIR *) stream->co.handle); @@ -482,32 +482,32 @@ static struct strm_ops dir_ops = { }; -obj_t *make_stdio_stream(FILE *f, obj_t *descr, obj_t *input, obj_t *output) +val make_stdio_stream(FILE *f, val descr, val input, val output) { struct stdio_handle *h = (struct stdio_handle *) chk_malloc(sizeof *h); - obj_t *stream = cobj((void *) h, stream_t, &stdio_ops.cobj_ops); + val stream = cobj((void *) h, stream_t, &stdio_ops.cobj_ops); h->f = f; h->descr = descr; utf8_decoder_init(&h->ud); return stream; } -obj_t *make_pipe_stream(FILE *f, obj_t *descr, obj_t *input, obj_t *output) +val make_pipe_stream(FILE *f, val descr, val input, val output) { struct stdio_handle *h = (struct stdio_handle *) chk_malloc(sizeof *h); - obj_t *stream = cobj((void *) h, stream_t, &pipe_ops.cobj_ops); + val stream = cobj((void *) h, stream_t, &pipe_ops.cobj_ops); h->f = f; h->descr = descr; utf8_decoder_init(&h->ud); return stream; } -obj_t *make_string_input_stream(obj_t *string) +val make_string_input_stream(val string) { return cobj((void *) cons(string, zero), stream_t, &string_in_ops.cobj_ops); } -obj_t *make_string_byte_input_stream(obj_t *string) +val make_string_byte_input_stream(val string) { type_assert (stringp(string), (lit("~a is not a string"), string, nao)); @@ -521,7 +521,7 @@ obj_t *make_string_byte_input_stream(obj_t *string) } } -obj_t *make_string_output_stream(void) +val make_string_output_stream(void) { struct string_output *so = (struct string_output *) chk_malloc(sizeof *so); so->size = 128; @@ -531,7 +531,7 @@ obj_t *make_string_output_stream(void) return cobj((void *) so, stream_t, &string_out_ops.cobj_ops); } -obj_t *get_string_from_stream(obj_t *stream) +val get_string_from_stream(val stream) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, @@ -539,7 +539,7 @@ obj_t *get_string_from_stream(obj_t *stream) if (stream->co.ops == &string_out_ops.cobj_ops) { struct string_output *so = (struct string_output *) stream->co.handle; - obj_t *out = nil; + val out = nil; stream->co.handle = 0; @@ -552,19 +552,19 @@ obj_t *get_string_from_stream(obj_t *stream) free(so); return out; } else if (stream->co.ops == &string_in_ops.cobj_ops) { - obj_t *pair = (obj_t *) stream->co.handle; + val pair = (val ) stream->co.handle; return pair ? car(pair) : nil; } else { abort(); /* not a string input or output stream */ } } -obj_t *make_dir_stream(DIR *dir) +val make_dir_stream(DIR *dir) { return cobj((void *) dir, stream_t, &dir_ops.cobj_ops); } -obj_t *close_stream(obj_t *stream, obj_t *throw_on_error) +val close_stream(val stream, val throw_on_error) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -576,7 +576,7 @@ obj_t *close_stream(obj_t *stream, obj_t *throw_on_error) } } -obj_t *get_line(obj_t *stream) +val get_line(val stream) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -588,7 +588,7 @@ obj_t *get_line(obj_t *stream) } } -obj_t *get_char(obj_t *stream) +val get_char(val stream) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -600,7 +600,7 @@ obj_t *get_char(obj_t *stream) } } -obj_t *get_byte(obj_t *stream) +val get_byte(val stream) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -612,8 +612,8 @@ obj_t *get_byte(obj_t *stream) } } -static obj_t *vformat_num(obj_t *stream, const char *str, - int width, int left, int pad, int precision) +static val vformat_num(val stream, const char *str, + int width, int left, int pad, int precision) { int len = strlen(str); int truewidth = (width < precision) ? width : precision; @@ -642,7 +642,7 @@ static obj_t *vformat_num(obj_t *stream, const char *str, return t; } -obj_t *vformat_str(obj_t *stream, obj_t *str, int width, int left, +val vformat_str(val stream, val str, int width, int left, int precision) { const wchar_t *cstr = c_str(str); @@ -668,7 +668,7 @@ obj_t *vformat_str(obj_t *stream, obj_t *str, int width, int left, return t; } -obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) +val vformat(val stream, val fmtstr, va_list vl) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -681,12 +681,12 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) } state = vf_init, saved_state = vf_init; int width = 0, precision = 0, digits = 0; int left = 0, zeropad = 0; - long val; + long value; void *ptr; char num_buf[64]; for (;;) { - obj_t *obj; + val obj; wchar_t ch = *fmt++; switch (state) { @@ -730,7 +730,7 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) digits = ch - '0'; continue; case '*': - obj = va_arg(vl, obj_t *); + obj = va_arg(vl, val); width = c_num(obj); state = vf_precision; continue; @@ -749,7 +749,7 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) digits = ch - '0'; continue; case '*': - obj = va_arg(vl, obj_t *); + obj = va_arg(vl, val); width = c_num(obj); precision = vf_precision; continue; @@ -791,27 +791,27 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) state = vf_init; switch (ch) { case 'x': - obj = va_arg(vl, obj_t *); - val = c_num(obj); - sprintf(num_buf, "%lx", val); + obj = va_arg(vl, val); + value = c_num(obj); + sprintf(num_buf, "%lx", value); goto output_num; case 'X': - obj = va_arg(vl, obj_t *); - val = c_num(obj); - sprintf(num_buf, "%lX", val); + obj = va_arg(vl, val); + value = c_num(obj); + sprintf(num_buf, "%lX", value); goto output_num; case 'o': - obj = va_arg(vl, obj_t *); - val = c_num(obj); - sprintf(num_buf, "%lo", val); + obj = va_arg(vl, val); + value = c_num(obj); + sprintf(num_buf, "%lo", value); goto output_num; case 'a': - obj = va_arg(vl, obj_t *); + obj = va_arg(vl, val); if (obj == nao) goto premature; if (nump(obj)) { - val = c_num(obj); - sprintf(num_buf, "%ld", val); + value = c_num(obj); + sprintf(num_buf, "%ld", value); goto output_num; } else if (stringp(obj)) { if (!vformat_str(stream, obj, width, left, precision)) @@ -821,12 +821,12 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) obj_pprint(obj, stream); continue; case 's': - obj = va_arg(vl, obj_t *); + obj = va_arg(vl, val); if (obj == nao) goto premature; if (nump(obj)) { - val = c_num(obj); - sprintf(num_buf, "%ld", val); + value = c_num(obj); + sprintf(num_buf, "%ld", value); if (vformat_num(stream, num_buf, 0, 0, 0, 0)) return nil; continue; @@ -835,8 +835,8 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) continue; case 'p': ptr = va_arg(vl, void *); - val = (int) ptr; - sprintf(num_buf, "0x%lx", val); + value = (int) ptr; + sprintf(num_buf, "0x%lx", value); goto output_num; default: abort(); @@ -855,7 +855,7 @@ obj_t *vformat(obj_t *stream, obj_t *fmtstr, va_list vl) } - if (va_arg(vl, obj_t *) != nao) + if (va_arg(vl, val) != nao) internal_error("unterminated format argument list"); return t; premature: @@ -864,7 +864,7 @@ toobig: internal_error("ridiculous precision or field width in format"); } -obj_t *format(obj_t *stream, obj_t *str, ...) +val format(val stream, val str, ...) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -872,7 +872,7 @@ obj_t *format(obj_t *stream, obj_t *str, ...) { va_list vl; - obj_t *ret; + val ret; va_start (vl, str); ret = vformat(stream, str, vl); va_end (vl); @@ -880,7 +880,7 @@ obj_t *format(obj_t *stream, obj_t *str, ...) } } -obj_t *put_string(obj_t *stream, obj_t *string) +val put_string(val stream, val string) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -892,7 +892,7 @@ obj_t *put_string(obj_t *stream, obj_t *string) } } -obj_t *put_char(obj_t *stream, obj_t *ch) +val put_char(val stream, val ch) { type_check (stream, COBJ); type_assert (stream->co.cls == stream_t, (lit("~a is not a stream"), @@ -904,14 +904,14 @@ obj_t *put_char(obj_t *stream, obj_t *ch) } } -obj_t *put_line(obj_t *stream, obj_t *string) +val put_line(val stream, val string) { return (put_string(stream, string), put_char(stream, chr('\n'))); } void stream_init(void) { - protect(&std_input, &std_output, &std_error, (obj_t **) 0); + protect(&std_input, &std_output, &std_error, (val *) 0); std_input = make_stdio_stream(stdin, string(L"stdin"), t, nil); std_output = make_stdio_stream(stdout, string(L"stdout"), nil, t); std_error = make_stdio_stream(stderr, string(L"stderr"), nil, t); |