diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-05-29 08:13:58 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-05-29 08:13:58 -0700 |
commit | a3d38b3ffebe842b8e5d5ddb8e273c285302939e (patch) | |
tree | e7e17a745e984c9a80ad458a6c631f3633555dfe | |
parent | abb24b028898ccf39a82234e3bcf734153bbf9c0 (diff) | |
download | txr-a3d38b3ffebe842b8e5d5ddb8e273c285302939e.tar.gz txr-a3d38b3ffebe842b8e5d5ddb8e273c285302939e.tar.bz2 txr-a3d38b3ffebe842b8e5d5ddb8e273c285302939e.zip |
Support byte oriented mode in dgram sockets.
* socket.c (struct dgram_stream): New member, is_byte_oriented.
(dgram_get_char): In byte oriented mode, just get one byte
and convert to a character just like stdio_get_char: zero
goes to U+DC00, and 0x01-0xFF go to U+0001 to U+FFFF.
(dgram_get_prop, dgram_set_prop): Handle :byte-oriented
property for getting and setting the is_byte_oriented flag.
-rw-r--r-- | socket.c | 22 | ||||
-rw-r--r-- | stream.h | 2 |
2 files changed, 20 insertions, 4 deletions
@@ -70,6 +70,7 @@ struct dgram_stream { int rx_max, rx_size, rx_pos; int tx_pos; unsigned is_connected : 1; + unsigned is_byte_oriented : 1; }; val sockaddr_in_s, sockaddr_in6_s, sockaddr_un_s, addrinfo_s; @@ -444,8 +445,17 @@ static val dgram_get_char(val stream) if (d->unget_c) { return rcyc_pop(&d->unget_c); } else { - wint_t ch = utf8_decode(&d->ud, dgram_get_byte_callback, - coerce(mem_t *, d)); + wint_t ch; + + if (d->is_byte_oriented) { + ch = dgram_get_byte_callback(coerce(mem_t *, d)); + if (ch == 0) + ch = 0xDC00; + } else { + ch = utf8_decode(&d->ud, dgram_get_byte_callback, + coerce(mem_t *, d)); + } + return (ch != WEOF) ? chr(ch) : nil; } } @@ -543,6 +553,8 @@ static val dgram_get_prop(val stream, val ind) return format(nil, lit("connected ~s"), d->peer, nao); return lit("disconnected"); + } else if (ind == byte_oriented_k) { + return d->is_byte_oriented ? t : nil; } return nil; @@ -550,10 +562,14 @@ static val dgram_get_prop(val stream, val ind) static val dgram_set_prop(val stream, val ind, val prop) { + struct dgram_stream *d = coerce(struct dgram_stream *, stream->co.handle); + if (ind == addr_k) { - struct dgram_stream *d = coerce(struct dgram_stream *, stream->co.handle); set(mkloc(d->addr, stream), prop); return t; + } else if (ind == byte_oriented_k) { + d->is_byte_oriented = prop ? 1 : 0; + return t; } return nil; @@ -107,7 +107,7 @@ struct stdio_mode { loc lookup_var_l(val env, val sym); extern val from_start_k, from_current_k, from_end_k; -extern val real_time_k, name_k, addr_k, fd_k; +extern val real_time_k, name_k, addr_k, fd_k, byte_oriented_k; extern val format_s; extern val stdio_stream_s; |