Пример #1
0
Файл: test.c Проект: 9re/ltsv4c
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); 
}
Пример #2
0
Файл: ltsv4c.c Проект: kjdev/lq
static LTSV *parse_ltsv(const char **string) {
    LTSV *ltsv = ltsv_init();
    LTSV_Record *record;
    if (!ltsv) {
        fprintf(stderr, "couldn't allocate LTSV!\n");
        return NULL;
    }
    while (**string != '\0') {
        record = parse_record(string);
        if (!record) {
            if (!is_newline(**string)) {
                goto bail;
            }

            fprintf(stderr, "string: %c\n", **string);

            return ltsv;
        }

        ltsv_add(ltsv, record);
        if (**string == 0x0d) {
            skip_char(string);
        }
        if (**string == 0x0a) {
            skip_char(string);
        }
    }

    return ltsv;

bail:
    ltsv_free(ltsv);
    return NULL;
}