Răsfoiți Sursa

libobs/util: Add functions to get/set the cwd

Adds functions to get or set the current working directory.
jp9000 10 ani în urmă
părinte
comite
5c0f4546ea
3 a modificat fișierele cu 46 adăugiri și 0 ștergeri
  1. 10 0
      libobs/util/platform-nix.c
  2. 33 0
      libobs/util/platform-windows.c
  3. 3 0
      libobs/util/platform.h

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

@@ -412,3 +412,13 @@ error:
 	fclose(file_in);
 	return ret;
 }
+
+char *os_getcwd(char *path, size_t size)
+{
+	return getcwd(path, size);
+}
+
+int os_chdir(const char *path)
+{
+	return chdir(path);
+}

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

@@ -520,3 +520,36 @@ error:
 	bfree(file_out_utf16);
 	return code;
 }
+
+char *os_getcwd(char *path, size_t size)
+{
+	wchar_t *path_w;
+	DWORD len;
+
+	len = GetCurrentDirectoryW(0, NULL);
+	if (!len)
+		return NULL;
+
+	path_w = bmalloc((len + 1) * sizeof(wchar_t));
+	GetCurrentDirectoryW(len + 1, path_w);
+	os_wcs_to_utf8(path_w, (size_t)len, path, size);
+	bfree(path_w);
+
+	return path;
+}
+
+int os_chdir(const char *path)
+{
+	wchar_t *path_w = NULL;
+	size_t size;
+	int ret;
+
+	size = os_utf8_to_wcs_ptr(path, 0, &path_w);
+	if (!path_w)
+		return -1;
+
+	ret = SetCurrentDirectoryW(path_w) ? 0 : -1;
+	bfree(path_w);
+
+	return ret;
+}

+ 3 - 0
libobs/util/platform.h

@@ -130,6 +130,9 @@ EXPORT void os_globfree(os_glob_t *pglob);
 EXPORT int os_unlink(const char *path);
 EXPORT int os_rmdir(const char *path);
 
+EXPORT char *os_getcwd(char *path, size_t size);
+EXPORT int os_chdir(const char *path);
+
 #define MKDIR_EXISTS   1
 #define MKDIR_SUCCESS  0
 #define MKDIR_ERROR   -1