示例#1
0
/**
  Testuje wczytywanie drzewa.
  @param state Środowisko testowe.
  */
static void trie_load_test(void** state)
{
    Trie *trie = NULL;

    IO *io = io_new(stdin, stdout, stderr);

    push_word_to_io_mock(L"");
    trie = trie_load(io);
    assert_non_null(trie);
    trie_done(trie);

    // Poprawny zapis
    push_word_to_io_mock(L"ciupagą*^^^^^^^\n");
    trie = trie_load(io);
    pop_remaining_chars(io);
    assert_true(trie_has_word(trie, L"ciupagą"));
    assert_false(trie_has_word(trie, L"ciupaga"));
    assert_false(trie_has_word(trie, L"ciupag"));
    assert_false(trie_has_word(trie, L"ciupagąą"));
    trie_done(trie);

    // Próba dojścia wyżej niż korzeń
    push_word_to_io_mock(L"a*^^\n");
    trie = trie_load(io);
    pop_remaining_chars(io);
    assert_null(trie);

    // Znaki spoza alfabetu
    push_word_to_io_mock(L"&*^\n");
    trie = trie_load(io);
    pop_remaining_chars();
    assert_null(trie);

    io_done(io);
}
示例#2
0
struct dictionary * dictionary_load(FILE* stream)
{
    struct dictionary *dict =  malloc(sizeof(struct dictionary));
    if (dict == NULL)
        return NULL;
    dict->tree = trie_load(stream);
    if (dict->tree == NULL)
    {
        free(dict);
        return NULL;
    }
    return dict;
}
示例#3
0
文件: tries.c 项目: edechter/yap
static int p_trie_load(void) {
  TrEntry data;
  const char *file_str;
  FILE *file;

  /* check args */
  if (!YAP_IsVarTerm(arg_trie)) 
    return FALSE;
  if (!YAP_IsAtomTerm(arg_file))
    return FALSE;

  /* open file */
  file_str = YAP_AtomName(YAP_AtomOfTerm(arg_file));
  if (!(file = fopen(file_str, "r")))
    return FALSE;

  /* load trie and close file */
  if (!(data = trie_load(file)))
    return FALSE;
  if (fclose(file))
    return FALSE;
  return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) data));
}
示例#4
0
geodb_t *geodb_init(char *dir) {
    if (dir == NULL) return NULL;

    geodb_t *gdb = malloc(sizeof(geodb_t));

    if (gdb == NULL) return NULL;

    char_array *path = char_array_new_size(strlen(dir));
    char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_NAMES_TRIE_FILENAME);

    char *names_path = char_array_get_string(path);

    gdb->names = trie_load(names_path);
    if (gdb->names == NULL) {
        goto exit_geodb_created;
    }

    char_array_clear(path);

    char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_FEATURES_TRIE_FILENAME);

    char *features_path = char_array_get_string(path);

    gdb->features = trie_load(features_path);
    if(gdb->features == NULL) {
        goto exit_geodb_created;
    }

    char_array_clear(path);

    char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_POSTAL_CODES_FILENAME);
    char *postal_codes_path = char_array_get_string(path);

    FILE *f = fopen(postal_codes_path, "rb");

    uint64_t num_postal_strings = 0;
    if (!file_read_uint64(f, (uint64_t *)&num_postal_strings)) {
        goto exit_geodb_created;
    }

    size_t postal_codes_str_len;

    if (!file_read_uint64(f, (uint64_t *)&postal_codes_str_len)) {
        goto exit_geodb_created;
    }

    char_array *array = char_array_new_size(postal_codes_str_len);

    if (!file_read_chars(f, array->a, postal_codes_str_len)) {
        goto exit_geodb_created;
    }

    array->n = postal_codes_str_len;

    gdb->postal_codes = cstring_array_from_char_array(array);

    if (cstring_array_num_strings(gdb->postal_codes) != num_postal_strings) {
        goto exit_geodb_created;
    }

    fclose(f);
    char_array_clear(path);

    char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_HASH_FILENAME);

    char *hash_file_path = strdup(char_array_get_string(path));

    char_array_clear(path);

    char_array_cat_joined(path, PATH_SEPARATOR, true, 2, dir, GEODB_LOG_FILENAME);

    char *log_path = char_array_get_string(path);

    gdb->hash_reader = NULL;

    if ((sparkey_hash_open(&gdb->hash_reader, hash_file_path, log_path)) != SPARKEY_SUCCESS) {
        free(hash_file_path);
        char_array_destroy(path);
        goto exit_geodb_created;
    }

    free(hash_file_path);
    char_array_destroy(path);

    gdb->log_iter = NULL;

    if ((sparkey_logiter_create(&gdb->log_iter, sparkey_hash_getreader(gdb->hash_reader))) != SPARKEY_SUCCESS) {
        goto exit_geodb_created;
    }

    gdb->value_buf = char_array_new_size(sparkey_logreader_maxvaluelen(sparkey_hash_getreader(gdb->hash_reader)));
    if (gdb->value_buf == NULL) {
        goto exit_geodb_created;
    }

    gdb->geoname = geoname_new();
    if (gdb->geoname == NULL) {
        goto exit_geodb_created;
    }

    gdb->postal_code = gn_postal_code_new();
    if (gdb->postal_code == NULL) {
        goto exit_geodb_created;
    }

    return gdb;

exit_geodb_created:
    geodb_destroy(gdb);
    return NULL;
}