static void check_trie_with_file(const trie_t *db, const char *file) { file_map_t map; const char *p, *end; char line[BUFSIZ]; if (!file_map_open(&map, file, false)) { return; } p = map.map; end = map.end; while (end > p && end[-1] != '\n') { --end; } if (end != map.end) { warn("file %s miss a final \\n, ignoring last line", file); } while (p < end && p != NULL) { const char *eol = (char *)memchr(p, '\n', end - p); if (eol == NULL) { eol = end; } if (eol - p > BUFSIZ) { p = eol - BUFSIZ; } int i = 0; if (*p != '#' && p != eol) { #if 1 for (const char *s = eol - 1 ; s >= p ; --s) { line[i++] = ascii_tolower(*s); } #else memcpy(line, p, eol - p); i = eol - p; #endif line[i] = '\0'; if (!trie_lookup(db, line)) { warn("'%s' not found in the trie", line); } strcat(line, "coucou"); if (trie_lookup(db, line)) { warn("'%s' found in trie", line); } if (!trie_prefix(db, line)) { warn("'%s' has no prefix in trie", line); } } p = eol + 1; } file_map_close(&map); }
static trie_t *create_trie_from_file(const char *file) { trie_t *db; file_map_t map; const char *p, *end; char line[BUFSIZ]; if (!file_map_open(&map, file, false)) { return NULL; } p = map.map; end = map.end; while (end > p && end[-1] != '\n') { --end; } if (end != map.end) { warn("file %s miss a final \\n, ignoring last line", file); } db = trie_new(); while (p < end && p != NULL) { const char *eol = (char *)memchr(p, '\n', end - p); if (eol == NULL) { eol = end; } if (eol - p > BUFSIZ) { p = eol - BUFSIZ; } int i = 0; if (*p != '#' && p != eol) { #if 1 for (const char *s = eol - 1 ; s >= p ; --s) { line[i++] = ascii_tolower(*s); } #else memcpy(line, p, eol - p); i = eol - p; #endif line[i] = '\0'; trie_insert(db, line); } p = eol + 1; } file_map_close(&map); trie_compile(db, false); return db; }
static char *read_query(const char *basepath, const char *filename, char *buff, char **end, query_t *q) { char path[FILENAME_MAX]; snprintf(path, FILENAME_MAX, "%s%s", basepath, filename); { file_map_t map; if (!file_map_open(&map, path, false)) { UNIXERR("open"); return NULL; } if (map.end - map.map >= BUFSIZ) { err("File too large for a testcase: %s", path); file_map_close(&map); return NULL; } memcpy(buff, map.map, map.end - map.map); if (end != NULL) { *end = buff + (map.end - map.map); **end = '\0'; } else { buff[map.end - map.map] = '\0'; } file_map_close(&map); } char *eoq = strstr(buff, "\n\n"); if (eoq == NULL) { return NULL; } if (!query_parse(q, buff)) { err("Cannot parse query from file %s", filename); return NULL; } return eoq + 2; }