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); } }
pic_sym * pic_intern(pic_state *pic, pic_str *str) { xh_entry *e; pic_sym *sym; char *cstr; e = xh_get_str(&pic->syms, pic_str_cstr(str)); if (e) { sym = xh_val(e, pic_sym *); pic_gc_protect(pic, pic_obj_value(sym)); return sym; } cstr = pic_malloc(pic, pic_strlen(str) + 1); strcpy(cstr, pic_str_cstr(str)); sym = pic_make_symbol(pic, str); xh_put_str(&pic->syms, cstr, &sym); return sym; }
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); } }