فهرست منبع

Update android low-level file

Daniele Bartolini 12 سال پیش
والد
کامیت
4f5e64775f
2فایلهای تغییر یافته به همراه106 افزوده شده و 56 حذف شده
  1. 61 28
      src/os/android/File.cpp
  2. 45 28
      src/os/android/File.h

+ 61 - 28
src/os/android/File.cpp

@@ -23,105 +23,138 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
+#include <cassert>
+#include <stdio.h>
+
+#include "OS.h"
 #include "File.h"
-#include "Log.h"
-#include "MathUtils.h"
 #include "AndroidOS.h"
-#include <cassert>
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 File::File() :
-	m_asset(NULL), m_mode(FOM_READ)
+	m_file_handle(NULL),
+	m_mode(SOM_READ)
 {
 }
 
 //-----------------------------------------------------------------------------
 File::~File()
+{
+	close();
+}
+
+//-----------------------------------------------------------------------------
+void File::close()
 {
 	if (m_asset != NULL)
 	{
 		AAsset_close(m_asset);
+		m_asset = NULL;
 	}
 }
 
 //-----------------------------------------------------------------------------
-bool File::is_valid()
+bool File::is_open() const
 {
 	return m_asset != NULL;
 }
 
 //-----------------------------------------------------------------------------
-FileOpenMode File::mode()
+StreamOpenMode File::mode()
 {
 	return m_mode;
 }
 
 //-----------------------------------------------------------------------------
-File* File::open(const char* path, FileOpenMode mode)
+size_t File::size() const
+{
+	assert(m_asset != NULL);
+	
+	return AAsset_getLength(m_asset);
+}
+
+//-----------------------------------------------------------------------------
+bool File::open(const char* path, StreamOpenMode mode)
 {
-	File* f = new File();
+	assert(!is_open());
 
-	f->m_asset = AAssetManager_open(os::get_android_asset_manager(), path, AASSET_MODE_RANDOM);
+	// Android assets are always read-only
+	(void) mode;
+	m_mode = SOM_READ;
 
-	if (f->m_asset == NULL)
+	m_asset = AAssetManager_open(os::get_android_asset_manager(), path, AASSET_MODE_RANDOM);
+
+	if (m_asset == NULL)
 	{
-		Log::E("File::Open: Could not open file %s", path);
-		return NULL;
+		os::printf("Could not open asset %s", path);
+
+		return false;
 	}
 
-	f->m_mode = FOM_READ;
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+size_t File::read(void* data, size_t size)
+{
+	assert(m_asset != NULL);
+	assert(data != NULL);
 
-	return f;
+	return (size_t)AAsset_read(m_asset, data, size);
 }
 
 //-----------------------------------------------------------------------------
-size_t File::read(void* ptr, size_t size, size_t nmemb)
+size_t File::write(const void* data, size_t size)
 {
 	assert(m_asset != NULL);
-	assert(ptr != NULL);
+	assert(data != NULL);
 
-	return (size_t)AAsset_read(m_asset, ptr, size * nmemb);
+	os::printf("Android asset directory is read-only!");
+
+	return 0;
 }
 
 //-----------------------------------------------------------------------------
-size_t File::write(const void* ptr, size_t size, size_t nmemb)
+void File::seek(size_t position)
 {
-	Log::W("Cannot write to Android asset directory!!!");
+	assert(m_asset != NULL);
+
+	assert(AAsset_seek(m_asset, (off_t)position, SEEK_SET) != (off_t) -1);
 }
 
 //-----------------------------------------------------------------------------
-int File::seek(int32_t offset, int whence)
+void File::seek_to_end()
 {
 	assert(m_asset != NULL);
 
-	return AAsset_seek(m_asset, (off_t)offset, whence);
+	assert(AAsset_seek(m_asset, 0, SEEK_END) != (off_t) -1);
 }
 
 //-----------------------------------------------------------------------------
-int32_t File::tell()
+void File::skip(size_t bytes)
 {
 	assert(m_asset != NULL);
-	
-	return (int32_t)(AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
+
+	assert(AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR) != (off_t) -1);
 }
 
 //-----------------------------------------------------------------------------
-int File::eof()
+size_t File::position() const
 {
 	assert(m_asset != NULL);
 	
-	return (int)(AAsset_getRemainingLength(m_asset) == 0);
+	return (size_t) (AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
 }
 
 //-----------------------------------------------------------------------------
-size_t File::size()
+bool File::eof() const
 {
 	assert(m_asset != NULL);
 	
-	return AAsset_getLength(m_asset);
+	return AAsset_getRemainingLength(m_asset) == 0;
 }
 
 } // namespace crown

+ 45 - 28
src/os/android/File.h

@@ -25,54 +25,71 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "Types.h"
 #include <sys/types.h>
 #include <android/asset_manager.h>
 
-namespace crown
-{
+#include "Types.h"
+#include "Stream.h"
 
-/**
-	Enumerates file opening modes.
-*/
-enum FileOpenMode
+namespace crown
 {
-	FOM_READ	= 1,
-	FOM_WRITE	= 2
-};
 
-/**
-	Standard C file wrapper.
-*/
+/// Android assets wrapper
 class File
 {
-
 public:
 
-						~File();
+							File();
+							~File();
 
-	bool				is_valid();
+	/// Opens the file located at @path with the given @mode.
+	bool					open(const char* path, StreamOpenMode mode);
 
-	FileOpenMode		mode();
+	/// Closes the file.
+	void					close();
 
-	size_t				read(void* ptr, size_t size, size_t nmemb);
-	size_t				write(const void* ptr, size_t size, size_t nmemb);
-	int					seek(int32_t offset, int whence);
-	int32_t				tell();
+	bool					is_open() const;
 
-	int					eof();
+	/// Return the size of the file in bytes.
+	size_t					size() const;
 
-	size_t				size();
+	/// Returs the mode used to open the file.
+	StreamOpenMode			mode();
 
-	static File*		open(const char* path, FileOpenMode mode);
+	/// Reads @size bytes from the file and stores it into @data.
+	/// Returns the number of bytes read.
+	size_t					read(void* data, size_t size);
 
-private:
+	/// Writes @size bytes of data stored in @data and returns the
+	/// number of bytes written.
+	size_t					write(const void* data, size_t size);
 
-	AAsset*				m_asset;
-	FileOpenMode		m_mode;
+	/// Moves the file pointer to the given @position.
+	void					seek(size_t position);
 
-						File();
+	/// Moves the file pointer to the end of the file.
+	void					seek_to_end();
+
+	/// Moves the file pointer @bytes bytes ahead the current
+	/// file pointer position.
+	void					skip(size_t bytes);
+
+	/// Returns the position of the file pointer from the
+	/// start of the file in bytes.
+	size_t					position() const;
+
+	/// Returns whether the file pointer is at the end of the file.
+	bool					eof() const;
+
+private:
+
+	AAsset*					m_asset;
+	StreamOpenMode			m_mode;
 };
 
 } // namespace crown
 
+
+
+} // namespace crown
+