make `read-bytevector(!)` efficint, export `u8-ready?`.

This commit is contained in:
Sunrim KIM (keen) 2014-05-26 04:08:39 +09:00
parent cffaccf8ef
commit a73a92cfdb
2 changed files with 20 additions and 22 deletions

View File

@ -511,21 +511,15 @@ static pic_value
pic_port_read_blob(pic_state *pic){ pic_port_read_blob(pic_state *pic){
struct pic_port *port = pic_stdin(pic); struct pic_port *port = pic_stdin(pic);
int k, i; int k, i;
char c, *buf; char *buf;
pic_get_args(pic, "i|p", &k, &port); pic_get_args(pic, "i|p", &k, &port);
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector"); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector");
buf = pic_calloc(pic, k, sizeof(char)); buf = pic_calloc(pic, k, sizeof(char));
for(i = 0; i < k; i++){ i = xfread(buf, sizeof(char), k, port->file);
c = xfgetc(port->file); if ( i == 0 ) {
if( c == EOF ){
break;
}
buf[i] = c;
}
if ( i == 0 && c == EOF) {
return pic_eof_object(); return pic_eof_object();
} }
else { else {
@ -538,8 +532,8 @@ static pic_value
pic_port_read_blob_ip(pic_state *pic){ pic_port_read_blob_ip(pic_state *pic){
struct pic_port *port; struct pic_port *port;
struct pic_blob *bv; struct pic_blob *bv;
int i, n, start, end; int i, n, start, end, len;
char c; char *buf;
n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end); n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end);
switch (n) { switch (n) {
@ -552,19 +546,18 @@ pic_port_read_blob_ip(pic_state *pic){
} }
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!"); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!");
len = end - start;
for(i = start; i < end; i++){ buf = pic_calloc(pic, len, sizeof(char));
c = xfgetc(port->file); i = xfread(buf, sizeof(char), len, port->file);
if( c == EOF ){ memcpy(bv->data + start, buf, i);
break; pic_free(pic, buf);
}
bv->data[i] = c; if ( i == 0) {
}
if ( i == start && c == EOF) {
return pic_eof_object(); return pic_eof_object();
} }
else { else {
return pic_int_value(i - start); return pic_int_value(i);
} }
} }
@ -708,7 +701,7 @@ pic_init_port(pic_state *pic)
pic_defun(pic, "read-string", pic_port_read_string); pic_defun(pic, "read-string", pic_port_read_string);
pic_defun(pic, "read-u8", pic_port_read_byte); pic_defun(pic, "read-u8", pic_port_read_byte);
pic_defun(pic, "peek-u8", pic_port_peek_byte); pic_defun(pic, "peek-u8", pic_port_peek_byte);
/* pic_defun(pic, "u8-ready?", pic_port_byte_ready_p); */ pic_defun(pic, "u8-ready?", pic_port_byte_ready_p);
pic_defun(pic, "read-bytevector", pic_port_read_blob); pic_defun(pic, "read-bytevector", pic_port_read_blob);
pic_defun(pic, "read-bytevector!", pic_port_read_blob_ip); pic_defun(pic, "read-bytevector!", pic_port_read_blob_ip);

View File

@ -24,6 +24,11 @@
(newline) (newline)
(display "read-bytevector!: read size: ") (display "read-bytevector!: read size: ")
(write (read-bytevector! buf byte-port 1 3)) (write (read-bytevector! buf byte-port 1 3))
(display " read content: ") (display ": read content: ")
(write buf)
(newline)
(display "read-bytevector!: read size: ")
(write (read-bytevector! buf byte-port))
(display ": read content: ")
(write buf) (write buf)
(newline)) (newline))