00001
00002
00003
00004
00005
00006 #ifndef LIBUTIL_H
00007 #define LIBUTIL_H
00008
00009 #include "defines.h"
00010 #include "enum.h"
00011 #include <cctype>
00012 #include <string>
00013 #include <vector>
00014 #include <map>
00015
00016 extern const char *standard_plural_qualifiers[];
00017
00018
00019
00020
00021 std::string apply_description(description_level_type desc,
00022 const std::string &name,
00023 int quantity = 1,
00024 bool num_in_words = false);
00025
00026 description_level_type description_type_by_name(const char *desc);
00027
00028 std::string &escape_path_spaces(std::string &s);
00029
00030 std::string lowercase_string(std::string s);
00031 std::string &lowercase(std::string &s);
00032 std::string &uppercase(std::string &s);
00033 std::string upcase_first(std::string);
00034
00035 void wait_for_keypress();
00036 bool key_is_escape(int key);
00037
00038 #define CASE_ESCAPE case ESCAPE: case CONTROL('G'): case -1:
00039
00040
00041 static inline int unscale_round_up(int number, int scale)
00042 {
00043 return ((number + scale - 1) / scale);
00044 }
00045
00046
00047 static inline int isadigit(int c)
00048 {
00049 return (c >= '0' && c <= '9');
00050 }
00051
00052
00053 static inline int isalower(int c)
00054 {
00055 return (c >= 'a' && c <= 'z');
00056 }
00057
00058 static inline int isaupper(int c)
00059 {
00060 return (c >= 'A' && c <= 'Z');
00061 }
00062
00063 static inline int isaalpha(int c)
00064 {
00065 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
00066 }
00067
00068 static inline int isaalnum(int c)
00069 {
00070 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
00071 }
00072
00073 bool ends_with(const std::string &s, const std::string &suffix);
00074
00075 #ifdef UNIX
00076 extern "C" int stricmp(const char *str1, const char *str2);
00077 #endif
00078 size_t strlcpy(char *dst, const char *src, size_t n);
00079
00080 int strwidth(const char *s);
00081 int strwidth(const std::string &s);
00082
00083
00084 #define TAG_UNFOUND -20404
00085 bool strip_tag(std::string &s, const std::string &tag, bool nopad = false);
00086 bool strip_suffix(std::string &s, const std::string &suffix);
00087 int strip_number_tag(std::string &s, const std::string &tagprefix);
00088 bool strip_bool_tag(std::string &s, const std::string &name,
00089 bool defval = false);
00090 std::string strip_tag_prefix(std::string &s, const std::string &tagprefix);
00091
00092 std::string article_a(const std::string &name, bool lowercase = true);
00093 std::string pluralise(
00094 const std::string &name,
00095 const char *stock_plural_quals[] = standard_plural_qualifiers,
00096 const char *no_of[] = NULL);
00097 std::string apostrophise(const std::string &name);
00098 std::string apostrophise_fixup(const std::string &msg);
00099
00100 std::string number_in_words(unsigned number, int pow = 0);
00101 std::string number_to_string(unsigned number, bool in_words = false);
00102
00103 bool shell_safe(const char *file);
00104
00109 int ends_with(const std::string &s, const char *suffixes[]);
00110
00111 std::string strip_filename_unsafe_chars(const std::string &s);
00112
00113 std::string vmake_stringf(const char *format, va_list args);
00114 std::string make_stringf(const char *format, ...);
00115
00116 std::string replace_all(std::string s,
00117 const std::string &tofind,
00118 const std::string &replacement);
00119
00120 std::string replace_all_of(std::string s,
00121 const std::string &tofind,
00122 const std::string &replacement);
00123
00124 int count_occurrences(const std::string &text, const std::string &searchfor);
00125
00126 void play_sound(const char *file);
00127
00128 std::string &trim_string(std::string &str);
00129 std::string &trim_string_right(std::string &str);
00130 std::string trimmed_string(std::string s);
00131
00132 inline bool starts_with(const std::string &s, const std::string &prefix)
00133 {
00134 return (s.rfind(prefix, 0) != std::string::npos);
00135 }
00136
00137 inline bool ends_with(const std::string &s, const std::string &suffix)
00138 {
00139 if (s.length() < suffix.length())
00140 return false;
00141 return (s.find(suffix, s.length() - suffix.length()) != std::string::npos);
00142 }
00143
00144
00145
00146
00147
00148
00149 std::vector<std::string> split_string(
00150 const std::string &sep,
00151 std::string s,
00152 bool trim = true,
00153 bool accept_empties = false,
00154 int nsplits = -1);
00155
00156 inline std::string lowercase_first(std::string s)
00157 {
00158 if (s.length())
00159 s[0] = tolower(s[0]);
00160 return (s);
00161 }
00162
00163 inline std::string uppercase_first(std::string s)
00164 {
00165 if (s.length())
00166 s[0] = toupper(s[0]);
00167 return (s);
00168 }
00169
00170 template <typename Z>
00171 std::string comma_separated_line(Z start, Z end,
00172 const std::string &andc = " and ",
00173 const std::string &comma = ", ")
00174 {
00175 std::string text;
00176 for (Z i = start; i != end; ++i)
00177 {
00178 if (i != start)
00179 {
00180 Z tmp = i;
00181 if (++tmp != end)
00182 text += comma;
00183 else
00184 text += andc;
00185 }
00186
00187 text += *i;
00188 }
00189 return (text);
00190 }
00191
00192 #ifdef NEED_USLEEP
00193 void usleep(unsigned long time);
00194 #endif
00195
00196 #ifndef USE_TILE
00197 coord_def cgettopleft(GotoRegion region = GOTO_CRT);
00198 coord_def cgetpos(GotoRegion region = GOTO_CRT);
00199 void cgotoxy(int x, int y, GotoRegion region = GOTO_CRT);
00200 GotoRegion get_cursor_region();
00201 #endif
00202 coord_def cgetsize(GotoRegion region = GOTO_CRT);
00203 void cscroll(int n, GotoRegion region);
00204
00205 #ifdef TARGET_OS_WINDOWS
00206 enum taskbar_pos
00207 {
00208 TASKBAR_NO = 0x00,
00209 TASKBAR_BOTTOM = 0x01,
00210 TASKBAR_TOP = 0x02,
00211 TASKBAR_LEFT = 0x04,
00212 TASKBAR_RIGHT = 0x08,
00213 TASKBAR_H = 0x03,
00214 TASKBAR_V = 0x0C
00215 };
00216
00217 int get_taskbar_size();
00218 taskbar_pos get_taskbar_pos();
00219 #endif
00220
00221 class mouse_control
00222 {
00223 public:
00224 mouse_control(mouse_mode mode)
00225 {
00226 m_previous_mode = ms_current_mode;
00227 ms_current_mode = mode;
00228 }
00229
00230 ~mouse_control()
00231 {
00232 ms_current_mode = m_previous_mode;
00233 }
00234
00235 static mouse_mode current_mode() { return ms_current_mode; }
00236
00237 private:
00238 mouse_mode m_previous_mode;
00239 static mouse_mode ms_current_mode;
00240 };
00241
00242 #endif