diff --git a/coding/file_container.cpp b/coding/file_container.cpp index 6af4112311..28716334e1 100644 --- a/coding/file_container.cpp +++ b/coding/file_container.cpp @@ -22,6 +22,13 @@ template void Write(TSink & sink, FilesContainerBase::Info const & WriteVarUint(sink, i.m_size); } +string DebugPrint(FilesContainerBase::Info const & info) +{ + ostringstream ss; + ss << "{ " << info.m_tag << ", " << info.m_offset << ", " << info.m_size << " }"; + return ss.str(); +} + ///////////////////////////////////////////////////////////////////////////// // FilesContainerBase ///////////////////////////////////////////////////////////////////////////// @@ -95,17 +102,23 @@ void FilesContainerW::Open(FileWriter::Op op) case FileWriter::OP_WRITE_EXISTING: { - { - // read an existing service info - FileReader reader(m_name); - ReadInfo(reader); - } - - // Important: in append mode we should sort info-vector by offsets - sort(m_info.begin(), m_info.end(), LessOffset()); - break; + // read an existing service info + FileReader reader(m_name); + ReadInfo(reader); } + // Important: in append mode we should sort info-vector by offsets + sort(m_info.begin(), m_info.end(), LessOffset()); + + // Check that all offsets are unique +#ifdef DEBUG + for (size_t i = 1; i < m_info.size(); ++i) + ASSERT(m_info[i-1].m_offset < m_info[i].m_offset || + m_info[i-1].m_size == 0 || + m_info[i].m_size == 0, ()); +#endif + break; + default: ASSERT ( false, ("Unsupported options") ); break; diff --git a/coding/file_container.hpp b/coding/file_container.hpp index ad7f64af80..715023d0c9 100644 --- a/coding/file_container.hpp +++ b/coding/file_container.hpp @@ -5,6 +5,7 @@ #include "../std/vector.hpp" #include "../std/string.hpp" + class FilesContainerBase { public: @@ -18,6 +19,8 @@ public: Info() {} Info(Tag const & tag, uint64_t offset) : m_tag(tag), m_offset(offset) {} + + friend string DebugPrint(Info const & info); }; protected: @@ -36,19 +39,12 @@ protected: return (t1 < t2.m_tag); } }; + struct LessOffset { bool operator() (Info const & t1, Info const & t2) const { - if (t1.m_offset == t2.m_offset) - { - // Element with nonzero size should be the last one, - // for correct append writer mode (FilesContainerW::GetWriter). - ASSERT ( t1.m_size == 0 || t2.m_size == 0, (t1.m_size, t2.m_size) ); - return (t1.m_size < t2.m_size); - } - else - return (t1.m_offset < t2.m_offset); + return (t1.m_offset < t2.m_offset); } bool operator() (Info const & t1, uint64_t const & t2) const { @@ -59,6 +55,7 @@ protected: return (t1 < t2.m_offset); } }; + class EqualTag { Tag const & m_tag;