]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/string.cpp
Fix BSD iconv declaration
[minetest.git] / src / util / string.cpp
index 689f58f7f304d5ad8f14205dfa992e17879b0ca2..d70c26015deee23a960e86c76940e6f095dd9817 100644 (file)
@@ -39,16 +39,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        #include <windows.h>
 #endif
 
-#ifdef __NetBSD__
-       #include <sys/param.h>
-       #if __NetBSD_Version__ <= 999001500
-               #define BSD_ICONV_USED
-       #endif
-#elif defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__OpenBSD__) || \
-       defined(__DragonFly__))
-       #define BSD_ICONV_USED
-#endif
-
 #ifndef _WIN32
 
 static bool convert(const char *to, const char *from, char *outbuf,
@@ -56,11 +46,7 @@ static bool convert(const char *to, const char *from, char *outbuf,
 {
        iconv_t cd = iconv_open(to, from);
 
-#ifdef BSD_ICONV_USED
-       const char *inbuf_ptr = inbuf;
-#else
        char *inbuf_ptr = inbuf;
-#endif
        char *outbuf_ptr = outbuf;
 
        size_t *inbuf_left_ptr = &inbuf_size;
@@ -494,6 +480,7 @@ const static std::unordered_map<std::string, u32> s_named_colors = {
        {"plum",                 0xdda0dd},
        {"powderblue",           0xb0e0e6},
        {"purple",               0x800080},
+       {"rebeccapurple",        0x663399},
        {"red",                  0xff0000},
        {"rosybrown",            0xbc8f8f},
        {"royalblue",            0x4169e1},
@@ -864,40 +851,29 @@ static const std::array<std::wstring, 30> disallowed_dir_names = {
 static const std::wstring disallowed_path_chars = L"<>:\"/\\|?*.";
 
 
-/**
- * @param str
- * @return A copy of \p str with trailing whitespace removed.
- */
-static std::wstring wrtrim(const std::wstring &str)
-{
-       size_t back = str.size();
-       while (back > 0 && std::isspace(str[back - 1]))
-               --back;
-
-       return str.substr(0, back);
-}
-
-
-/**
- * Sanitize the name of a new directory. This consists of two stages:
- * 1. Check for 'reserved filenames' that can't be used on some filesystems
- *     and add a prefix to them
- * 2. Remove 'unsafe' characters from the name by replacing them with '_'
- */
 std::string sanitizeDirName(const std::string &str, const std::string &optional_prefix)
 {
        std::wstring safe_name = utf8_to_wide(str);
 
-       std::wstring dev_name = wrtrim(safe_name);
-
        for (std::wstring disallowed_name : disallowed_dir_names) {
-               if (str_equal(dev_name, disallowed_name, true)) {
+               if (str_equal(safe_name, disallowed_name, true)) {
                        safe_name = utf8_to_wide(optional_prefix) + safe_name;
                        break;
                }
        }
 
-       for (unsigned long i = 0; i < safe_name.length(); i++) {
+       // Replace leading and trailing spaces with underscores.
+       size_t start = safe_name.find_first_not_of(L' ');
+       size_t end = safe_name.find_last_not_of(L' ');
+       if (start == std::wstring::npos || end == std::wstring::npos)
+               start = end = safe_name.size();
+       for (size_t i = 0; i < start; i++)
+               safe_name[i] = L'_';
+       for (size_t i = end + 1; i < safe_name.size(); i++)
+               safe_name[i] = L'_';
+
+       // Replace other disallowed characters with underscores
+       for (size_t i = 0; i < safe_name.length(); i++) {
                bool is_valid = true;
 
                // Unlikely, but control characters should always be blacklisted
@@ -909,7 +885,7 @@ std::string sanitizeDirName(const std::string &str, const std::string &optional_
                }
 
                if (!is_valid)
-                       safe_name[i] = '_';
+                       safe_name[i] = L'_';
        }
 
        return wide_to_utf8(safe_name);