Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvng <viktor.govako@gmail.com>2015-10-09 18:46:57 +0300
committervng <viktor.govako@gmail.com>2015-10-12 12:36:38 +0300
commit93fb8cdc33a6a515c4de798f773680db1562a05d (patch)
treec3acb62800885690efe760492b385fd93eb0fce2 /coding/file_container.cpp
parentf4082a8ef7f6a7057f66afb7369101bc59937cd7 (diff)
[mwm] Put feature offsets table into MWM container.
Diffstat (limited to 'coding/file_container.cpp')
-rw-r--r--coding/file_container.cpp127
1 files changed, 81 insertions, 46 deletions
diff --git a/coding/file_container.cpp b/coding/file_container.cpp
index dedcd2a13a..52d189679e 100644
--- a/coding/file_container.cpp
+++ b/coding/file_container.cpp
@@ -81,7 +81,20 @@ FilesContainerR::ReaderT FilesContainerR::GetReader(Tag const & tag) const
if (p)
return m_source.SubReader(p->m_offset, p->m_size);
else
- MYTHROW(Reader::OpenException, (tag));
+ MYTHROW(Reader::OpenException, ("Can't find section:", tag));
+}
+
+pair<uint64_t, uint64_t> FilesContainerR::GetAbsoluteOffsetAndSize(Tag const & tag) const
+{
+ Info const * p = GetInfo(tag);
+ if (p)
+ {
+ auto reader = dynamic_cast<FileReader const *>(m_source.GetPtr());
+ uint64_t const offset = reader ? reader->GetOffset() : 0;
+ return make_pair(offset + p->m_offset, p->m_size);
+ }
+ else
+ MYTHROW(Reader::OpenException, ("Can't find section:", tag));
}
FilesContainerBase::Info const * FilesContainerBase::GetInfo(Tag const & tag) const
@@ -95,28 +108,14 @@ FilesContainerBase::Info const * FilesContainerBase::GetInfo(Tag const & tag) co
return 0;
}
+namespace detail
+{
/////////////////////////////////////////////////////////////////////////////
-// FilesMappingContainer
+// MappedFile
/////////////////////////////////////////////////////////////////////////////
-
-FilesMappingContainer::FilesMappingContainer(string const & fName)
-{
- Open(fName);
-}
-
-FilesMappingContainer::~FilesMappingContainer()
+void MappedFile::Open(string const & fName)
{
Close();
-}
-
-void FilesMappingContainer::Open(string const & fName)
-{
- Close();
-
- {
- FileReader reader(fName);
- ReadInfo(reader);
- }
#ifdef OMIM_OS_WINDOWS
m_hFile = CreateFileA(fName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
@@ -130,11 +129,9 @@ void FilesMappingContainer::Open(string const & fName)
if (m_fd == -1)
MYTHROW(Reader::OpenException, ("Can't open file:", fName));
#endif
-
- m_name = fName;
}
-void FilesMappingContainer::Close()
+void MappedFile::Close()
{
#ifdef OMIM_OS_WINDOWS
if (m_hMapping != INVALID_HANDLE_VALUE)
@@ -148,42 +145,80 @@ void FilesMappingContainer::Close()
m_fd = -1;
}
#endif
-
- m_name.clear();
}
-FilesMappingContainer::Handle FilesMappingContainer::Map(Tag const & tag) const
+MappedFile::Handle MappedFile::Map(uint64_t off, uint64_t size, string const & tag) const
{
- Info const * p = GetInfo(tag);
- if (p)
- {
#ifdef OMIM_OS_WINDOWS
- SYSTEM_INFO sysInfo;
- memset(&sysInfo, 0, sizeof(sysInfo));
- GetSystemInfo(&sysInfo);
- long const offsetAlign = sysInfo.dwAllocationGranularity;
+ SYSTEM_INFO sysInfo;
+ memset(&sysInfo, 0, sizeof(sysInfo));
+ GetSystemInfo(&sysInfo);
+ long const offsetAlign = sysInfo.dwAllocationGranularity;
#else
- long const offsetAlign = sysconf(_SC_PAGE_SIZE);
+ long const offsetAlign = sysconf(_SC_PAGE_SIZE);
#endif
- uint64_t const offset = (p->m_offset / offsetAlign) * offsetAlign;
- ASSERT_LESS_OR_EQUAL(offset, p->m_offset, ());
- uint64_t const length = p->m_size + (p->m_offset - offset);
- ASSERT_GREATER_OR_EQUAL(length, p->m_size, ());
+ uint64_t const offset = (off / offsetAlign) * offsetAlign;
+ ASSERT_LESS_OR_EQUAL(offset, off, ());
+ uint64_t const length = size + (off - offset);
+ ASSERT_GREATER_OR_EQUAL(length, size, ());
#ifdef OMIM_OS_WINDOWS
- void * pMap = MapViewOfFile(m_hMapping, FILE_MAP_READ, offset >> (sizeof(DWORD) * 8), DWORD(offset), length);
- if (pMap == NULL)
- MYTHROW(Reader::OpenException, ("Can't map section:", tag, "with [offset, size]:", *p, "win last error:", GetLastError()));
+ void * pMap = MapViewOfFile(m_hMapping, FILE_MAP_READ, offset >> (sizeof(DWORD) * 8), DWORD(offset), length);
+ if (pMap == NULL)
+ MYTHROW(Reader::OpenException, ("Can't map section:", tag, "with [offset, size]:", off, size, "win last error:", GetLastError()));
#else
- void * pMap = mmap(0, length, PROT_READ, MAP_SHARED, m_fd, offset);
- if (pMap == MAP_FAILED)
- MYTHROW(Reader::OpenException, ("Can't map section:", tag, "with [offset, size]:", *p));
+ void * pMap = mmap(0, length, PROT_READ, MAP_SHARED, m_fd, offset);
+ if (pMap == MAP_FAILED)
+ MYTHROW(Reader::OpenException, ("Can't map section:", tag, "with [offset, size]:", off, size));
#endif
- char const * data = reinterpret_cast<char const *>(pMap);
- char const * d = data + (p->m_offset - offset);
- return Handle(d, data, p->m_size, length);
+ char const * data = reinterpret_cast<char const *>(pMap);
+ char const * d = data + (off - offset);
+ return Handle(d, data, size, length);
+}
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// FilesMappingContainer
+/////////////////////////////////////////////////////////////////////////////
+
+FilesMappingContainer::FilesMappingContainer(string const & fName)
+{
+ Open(fName);
+}
+
+FilesMappingContainer::~FilesMappingContainer()
+{
+ Close();
+}
+
+void FilesMappingContainer::Open(string const & fName)
+{
+ {
+ FileReader reader(fName);
+ ReadInfo(reader);
+ }
+
+ m_file.Open(fName);
+
+ m_name = fName;
+}
+
+void FilesMappingContainer::Close()
+{
+ m_file.Close();
+
+ m_name.clear();
+}
+
+FilesMappingContainer::Handle FilesMappingContainer::Map(Tag const & tag) const
+{
+ Info const * p = GetInfo(tag);
+ if (p)
+ {
+ ASSERT_EQUAL(tag, p->m_tag, ());
+ return m_file.Map(p->m_offset, p->m_size, tag);
}
else
MYTHROW(Reader::OpenException, ("Can't find section:", tag));