static LTSV_Record *parse_record(const char **string) { LTSV_Record *output_record = ltsv_record_init(); const char *label = NULL, *value; if (!output_record) { fprintf(stderr, "couldn't allocate LTSV_Record!\n"); return NULL; } while (**string != '\0') { label = parse_string(string, &label_char_test_func); if (!label) { fprintf(stderr, "could not parse label!\n"); goto bail; } if (strlen(label) == 0) { if (is_newline(**string)) { free((void*)label); return output_record; } fprintf(stderr, "invalid character for label: %c\n", **string); goto bail; } if (**string != ':') { fprintf(stderr, "':' not found: '%c' found instead!\n", **string); goto bail; } skip_char(string); value = parse_string(string, &field_char_test_func); if (ltsv_record_get_value(output_record, label)) { fprintf(stderr, "dupliate entry for record: %s\n", label); goto bail; } ltsv_record_add_entry(output_record, label, value); if (**string == '\t') { skip_char(string); } free((void*)label); } return output_record; bail: if (label) { free((void*)label); } ltsv_record_free(output_record); return NULL; }
static int ltsv_record_add_entry(LTSV_Record *record, const char *key, const char *value) { size_t index; if (record->count >= record->capacity) { size_t new_capacity = MAX(record->capacity * 2, STARTING_CAPACITY); if (ltsv_record_resize(record, new_capacity) == ERROR) { return ERROR; } } if (ltsv_record_get_value(record, key) != NULL) { return ERROR; } index = record->count; record->labels[index] = ltsv_strndup(key, strlen(key)); if (!record->labels[index]) { return ERROR; } record->values[index] = value; record->count++; return SUCCESS; }
void test_suite_0(void) { LTSV *ltsv = ltsv_parse_file("tests/test_0.txt"); LTSV_Record *record; int ltsv_count; TEST(ltsv); if (!ltsv) { fprintf(stderr, "parse failed: tests/test_0.txt\n"); return; } ltsv_count = ltsv_get_count(ltsv); TEST(ltsv_count == 4); if (ltsv_count != 4) { fprintf(stderr, "ltsv count doesn't match!\n"); return; } record = ltsv_get_record(ltsv, 0); TEST(STREQ(ltsv_record_get_value(record, "a"), "05/02/2013:12:00:00 +0900")); TEST(STREQ(ltsv_record_get_value(record, "b"), "x.x.x.x")); TEST(STREQ(ltsv_record_get_value(record, "c"), "nn")); record = ltsv_get_record(ltsv, 1); TEST(STREQ(ltsv_record_get_value(record, "b"), "05/02/2013:12:01:00 +0900")); TEST(STREQ(ltsv_record_get_value(record, "c"), "x.y.x.y")); TEST(STREQ(ltsv_record_get_value(record, "a"), "nm")); record = ltsv_get_record(ltsv, 2); TEST(STREQ(ltsv_record_get_value(record, "c"), "05/02/2013:12:02:00 +0900")); TEST(STREQ(ltsv_record_get_value(record, "b"), "x.z.x.z")); TEST(STREQ(ltsv_record_get_value(record, "utf-8"), "あいうえお")); record = ltsv_get_record(ltsv, 3); TEST(STREQ(ltsv_record_get_value(record, "host"), "127.0.0.1")); TEST(STREQ(ltsv_record_get_value(record, "ident"), "-")); TEST(STREQ(ltsv_record_get_value(record, "time"), "[10/Oct/2000:13:55:36 -0700]")); TEST(STREQ(ltsv_record_get_value(record, "req"), "GET /apache_pb.gif HTTP/1.0")); TEST(STREQ(ltsv_record_get_value(record, "status"), "200")); TEST(STREQ(ltsv_record_get_value(record, "size"), "2326")); TEST(STREQ(ltsv_record_get_value(record, "referer"), "http://www.example.com/start.html")); TEST(STREQ(ltsv_record_get_value(record, "ua"), "Mozilla/4.08 [en] (Win98; I ;Nav)")); ltsv_free(ltsv); ltsv = ltsv_parse_string("label:text\thoge:fuga\nmm:value:1"); ltsv_count = ltsv_get_count(ltsv); TEST(ltsv_count == 2); if (ltsv_count != 2) { fprintf(stderr, "ltsv count doesn't match!%d \n", ltsv_count); return; } record = ltsv_get_record(ltsv, 0); TEST(STREQ(ltsv_record_get_value(record, "label"), "text")); TEST(STREQ(ltsv_record_get_value(record, "hoge"), "fuga")); record = ltsv_get_record(ltsv, 1); TEST(STREQ(ltsv_record_get_value(record, "mm"), "value:1")); ltsv_free(ltsv); }