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; }
int test_word_tokenizer_2() { const char *test_name = "test_word_tokenizer_2"; char *line = "'word 1' word2\t345WORD 'word 8' \"another\tword\" word_10\n"; struct word_tokenizer *wt = create_word_tokenizer(line); char *word; word = wt_next(wt); if (strcmp(word, "'word 1'") != 0) { printf ("%s: expected word1, got %s\n", test_name, word); return 1; } word = wt_next(wt); if (strcmp(word, "word2") != 0) { printf ("%s: expected word2, got %s\n", test_name, word); return 1; } word = wt_next(wt); if (strcmp(word, "345WORD") != 0) { printf ("%s: expected 345WORD, got %s\n", test_name, word); return 1; } word = wt_next(wt); if (strcmp(word, "'word 8'") != 0) { printf ("%s: expected 'word 8', got %s\n", test_name, word); return 1; } word = wt_next(wt); if (strcmp(word, "\"another\tword\"") != 0) { printf ("%s: expected \"another word\", got %s\n", test_name, word); return 1; } word = wt_next(wt); if (strcmp(word, "word_10") != 0) { printf ("%s: expected word_10, got %s\n", test_name, word); return 1; } word = wt_next(wt); if (NULL != word) { printf ("%s: expected NULL, got %s\n", test_name, word); return 1; } printf("%s ok.\n", test_name); return 0; }
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; }