Example #1
0
static void
S_add_document(Indexer *indexer, const char *title, const char *content) {
    Doc *doc = Doc_new(NULL, 0);

    {
        // Store 'title' field   
        String *field_str = Str_newf("title");
        String *value_str = Str_new_from_utf8(title, strlen(title));
        Doc_Store(doc, field_str, (Obj*)value_str);
        DECREF(field_str);
        DECREF(value_str);
    }

    {
        // Store 'content' field   
        String *field_str = Str_newf("content");
        String *value_str = Str_new_from_utf8(content, strlen(content));
        Doc_Store(doc, field_str, (Obj*)value_str);
        DECREF(field_str);
        DECREF(value_str);
    }

    Indexer_Add_Doc(indexer, doc, 1.0);

    DECREF(doc);
}
Example #2
0
static void
S_search(IndexSearcher *searcher, const char *query) {
    printf("Searching for: %s\n", query);

    // Execute search query.
    String *query_str = Str_new_from_utf8(query, strlen(query));
    Hits   *hits      = IxSearcher_Hits(searcher, (Obj*)query_str, 0, 10, NULL);

    String *field_str = Str_newf("title");
    HitDoc *hit;
    int i = 1;

    // Loop over search results.
    while (NULL != (hit = Hits_Next(hits))) {
        String *value_str = (String*)HitDoc_Extract(hit, field_str);
        char *value = Str_To_Utf8(value_str);

        printf("Result %d: %s\n", i, value);

        free(value);
        DECREF(value_str);
        DECREF(hit);
        i++;
    }

    printf("\n");

    DECREF(query_str);
    DECREF(hits);
    DECREF(field_str);
}
Example #3
0
static void
test_escapes(TestBatchRunner *runner) {
    for (int i = 0; control_escapes[i] != NULL; i++) {
        String     *string  = Str_new_from_char(i);
        const char *escaped = control_escapes[i];
        String     *json    = Json_to_json((Obj*)string);
        String     *trimmed = Str_Trim(json);
        String     *decoded = (String*)Json_from_json(json);

        String *json_wanted = Str_newf("\"%s\"", escaped);
        TEST_TRUE(runner, Str_Equals(json_wanted, (Obj*)trimmed),
                  "encode control escape: %s", escaped);

        TEST_TRUE(runner, decoded != NULL && Str_Equals(string, (Obj*)decoded),
                  "decode control escape: %s", escaped);

        DECREF(string);
        DECREF(json);
        DECREF(trimmed);
        DECREF(decoded);
        DECREF(json_wanted);
    }

    for (int i = 0; quote_escapes_source[i] != NULL; i++) {
        const char *source  = quote_escapes_source[i];
        const char *escaped = quote_escapes_json[i];
        String *string  = Str_new_from_utf8(source, strlen(source));
        String *json    = Json_to_json((Obj*)string);
        String *trimmed = Str_Trim(json);
        String *decoded = (String*)Json_from_json(json);

        String *json_wanted = Str_newf("\"%s\"", escaped);
        TEST_TRUE(runner, Str_Equals(json_wanted, (Obj*)trimmed),
                  "encode quote/backslash escapes: %s", source);

        TEST_TRUE(runner, decoded != NULL && Str_Equals(string, (Obj*)decoded),
                  "decode quote/backslash escapes: %s", source);

        DECREF(string);
        DECREF(json);
        DECREF(trimmed);
        DECREF(decoded);
        DECREF(json_wanted);
    }
}
Example #4
0
String*
TestUtils_get_str(const char *ptr) {
    return Str_new_from_utf8(ptr, strlen(ptr));
}
Example #5
0
StupidHashCharBuf*
StupidHashCharBuf_new(const char *text) {
    return (StupidHashCharBuf*)Str_new_from_utf8(text, strlen(text));
}
Example #6
0
static String*
S_get_str(const char *string) {
    return Str_new_from_utf8(string, strlen(string));
}
Example #7
0
Doc*
S_parse_file(const char *filename) {
    size_t bytes = strlen(uscon_source) + 1 + strlen(filename) + 1;
    char *path = (char*)malloc(bytes);
    path[0] = '\0';
    strcat(path, uscon_source);
    strcat(path, "/");
    strcat(path, filename);

    FILE *stream = fopen(path, "r");
    if (stream == NULL) {
        perror(path);
        exit(1);
    }

    char *title    = NULL;
    char *bodytext = NULL;
    if (fscanf(stream, "%m[^\r\n] %m[\x01-\x7F]", &title, &bodytext) != 2) {
        fprintf(stderr, "Can't extract title/bodytext from '%s'", path);
        exit(1);
    }

    const char *category = NULL;
    if (S_starts_with(filename, "art")) {
        category = "article";
    }
    else if (S_starts_with(filename, "amend")) {
        category = "amendment";
    }
    else if (S_starts_with(filename, "preamble")) {
        category = "preamble";
    }
    else {
        fprintf(stderr, "Can't derive category for %s", filename);
        exit(1);
    }

    Doc *doc = Doc_new(NULL, 0);

    {
        // Store 'title' field
        String *field = Str_newf("title");
        String *value = Str_new_from_utf8(title, strlen(title));
        Doc_Store(doc, field, (Obj*)value);
        DECREF(field);
        DECREF(value);
    }

    {
        // Store 'content' field
        String *field = Str_newf("content");
        String *value = Str_new_from_utf8(bodytext, strlen(bodytext));
        Doc_Store(doc, field, (Obj*)value);
        DECREF(field);
        DECREF(value);
    }

    {
        // Store 'url' field
        String *field = Str_newf("url");
        String *value = Str_new_from_utf8(filename, strlen(filename));
        Doc_Store(doc, field, (Obj*)value);
        DECREF(field);
        DECREF(value);
    }

    {
        // Store 'category' field
        String *field = Str_newf("category");
        String *value = Str_new_from_utf8(category, strlen(category));
        Doc_Store(doc, field, (Obj*)value);
        DECREF(field);
        DECREF(value);
    }

    fclose(stream);
    free(bodytext);
    free(title);
    free(path);
    return doc;
}