picrin/extlib/benz/port.c

373 lines
8.2 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-18 10:20:15 -05:00
#include "picrin/object.h"
2014-08-25 00:38:09 -04:00
2016-02-19 00:50:12 -05:00
#undef EOF
#define EOF (-1)
2015-06-18 16:00:36 -04:00
struct pic_port *
2016-02-18 15:54:50 -05:00
pic_make_port(pic_state *pic, xFILE *file)
{
struct pic_port *port;
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT);
port->file = file;
2014-08-25 00:38:09 -04:00
return port;
}
void
pic_close_port(pic_state *pic, struct pic_port *port)
{
2016-02-18 15:54:50 -05:00
if (port->file->flag == 0) {
2015-06-18 12:04:04 -04:00
return;
}
2015-06-18 13:05:56 -04:00
if (xfclose(pic, port->file) == EOF) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "close-port: failure");
2014-08-25 00:38:09 -04:00
}
}
static pic_value
pic_port_input_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
2016-02-18 15:54:50 -05:00
if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_READ) != 0) {
return pic_true_value(pic);
2016-02-18 15:54:50 -05:00
} else {
return pic_false_value(pic);
2014-08-25 00:38:09 -04:00
}
}
static pic_value
pic_port_output_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
2016-02-18 15:54:50 -05:00
if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_WRITE) != 0) {
return pic_true_value(pic);
2014-08-25 00:38:09 -04:00
}
else {
return pic_false_value(pic);
2014-08-25 00:38:09 -04:00
}
}
static pic_value
pic_port_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_port_p(pic, v));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_eof_object_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_eof_p(pic, v));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_eof_object(pic_state *pic)
{
pic_get_args(pic, "");
return pic_eof_object(pic);
2014-08-25 00:38:09 -04:00
}
2014-09-08 13:12:51 -04:00
static pic_value
pic_port_port_open_p(pic_state *pic)
{
struct pic_port *port;
pic_get_args(pic, "p", &port);
2016-02-18 15:54:50 -05:00
return pic_bool_value(pic, port->file->flag != 0);
2014-09-08 13:12:51 -04:00
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_port_close_port(pic_state *pic)
{
struct pic_port *port;
pic_get_args(pic, "p", &port);
pic_close_port(pic, port);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
2016-02-18 15:54:50 -05:00
#define assert_port_profile(port, flags, caller) do { \
if ((port->file->flag & (flags)) != (flags)) { \
switch (flags) { \
case X_WRITE: \
2014-09-16 10:43:15 -04:00
pic_errorf(pic, caller ": expected output port"); \
2016-02-18 15:54:50 -05:00
case X_READ: \
2014-09-16 10:43:15 -04:00
pic_errorf(pic, caller ": expected input port"); \
2014-08-25 00:38:09 -04:00
} \
} \
2016-02-18 15:54:50 -05:00
if (port->file->flag == 0) { \
2015-06-18 10:26:31 -04:00
pic_errorf(pic, caller ": expected open port"); \
2014-08-25 00:38:09 -04:00
} \
} while (0)
static pic_value
2016-02-18 15:54:50 -05:00
pic_port_open_input_bytevector(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
struct pic_blob *blob;
2016-02-18 15:54:50 -05:00
xFILE *file;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "b", &blob);
2016-02-18 15:54:50 -05:00
file = xfopen_buf(pic, (const char *)blob->data, blob->len, "r");
2014-08-25 00:38:09 -04:00
2016-02-18 15:54:50 -05:00
return pic_obj_value(pic_make_port(pic, file));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_open_output_bytevector(pic_state *pic)
{
pic_get_args(pic, "");
2016-02-18 15:54:50 -05:00
return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w")));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_get_output_bytevector(pic_state *pic)
{
struct pic_port *port = pic_stdout(pic);
2016-02-18 15:54:50 -05:00
const char *buf;
int len;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "|p", &port);
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_WRITE, "get-output-bytevector");
2014-08-25 00:38:09 -04:00
2016-02-18 15:54:50 -05:00
if (xfget_buf(pic, port->file, &buf, &len) < 0) {
pic_errorf(pic, "port was not created by open-output-bytevector");
2015-05-28 10:28:55 -04:00
}
2016-02-18 15:54:50 -05:00
return pic_obj_value(pic_blob_value(pic, (unsigned char *)buf, len));
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-18 15:54:50 -05:00
pic_port_read_u8(pic_state *pic){
2014-08-25 00:38:09 -04:00
struct pic_port *port = pic_stdin(pic);
int c;
pic_get_args(pic, "|p", &port);
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_READ, "read-u8");
2015-06-18 13:05:56 -04:00
if ((c = xfgetc(pic, port->file)) == EOF) {
return pic_eof_object(pic);
2014-08-25 00:38:09 -04:00
}
return pic_int_value(pic, c);
2014-08-25 00:38:09 -04:00
}
static pic_value
2016-02-18 15:54:50 -05:00
pic_port_peek_u8(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
int c;
struct pic_port *port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_READ, "peek-u8");
2014-08-25 00:38:09 -04:00
2015-06-18 13:05:56 -04:00
c = xfgetc(pic, port->file);
2014-08-25 00:38:09 -04:00
if (c == EOF) {
return pic_eof_object(pic);
2014-08-25 00:38:09 -04:00
}
else {
xungetc(c, port->file);
return pic_int_value(pic, c);
2014-08-25 00:38:09 -04:00
}
}
static pic_value
2016-02-18 15:54:50 -05:00
pic_port_u8_ready_p(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
struct pic_port *port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_READ, "u8-ready?");
2014-08-25 00:38:09 -04:00
2016-02-18 15:54:50 -05:00
return pic_true_value(pic); /* FIXME: always returns #t */
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_read_bytevector(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
struct pic_port *port = pic_stdin(pic);
struct pic_blob *blob;
2015-08-26 06:04:27 -04:00
int k, i;
2014-08-25 00:38:09 -04:00
2015-08-26 06:04:27 -04:00
pic_get_args(pic, "i|p", &k, &port);
2014-08-25 00:38:09 -04:00
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_READ, "read-bytevector");
2014-08-25 00:38:09 -04:00
2016-02-18 09:59:33 -05:00
blob = pic_blob_value(pic, 0, k);
2014-08-25 00:38:09 -04:00
2015-06-18 13:05:56 -04:00
i = xfread(pic, blob->data, sizeof(char), k, port->file);
2014-09-26 03:13:53 -04:00
if (i == 0) {
return pic_eof_object(pic);
2014-08-25 00:38:09 -04:00
}
else {
pic_realloc(pic, blob->data, i);
blob->len = i;
return pic_obj_value(blob);
}
}
static pic_value
pic_port_read_bytevector_ip(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
struct pic_port *port;
struct pic_blob *bv;
char *buf;
2015-08-26 06:04:27 -04:00
int n, start, end, i, len;
2014-08-25 00:38:09 -04:00
2015-08-26 06:04:27 -04:00
n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 1:
port = pic_stdin(pic);
case 2:
start = 0;
case 3:
2014-09-27 06:48:58 -04:00
end = bv->len;
2014-08-25 00:38:09 -04:00
}
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_READ, "read-bytevector!");
2014-09-26 03:13:53 -04:00
2014-09-27 06:48:58 -04:00
if (end < start) {
2014-09-26 03:13:53 -04:00
pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index");
}
2014-09-27 06:48:58 -04:00
len = end - start;
2014-08-25 00:38:09 -04:00
buf = pic_calloc(pic, len, sizeof(char));
2015-06-18 13:05:56 -04:00
i = xfread(pic, buf, sizeof(char), len, port->file);
2014-08-25 00:38:09 -04:00
memcpy(bv->data + start, buf, i);
pic_free(pic, buf);
2014-09-26 03:13:53 -04:00
if (i == 0) {
return pic_eof_object(pic);
2014-08-25 00:38:09 -04:00
}
else {
return pic_int_value(pic, i);
2014-08-25 00:38:09 -04:00
}
}
static pic_value
2016-02-18 15:54:50 -05:00
pic_port_write_u8(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
int i;
struct pic_port *port = pic_stdout(pic);
pic_get_args(pic, "i|p", &i, &port);
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_WRITE, "write-u8");
2014-08-25 00:38:09 -04:00
2015-06-18 13:05:56 -04:00
xfputc(pic, i, port->file);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_write_bytevector(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
struct pic_blob *blob;
struct pic_port *port;
2015-08-26 06:04:27 -04:00
int n, start, end, i;
2014-08-25 00:38:09 -04:00
2015-08-26 06:04:27 -04:00
n = pic_get_args(pic, "b|pii", &blob, &port, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 1:
port = pic_stdout(pic);
case 2:
start = 0;
case 3:
2014-09-27 06:48:58 -04:00
end = blob->len;
2014-08-25 00:38:09 -04:00
}
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_WRITE, "write-bytevector");
2014-08-25 00:38:09 -04:00
for (i = start; i < end; ++i) {
2015-06-18 13:05:56 -04:00
xfputc(pic, blob->data[i], port->file);
2014-08-25 00:38:09 -04:00
}
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_port_flush(pic_state *pic)
{
struct pic_port *port = pic_stdout(pic);
pic_get_args(pic, "|p", &port);
2016-02-18 15:54:50 -05:00
assert_port_profile(port, X_WRITE, "flush-output-port");
2014-08-25 00:38:09 -04:00
2015-06-18 13:05:56 -04:00
xfflush(pic, port->file);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
2016-02-18 15:54:50 -05:00
static pic_value
coerce_port(pic_state *pic)
{
struct pic_port *port;
pic_get_args(pic, "p", &port);
return pic_obj_value(port);
}
#define DEFINE_PORT(pic, name, file) \
pic_defvar(pic, name, pic_obj_value(pic_make_port(pic, file)), coerce)
2014-08-25 00:38:09 -04:00
void
pic_init_port(pic_state *pic)
{
2016-02-18 15:54:50 -05:00
struct pic_proc *coerce = pic_lambda(pic, coerce_port, 0);
DEFINE_PORT(pic, "current-input-port", xstdin);
DEFINE_PORT(pic, "current-output-port", xstdout);
DEFINE_PORT(pic, "current-error-port", xstderr);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "port?", pic_port_port_p);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "input-port?", pic_port_input_port_p);
pic_defun(pic, "output-port?", pic_port_output_port_p);
2014-09-08 13:12:51 -04:00
pic_defun(pic, "port-open?", pic_port_port_open_p);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "close-port", pic_port_close_port);
pic_defun(pic, "eof-object?", pic_port_eof_object_p);
pic_defun(pic, "eof-object", pic_port_eof_object);
2014-08-25 00:38:09 -04:00
/* input */
2016-02-18 15:54:50 -05:00
pic_defun(pic, "read-u8", pic_port_read_u8);
pic_defun(pic, "peek-u8", pic_port_peek_u8);
pic_defun(pic, "u8-ready?", pic_port_u8_ready_p);
pic_defun(pic, "read-bytevector", pic_port_read_bytevector);
pic_defun(pic, "read-bytevector!", pic_port_read_bytevector_ip);
2014-08-25 00:38:09 -04:00
/* output */
2016-02-18 15:54:50 -05:00
pic_defun(pic, "write-u8", pic_port_write_u8);
pic_defun(pic, "write-bytevector", pic_port_write_bytevector);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "flush-output-port", pic_port_flush);
2016-02-18 15:54:50 -05:00
/* string I/O */
pic_defun(pic, "open-input-bytevector", pic_port_open_input_bytevector);
pic_defun(pic, "open-output-bytevector", pic_port_open_output_bytevector);
pic_defun(pic, "get-output-bytevector", pic_port_get_output_bytevector);
2014-08-25 00:38:09 -04:00
}