From 0e6cfcbd3540ba64781eaac4f90ec2a4c2cc2eb6 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Wed, 4 Jan 2012 14:12:44 +0300 Subject: [PATCH] [android] Switched from FileReader to MmapReader --- coding/coding_tests/zip_reader_test.cpp | 10 +++++----- coding/mmap_reader.cpp | 7 +++++++ coding/mmap_reader.hpp | 4 ++++ coding/zip_reader.hpp | 6 +++--- platform/platform_android.cpp | 4 ++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/coding/coding_tests/zip_reader_test.cpp b/coding/coding_tests/zip_reader_test.cpp index 43b76a018f..c533fa029d 100644 --- a/coding/coding_tests/zip_reader_test.cpp +++ b/coding/coding_tests/zip_reader_test.cpp @@ -34,7 +34,7 @@ UNIT_TEST(ZipReaderSmoke) r.ReadAsString(s); TEST_EQUAL(s, "Test\n", ("Invalid zip file contents")); } - catch (FileReader::Exception const & e) + catch (std::exception const & e) { noException = false; LOG(LERROR, (e.what())); @@ -47,7 +47,7 @@ UNIT_TEST(ZipReaderSmoke) { ZipFileReader r("some_nonexisting_filename", "test.txt"); } - catch (FileReader::Exception const &) + catch (std::exception const &) { noException = false; } @@ -59,7 +59,7 @@ UNIT_TEST(ZipReaderSmoke) { ZipFileReader r(ZIPFILE, "test"); } - catch (FileReader::Exception const &) + catch (std::exception const &) { noException = false; } @@ -110,7 +110,7 @@ UNIT_TEST(ZipFilesList) TEST_EQUAL(files[1], "2.txt", ()); TEST_EQUAL(files[2], "3.ttt", ()); } - catch (FileReader::OpenException const & e) + catch (std::exception const & e) { TEST(false, ("Can't get list of files inside zip", e.what())); } @@ -120,7 +120,7 @@ UNIT_TEST(ZipFilesList) vector files = ZipFileReader::FilesList(ZIPFILE_INVALID); TEST(false, ("This test shouldn't be reached - exception should be thrown")); } - catch (FileReader::OpenException const &) + catch (std::exception const &) { } diff --git a/coding/mmap_reader.cpp b/coding/mmap_reader.cpp index 14712869ff..31f0c35d4d 100644 --- a/coding/mmap_reader.cpp +++ b/coding/mmap_reader.cpp @@ -89,3 +89,10 @@ uint8_t * MmapReader::Data() const { return m_data->m_memory; } + +void MmapReader::SetOffsetAndSize(uint64_t offset, uint64_t size) +{ + ASSERT_LESS_OR_EQUAL(offset + size, Size(), (offset, size)); + m_offset = offset; + m_size = size; +} diff --git a/coding/mmap_reader.hpp b/coding/mmap_reader.hpp index 55ff0b0ba4..c1cf2a0bdb 100644 --- a/coding/mmap_reader.hpp +++ b/coding/mmap_reader.hpp @@ -25,4 +25,8 @@ public: /// Direct file/memory access uint8_t * Data() const; + +protected: + // Used in special derived readers. + void SetOffsetAndSize(uint64_t offset, uint64_t size); }; diff --git a/coding/zip_reader.hpp b/coding/zip_reader.hpp index 7128b587dd..129ae14234 100644 --- a/coding/zip_reader.hpp +++ b/coding/zip_reader.hpp @@ -1,12 +1,12 @@ #pragma once -#include "file_reader.hpp" +#include "mmap_reader.hpp" #include "../base/exception.hpp" -class ZipFileReader : public FileReader +class ZipFileReader : public MmapReader { - typedef FileReader base_type; + typedef MmapReader base_type; public: DECLARE_EXCEPTION(OpenZipException, OpenException); diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index a0f43fff85..9475fbedd8 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -1,6 +1,6 @@ #include "platform.hpp" -#include "../coding/file_reader.hpp" +#include "../coding/mmap_reader.hpp" #include "../coding/zip_reader.hpp" #include @@ -23,7 +23,7 @@ bool Platform::IsFileExistsByFullPath(string const & filePath) ModelReader * Platform::GetReader(string const & file) const { if (IsFileExistsByFullPath(m_writableDir + file)) - return new FileReader(ReadPathForFile(file), 10, 12); + return new MmapReader(ReadPathForFile(file)); else { ASSERT_EQUAL(file.find("assets/"), string::npos, ("Do not use assets/, only file name"));