Daniele Bartolini 11 лет назад
Родитель
Сommit
f36e78ba23

+ 7 - 7
engine/core/filesystem/disk_filesystem.cpp

@@ -9,19 +9,21 @@
 #include "disk_file.h"
 #include "vector.h"
 #include "path.h"
+#include "os.h"
 
 namespace crown
 {
 
 DiskFilesystem::DiskFilesystem()
 {
-	os::getcwd(_root_path, MAX_PATH_LENGTH);
+	char buf[512];
+	os::getcwd(buf, sizeof(buf));
+	_prefix = buf;
 }
 
-DiskFilesystem::DiskFilesystem(const char* root_path)
+DiskFilesystem::DiskFilesystem(const char* prefix)
+	: _prefix(prefix)
 {
-	CE_ASSERT_NOT_NULL(root_path);
-	strncpy(_root_path, root_path, MAX_PATH_LENGTH);
 }
 
 File* DiskFilesystem::open(const char* path, FileOpenMode mode)
@@ -139,9 +141,7 @@ void DiskFilesystem::get_absolute_path(const char* path, DynamicString& os_path)
 		return;
 	}
 
-	os_path += _root_path;
-	os_path += PATH_SEPARATOR;
-	os_path += path;
+	path::join(_prefix.c_str(), path, os_path);
 }
 
 } // namespace crown

+ 4 - 5
engine/core/filesystem/disk_filesystem.h

@@ -6,7 +6,6 @@
 #pragma once
 
 #include "filesystem.h"
-#include "os.h" // for max_path_length
 
 namespace crown
 {
@@ -27,10 +26,10 @@ public:
 	/// the engine executable.
 	DiskFilesystem();
 
-	/// Sets the root path to the given @a root_path.
+	/// Sets the root path to the given @a prefix.
 	/// @note
-	/// The @a root_path must be absolute.
-	DiskFilesystem(const char* root_path);
+	/// The @a prefix must be absolute.
+	DiskFilesystem(const char* prefix);
 
 	/// Opens the file at the given @a path with the given @a mode.
 	File* open(const char* path, FileOpenMode mode);
@@ -69,7 +68,7 @@ public:
 
 private:
 
-	char _root_path[MAX_PATH_LENGTH];
+	DynamicString _prefix;
 };
 
 } // namespace crown

+ 1 - 1
engine/core/filesystem/network_file.cpp

@@ -24,7 +24,7 @@ NetworkFile::NetworkFile(const NetAddress& addr, uint16_t port, const char* file
 	, _port(port)
 	, _position(0)
 {
-	strncpy(_filename, filename, MAX_PATH_LENGTH);
+	strncpy(_filename, filename, 1024);
 	_socket.connect(addr, port);
 }
 

+ 1 - 1
engine/core/filesystem/network_file.h

@@ -68,7 +68,7 @@ public:
 
 private:
 
-	char _filename[MAX_PATH_LENGTH];
+	char _filename[1024];
 	NetAddress _address;
 	uint16_t _port;
 	TCPSocket _socket;

+ 1 - 55
engine/core/os.h

@@ -35,18 +35,6 @@
 
 namespace crown
 {
-
-#if CROWN_PLATFORM_LINUX
-	const size_t	MAX_PATH_LENGTH = 1024;
-	const char		PATH_SEPARATOR = '/';
-#elif CROWN_PLATFORM_WINDOWS
-	const size_t	MAX_PATH_LENGTH = 1024;
-	const char		PATH_SEPARATOR = '\\';
-#elif CROWN_PLATFORM_ANDROID
-	const size_t	MAX_PATH_LENGTH = 1024;
-	const char		PATH_SEPARATOR = '/';
-#endif
-
 namespace os
 {
 	inline void log(const char* msg)
@@ -191,7 +179,7 @@ namespace os
 		HANDLE file = INVALID_HANDLE_VALUE;
 		WIN32_FIND_DATA ffd;
 
-		char cur_path[MAX_PATH_LENGTH];
+		char cur_path[1024];
 
 		strncpy(cur_path, path, strlen(path) + 1);
 		strncat(cur_path, "\\*", 2);
@@ -218,48 +206,6 @@ namespace os
 #endif
 	}
 
-	/// Returns os-dependent path from os-indipendent @a path
-	inline const char* normalize_path(const char* path)
-	{
-#if CROWN_PLATFORM_POSIX
-		static char norm[MAX_PATH_LENGTH];
-		char* cur = norm;
-
-		while ((*path) != '\0')
-		{
-			if ((*path) == '\\')
-			{
-				(*cur) = PATH_SEPARATOR;
-			}
-			else
-			{
-				(*cur) = (*path);
-			}
-
-			path++;
-			cur++;
-		}
-
-		return norm;
-#elif CROWN_PLATFORM_WINDOWS
-		static char norm[MAX_PATH_LENGTH];
-
-		for (uint32_t i = 0; i < strlen(path)+1; i++)
-		{
-			if (path[i] == '/')
-			{
-				norm[i] = '\\';
-			}
-			else
-			{
-				norm[i] = path[i];
-			}
-		}
-
-		return norm;
-#endif
-	}
-
 	inline const char* getcwd(char* buf, size_t size)
 	{
 #if CROWN_PLATFORM_POSIX

+ 57 - 2
engine/core/strings/path.cpp

@@ -5,19 +5,26 @@
 
 #include "path.h"
 #include "platform.h"
+#include "string_utils.h"
 #include <ctype.h> // isalpha
 
 namespace crown
 {
 namespace path
 {
+#if CROWN_PLATFORM_POSIX
+	const char SEPARATOR = '/';
+#elif CROWN_PLATFORM_WINDOWS
+	const char SEPARATOR = '\\';
+#endif // CROWN_PLATFORM_POSIX
+
 	bool is_absolute_path(const char* path)
 	{
 		CE_ASSERT(path != NULL, "Path must be != NULL");
 #if CROWN_PLATFORM_POSIX
-		return strlen(path) > 0 && path[0] == '/';
+		return strlen(path) > 0 && path[0] == SEPARATOR;
 #elif CROWN_PLATFORM_WINDOWS
-		return strlen(path) > 2 && isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
+		return strlen(path) > 2 && isalpha(path[0]) && path[1] == ':' && path[2] == SEPARATOR;
 #endif
 	}
 
@@ -31,6 +38,54 @@ namespace path
 #endif
 	}
 
+	void join(const char* p1, const char* p2, DynamicString& path)
+	{
+		path += p1;
+		path += SEPARATOR;
+		path += p2;
+	}
+
+	const char* normalize_path(const char* path)
+	{
+#if CROWN_PLATFORM_POSIX
+		static char norm[1024];
+		char* cur = norm;
+
+		while ((*path) != '\0')
+		{
+			if ((*path) == '\\')
+			{
+				(*cur) = SEPARATOR;
+			}
+			else
+			{
+				(*cur) = (*path);
+			}
+
+			path++;
+			cur++;
+		}
+
+		return norm;
+#elif CROWN_PLATFORM_WINDOWS
+		static char norm[1024];
+
+		for (uint32_t i = 0; i < strlen(path)+1; i++)
+		{
+			if (path[i] == '/')
+			{
+				norm[i] = SEPARATOR;
+			}
+			else
+			{
+				norm[i] = path[i];
+			}
+		}
+
+		return norm;
+#endif
+	}
+
 	/// Returns the pathname of the path.
 	/// @note
 	/// e.g. "/home/project/texture.tga" -> "/home/project"

+ 8 - 2
engine/core/strings/path.h

@@ -5,11 +5,11 @@
 
 #pragma once
 
-#include "string_utils.h"
+#include "platform.h"
+#include "dynamic_string.h"
 
 namespace crown
 {
-
 /// @defgroup Path Path
 
 /// Functions for operating on strings as file paths.
@@ -23,6 +23,12 @@ namespace path
 	/// Returns whether the @a path is the root path.
 	bool is_root_path(const char* path);
 
+	/// Appends path @a p2 to @a p1 and fills @a path with the result.
+	void join(const char* p1, const char* p2, DynamicString& path);
+
+	/// Returns os-dependent path from os-indipendent @a path
+	const char* normalize(const char* path);
+
 	void pathname(const char* path, char* str, size_t len);
 	void filename(const char* path, char* str, size_t len);
 	void basename(const char* path, char* str, size_t len);

+ 5 - 3
engine/main/main_android.cpp

@@ -232,9 +232,11 @@ void android_main(struct android_app* app)
 	ConfigSettings cs;
 
 	memory_globals::init();
-	// DiskFilesystem src_fs(cls.source_dir);
-	ApkFilesystem src_fs(app->activity->assetManager);
-	parse_config_file(src_fs, cs);
+	{
+		// DiskFilesystem src_fs(cls.source_dir);
+		ApkFilesystem src_fs(app->activity->assetManager);
+		parse_config_file(src_fs, cs);
+	}
 
 	console_server_globals::init(cs.console_port, false);
 

+ 4 - 2
engine/main/main_linux.cpp

@@ -415,8 +415,10 @@ int main(int argc, char** argv)
 	parse_command_line(argc, argv, cs);
 
 	memory_globals::init();
-	DiskFilesystem src_fs(cs.source_dir);
-	parse_config_file(src_fs, cs);
+	{
+		DiskFilesystem fs(cs.source_dir);
+		parse_config_file(fs, cs);
+	}
 
 	console_server_globals::init(cs.console_port, cs.wait_console);
 

+ 4 - 2
engine/main/main_windows.cpp

@@ -339,8 +339,10 @@ int main(int argc, char** argv)
 	parse_command_line(argc, argv, cs);
 
 	memory_globals::init();
-	DiskFilesystem src_fs(cs.source_dir);
-	parse_config_file(src_fs, cs);
+	{
+		DiskFilesystem fs(cs.source_dir);
+		parse_config_file(fs, cs);
+	}
 
 	console_server_globals::init(cs.console_port, cs.wait_console);