Просмотр исходного кода

Fix Filesystem docs and other minor fixes

Daniele Bartolini 12 лет назад
Родитель
Сommit
a0fb4e14b5
2 измененных файлов с 49 добавлено и 56 удалено
  1. 17 31
      src/Filesystem.cpp
  2. 32 25
      src/Filesystem.h

+ 17 - 31
src/Filesystem.cpp

@@ -58,26 +58,16 @@ const char* Filesystem::build_os_path(const char* base_path, const char* relativ
 {
 	static char os_path[os::MAX_PATH_LENGTH];
 
-	size_t i = 0;
+	string::strncpy(os_path, base_path, os::MAX_PATH_LENGTH);
 
-	while (*base_path != '\0')
-	{
-		os_path[i++] = *base_path;
-		base_path++;
-	}
+	size_t base_path_len = string::strlen(base_path);
 
-	os_path[i++] = '/';
-
-	while (*relative_path != '\0')
-	{
-		os_path[i++] = *relative_path;
-		relative_path++;
-	}
+	os_path[base_path_len] = os::PATH_SEPARATOR;
 
-	os_path[i] = '\0';
+	string::strncpy(&os_path[base_path_len + 1], relative_path, os::MAX_PATH_LENGTH);
 
-	// Replace Crown-specific path separator with OS-speficic one
-	for (size_t j = 0; j < i; j++)
+	// FIXME FIXME FIXME Replace Crown-specific path separator with OS-speficic one
+	for (size_t j = 0; j < string::strlen(os_path); j++)
 	{
 		if (os_path[j] == '/')
 		{
@@ -89,12 +79,12 @@ const char* Filesystem::build_os_path(const char* base_path, const char* relativ
 }
 
 //-----------------------------------------------------------------------------
-bool Filesystem::get_info(const char* base_path, const char* relative_path, FilesystemEntry& info)
+bool Filesystem::get_info(const char* relative_path, FilesystemEntry& info)
 {
 	// Entering OS-DEPENDENT-PATH-MODE
-	// (i.e. os_path is of the form: C:\babbeo\relative_path or /babbeo/relative_path)
+	// (i.e. os_path is of the form: C:\foo\relative_path or /foo/relative_path)
 
-	const char* os_path = build_os_path(base_path, relative_path);
+	const char* os_path = build_os_path(m_root_path, relative_path);
 	
 	string::strncpy(info.os_path, os_path, os::MAX_PATH_LENGTH);
 	string::strncpy(info.relative_path, relative_path, os::MAX_PATH_LENGTH);
@@ -104,8 +94,7 @@ bool Filesystem::get_info(const char* base_path, const char* relative_path, File
 		info.type = FilesystemEntry::FILE;
 		return true;
 	}
-
-	if (os::is_dir(os_path))
+	else if (os::is_dir(os_path))
 	{
 		info.type = FilesystemEntry::DIRECTORY;
 		return true;
@@ -121,7 +110,7 @@ bool Filesystem::exists(const char* relative_path)
 {
 	FilesystemEntry dummy;
 
-	return get_info(m_root_path, relative_path, dummy);
+	return get_info(relative_path, dummy);
 }
 
 //-----------------------------------------------------------------------------
@@ -129,7 +118,7 @@ bool Filesystem::is_file(const char* relative_path)
 {
 	FilesystemEntry info;
 
-	if (get_info(m_root_path, relative_path, info))
+	if (get_info(relative_path, info))
 	{
 		return info.type == FilesystemEntry::FILE;
 	}
@@ -142,7 +131,7 @@ bool Filesystem::is_dir(const char* relative_path)
 {
 	FilesystemEntry info;
 
-	if (get_info(m_root_path, relative_path, info))
+	if (get_info(relative_path, info))
 	{
 		return info.type == FilesystemEntry::DIRECTORY;
 	}
@@ -183,20 +172,17 @@ bool Filesystem::delete_dir(const char* relative_path)
 }
 
 //-----------------------------------------------------------------------------
-Stream* Filesystem::open(const char* relative_path, StreamOpenMode mode)
+FileStream* Filesystem::open(const char* relative_path, StreamOpenMode mode)
 {
 	FilesystemEntry info;
-	Stream* stream;
-
-	get_info(m_root_path, relative_path, info);
 
-	stream = new FileStream(mode, info.os_path);
+	assert(get_info(relative_path, info) && info.type == FilesystemEntry::FILE);
 
-	return stream;
+	return new FileStream(mode, info.os_path);
 }
 
 //-----------------------------------------------------------------------------
-void Filesystem::close(Stream* stream)
+void Filesystem::close(FileStream* stream)
 {
 	delete stream;
 }

+ 32 - 25
src/Filesystem.h

@@ -26,8 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "String.h"
-#include "Stream.h"
 #include "OS.h"
+#include "Stream.h"
 
 namespace crown
 {
@@ -49,8 +49,8 @@ struct FilesystemEntry
 	char			relative_path[os::MAX_PATH_LENGTH];	///< Relative path of the entry
 };
 
-/// Filesystem.
-///
+class FileStream;
+
 /// Provides a platform-independent way to access files and directories
 /// on the host filesystem.
 ///
@@ -71,14 +71,13 @@ struct FilesystemEntry
 ///                             // so it refers to "/home/foo/bar.txt"
 ///
 /// The filesystem will take care of the necessary path conversions.
-/// The root path can be really anything: platform-specific/absolute/relative/whatever.
-/// Examples of valid root paths.
+/// The root path must be an absolute path for the underlying operating system.
+/// Examples of valid root paths:
 ///
 /// 1) "/home/foo"
 /// 2) "C:\Users\Phil"
-/// 3) "\abc$\/..)?/"
 ///
-/// The relative paths must follow some strict rules:
+/// The relative paths, used to access files, must follow some strict rules:
 ///
 /// a) Only unix-like pathnames (i.e. case sensitive and using '/' as separator)
 ///    are allowed.
@@ -94,53 +93,61 @@ struct FilesystemEntry
 ///
 /// Examples of valid relative paths.
 ///
-/// data/textures/grass.texture
-/// grass.texture
-/// foo/bar
+/// 1) data/textures/grass.texture
+/// 2) grass.texture
+/// 3) foo/bar
 class Filesystem
 {
 public:
 
-						/// The @root_path must be absolute.
+	/// The @root_path must be absolute.
 						Filesystem(const char* root_path);
 						~Filesystem();
 
-						/// Returns the root path of the filesystem
+	/// Returns the root path of the filesystem
 	const char*			root_path() const;
 
-						/// TODO
-	bool				get_info(const char* base_path, const char* relative_path, FilesystemEntry& info);
+	/// Returns whether the @relative_path exists and fills @info with
+	/// with informations about the given @relative_path path
+	bool				get_info(const char* relative_path, FilesystemEntry& info);
 	
-						/// Returns whether the @relative_path exists on disk
+	/// Returns whether the @relative_path exists on disk
 	bool				exists(const char* relative_path);
 
-						/// Returns whether @relative_path is a regular file
+	/// Returns whether @relative_path is a regular file
 	bool				is_file(const char* relative_path);
-						/// Returns whether @relative_path if a directory
+
+	/// Returns whether @relative_path is a directory
 	bool				is_dir(const char* relative_path);
 
-						/// Creates a regular file named @relative_path
+	/// Creates a regular file named @relative_path
 	bool				create_file(const char* relative_path);
-						/// Creates a directory named @relative_path
+
+	/// Creates a directory named @relative_path
 	bool				create_dir(const char* relative_path);
 
-						/// Deletes the regular file @relative_path
+	/// Deletes the regular file @relative_path
 	bool				delete_file(const char* relative_path);
-						/// Deletes the directory @relative_path
+
+	/// Deletes the directory @relative_path
 	bool				delete_dir(const char* relative_path);
 
-	Stream*				open(const char* relative_path, StreamOpenMode mode);
-	void				close(Stream* stream);
+	/// Opens the file @relative_path with the specified access @mode
+	FileStream*			open(const char* relative_path, StreamOpenMode mode);
+
+	/// Closes a previously opened file @stream
+	void				close(FileStream* stream);
 	
 private:
 
-	const char*			build_os_path(const char* basePath, const char* relative_path);
+	// Builds the OS-dependent path from base_path and relative_path
+	const char*			build_os_path(const char* base_path, const char* relative_path);
 	
 private:
 
 	char				m_root_path[os::MAX_PATH_LENGTH];
 
-						// Disable copying
+	// Disable copying
 						Filesystem(const Filesystem&);
 	Filesystem&			operator=(const Filesystem&);