diff options
Diffstat (limited to 'crawl-ref/source/database.cc')
-rw-r--r-- | crawl-ref/source/database.cc | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/crawl-ref/source/database.cc b/crawl-ref/source/database.cc index f8c97a354c..0e9b598f9c 100644 --- a/crawl-ref/source/database.cc +++ b/crawl-ref/source/database.cc @@ -152,6 +152,63 @@ datum database_fetch(DBM *database, const std::string &key) return result; } +std::vector<std::string> database_find_keys(DBM *database, + const std::string ®ex, + bool ignore_case, + db_find_filter filter) +{ + text_pattern tpat(regex, ignore_case); + std::vector<std::string> matches; + + datum dbKey = dbm_firstkey(database); + + while (dbKey.dptr != NULL) + { + std::string key((const char *)dbKey.dptr, dbKey.dsize); + + if (tpat.matches(key) && + key.find("__") == std::string::npos + && (filter == NULL || !(*filter)(key, ""))) + { + matches.push_back(key); + } + + dbKey = dbm_nextkey(database); + } + + return (matches); +} + +std::vector<std::string> database_find_bodies(DBM *database, + const std::string ®ex, + bool ignore_case, + db_find_filter filter) +{ + text_pattern tpat(regex, ignore_case); + std::vector<std::string> matches; + + datum dbKey = dbm_firstkey(database); + + while (dbKey.dptr != NULL) + { + std::string key((const char *)dbKey.dptr, dbKey.dsize); + + datum dbBody = dbm_fetch(database, dbKey); + std::string body((const char *)dbBody.dptr, dbBody.dsize); + + if (tpat.matches(body) && + key.find("__") == std::string::npos + && (filter == NULL || !(*filter)(key, body))) + { + matches.push_back(key); + } + + dbKey = dbm_nextkey(database); + } + + return (matches); +} + /////////////////////////////////////////////////////////////////////////// // Internal DB utility functions static void trim_right(std::string &s) @@ -403,6 +460,30 @@ std::string getLongDescription(const std::string &key) return std::string((const char *)result.dptr, result.dsize); } +std::vector<std::string> getLongDescKeysByRegex(const std::string ®ex, + db_find_filter filter) +{ + if (!descriptionDB) + { + std::vector<std::string> empty; + return (empty); + } + + return database_find_keys(descriptionDB, regex, true, filter); +} + +std::vector<std::string> getLongDescBodiesByRegex(const std::string ®ex, + db_find_filter filter) +{ + if (!descriptionDB) + { + std::vector<std::string> empty; + return (empty); + } + + return database_find_bodies(descriptionDB, regex, true, filter); +} + static std::vector<std::string> description_txt_paths() { std::vector<std::string> txt_file_names; |