diff options
author | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-10-24 12:42:08 +0000 |
---|---|---|
committer | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2007-10-24 12:42:08 +0000 |
commit | c19b2fdd21ba70ded0b4f42a8c7d018108d2d70d (patch) | |
tree | 4b4c96386291048dbb254a73cf7a5d80c604b0e7 /crawl-ref/source/libutil.cc | |
parent | 873dd0bfae9f9f78b3e978edb574e4793af7b03f (diff) | |
download | crawl-ref-c19b2fdd21ba70ded0b4f42a8c7d018108d2d70d.tar.gz crawl-ref-c19b2fdd21ba70ded0b4f42a8c7d018108d2d70d.zip |
[1818799] Reduce message spam from Olgreb's toxic radiance.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@2535 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/libutil.cc')
-rw-r--r-- | crawl-ref/source/libutil.cc | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/crawl-ref/source/libutil.cc b/crawl-ref/source/libutil.cc index 586de1db77..ed0b640680 100644 --- a/crawl-ref/source/libutil.cc +++ b/crawl-ref/source/libutil.cc @@ -18,6 +18,7 @@ #include "externs.h" #include "macro.h" #include "stuff.h" +#include <sstream> #include <stdio.h> #include <ctype.h> #include <stdarg.h> @@ -75,6 +76,39 @@ description_level_type description_type_by_name(const char *desc) return DESC_PLAIN; } +std::string number_to_string(unsigned number, bool in_words) +{ + return (in_words? number_in_words(number) : make_stringf("%u", number)); +} + +std::string apply_description(description_level_type desc, + const std::string &name, + int quantity, bool in_words) +{ + switch (desc) + { + case DESC_CAP_THE: + return ("The " + name); + case DESC_NOCAP_THE: + return ("the " + name); + case DESC_CAP_A: + return (quantity > 1? + number_to_string(quantity, in_words) + name + : article_a(name, false)); + case DESC_NOCAP_A: + return (quantity > 1? + number_to_string(quantity, in_words) + name + : article_a(name, true)); + case DESC_CAP_YOUR: + return ("Your " + name); + case DESC_NOCAP_YOUR: + return ("your " + name); + case DESC_PLAIN: + default: + return (name); + } +} + // Should return true if the filename contains nothing that // the shell can do damage with. bool shell_safe(const char *file) @@ -527,6 +561,77 @@ int snprintf( char *str, size_t size, const char *format, ... ) #endif +////////////////////////////////////////////////////////////////////////// +// named_thing_collection + +named_thing_collection::named_thing_collection() + : names(), nnames(0u) +{ +} + +void named_thing_collection::add_thing(const std::string &name) +{ + names[name]++; + nnames++; +} + +size_t named_thing_collection::size() const +{ + return (nnames); +} + +bool named_thing_collection::empty() const +{ + return (!nnames); +} + +std::string named_thing_collection::describe( + description_level_type desc, + const char **plural_qualifiers, + const char **no_qualifier_suffixes) const +{ + if (empty()) + return (""); + + std::ostringstream out; + for (name_count_map::const_iterator i = names.begin(); + i != names.end(); ) + { + const std::pair<std::string, int> &curr(*i); + if (i != names.begin()) + { + ++i; + out << (i == names.end()? " and " : ", "); + } + else + ++i; + + const std::string name = + curr.second > 1? pluralise(curr.first, plural_qualifiers, + no_qualifier_suffixes) + : curr.first; + out << apply_description(desc, name, curr.second); + + switch (desc) + { + case DESC_CAP_A: + desc = DESC_NOCAP_A; + break; + case DESC_CAP_THE: + desc = DESC_NOCAP_THE; + break; + case DESC_CAP_YOUR: case DESC_NOCAP_YOUR: + desc = DESC_PLAIN; + break; + default: + break; + } + } + return (out.str()); +} + +///////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////// // Pattern matching |