Răsfoiți Sursa

libobs/util: Add os_get_abs_path(_ptr) functions

These functions resolve the absolute path from a relative path.
jp9000 10 ani în urmă
părinte
comite
d542663478
4 a modificat fișierele cu 65 adăugiri și 18 ștergeri
  1. 2 18
      libobs/obs-windows.c
  2. 29 0
      libobs/util/platform-nix.c
  3. 31 0
      libobs/util/platform-windows.c
  4. 3 0
      libobs/util/platform.h

+ 2 - 18
libobs/obs-windows.c

@@ -508,22 +508,6 @@ void obs_key_combination_to_str(obs_key_combination_t combination,
 	}
 }
 
-static char *get_abs_path(const char *path)
-{
-	wchar_t wpath[512];
-	wchar_t wabspath[512];
-	char *abspath = NULL;
-	size_t len;
-
-	len = os_utf8_to_wcs(path, 0, wpath, 512);
-	if (!len)
-		return NULL;
-
-	if (_wfullpath(wabspath, wpath, 512) != NULL)
-		os_wcs_to_utf8_ptr(wabspath, 0, &abspath);
-	return abspath;
-}
-
 bool sym_initialize_called = false;
 
 void reset_win32_symbol_paths(void)
@@ -561,7 +545,7 @@ void reset_win32_symbol_paths(void)
 	if (!initialize_success)
 		return;
 
-	abspath = get_abs_path(".");
+	abspath = os_get_abs_path_ptr(".");
 	if (abspath)
 		da_push_back(paths, &abspath);
 
@@ -591,7 +575,7 @@ void reset_win32_symbol_paths(void)
 		}
 
 		if (!found) {
-			abspath = get_abs_path(path.array);
+			abspath = os_get_abs_path_ptr(path.array);
 			if (abspath)
 				da_push_back(paths, &abspath);
 		}

+ 29 - 0
libobs/util/platform-nix.c

@@ -20,6 +20,7 @@
 #include <sys/stat.h>
 #include <dirent.h>
 #include <stdlib.h>
+#include <limits.h>
 #include <dlfcn.h>
 #include <unistd.h>
 #include <glob.h>
@@ -241,6 +242,34 @@ bool os_file_exists(const char *path)
 	return access(path, F_OK) == 0;
 }
 
+size_t os_get_abs_path(const char *path, char *abspath, size_t size)
+{
+	size_t min_size = size < PATH_MAX ? size : PATH_MAX;
+	char newpath[PATH_MAX];
+	int ret;
+
+	if (!abspath)
+		return 0;
+
+	if (!realpath(path, newpath))
+		return 0;
+
+	ret = snprintf(abspath, min_size, "%s", newpath);
+	return ret >= 0 ? ret : 0;
+}
+
+char *os_get_abs_path_ptr(const char *path)
+{
+	char *ptr = bmalloc(512);
+
+	if (!os_get_abs_path(path, ptr, 512)) {
+		bfree(ptr);
+		ptr = NULL;
+	}
+
+	return ptr;
+}
+
 struct os_dir {
 	const char       *path;
 	DIR              *dir;

+ 31 - 0
libobs/util/platform-windows.c

@@ -263,6 +263,37 @@ bool os_file_exists(const char *path)
 	return hFind != INVALID_HANDLE_VALUE;
 }
 
+size_t os_get_abs_path(const char *path, char *abspath, size_t size)
+{
+	wchar_t wpath[512];
+	wchar_t wabspath[512];
+	size_t out_len = 0;
+	size_t len;
+
+	if (!abspath)
+		return 0;
+
+	len = os_utf8_to_wcs(path, 0, wpath, 512);
+	if (!len)
+		return 0;
+
+	if (_wfullpath(wabspath, wpath, 512) != NULL)
+		out_len = os_wcs_to_utf8(wabspath, 0, abspath, size);
+	return out_len;
+}
+
+char *os_get_abs_path_ptr(const char *path)
+{
+	char *ptr = bmalloc(512);
+
+	if (!os_get_abs_path(path, ptr, 512)) {
+		bfree(ptr);
+		ptr = NULL;
+	}
+
+	return ptr;
+}
+
 struct os_dir {
 	HANDLE           handle;
 	WIN32_FIND_DATA  wfd;

+ 3 - 0
libobs/util/platform.h

@@ -101,6 +101,9 @@ EXPORT char *os_get_config_path_ptr(const char *name);
 
 EXPORT bool os_file_exists(const char *path);
 
+EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);
+EXPORT char *os_get_abs_path_ptr(const char *path);
+
 struct os_dir;
 typedef struct os_dir os_dir_t;