summaryrefslogtreecommitdiff
path: root/modules/mono/utils
diff options
context:
space:
mode:
Diffstat (limited to 'modules/mono/utils')
-rw-r--r--modules/mono/utils/path_utils.cpp127
-rw-r--r--modules/mono/utils/path_utils.h32
-rw-r--r--modules/mono/utils/string_utils.cpp81
-rw-r--r--modules/mono/utils/string_utils.h15
-rw-r--r--modules/mono/utils/thread_local.h6
5 files changed, 205 insertions, 56 deletions
diff --git a/modules/mono/utils/path_utils.cpp b/modules/mono/utils/path_utils.cpp
index 6e431f51e7..20863b1afe 100644
--- a/modules/mono/utils/path_utils.cpp
+++ b/modules/mono/utils/path_utils.cpp
@@ -36,16 +36,21 @@
#include "core/project_settings.h"
#ifdef WINDOWS_ENABLED
+#include <windows.h>
+
#define ENV_PATH_SEP ";"
#else
-#define ENV_PATH_SEP ":"
#include <limits.h>
+#include <unistd.h>
+
+#define ENV_PATH_SEP ":"
#endif
#include <stdlib.h>
-String path_which(const String &p_name) {
+namespace path {
+String find_executable(const String &p_name) {
#ifdef WINDOWS_ENABLED
Vector<String> exts = OS::get_singleton()->get_environment("PATHEXT").split(ENV_PATH_SEP, false);
#endif
@@ -55,7 +60,7 @@ String path_which(const String &p_name) {
return String();
for (int i = 0; i < env_path.size(); i++) {
- String p = path_join(env_path[i], p_name);
+ String p = path::join(env_path[i], p_name);
#ifdef WINDOWS_ENABLED
for (int j = 0; j < exts.size(); j++) {
@@ -73,42 +78,96 @@ String path_which(const String &p_name) {
return String();
}
-void fix_path(const String &p_path, String &r_out) {
- r_out = p_path.replace("\\", "/");
+String cwd() {
+#ifdef WINDOWS_ENABLED
+ const DWORD expected_size = ::GetCurrentDirectoryW(0, NULL);
+
+ String buffer;
+ buffer.resize((int)expected_size);
+ if (::GetCurrentDirectoryW(expected_size, buffer.ptrw()) == 0)
+ return ".";
+
+ return buffer.simplify_path();
+#else
+ char buffer[PATH_MAX];
+ if (::getcwd(buffer, sizeof(buffer)) == NULL)
+ return ".";
+
+ String result;
+ if (result.parse_utf8(buffer))
+ return ".";
- while (true) { // in case of using 2 or more slash
- String compare = r_out.replace("//", "/");
- if (r_out == compare)
- break;
- else
- r_out = compare;
+ return result.simplify_path();
+#endif
+}
+
+String abspath(const String &p_path) {
+ if (p_path.is_abs_path()) {
+ return p_path.simplify_path();
+ } else {
+ return path::join(path::cwd(), p_path).simplify_path();
}
}
-bool rel_path_to_abs(const String &p_existing_path, String &r_abs_path) {
+String realpath(const String &p_path) {
#ifdef WINDOWS_ENABLED
- CharType ret[_MAX_PATH];
- if (::_wfullpath(ret, p_existing_path.c_str(), _MAX_PATH)) {
- String abspath = String(ret).replace("\\", "/");
- int pos = abspath.find(":/");
- if (pos != -1) {
- r_abs_path = abspath.substr(pos - 1, abspath.length());
- } else {
- r_abs_path = abspath;
- }
- return true;
- }
-#else
- char *resolved_path = ::realpath(p_existing_path.utf8().get_data(), NULL);
- if (resolved_path) {
- String retstr;
- bool success = !retstr.parse_utf8(resolved_path);
- ::free(resolved_path);
- if (success) {
- r_abs_path = retstr;
- return true;
- }
+ // Open file without read/write access
+ HANDLE hFile = ::CreateFileW(p_path.c_str(), 0,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+
+ if (hFile == INVALID_HANDLE_VALUE)
+ return p_path;
+
+ const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, NULL, 0, FILE_NAME_NORMALIZED);
+
+ if (expected_size == 0) {
+ ::CloseHandle(hFile);
+ return p_path;
}
+
+ String buffer;
+ buffer.resize((int)expected_size);
+ ::GetFinalPathNameByHandleW(hFile, buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
+
+ ::CloseHandle(hFile);
+ return buffer.simplify_path();
+#elif UNIX_ENABLED
+ char *resolved_path = ::realpath(p_path.utf8().get_data(), NULL);
+
+ if (!resolved_path)
+ return p_path;
+
+ String result;
+ bool parse_ok = result.parse_utf8(resolved_path);
+ ::free(resolved_path);
+
+ if (parse_ok)
+ return p_path;
+
+ return result.simplify_path();
#endif
- return false;
}
+
+String join(const String &p_a, const String &p_b) {
+ if (p_a.empty())
+ return p_b;
+
+ const CharType a_last = p_a[p_a.length() - 1];
+ if ((a_last == '/' || a_last == '\\') ||
+ (p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
+ return p_a + p_b;
+ }
+
+ return p_a + "/" + p_b;
+}
+
+String join(const String &p_a, const String &p_b, const String &p_c) {
+ return path::join(path::join(p_a, p_b), p_c);
+}
+
+String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
+ return path::join(path::join(path::join(p_a, p_b), p_c), p_d);
+}
+
+} // namespace path
diff --git a/modules/mono/utils/path_utils.h b/modules/mono/utils/path_utils.h
index 69edf4deb7..ca25bc09f7 100644
--- a/modules/mono/utils/path_utils.h
+++ b/modules/mono/utils/path_utils.h
@@ -31,24 +31,32 @@
#ifndef PATH_UTILS_H
#define PATH_UTILS_H
+#include "core/string_builder.h"
#include "core/ustring.h"
-_FORCE_INLINE_ String path_join(const String &e1, const String &e2) {
- return e1.plus_file(e2);
-}
+namespace path {
-_FORCE_INLINE_ String path_join(const String &e1, const String &e2, const String &e3) {
- return e1.plus_file(e2).plus_file(e3);
-}
+String join(const String &p_a, const String &p_b);
+String join(const String &p_a, const String &p_b, const String &p_c);
+String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d);
-_FORCE_INLINE_ String path_join(const String &e1, const String &e2, const String &e3, const String &e4) {
- return e1.plus_file(e2).plus_file(e3).plus_file(e4);
-}
+String find_executable(const String &p_name);
-String path_which(const String &p_name);
+/// Returns a normalized absolute path to the current working directory
+String cwd();
-void fix_path(const String &p_path, String &r_out);
+/**
+ * Obtains a normalized absolute path to p_path. Symbolic links are
+ * not resolved. The path p_path might not exist in the file system.
+ */
+String abspath(const String &p_path);
-bool rel_path_to_abs(const String &p_existing_path, String &r_abs_path);
+/**
+ * Obtains a normalized path to p_path with symbolic links resolved.
+ * The resulting path might be either a relative or an absolute path.
+ */
+String realpath(const String &p_path);
+
+} // namespace path
#endif // PATH_UTILS_H
diff --git a/modules/mono/utils/string_utils.cpp b/modules/mono/utils/string_utils.cpp
index c390f8b9c2..88366a6a03 100644
--- a/modules/mono/utils/string_utils.cpp
+++ b/modules/mono/utils/string_utils.cpp
@@ -32,6 +32,9 @@
#include "core/os/file_access.h"
+#include <stdio.h>
+#include <stdlib.h>
+
namespace {
int sfind(const String &p_text, int p_from) {
@@ -41,7 +44,7 @@ int sfind(const String &p_text, int p_from) {
int src_len = 2;
int len = p_text.length();
- if (src_len == 0 || len == 0)
+ if (len == 0)
return -1;
const CharType *src = p_text.c_str();
@@ -52,10 +55,7 @@ int sfind(const String &p_text, int p_from) {
for (int j = 0; j < src_len; j++) {
int read_pos = i + j;
- if (read_pos >= len) {
- ERR_PRINT("read_pos >= len");
- return -1;
- };
+ ERR_FAIL_COND_V(read_pos >= len, -1);
switch (j) {
case 0:
@@ -63,7 +63,7 @@ int sfind(const String &p_text, int p_from) {
break;
case 1: {
CharType c = src[read_pos];
- found = src[read_pos] == 's' || (c >= '0' || c <= '4');
+ found = src[read_pos] == 's' || (c >= '0' && c <= '4');
break;
}
default:
@@ -165,7 +165,7 @@ Error read_all_file_utf8(const String &p_path, String &r_content) {
PoolVector<uint8_t> sourcef;
Error err;
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
- ERR_FAIL_COND_V(err != OK, err);
+ ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
int len = f->get_len();
sourcef.resize(len + 1);
@@ -184,3 +184,70 @@ Error read_all_file_utf8(const String &p_path, String &r_content) {
r_content = source;
return OK;
}
+
+// TODO: Move to variadic templates once we upgrade to C++11
+
+String str_format(const char *p_format, ...) {
+ va_list list;
+
+ va_start(list, p_format);
+ String res = str_format(p_format, list);
+ va_end(list);
+
+ return res;
+}
+// va_copy was defined in the C99, but not in C++ standards before C++11.
+// When you compile C++ without --std=c++<XX> option, compilers still define
+// va_copy, otherwise you have to use the internal version (__va_copy).
+#if !defined(va_copy)
+#if defined(__GNUC__)
+#define va_copy(d, s) __va_copy((d), (s))
+#else
+#define va_copy(d, s) ((d) = (s))
+#endif
+#endif
+
+#if defined(MINGW_ENABLED) || defined(_MSC_VER) && _MSC_VER < 1900
+#define gd_vsnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf_s(m_buffer, m_count, _TRUNCATE, m_format, m_args_copy)
+#define gd_vscprintf(m_format, m_args_copy) _vscprintf(m_format, m_args_copy)
+#else
+#define gd_vsnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf(m_buffer, m_count, m_format, m_args_copy)
+#define gd_vscprintf(m_format, m_args_copy) vsnprintf(NULL, 0, p_format, m_args_copy)
+#endif
+
+String str_format(const char *p_format, va_list p_list) {
+ char *buffer = str_format_new(p_format, p_list);
+
+ String res(buffer);
+ memdelete_arr(buffer);
+
+ return res;
+}
+
+char *str_format_new(const char *p_format, ...) {
+ va_list list;
+
+ va_start(list, p_format);
+ char *res = str_format_new(p_format, list);
+ va_end(list);
+
+ return res;
+}
+
+char *str_format_new(const char *p_format, va_list p_list) {
+ va_list list;
+
+ va_copy(list, p_list);
+ int len = gd_vscprintf(p_format, list);
+ va_end(list);
+
+ len += 1; // for the trailing '/0'
+
+ char *buffer(memnew_arr(char, len));
+
+ va_copy(list, p_list);
+ gd_vsnprintf(buffer, len, p_format, list);
+ va_end(list);
+
+ return buffer;
+}
diff --git a/modules/mono/utils/string_utils.h b/modules/mono/utils/string_utils.h
index 61765ccfd8..e7f02955bd 100644
--- a/modules/mono/utils/string_utils.h
+++ b/modules/mono/utils/string_utils.h
@@ -34,6 +34,8 @@
#include "core/ustring.h"
#include "core/variant.h"
+#include <stdarg.h>
+
String sformat(const String &p_text, const Variant &p1 = Variant(), const Variant &p2 = Variant(), const Variant &p3 = Variant(), const Variant &p4 = Variant(), const Variant &p5 = Variant());
#ifdef TOOLS_ENABLED
@@ -44,4 +46,17 @@ String escape_csharp_keyword(const String &p_name);
Error read_all_file_utf8(const String &p_path, String &r_content);
+#if defined(__GNUC__)
+#define _PRINTF_FORMAT_ATTRIBUTE_1_0 __attribute__((format(printf, 1, 0)))
+#define _PRINTF_FORMAT_ATTRIBUTE_1_2 __attribute__((format(printf, 1, 2)))
+#else
+#define _PRINTF_FORMAT_ATTRIBUTE_1_0
+#define _PRINTF_FORMAT_ATTRIBUTE_1_2
+#endif
+
+String str_format(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_1_2;
+String str_format(const char *p_format, va_list p_list) _PRINTF_FORMAT_ATTRIBUTE_1_0;
+char *str_format_new(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_1_2;
+char *str_format_new(const char *p_format, va_list p_list) _PRINTF_FORMAT_ATTRIBUTE_1_0;
+
#endif // STRING_FORMAT_H
diff --git a/modules/mono/utils/thread_local.h b/modules/mono/utils/thread_local.h
index 276db8830b..e52b6e73ef 100644
--- a/modules/mono/utils/thread_local.h
+++ b/modules/mono/utils/thread_local.h
@@ -39,7 +39,7 @@
#error Platform or compiler not supported
#endif
-#ifdef __GNUC__
+#if defined(__GNUC__)
#ifdef HAVE_GCC___THREAD
#define _THREAD_LOCAL_(m_t) __thread m_t
@@ -47,7 +47,7 @@
#define USE_CUSTOM_THREAD_LOCAL
#endif
-#elif _MSC_VER
+#elif defined(_MSC_VER)
#ifdef HAVE_DECLSPEC_THREAD
#define _THREAD_LOCAL_(m_t) __declspec(thread) m_t
@@ -76,7 +76,7 @@ struct ThreadLocalStorage {
void *get_value() const;
void set_value(void *p_value) const;
- void alloc(void(_CALLBACK_FUNC_ *p_dest_callback)(void *));
+ void alloc(void(_CALLBACK_FUNC_ *p_destr_callback)(void *));
void free();
private: