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; }
bool address_dictionary_read(FILE *f) { if (address_dict != NULL) return false; uint32_t signature; if (!file_read_uint32(f, &signature) || signature != ADDRESS_DICTIONARY_SIGNATURE) { return false; } address_dict = malloc(sizeof(address_dictionary_t)); if (address_dict == NULL) return false; uint32_t canonical_str_len; if (!file_read_uint32(f, &canonical_str_len)) { goto exit_address_dict_created; } char_array *array = char_array_new_size(canonical_str_len); if (array == NULL) { goto exit_address_dict_created; } if (!file_read_chars(f, array->a, canonical_str_len)) { char_array_destroy(array); goto exit_address_dict_created; } array->n = canonical_str_len; address_dict->canonical = cstring_array_from_char_array(array); uint32_t num_values; if (!file_read_uint32(f, &num_values)) { goto exit_address_dict_created; } address_dict->values = address_expansion_value_array_new_size(num_values); for (uint32_t i = 0; i < num_values; i++) { address_expansion_value_t *value = address_expansion_value_read(f); if (value == NULL) { goto exit_address_dict_created; } address_expansion_value_array_push(address_dict->values, value); } address_dict->trie = trie_read(f); if (address_dict->trie == NULL) { goto exit_address_dict_created; } return true; exit_address_dict_created: address_dictionary_destroy(address_dict); return false; }