1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/**
* @file
* @brief System independent functions
**/
#pragma once
#include <cctype>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <unordered_set>
#include "defines.h"
#include "coord-def.h"
#include "description-level-type.h"
#include "enum.h"
using std::string;
using std::unique_ptr;
using std::vector;
bool key_is_escape(int key);
// numeric string functions
#define CASE_ESCAPE case ESCAPE: case CONTROL('G'): case -1:
// Unscales a fixed-point number, rounding up.
static inline int unscale_round_up(int number, int scale)
{
return (number + scale - 1) / scale;
}
// Chinese rod numerals are _not_ digits for our purposes.
static inline bool isadigit(int c)
{
return c >= '0' && c <= '9';
}
// 'รค' is a letter, but not a valid inv slot/etc.
static inline bool isalower(int c)
{
return c >= 'a' && c <= 'z';
}
static inline bool isaupper(int c)
{
return c >= 'A' && c <= 'Z';
}
static inline bool isaalpha(int c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static inline bool isaalnum(int c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
}
static inline char32_t toalower(char32_t c)
{
return isaupper(c) ? c + 'a' - 'A' : c;
}
// Same thing with signed int, so we can pass though -1 undisturbed.
static inline int toalower(int c)
{
return isaupper(c) ? c + 'a' - 'A' : c;
}
static inline char32_t toaupper(char32_t c)
{
return isalower(c) ? c + 'A' - 'a' : c;
}
// Same thing with signed int, so we can pass though -1 undisturbed.
static inline int toaupper(int c)
{
return isalower(c) ? c + 'A' - 'a' : c;
}
template<typename T> inline T tolower_safe(T c)
{
return isaupper(c) ? toalower(c) : tolower(c);
}
template<typename T> inline T toupper_safe(T c)
{
return isalower(c) ? toaupper(c) : toupper(c);
}
int numcmp(const char *a, const char *b, int limit = 0);
bool numcmpstr(const string &a, const string &b);
bool version_is_stable(const char *ver);
// String "tags"
#define TAG_UNFOUND -20404
bool strip_tag(string &s, const string &tag, bool nopad = false);
int strip_number_tag(string &s, const string &tagprefix);
vector<string> strip_multiple_tag_prefix(string &s, const string &tagprefix);
string strip_tag_prefix(string &s, const string &tagprefix);
const string tag_without_prefix(const string &s, const string &tagprefix);
unordered_set<string> parse_tags(const string &tags);
bool parse_int(const char *s, int &i);
// String 'descriptions'
description_level_type description_type_by_name(const char *desc);
bool shell_safe(const char *file);
string unwrap_desc(string&& desc);
/** Ignore any number of arguments and return true.
*
* @return true
*/
template<class ... Ts> bool always_true(Ts ...) { return true; }
/** Remove an element from a vector without preserving order.
* The indicated element is replaced by the last element of the vector.
*
* @tparam Z The value type of the vector.
* @param vec The vector to modify.
* @param which The index of the element to remove.
*/
template <typename Z>
void erase_any(vector<Z> &vec, unsigned long which)
{
if (which != vec.size() - 1)
vec[which] = std::move(vec[vec.size() - 1]);
vec.pop_back();
}
/** A comparator to compare pairs by their second elements.
*
* @tparam T The type of the pairs (not the second element!)
* @param left,right The pairs to be compared.
* @return true if left.second > right.second, false otherwise.
*/
template<typename T>
struct greater_second
{
bool operator()(const T & left, const T & right)
{
return left.second > right.second;
}
};
/** Delete all the pointers in a container and clear the container.
*
* @tparam T The type of the container; must be a container of pointers
* to non-arrays.
* @param collection The container to clear.
*/
template<class T>
static void deleteAll(T& collection)
{
for (auto ptr : collection)
delete ptr;
collection.clear();
}
/** Find a map element by key, and return a pointer to its value.
*
* @tparam M The type of the map; may be const.
* @param map The map in which to do the lookup.
* @param obj The key to search for.
* @return a pointer to the value associated with obj if found,
* otherwise null. The pointer is const if the map is.
*/
template<class M>
auto map_find(M &map, const typename M::key_type &obj)
-> decltype(&map.begin()->second)
{
auto it = map.find(obj);
return it == map.end() ? nullptr : &it->second;
}
/** Find a map element by key, and return its value or a default.
* Only intended for use with simple map values where copying is not
* a concern; use map_find if the returned value should not be
* copied.
*
* @tparam M The type of the map; may be const.
* @param map The map in which to do the lookup.
* @param key The key to search for.
* @param unfound The object to return if the key was not found.
*
* @return a copy of the value associated with key if found, otherwise
* a copy of unfound.
*/
template<class M>
typename M::mapped_type lookup(M &map, const typename M::key_type &key,
const typename M::mapped_type &unfound)
{
auto it = map.find(key);
return it == map.end() ? unfound : it->second;
}
// Delete when we upgrade to C++14!
// we don't compile pre c++11, and c++14 and up provide make_unique
#if !defined(TARGET_COMPILER_VC) && (__cplusplus == 201103L)
template<typename T, typename... Args>
unique_ptr<T> make_unique(Args&&... args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif
/** Remove from a container all elements matching a predicate.
*
* @tparam C the container type. Must be reorderable (not a map or set!),
* and must provide an erase() method taking two iterators.
* @tparam P the predicate type, typically but not necessarily
* function<bool (const C::value_type &)>
* @param container The container to modify.
* @param pred The predicate to test.
*
* @post The container contains only those elements for which pred(elt)
* returns false, in the same relative order.
*/
template<class C, class P>
void erase_if(C &container, P pred)
{
container.erase(remove_if(begin(container), end(container), pred),
end(container));
}
/** Remove from a container all elements with a given value.
*
* @tparam C the container type. Must be reorderable (not a map or set!),
* must provide an erase() method taking two iterators, and
* must provide a value_type type member.
* @param container The container to modify.
* @param val The value to remove.
*
* @post The container contains only those elements not equal to val, in the
* in the same relative order.
*/
template<class C>
void erase_val(C &container, const typename C::value_type &val)
{
container.erase(remove(begin(container), end(container), val),
end(container));
}
static inline int sqr(int x)
{
return x * x;
}
unsigned int isqrt(unsigned int x);
int isqrt_ceil(int x);
static inline bool testbits(uint64_t flags, uint64_t test)
{
return (flags & test) == test;
}
template<class E, int Exp>
static inline bool testbits(enum_bitfield<E, Exp> flags,
enum_bitfield<E, Exp> test)
{
return (flags & test) == test;
}
template<class E, int Exp>
static inline bool testbits(enum_bitfield<E, Exp> flags, E test)
{
return (flags & test) == test;
}
coord_def cgetsize(GotoRegion region = GOTO_CRT);
void cscroll(int n, GotoRegion region);
string untag_tiles_console(string s);
string colour_string(string in, int col);
#ifdef TARGET_OS_WINDOWS
enum taskbar_pos
{
TASKBAR_NO = 0x00,
TASKBAR_BOTTOM = 0x01,
TASKBAR_TOP = 0x02,
TASKBAR_LEFT = 0x04,
TASKBAR_RIGHT = 0x08,
TASKBAR_H = 0x03,
TASKBAR_V = 0x0C
};
int get_taskbar_size();
taskbar_pos get_taskbar_pos();
void text_popup(const string& text, const wchar_t *caption);
#endif
class mouse_control
{
public:
mouse_control(mouse_mode mode);
~mouse_control();
static mouse_mode current_mode() { return ms_current_mode; }
private:
mouse_mode m_previous_mode;
static mouse_mode ms_current_mode;
};
void handle_hangup(int);
void init_signals();
void release_cli_signals();
|