Przeglądaj źródła

libobs/util: Add os_rename function

Allows moving/renaming of files.
jp9000 10 lat temu
rodzic
commit
50f4793c99

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

@@ -14,6 +14,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <stdio.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -303,6 +304,11 @@ int os_mkdir(const char *path)
 	return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
 }
 
+int os_rename(const char *old_path, const char *new_path)
+{
+	return rename(old_path, new_path);
+}
+
 #if !defined(__APPLE__)
 os_performance_token_t *os_request_high_performance(const char *reason)
 {

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

@@ -413,6 +413,26 @@ int os_mkdir(const char *path)
 	return MKDIR_SUCCESS;
 }
 
+int os_rename(const char *old_path, const char *new_path)
+{
+	wchar_t *old_path_utf16 = NULL;
+	wchar_t *new_path_utf16 = NULL;
+	int code = -1;
+
+	if (!os_utf8_to_wcs_ptr(old_path, 0, &old_path_utf16)) {
+		return -1;
+	}
+	if (!os_utf8_to_wcs_ptr(new_path, 0, &new_path_utf16)) {
+		goto error;
+	}
+
+	code = MoveFileW(old_path_utf16, new_path_utf16) ? 0 : -1;
+
+error:
+	bfree(old_path_utf16);
+	bfree(new_path_utf16);
+	return code;
+}
 
 BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
 {

+ 1 - 0
libobs/util/platform.h

@@ -134,6 +134,7 @@ EXPORT int os_unlink(const char *path);
 #define MKDIR_ERROR   -1
 
 EXPORT int os_mkdir(const char *path);
+EXPORT int os_rename(const char *old_path, const char *new_path);
 
 #ifdef _MSC_VER
 #define strtoll _strtoi64