slls_t* split_csvlite_header_line_multi_ifs(char* line, char* ifs, int ifslen, int allow_repeat_ifs) { slls_t* plist = slls_alloc(); if (*line == 0) // empty string splits to empty list return plist; char* p = line; if (allow_repeat_ifs) { while (streqn(p, ifs, ifslen)) p += ifslen; } char* start = p; for ( ; *p; p++) { if (streqn(p, ifs, ifslen)) { *p = 0; p += ifslen; if (allow_repeat_ifs) { while (streqn(p, ifs, ifslen)) p += ifslen; } slls_add_no_free(plist, start); start = p; } } if (allow_repeat_ifs && *start == 0) { ; // OK } else { slls_add_no_free(plist, start); } return plist; }
static slls_t* lrec_reader_mmap_csvlite_get_header_multi_seps(file_reader_mmap_state_t* phandle, lrec_reader_mmap_csvlite_state_t* pstate) { char* irs = pstate->irs; char* ifs = pstate->ifs; int irslen = pstate->irslen; int ifslen = pstate->ifslen; int allow_repeat_ifs = pstate->allow_repeat_ifs; slls_t* pheader_names = slls_alloc(); while ((phandle->eof - phandle->sol) >= irslen && streqn(phandle->sol, irs, irslen)) { phandle->sol += irslen; pstate->ilno++; } char* p = phandle->sol; if (allow_repeat_ifs) { while (streqn(p, ifs, ifslen)) p += ifslen; } char* header_name = p; for ( ; p < phandle->eof && *p; ) { if (streqn(p, irs, irslen)) { *p = 0; phandle->sol = p + irslen; pstate->ilno++; break; } else if (streqn(p, ifs, ifslen)) { *p = 0; slls_add_no_free(pheader_names, header_name); p += ifslen; if (allow_repeat_ifs) { while (streqn(p, ifs, ifslen)) p += ifslen; } header_name = p; } else { p++; } } slls_add_no_free(pheader_names, header_name); return pheader_names; }
slls_t* mlr_reference_keys_from_record(lrec_t* prec) { slls_t* plist = slls_alloc(); for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) { slls_add_no_free(plist, pe->key); } return plist; }
static slls_t* lrec_reader_mmap_csvlite_get_header_single_seps(file_reader_mmap_state_t* phandle, lrec_reader_mmap_csvlite_state_t* pstate) { char irs = pstate->irs[0]; char ifs = pstate->ifs[0]; int allow_repeat_ifs = pstate->allow_repeat_ifs; slls_t* pheader_names = slls_alloc(); while (phandle->sol < phandle->eof && *phandle->sol == irs) { phandle->sol++; pstate->ilno++; } char* p = phandle->sol; if (allow_repeat_ifs) { while (*p == ifs) p++; } char* header_name = p; for ( ; p < phandle->eof && *p; ) { if (*p == irs) { *p = 0; phandle->sol = p+1; pstate->ilno++; break; } else if (*p == ifs) { *p = 0; slls_add_no_free(pheader_names, header_name); p++; if (allow_repeat_ifs) { while (*p == ifs) p++; } header_name = p; } else { p++; } } slls_add_no_free(pheader_names, header_name); return pheader_names; }
// ---------------------------------------------------------------- slls_t* slls_from_line(char* line, char ifs, int allow_repeat_ifs) { slls_t* plist = slls_alloc(); if (*line == 0) // empty string splits to empty list return plist; char* start = line; for (char* p = line; *p; p++) { if (*p == ifs) { *p = 0; p++; if (allow_repeat_ifs) { while (*p == ifs) p++; } slls_add_no_free(plist, start); start = p; } } slls_add_no_free(plist, start); return plist; }
// ---------------------------------------------------------------- // xxx cmt mem-mgt slls_t* split_csv_header_line(char* line, char ifs, int allow_repeat_ifs) { slls_t* plist = slls_alloc(); if (*line == 0) // empty string splits to empty list return plist; char* start = line; for (char* p = line; *p; p++) { if (*p == ifs) { *p = 0; p++; // xxx hoist loop invariant at the cost of some code duplication if (allow_repeat_ifs) { while (*p == ifs) p++; } slls_add_no_free(plist, start); start = p; } } slls_add_no_free(plist, start); return plist; }
// ---------------------------------------------------------------- // xxx freeing contract. // xxx behavior on missing. doc, or make a second boolean flag. slls_t* mlr_selected_values_from_record(lrec_t* prec, slls_t* pselected_field_names) { slls_t* pvalue_list = slls_alloc(); for (sllse_t* pe = pselected_field_names->phead; pe != NULL; pe = pe->pnext) { char* selected_field_name = pe->value; char* value = lrec_get(prec, selected_field_name); if (value == NULL) { // xxx have stashed argv0 for error message. // xxx better to have filename + linenumber somehow. //fprintf(stderr, "Couldn't find field named \"%s\"\n", selected_field_name); //exit(1); } else { slls_add_no_free(pvalue_list, value); } } return pvalue_list; }
// ---------------------------------------------------------------- slls_t* slls_single_no_free(char* value) { slls_t* pslls = slls_alloc(); slls_add_no_free(pslls, value); return pslls; }
// ---------------------------------------------------------------- static char* test_lhmslv() { slls_t* aw = slls_alloc(); slls_add_no_free(aw, "a"); slls_add_no_free(aw, "w"); slls_t* ax = slls_alloc(); slls_add_no_free(ax, "a"); slls_add_no_free(ax, "x"); slls_t* ay = slls_alloc(); slls_add_no_free(ay, "a"); slls_add_no_free(ay, "y"); slls_t* bz = slls_alloc(); slls_add_no_free(bz, "b"); slls_add_no_free(bz, "z"); lhmslv_t *pmap = lhmslv_alloc(); mu_assert_lf(pmap->num_occupied == 0); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, ax)); mu_assert_lf(lhmslv_get(pmap, ax) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, ay)); mu_assert_lf(lhmslv_get(pmap, ay) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, bz)); mu_assert_lf(lhmslv_get(pmap, bz) == NULL); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_put(pmap, ax, "3"); mu_assert_lf(pmap->num_occupied == 1); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf( lhmslv_has_key(pmap, ax)); mu_assert_lf(streq(lhmslv_get(pmap, ax), "3")); mu_assert_lf(!lhmslv_has_key(pmap, ay)); mu_assert_lf(lhmslv_get(pmap, ay) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, bz)); mu_assert_lf(lhmslv_get(pmap, bz) == NULL); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_put(pmap, ay, "5"); mu_assert_lf(pmap->num_occupied == 2); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf( lhmslv_has_key(pmap, ax)); mu_assert_lf(streq(lhmslv_get(pmap, ax), "3")); mu_assert_lf( lhmslv_has_key(pmap, ay)); mu_assert_lf(streq(lhmslv_get(pmap, ay), "5")); mu_assert_lf(!lhmslv_has_key(pmap, bz)); mu_assert_lf(lhmslv_get(pmap, bz) == NULL); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_put(pmap, ax, "4"); mu_assert_lf(pmap->num_occupied == 2); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf( lhmslv_has_key(pmap, ax)); mu_assert_lf(streq(lhmslv_get(pmap, ax), "4")); mu_assert_lf( lhmslv_has_key(pmap, ay)); mu_assert_lf(streq(lhmslv_get(pmap, ay), "5")); mu_assert_lf(!lhmslv_has_key(pmap, bz)); mu_assert_lf(lhmslv_get(pmap, bz) == NULL); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_put(pmap, bz, "7"); mu_assert_lf(pmap->num_occupied == 3); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf( lhmslv_has_key(pmap, ax)); mu_assert_lf(streq(lhmslv_get(pmap, ax), "4")); mu_assert_lf( lhmslv_has_key(pmap, ay)); mu_assert_lf(streq(lhmslv_get(pmap, ay), "5")); mu_assert_lf( lhmslv_has_key(pmap, bz)); mu_assert_lf(streq(lhmslv_get(pmap, bz), "7")); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_remove(pmap, ay); mu_assert_lf(pmap->num_occupied == 2); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf( lhmslv_has_key(pmap, ax)); mu_assert_lf(streq(lhmslv_get(pmap, ax), "4")); mu_assert_lf(!lhmslv_has_key(pmap, ay)); mu_assert_lf(lhmslv_get(pmap, ay) == NULL); mu_assert_lf( lhmslv_has_key(pmap, bz)); mu_assert_lf(streq(lhmslv_get(pmap, bz), "7")); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_clear(pmap); mu_assert_lf(pmap->num_occupied == 0); mu_assert_lf(!lhmslv_has_key(pmap, aw)); mu_assert_lf(lhmslv_get(pmap, aw) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, ax)); mu_assert_lf(lhmslv_get(pmap, ax) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, ay)); mu_assert_lf(lhmslv_get(pmap, ay) == NULL); mu_assert_lf(!lhmslv_has_key(pmap, bz)); mu_assert_lf(lhmslv_get(pmap, bz) == NULL); mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_free(pmap); return NULL; }