Allow full circle rotation with 2degs step for plantlike drawtype.
[oweals/minetest.git] / src / util / string.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "string.h"
21 #include "pointer.h"
22 #include "numeric.h"
23
24 #include <sstream>
25 #include <iomanip>
26
27 #include "../sha1.h"
28 #include "../base64.h"
29 #include "../hex.h"
30 #include "../porting.h"
31
32 #ifdef __ANDROID__
33 const wchar_t* wide_chars = L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
34
35 int wctomb(char *s, wchar_t wc)
36 {
37         for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
38                 if (wc == wide_chars[j]) {
39                         *s = (char) (j+32);
40                         return 1;
41                 }
42                 else if (wc == L'\n') {
43                         *s = '\n';
44                         return 1;
45                 }
46         }
47         return -1;
48 }
49
50 int mbtowc(wchar_t *pwc, const char *s, size_t n)
51 {
52         std::wstring intermediate = narrow_to_wide(s);
53
54         if (intermediate.length() > 0) {
55                 *pwc = intermediate[0];
56                 return 1;
57         }
58         else {
59                 return -1;
60         }
61 }
62
63 std::wstring narrow_to_wide(const std::string& mbs) {
64         size_t wcl = mbs.size();
65
66         std::wstring retval = L"";
67
68         for (unsigned int i = 0; i < wcl; i++) {
69                 if (((unsigned char) mbs[i] >31) &&
70                  ((unsigned char) mbs[i] < 127)) {
71
72                         retval += wide_chars[(unsigned char) mbs[i] -32];
73                 }
74                 //handle newline
75                 else if (mbs[i] == '\n') {
76                         retval += L'\n';
77                 }
78         }
79
80         return retval;
81 }
82 #else
83
84 std::wstring narrow_to_wide(const std::string& mbs)
85 {
86         size_t wcl = mbs.size();
87         Buffer<wchar_t> wcs(wcl+1);
88         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
89         if(l == (size_t)(-1))
90                 return L"<invalid multibyte string>";
91         wcs[l] = 0;
92         return *wcs;
93 }
94
95 #endif
96
97 #ifdef __ANDROID__
98 std::string wide_to_narrow(const std::wstring& wcs) {
99         size_t mbl = wcs.size()*4;
100
101         std::string retval = "";
102         for (unsigned int i = 0; i < wcs.size(); i++) {
103                 wchar_t char1 = (wchar_t) wcs[i];
104
105                 if (char1 == L'\n') {
106                         retval += '\n';
107                         continue;
108                 }
109
110                 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
111                         wchar_t char2 = (wchar_t) wide_chars[j];
112
113                         if (char1 == char2) {
114                                 char toadd = (j+32);
115                                 retval += toadd;
116                                 break;
117                         }
118                 }
119         }
120
121         return retval;
122 }
123 #else
124 std::string wide_to_narrow(const std::wstring& wcs)
125 {
126         size_t mbl = wcs.size()*4;
127         SharedBuffer<char> mbs(mbl+1);
128         size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
129         if(l == (size_t)(-1)) {
130                 return "Character conversion failed!";
131         }
132         else
133                 mbs[l] = 0;
134         return *mbs;
135 }
136
137 #endif
138
139 // Get an sha-1 hash of the player's name combined with
140 // the password entered. That's what the server uses as
141 // their password. (Exception : if the password field is
142 // blank, we send a blank password - this is for backwards
143 // compatibility with password-less players).
144 std::string translatePassword(std::string playername, std::wstring password)
145 {
146         if(password.length() == 0)
147                 return "";
148
149         std::string slt = playername + wide_to_narrow(password);
150         SHA1 sha1;
151         sha1.addBytes(slt.c_str(), slt.length());
152         unsigned char *digest = sha1.getDigest();
153         std::string pwd = base64_encode(digest, 20);
154         free(digest);
155         return pwd;
156 }
157
158 std::string urlencode(std::string str)
159 {
160         // Encodes non-unreserved URI characters by a percent sign
161         // followed by two hex digits. See RFC 3986, section 2.3.
162         static const char url_hex_chars[] = "0123456789ABCDEF";
163         std::ostringstream oss(std::ios::binary);
164         for (u32 i = 0; i < str.size(); i++) {
165                 unsigned char c = str[i];
166                 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
167                         oss << c;
168                 else
169                         oss << "%"
170                                 << url_hex_chars[(c & 0xf0) >> 4]
171                                 << url_hex_chars[c & 0x0f];
172         }
173         return oss.str();
174 }
175
176 std::string urldecode(std::string str)
177 {
178         // Inverse of urlencode
179         std::ostringstream oss(std::ios::binary);
180         for (u32 i = 0; i < str.size(); i++) {
181                 unsigned char highvalue, lowvalue;
182                 if (str[i] == '%' &&
183                                 hex_digit_decode(str[i+1], highvalue) &&
184                                 hex_digit_decode(str[i+2], lowvalue)) {
185                         oss << (char) ((highvalue << 4) | lowvalue);
186                         i += 2;
187                 }
188                 else
189                         oss << str[i];
190         }
191         return oss.str();
192 }
193
194 u32 readFlagString(std::string str, FlagDesc *flagdesc, u32 *flagmask)
195 {
196         u32 result = 0, mask = 0;
197         char *s = &str[0];
198         char *flagstr, *strpos = NULL;
199
200         while ((flagstr = strtok_r(s, ",", &strpos))) {
201                 s = NULL;
202
203                 while (*flagstr == ' ' || *flagstr == '\t')
204                         flagstr++;
205
206                 bool flagset = true;
207                 if (!strncasecmp(flagstr, "no", 2)) {
208                         flagset = false;
209                         flagstr += 2;
210                 }
211
212                 for (int i = 0; flagdesc[i].name; i++) {
213                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
214                                 mask |= flagdesc[i].flag;
215                                 if (flagset)
216                                         result |= flagdesc[i].flag;
217                                 break;
218                         }
219                 }
220         }
221
222         if (flagmask)
223                 *flagmask = mask;
224
225         return result;
226 }
227
228 std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask)
229 {
230         std::string result;
231
232         for (int i = 0; flagdesc[i].name; i++) {
233                 if (flagmask & flagdesc[i].flag) {
234                         if (!(flags & flagdesc[i].flag))
235                                 result += "no";
236
237                         result += flagdesc[i].name;
238                         result += ", ";
239                 }
240         }
241
242         size_t len = result.length();
243         if (len >= 2)
244                 result.erase(len - 2, 2);
245
246         return result;
247 }
248
249 size_t mystrlcpy(char *dst, const char *src, size_t size)
250 {
251         size_t srclen  = strlen(src) + 1;
252         size_t copylen = MYMIN(srclen, size);
253
254         if (copylen > 0) {
255                 memcpy(dst, src, copylen);
256                 dst[copylen - 1] = '\0';
257         }
258
259         return srclen;
260 }
261
262 char *mystrtok_r(char *s, const char *sep, char **lasts)
263 {
264         char *t;
265
266         if (!s)
267                 s = *lasts;
268
269         while (*s && strchr(sep, *s))
270                 s++;
271
272         if (!*s)
273                 return NULL;
274
275         t = s;
276         while (*t) {
277                 if (strchr(sep, *t)) {
278                         *t++ = '\0';
279                         break;
280                 }
281                 t++;
282         }
283         
284         *lasts = t;
285         return s;
286 }
287
288 u64 read_seed(const char *str)
289 {
290         char *endptr;
291         u64 num;
292         
293         if (str[0] == '0' && str[1] == 'x')
294                 num = strtoull(str, &endptr, 16);
295         else
296                 num = strtoull(str, &endptr, 10);
297                 
298         if (*endptr)
299                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
300                 
301         return num;
302 }