Daniele Bartolini 2 лет назад
Родитель
Сommit
b4d25dc4b8
2 измененных файлов с 33 добавлено и 1 удалено
  1. 18 1
      src/core/os.cpp
  2. 15 0
      src/core/os.h

+ 18 - 1
src/core/os.cpp

@@ -24,7 +24,7 @@
 	#include <dirent.h>   // opendir, readdir
 	#include <dlfcn.h>    // dlopen, dlclose, dlsym
 	#include <errno.h>
-	#include <stdio.h>    // fputs
+	#include <stdio.h>    // fputs, rename
 	#include <stdlib.h>   // getenv
 	#include <string.h>   // memset
 	#include <sys/wait.h> // wait
@@ -310,6 +310,23 @@ namespace os
 #endif
 	}
 
+	RenameResult rename(const char *old_name, const char *new_name)
+	{
+		RenameResult rr;
+#if CROWN_PLATFORM_WINDOWS
+		if (MoveFile(old_name, new_name) != 0)
+			rr.error = RenameResult::SUCCESS;
+		else
+			rr.error = RenameResult::UNKNOWN;
+#else
+		if (::rename(old_name, new_name) == 0)
+			rr.error == RenameResult::SUCCESS;
+		else
+			rr.error = RenameResult::UNKNOWN;
+#endif
+		return rr;
+	}
+
 } // namespace os
 
 } // namespace crown

+ 15 - 0
src/core/os.h

@@ -69,6 +69,18 @@ struct AccessFlags
 	};
 };
 
+/// Result from os::rename().
+///
+/// @a ingroup OS
+struct RenameResult
+{
+	enum
+	{
+		SUCCESS, ///<
+		UNKNOWN  ///< Unknown error.
+	} error;
+};
+
 /// Operating system functions.
 ///
 /// @ingroup OS
@@ -122,6 +134,9 @@ namespace os
 	///
 	s32 access(const char *path, u32 flags);
 
+	///
+	RenameResult rename(const char *old_name, const char *new_name);
+
 } // namespace os
 
 } // namespace crown