struct hash *read_map(const char *filename) { const unsigned int HASH_SIZE = 1000; /* most trees will have fewer nodes */ const double LOAD_THRESHOLD = 0.8; const unsigned RESIZE_FACTOR = 10; FILE *map_file = fopen(filename, "r"); if (NULL == map_file) { perror(NULL); exit(EXIT_FAILURE); } //struct hash *map = create_hash(HASH_SIZE); struct hash *map = create_dynamic_hash(HASH_SIZE, LOAD_THRESHOLD, RESIZE_FACTOR); if (NULL == map) { perror(NULL); exit(EXIT_FAILURE); } char *line; while (NULL != (line = read_line(map_file))) { /* Skip comments and lines that are empty or all whitespace */ if ('#' == line[0] || is_all_whitespace(line)) { free(line); continue; } char *key, *value; struct word_tokenizer *wtok = create_word_tokenizer(line); if (NULL == wtok) { perror(NULL); exit(EXIT_FAILURE); } key = wt_next(wtok); /* find first whitespace */ if (NULL == key) { fprintf (stderr, "Wrong format in line '%s' - aborting.\n", line); exit(EXIT_FAILURE); } value = wt_next(wtok); if (NULL == value) { /* If 2nd token is NULL, replace label with empty * string */ value = strdup(""); } if (! hash_set(map, key, (void *) value)) { perror(NULL); exit(EXIT_FAILURE); } destroy_word_tokenizer(wtok); free(key); /* copied by hash_set(), so can be free()d now */ free(line); } return map; }
struct hash *read_map(const char *filename) { const int HASH_SIZE = 1000; /* most trees will have fewer nodes */ FILE *map_file = fopen(filename, "r"); if (NULL == map_file) { perror(NULL); exit(EXIT_FAILURE); } struct hash *map = create_hash(HASH_SIZE); if (NULL == map) { perror(NULL); exit(EXIT_FAILURE); } char *line; while (NULL != (line = read_line(map_file))) { /* Skip comments and lines that are empty or all whitespace */ if ('#' == line[0] || is_all_whitespace(line)) { free(line); continue; } char *key, *value; struct word_tokenizer *wtok = create_word_tokenizer(line); if (NULL == wtok) { perror(NULL); exit(EXIT_FAILURE); } key = wt_next(wtok); /* find first whitespace */ if (NULL == key) { fprintf (stderr, "Wrong format in line %s - aborting.\n", line); exit(EXIT_FAILURE); } value = wt_next(wtok); if (NULL == value) { fprintf (stderr, "Wrong format in line %s - aborting.\n", line); exit(EXIT_FAILURE); } if (! hash_set(map, key, (void *) value)) { perror(NULL); exit(EXIT_FAILURE); } destroy_word_tokenizer(wtok); free(key); /* copied by hash_set(), so can be free()d now */ free(line); } return map; }