DFSCH_DEFINE_PRIMITIVE(http_split_query, NULL) { char* pos; char* uri; DFSCH_STRING_ARG(args, uri); DFSCH_ARG_END(args); pos = strchr(uri, '?'); if (!pos) { return dfsch_list(1, dfsch_make_string_cstr(uri)); } else { return dfsch_list(2, dfsch_make_string_buf(uri, pos-uri), dfsch_make_string_cstr(pos+1)); } }
DFSCH_DEFINE_PRIMITIVE(port_read_buf, NULL){ dfsch_object_t* port; size_t len; char* buf; DFSCH_LONG_ARG(args, len); DFSCH_OBJECT_ARG_OPT(args, port, dfsch_current_input_port()); DFSCH_ARG_END(args); buf = GC_MALLOC_ATOMIC(len); len = dfsch_port_read_buf(port, buf, len); if (len == 0){ return NULL; } return dfsch_make_string_buf(buf, len); }
static dfsch_object_t* regex_substrings(regex_t* regex, char* string, int sub_count, int flags){ regmatch_t *match; int i; int count; dfsch_object_t* vector; match = GC_MALLOC_ATOMIC(sizeof(regmatch_t)*sub_count); if (regexec(regex, string, sub_count, match, flags) == REG_NOMATCH){ GC_FREE(match); return NULL; } count = 0; for (i = 0; i < sub_count; i++){ if (match[i].rm_so == -1) // No more substring matches break; count ++; } vector = dfsch_make_vector(count, NULL); for (i = 0; i < count; i++){ dfsch_vector_set(vector, i, dfsch_vector(3, dfsch_make_number_from_long(match[i].rm_so), dfsch_make_number_from_long(match[i].rm_eo), dfsch_make_string_buf(string+match[i].rm_so, match[i].rm_eo-match[i].rm_so))); } GC_FREE(match); return vector; }
dfsch_object_t* dfsch_csv_read_line(dfsch_object_t* port, dfsch_csv_params_t* params){ char ch; dfsch_object_t* ret = NULL; dfsch_list_collector_t* lc = NULL; partial_string_t ps; if (!params){ params = &default_params; } DFSCH_UNWIND { dfsch_port_batch_read_start(port); start: ch = dfsch_port_batch_read(port); if (ch != -1) { lc = dfsch_make_list_collector(); } else { goto out; } ps_init(&ps); goto field_dispatch; field: ch = dfsch_port_batch_read(port); field_dispatch: if (ch == params->quote){ goto quoted; } else if (ch == params->delim){ dfsch_list_collect(lc, dfsch_make_string_buf(ps.buf, ps.ptr)); ps_init(&ps); } else if (ch == -1 || ch == '\n'){ dfsch_list_collect(lc, dfsch_make_string_buf(ps.buf, ps.ptr)); goto out; } else if (ch == params->escape){ ch = dfsch_port_batch_read(port); if (ch == -1){ dfsch_error("Unexpected end of file", port); } ps_append(&ps, ch); } else { ps_append(&ps, ch); } goto field; quoted: ch = dfsch_port_batch_read(port); if (ch == params->quote){ ch = dfsch_port_batch_read(port); if (ch != params->quote){ goto field_dispatch; } else { ps_append(&ps, ch); } } else if (ch == params->escape){ ch = dfsch_port_batch_read(port); if (ch == -1){ dfsch_error("Unexpected end of file", port); } ps_append(&ps, ch); } else { if (ch == -1){ dfsch_error("Unexpected end of file", port); } ps_append(&ps, ch); } goto quoted; out: if (lc){ ret = dfsch_list_2_vector(dfsch_collected_list(lc)); } else { ret = NULL; } } DFSCH_PROTECT { dfsch_port_batch_read_end(port); } DFSCH_PROTECT_END; return ret; }