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

Use standard POSIX file impl for android

mikymod 12 лет назад
Родитель
Сommit
9b5b3feffe
2 измененных файлов с 34 добавлено и 86 удалено
  1. 33 25
      src/os/android/OsFile.cpp
  2. 1 61
      src/os/android/OsFile.h

+ 33 - 25
src/os/android/OsFile.cpp

@@ -24,23 +24,23 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 OTHER DEALINGS IN THE SOFTWARE.
 */
 */
 
 
+#include <stdio.h>
+
 #include "Assert.h"
 #include "Assert.h"
 #include "OS.h"
 #include "OS.h"
 #include "OsFile.h"
 #include "OsFile.h"
-#include "AndroidOS.h"
 
 
 namespace crown
 namespace crown
 {
 {
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-OsFile::OsFile(const char* path, FileOpenMode mode)
+OsFile::OsFile(const char* path, FileOpenMode mode) :
+	m_file_handle(NULL)
 {
 {
-	// Android assets are always read-only
-	(void) mode;
-	m_mode = FOM_READ;
-	m_asset = AAssetManager_open(os::get_android_asset_manager(), path, AASSET_MODE_RANDOM);
+	m_file_handle = fopen(path, (mode == FOM_READ) ? "rb" : "wb");
+	CE_ASSERT(m_file_handle != NULL, "Unable to open file: %s", path);
 
 
-	CE_ASSERT(m_asset != NULL, "Unable to open file: %s", path);
+	m_mode = mode;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -52,21 +52,21 @@ OsFile::~OsFile()
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void OsFile::close()
 void OsFile::close()
 {
 {
-	if (m_asset != NULL)
+	if (m_file_handle != NULL)
 	{
 	{
-		AAsset_close(m_asset);
-		m_asset = NULL;
+		fclose(m_file_handle);
+		m_file_handle = NULL;
 	}
 	}
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 bool OsFile::is_open() const
 bool OsFile::is_open() const
 {
 {
-	return m_asset != NULL;
+	return m_file_handle != NULL;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-FileOpenMode OsFile::mode() const
+FileOpenMode OsFile::mode()
 {
 {
 	return m_mode;
 	return m_mode;
 }
 }
@@ -74,7 +74,17 @@ FileOpenMode OsFile::mode() const
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 size_t OsFile::size() const
 size_t OsFile::size() const
 {
 {
-	return AAsset_getLength(m_asset);
+	size_t pos = position();
+
+	int fseek_result = fseek(m_file_handle, 0, SEEK_END);
+	CE_ASSERT(fseek_result == 0, "Failed to seek");
+
+	size_t size = position();
+
+	fseek_result = fseek(m_file_handle, (long) pos, SEEK_SET);
+	CE_ASSERT(fseek_result == 0, "Failed to seek");
+
+	return size;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -82,7 +92,7 @@ size_t OsFile::read(void* data, size_t size)
 {
 {
 	CE_ASSERT(data != NULL, "Data must be != NULL");
 	CE_ASSERT(data != NULL, "Data must be != NULL");
 
 
-	return (size_t)AAsset_read(m_asset, data, size);
+	return fread(data, 1, size, m_file_handle);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -90,42 +100,40 @@ size_t OsFile::write(const void* data, size_t size)
 {
 {
 	CE_ASSERT(data != NULL, "Data must be != NULL");
 	CE_ASSERT(data != NULL, "Data must be != NULL");
 
 
-	os::printf("Android asset directory is read-only!");
-
-	return 0;
+	return fwrite(data, 1, size, m_file_handle);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void OsFile::seek(size_t position)
 void OsFile::seek(size_t position)
 {
 {
-	off_t seek_result = AAsset_seek(m_asset, (off_t)position, SEEK_SET);
-	CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
+	int fseek_result = fseek(m_file_handle, (long) position, SEEK_SET);
+	CE_ASSERT(fseek_result == 0, "Failed to seek");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void OsFile::seek_to_end()
 void OsFile::seek_to_end()
 {
 {
-	off_t seek_result = AAsset_seek(m_asset, 0, SEEK_END);
-	CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
+	int fseek_result = fseek(m_file_handle, 0, SEEK_END);
+	CE_ASSERT(fseek_result == 0, "Failed to seek");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void OsFile::skip(size_t bytes)
 void OsFile::skip(size_t bytes)
 {
 {
-	off_t seek_result = AAsset_seek(m_asset, (off_t) bytes, SEEK_CUR);
-	CE_ASSERT(seek_result != (off_t) -1, "Failed to seek");
+	int fseek_result = fseek(m_file_handle, bytes, SEEK_CUR);
+	CE_ASSERT(fseek_result == 0, "Failed to seek");
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 size_t OsFile::position() const
 size_t OsFile::position() const
 {
 {
-	return (size_t) (AAsset_getLength(m_asset) - AAsset_getRemainingLength(m_asset));
+	return (size_t) ftell(m_file_handle);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 bool OsFile::eof() const
 bool OsFile::eof() const
 {
 {
-	return AAsset_getRemainingLength(m_asset) == 0;
+	return feof(m_file_handle) != 0;
 }
 }
 
 
 } // namespace crown
 } // namespace crown

+ 1 - 61
src/os/android/OsFile.h

@@ -26,64 +26,4 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #pragma once
 #pragma once
 
 
-#include <sys/types.h>
-#include <android/asset_manager.h>
-
-#include "Types.h"
-#include "File.h"
-
-namespace crown
-{
-
-/// Android assets wrapper
-class OsFile
-{
-public:
-
-	/// Opens the file located at @a path with the given @a mode.
-							OsFile(const char* path, FileOpenMode mode);
-							~OsFile();
-
-	/// Closes the file.
-	void					close();
-
-	bool					is_open() const;
-
-	/// Return the size of the file in bytes.
-	size_t					size() const;
-
-	/// Returs the mode used to open the file.
-	FileOpenMode			mode() const;
-
-	/// Reads @a size bytes from the file and stores it into @a data.
-	/// Returns the number of bytes read.
-	size_t					read(void* data, size_t size);
-
-	/// Writes @a size bytes of data stored in @a data and returns the
-	/// number of bytes written.
-	size_t					write(const void* data, size_t size);
-
-	/// Moves the file pointer to the given @a position.
-	void					seek(size_t position);
-
-	/// Moves the file pointer to the end of the file.
-	void					seek_to_end();
-
-	/// Moves the file pointer @a 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;
-	FileOpenMode			m_mode;
-};
-
-} // namespace crown
+#include "../posix/OsFile.h"