Răsfoiți Sursa

Introduce file sources

Daniele Bartolini 12 ani în urmă
părinte
comite
7703e8d723

+ 147 - 0
engine/core/filesystem/DiskFileSource.cpp

@@ -0,0 +1,147 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "DiskFileSource.h"
+#include "StringUtils.h"
+#include "TempAllocator.h"
+#include "DynamicString.h"
+#include "DiskFile.h"
+#include "OS.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+DiskFileSource::DiskFileSource()
+	: FileSource(true)
+{
+	string::strncpy(m_root_path, os::get_cwd(), MAX_PATH_LENGTH);
+}
+
+//-----------------------------------------------------------------------------
+DiskFileSource::DiskFileSource(const char* root_path)
+	: FileSource(true)
+{
+	CE_ASSERT_NOT_NULL(root_path);
+
+	string::strncpy(m_root_path, root_path, MAX_PATH_LENGTH);
+}
+
+//-----------------------------------------------------------------------------
+File* DiskFileSource::open(const char* path, FileOpenMode mode)
+{
+	CE_ASSERT_NOT_NULL(path);
+
+	TempAllocator256 alloc;
+	DynamicString abs_path(alloc);
+	get_absolute_path(path, abs_path);
+
+	return CE_NEW(default_allocator(), DiskFile)(mode, abs_path.c_str());
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::close(File* file)
+{
+	CE_ASSERT_NOT_NULL(file);
+
+	CE_DELETE(default_allocator(), file);
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::create_directory(const char* path)
+{
+	CE_ASSERT_NOT_NULL(path);
+
+	TempAllocator256 alloc;
+	DynamicString abs_path(alloc);
+	get_absolute_path(path, abs_path);
+
+	os::create_directory(abs_path.c_str());
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::delete_directory(const char* path)
+{
+	CE_ASSERT_NOT_NULL(path);
+
+	TempAllocator256 alloc;
+	DynamicString abs_path(alloc);
+	get_absolute_path(path, abs_path);
+
+	os::delete_directory(abs_path.c_str());
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::create_file(const char* path)
+{
+	CE_ASSERT_NOT_NULL(path);
+
+	TempAllocator256 alloc;
+	DynamicString abs_path(alloc);
+	get_absolute_path(path, abs_path);
+
+	os::create_file(abs_path.c_str());
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::delete_file(const char* path)
+{
+	CE_ASSERT_NOT_NULL(path);
+
+	TempAllocator256 alloc;
+	DynamicString abs_path(alloc);
+	get_absolute_path(path, abs_path);
+
+	os::delete_file(abs_path.c_str());
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::list_files(const char* path, Vector<DynamicString>& files)
+{
+	CE_ASSERT_NOT_NULL(path);
+
+	TempAllocator256 alloc;
+	DynamicString abs_path(alloc);
+	get_absolute_path(path, abs_path);
+
+	os::list_files(path, files);
+}
+
+//-----------------------------------------------------------------------------
+void DiskFileSource::get_absolute_path(const char* path, DynamicString& os_path)
+{
+	if (os::is_absolute_path(path))
+	{
+		os_path = path;
+		return;
+	}
+
+	os_path += m_root_path;
+	os_path += PATH_SEPARATOR;
+	os_path += path;
+}
+
+} // namespace crown

+ 90 - 0
engine/core/filesystem/DiskFileSource.h

@@ -0,0 +1,90 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "File.h"
+#include "FileSource.h"
+#include "Vector.h"
+#include "DynamicString.h"
+#include "OS.h"
+
+namespace crown
+{
+
+class DynamicString;
+
+/// Access files on disk.
+/// All the file paths can be either relative or absolute.
+/// When a relative path is given, it is automatically translated
+/// to its absolute counterpart based on the file source's root path.
+/// Accessing files using absolute path directly is also possible,
+/// but platform-specific and thus generally not recommended.
+class DiskFileSource : public FileSource
+{
+public:
+
+	/// Sets the root path to the current working directory of
+	/// the engine executable.
+	DiskFileSource();
+
+	/// Sets the root path to the given @a root_path.
+	/// @note
+	/// The @a root_path must be absolute.
+	DiskFileSource(const char* root_path);
+
+	/// Opens the file at the given @a path with the given @a mode.
+	File* open(const char* path, FileOpenMode mode);
+
+	/// Closes the given @a file.
+	void close(File* file);
+
+	/// Creates the directory at the given @a path.
+	void create_directory(const char* path);
+
+	/// Deletes the directory at the given @a path.
+	void delete_directory(const char* path);
+
+	/// Creates the file at the given @a path.
+	void create_file(const char* path);
+
+	/// Deletes the file at the given @a path.
+	void delete_file(const char* path);
+
+	/// Returns the relative file names in the given @a path.
+	void list_files(const char* path, Vector<DynamicString>& files);
+
+	/// Returns the absolute path of the given @a path based on
+	/// the root path of the file source. If @a path is absolute,
+	/// the given path is returned.
+	void get_absolute_path(const char* path, DynamicString& os_path);
+
+private:
+
+	char m_root_path[MAX_PATH_LENGTH];
+};
+
+} // namespace crown

+ 64 - 0
engine/core/filesystem/FileSource.h

@@ -0,0 +1,64 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "File.h"
+#include "Vector.h"
+#include "DynamicString.h"
+
+namespace crown
+{
+
+/// Abstract base class for providing access to files
+/// on a particular physical/logical device.
+class FileSource
+{
+public:
+
+	FileSource(bool can_write) : m_can_write(can_write) {}
+	virtual ~FileSource() {}
+
+	virtual File* open(const char* path, FileOpenMode mode) = 0;
+	virtual void close(File* file) = 0;
+
+	virtual void create_directory(const char* path) = 0;
+	virtual void delete_directory(const char* path) = 0;
+	virtual void create_file(const char* path) = 0;
+	virtual void delete_file(const char* path) = 0;
+	virtual void list_files(const char* path, Vector<DynamicString>& files) = 0;
+
+	bool can_write() const
+	{
+		return m_can_write;
+	}
+
+protected:
+
+	bool m_can_write;
+};
+
+} // namespace crown

+ 15 - 106
engine/core/filesystem/Filesystem.cpp

@@ -25,22 +25,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Filesystem.h"
-#include "Log.h"
-#include "OS.h"
-#include "DiskFile.h"
-#include "Memory.h"
-#include "Hash.h"
-#include "DiskMountPoint.h"
-#include "StringSetting.h"
-#include "MountPoint.h"
-
+#include "FileSource.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Filesystem::Filesystem() :
-	m_mount_point_head(NULL)
+Filesystem::Filesystem(FileSource& source)
+	: m_source(&source)
 {
 }
 
@@ -50,122 +42,39 @@ Filesystem::~Filesystem()
 }
 
 //-----------------------------------------------------------------------------
-void Filesystem::mount(MountPoint& mp)
+File* Filesystem::open(const char* path, FileOpenMode mode)
 {
-	if (m_mount_point_head != NULL)
-	{
-		mp.m_next = m_mount_point_head; 
-	}
-
-	m_mount_point_head = &mp;
-}
-
-//-----------------------------------------------------------------------------
-void Filesystem::umount(MountPoint& mp)
-{
-	MountPoint* current = m_mount_point_head;
-	MountPoint* previous;
-	MountPoint* tmp;
-	(void)tmp;
-
-	if (&mp == current)
-	{	
-		tmp = current;
-
-		current = current->m_next;
-
-		tmp = NULL;
-
-		return;
-	}
-	else
-	{
-		previous = current;
-		current = current->m_next;
-
-		while (current != NULL && &mp != current)
-		{
-			previous = current;
-
-			current = current->m_next;
-		}
-
-		if (current != NULL)
-		{
-			tmp = current;
-
-			previous->m_next = current->m_next;
-
-			tmp = NULL;
-
-			return;
-		}
-	}
+	return m_source->open(path, mode);
 }
 
 //-----------------------------------------------------------------------------
-File* Filesystem::open(const char* mount_point, const char* relative_path, FileOpenMode mode)
+void Filesystem::close(File* file)
 {
-	MountPoint* mp = find_mount_point(mount_point);
-
-	if (mp)
-	{
-		return mp->open(relative_path, mode);
-	}
-
-	return NULL;
+	m_source->close(file);
 }
 
 //-----------------------------------------------------------------------------
-void Filesystem::close(File* file)
+void Filesystem::create_directory(const char* path)
 {
-	CE_DELETE(default_allocator(), file);
+	m_source->create_directory(path);
 }
 
 //-----------------------------------------------------------------------------
-bool Filesystem::exists(const char* mount_point, const char* relative_path)
+void Filesystem::delete_directory(const char* path)
 {
-	MountPoint* mp = find_mount_point(mount_point);
-
-	if (mp)
-	{
-		return mp->exists(relative_path);
-	}
-
-	return false;
+	m_source->delete_directory(path);
 }
 
 //-----------------------------------------------------------------------------
-const char* Filesystem::os_path(const char* mount_point, const char* relative_path)
+void Filesystem::create_file(const char* path)
 {
-	MountPoint* mp = find_mount_point(mount_point);
-
-	if (mp)
-	{
-		return mp->os_path(relative_path);
-	}
-
-	return NULL;
+	m_source->create_file(path);
 }
 
 //-----------------------------------------------------------------------------
-MountPoint*	Filesystem::find_mount_point(const char* mount_point)
+void Filesystem::delete_file(const char* path)
 {
-	MountPoint* curr = m_mount_point_head;
-
-	uint32_t type_hash = hash::murmur2_32(mount_point, string::strlen(mount_point), 0);
-
-	while(curr != NULL)
-	{
-		if (curr->type() == type_hash)
-		{
-			return curr;
-		}
-
-		curr = curr->m_next;
-	}
-
-	return NULL;
+	m_source->delete_file(path);
 }
 
 } // namespace crown

+ 12 - 33
engine/core/filesystem/Filesystem.h

@@ -26,16 +26,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "StringUtils.h"
-#include "OS.h"
 #include "File.h"
-#include "HeapAllocator.h"
-#include "MountPoint.h"
 
 namespace crown
 {
 
-class File;
+class FileSource;
 
 /// Provides a platform-independent way to access files and directories
 /// on the host filesystem.
@@ -86,43 +82,26 @@ class Filesystem
 {
 public:
 
-						Filesystem();
+						Filesystem(FileSource& source);
 						~Filesystem();
 
-	/// Makes available mount point @a mp
-	void				mount(MountPoint& mp);
+	File*				open(const char* path, FileOpenMode mode);
+	void				close(File* file);
 
-	/// Makes unavailable mount point @a mp
-	void				umount(MountPoint& mp);
-
-	/// Opens the file @a relative_path with the specified access @a mode
-	/// contained in @a mount_point
-	File*				open(const char* mount_point, const char* relative_path, FileOpenMode mode);
-
-	/// Closes a previously opened file @a stream
-	void				close(File* stream);
-
-	/// Returns true if file @a relative_path exists in @a mount_point
-	bool				exists(const char* mount_point, const char* relative_path);
+	void				create_directory(const char* path);
+	void				delete_directory(const char* path);
+	void				create_file(const char* path);
+	void				delete_file(const char* path);
+		
+private:
 
-	/// Returns path of file @a relative_path in @a mount_point
-	const char*			os_path(const char* mount_point, const char* relative_path);
+	FileSource*			m_source;
 
-private:
-	
-	/// Returns the first mount point according to @a mount_point or NULL.
-	MountPoint*			find_mount_point(const char* mount_point);				
+private:			
 
 	// Disable copying
 						Filesystem(const Filesystem&);
 	Filesystem&			operator=(const Filesystem&);
-		
-private:
-
-	char				m_root_path[MAX_PATH_LENGTH];
-	MountPoint* 		m_mount_point_head;
-
-	friend class		Device;
 };
 
 } // namespace crown