1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#ifndef RME_COMMONS_H_
19#define RME_COMMONS_H_
20
21#include "main.h"
22#include "position.h"
23#include <stdio.h>
24#include <stdlib.h>
25#include <cmath>
26#include <iomanip>
27#include <string>
28
29#include "mt_rand.h"
30
31//
32inline bool testFlags(size_t flags, size_t test) {
33 return (flags & test) != 0;
34}
35
36int32_t uniform_random(int32_t minNumber, int32_t maxNumber);
37int32_t uniform_random(int32_t maxNumber);
38
39// Function-like convertions between float, int and doubles
40std::string i2s(int i);
41std::string f2s(double i);
42int s2i(std::string s);
43double s2f(std::string s);
44wxString i2ws(int i);
45wxString f2ws(double i);
46int ws2i(wxString s);
47double ws2f(wxString s);
48double frand();
49
50// replaces all instances of sought in str with replacement
51void replaceString(std::string& str, const std::string sought, const std::string replacement);
52// Removes all characters in t from source (from either start or beginning of the string)
53void trim_right(std::string& source, const std::string& t);
54void trim_left(std::string& source, const std::string& t);
55// Converts the argument to lower/uppercase
56void to_lower_str(std::string& source);
57void to_upper_str(std::string& source);
58std::string as_lower_str(const std::string& other);
59std::string as_upper_str(const std::string& other);
60
61// isFalseString returns true if the string is either "0", "false", "no", "not" or blank
62// isTrueString returns the opposite value of isFalseString
63bool isFalseString(std::string& str);
64bool isTrueString(std::string& str);
65
66// Generates a random number between low and high using the mersenne twister
67int random(int high);
68int random(int low, int high);
69
70// Unicode conversions
71std::wstring string2wstring(const std::string& utf8string);
72std::string wstring2string(const std::wstring& widestring);
73
74// Gets position values from ClipBoard
75bool posFromClipboard(Position& position, int mapWidth, int mapHeight, const std::string& input = "");
76
77// Returns 'yes' if the defined value is true or 'no' if it is false.
78wxString b2yn(bool v);
79
80wxColor colorFromEightBit(int color);
81
82// Standard math functions
83template <class T>
84inline T abs(T t) {
85 return (t < 0 ? -t : t);
86}
87
88template <class T, class U>
89inline T min(T t, U u) {
90 return (t < u ? t : u);
91}
92
93template <class T, class U>
94T max(T t, U u) {
95 return (t > u ? t : u);
96}
97
98template <class T, class U, class V>
99inline T min(T t, U u, V v) {
100 int min = t;
101 if (u < min) {
102 min = u;
103 }
104 if (v < min) {
105 min = v;
106 }
107 return min;
108}
109
110template <class T, class U, class V>
111inline T max(T t, U u, V v) {
112 int max = t;
113 if (u > max) {
114 max = u;
115 }
116 if (v > max) {
117 max = v;
118 }
119 return max;
120}
121
122#endif
123