static pic_value pic_port_read_string(pic_state *pic){ struct pic_port *port = pic_stdin(pic), *buf; pic_str *str; int k, i; int c; pic_get_args(pic, "i|p", &k, &port); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-stritg"); c = EOF; buf = pic_open_output_string(pic); for(i = 0; i < k; ++i) { if((c = xfgetc(port->file)) == EOF){ break; } xfputc(c, buf->file); } str = pic_get_output_string(pic, buf); if (pic_strlen(str) == 0 && c == EOF) { return pic_eof_object(); } else { return pic_obj_value(str); } }
static pic_value pic_port_open_output_string(pic_state *pic) { struct pic_port *port; pic_get_args(pic, ""); port = pic_open_output_string(pic); return pic_obj_value(port); }
pic_str * pic_vformat(pic_state *pic, const char *fmt, va_list ap) { struct pic_port *port; pic_str *str; port = pic_open_output_string(pic); pic_vfformat(pic, port->file, fmt, ap); str = pic_get_output_string(pic, port); pic_close_port(pic, port); return str; }
pic_value pic_xvformat(pic_state *pic, const char *fmt, va_list ap) { struct pic_port *port; pic_value irrs; port = pic_open_output_string(pic); irrs = pic_xvfformat(pic, port->file, fmt, ap); irrs = pic_cons(pic, pic_obj_value(pic_get_output_string(pic, port)), irrs); pic_close_port(pic, port); return irrs; }
static pic_value pic_port_read_line(pic_state *pic) { int c; struct pic_port *port = pic_stdin(pic), *buf; struct pic_string *str; pic_get_args(pic, "|p", &port); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-line"); buf = pic_open_output_string(pic); while ((c = xfgetc(port->file)) != EOF && c != '\n') { xfputc(c, buf->file); } str = pic_get_output_string(pic, buf); if (pic_strlen(str) == 0 && c == EOF) { return pic_eof_object(); } else { return pic_obj_value(str); } }