diff --git a/coding/internal/file_data.cpp b/coding/internal/file_data.cpp index 1375012eb5..de8be0f5a3 100644 --- a/coding/internal/file_data.cpp +++ b/coding/internal/file_data.cpp @@ -19,7 +19,7 @@ #include #ifdef OMIM_OS_WINDOWS - #include +#include #endif #ifdef OMIM_OS_TIZEN @@ -31,7 +31,7 @@ using namespace std; namespace base { FileData::FileData(string const & fileName, Op op) - : m_FileName(fileName), m_Op(op) + : m_FileName(fileName), m_Op(op) { char const * const modes [] = {"rb", "wb", "r+b", "ab"}; #ifdef OMIM_OS_TIZEN @@ -291,22 +291,21 @@ bool WriteToTempAndRenameToFile(string const & dest, functionin_avail()) - to << from.rdbuf(); + if (from.peek() != ifstream::traits_type::eof()) + to << buffer; } - bool CopyFileX(string const & fOld, string const & fNew) { try @@ -319,7 +318,6 @@ bool CopyFileX(string const & fOld, string const & fNew) if (ifs.peek() == ifstream::traits_type::eof()) return true; - ofs << ifs.rdbuf(); ofs.flush(); diff --git a/generator/collector_addresses.cpp b/generator/collector_addresses.cpp index d1d2e374f0..44d4ace2dc 100644 --- a/generator/collector_addresses.cpp +++ b/generator/collector_addresses.cpp @@ -18,8 +18,8 @@ namespace generator CollectorAddresses::CollectorAddresses(std::string const & filename) : CollectorInterface(filename) { - m_writer.exceptions(std::fstream::failbit | std::fstream::badbit); - m_writer.open(GetTmpFilename()); + m_stream.exceptions(std::fstream::failbit | std::fstream::badbit); + m_stream.open(GetTmpFilename()); } std::shared_ptr @@ -33,17 +33,18 @@ void CollectorAddresses::CollectFeature(feature::FeatureBuilder const & feature, std::string addr; auto const & checker = ftypes::IsBuildingChecker::Instance(); if (checker(feature.GetTypes()) && feature.FormatFullAddress(addr)) - m_writer << addr << "\n"; + m_stream << addr << "\n"; } void CollectorAddresses::Finish() { - if (m_writer.is_open()) - m_writer.close(); + if (m_stream.is_open()) + m_stream.close(); } void CollectorAddresses::Save() { + CHECK(!m_stream.is_open(), ("Finish() has not been called.")); if (Platform::IsFileExistsByFullPath(GetTmpFilename())) CHECK(base::CopyFileX(GetTmpFilename(), GetFilename()), ()); } @@ -55,6 +56,7 @@ void CollectorAddresses::Merge(CollectorInterface const & collector) void CollectorAddresses::MergeInto(CollectorAddresses & collector) const { + CHECK(!m_stream.is_open() || !collector.m_stream.is_open(), ("Finish() has not been called.")); base::AppendFileToFile(GetTmpFilename(), collector.GetTmpFilename()); } } // namespace generator diff --git a/generator/collector_addresses.hpp b/generator/collector_addresses.hpp index 554438a1df..dd239a45a1 100644 --- a/generator/collector_addresses.hpp +++ b/generator/collector_addresses.hpp @@ -30,6 +30,6 @@ public: void MergeInto(CollectorAddresses & collector) const override; private: - std::ofstream m_writer; + std::ofstream m_stream; }; } // namespace generator diff --git a/generator/collector_camera.cpp b/generator/collector_camera.cpp index 5c9623d579..ab7f542681 100644 --- a/generator/collector_camera.cpp +++ b/generator/collector_camera.cpp @@ -81,7 +81,7 @@ void CameraProcessor::ForEachCamera(Fn && toDo) const void CameraProcessor::ProcessWay(OsmElement const & element) { - m_waysWriter->Write(&element.m_id, sizeof(element.m_id)); + WriteToSink(*m_waysWriter, element.m_id); rw::WriteVectorOfPOD(*m_waysWriter, element.m_nodes); } @@ -89,13 +89,10 @@ void CameraProcessor::FillCameraInWays() { FileReader reader(m_waysFilename); ReaderSource src(reader); - auto const fileSize = reader.Size(); - auto currPos = reader.GetOffset(); - while (currPos < fileSize) + while (src.Size() > 0) { - uint64_t wayId; + uint64_t wayId = ReadPrimitiveFromSource(src); std::vector nodes; - src.Read(&wayId, sizeof(wayId)); rw::ReadVectorOfPOD(src, nodes); for (auto const & node : nodes) { @@ -105,7 +102,6 @@ void CameraProcessor::FillCameraInWays() m_cameraToWays[itCamera->first].push_back(wayId); } - currPos = src.Pos(); } } diff --git a/generator/collector_city_area.cpp b/generator/collector_city_area.cpp index 39c64d11ce..63f402f7e3 100644 --- a/generator/collector_city_area.cpp +++ b/generator/collector_city_area.cpp @@ -21,7 +21,7 @@ namespace generator { CityAreaCollector::CityAreaCollector(std::string const & filename) : CollectorInterface(filename), - m_witer(std::make_unique>(GetTmpFilename())) {} + m_writer(std::make_unique>(GetTmpFilename())) {} std::shared_ptr CityAreaCollector::Clone(std::shared_ptr const &) const @@ -36,16 +36,17 @@ void CityAreaCollector::CollectFeature(FeatureBuilder const & feature, OsmElemen auto copy = feature; if (copy.PreSerialize()) - m_witer->Write(copy); + m_writer->Write(copy); } void CityAreaCollector::Finish() { - m_witer.reset({}); + m_writer.reset({}); } void CityAreaCollector::Save() { + CHECK(!m_writer, ("Finish() has not been called.")); if (Platform::IsFileExistsByFullPath(GetTmpFilename())) CHECK(base::CopyFileX(GetTmpFilename(), GetFilename()), ()); } @@ -57,6 +58,7 @@ void CityAreaCollector::Merge(generator::CollectorInterface const & collector) void CityAreaCollector::MergeInto(CityAreaCollector & collector) const { + CHECK(!m_writer || !collector.m_writer, ("Finish() has not been called.")); base::AppendFileToFile(GetTmpFilename(), collector.GetTmpFilename()); } } // namespace generator diff --git a/generator/collector_city_area.hpp b/generator/collector_city_area.hpp index aa66c740d3..7754a173ce 100644 --- a/generator/collector_city_area.hpp +++ b/generator/collector_city_area.hpp @@ -29,6 +29,6 @@ public: void MergeInto(CityAreaCollector & collector) const override; private: - std::unique_ptr> m_witer; + std::unique_ptr> m_writer; }; } // namespace generator diff --git a/generator/collector_tag.cpp b/generator/collector_tag.cpp index b32119c643..33b96a9d76 100644 --- a/generator/collector_tag.cpp +++ b/generator/collector_tag.cpp @@ -19,8 +19,8 @@ CollectorTag::CollectorTag(std::string const & filename, std::string const & tag , m_tagKey(tagKey) , m_validator(validator) { - m_writer.exceptions(std::fstream::failbit | std::fstream::badbit); - m_writer.open(GetTmpFilename()); + m_stream.exceptions(std::fstream::failbit | std::fstream::badbit); + m_stream.open(GetTmpFilename()); } std::shared_ptr @@ -33,17 +33,18 @@ void CollectorTag::Collect(OsmElement const & el) { auto const tag = el.GetTag(m_tagKey); if (!tag.empty() && m_validator(tag)) - m_writer << GetGeoObjectId(el).GetEncodedId() << "\t" << tag << "\n"; + m_stream << GetGeoObjectId(el).GetEncodedId() << "\t" << tag << "\n"; } void CollectorTag::Finish() { - if (m_writer.is_open()) - m_writer.close(); + if (m_stream.is_open()) + m_stream.close(); } void CollectorTag::Save() { + CHECK(!m_stream.is_open(), ("Finish() has not been called.")); if (Platform::IsFileExistsByFullPath(GetTmpFilename())) CHECK(base::CopyFileX(GetTmpFilename(), GetFilename()), ()); } @@ -55,6 +56,7 @@ void CollectorTag::Merge(CollectorInterface const & collector) void CollectorTag::MergeInto(CollectorTag & collector) const { + CHECK(!m_stream.is_open() || !collector.m_stream.is_open(), ("Finish() has not been called.")); base::AppendFileToFile(GetTmpFilename(), collector.GetTmpFilename()); } } // namespace generator diff --git a/generator/collector_tag.hpp b/generator/collector_tag.hpp index 88eef2f841..28bd61a49d 100644 --- a/generator/collector_tag.hpp +++ b/generator/collector_tag.hpp @@ -42,7 +42,7 @@ public: void MergeInto(CollectorTag & collector) const override; private: - std::ofstream m_writer; + std::ofstream m_stream; std::string m_tagKey; Validator m_validator; }; diff --git a/generator/maxspeeds_collector.cpp b/generator/maxspeeds_collector.cpp index 1bd76c6ade..0a3791f50d 100644 --- a/generator/maxspeeds_collector.cpp +++ b/generator/maxspeeds_collector.cpp @@ -40,8 +40,8 @@ namespace generator MaxspeedsCollector::MaxspeedsCollector(string const & filename) : CollectorInterface(filename) { - m_writer.exceptions(fstream::failbit | fstream::badbit); - m_writer.open(GetTmpFilename()); + m_stream.exceptions(fstream::failbit | fstream::badbit); + m_stream.open(GetTmpFilename()); } @@ -71,7 +71,7 @@ void MaxspeedsCollector::CollectFeature(FeatureBuilder const &, OsmElement const SpeedInUnits dummySpeed; if (!ParseMaxspeedAndWriteToStream(t.m_value, dummySpeed, ss)) return; - m_writer << ss.str() << '\n'; + m_stream << ss.str() << '\n'; return; } @@ -114,17 +114,18 @@ void MaxspeedsCollector::CollectFeature(FeatureBuilder const &, OsmElement const ss << "," << strings::to_string(maxspeedBackward.GetSpeed()); } - m_writer << ss.str() << '\n'; + m_stream << ss.str() << '\n'; } void MaxspeedsCollector::Finish() { - if (m_writer.is_open()) - m_writer.close(); + if (m_stream.is_open()) + m_stream.close(); } void MaxspeedsCollector::Save() { + CHECK(!m_stream.is_open(), ("Finish() has not been called.")); LOG(LINFO, ("Saving maxspeed tag values to", GetFilename())); if (Platform::IsFileExistsByFullPath(GetTmpFilename())) CHECK(CopyFileX(GetTmpFilename(), GetFilename()), ()); @@ -137,6 +138,7 @@ void MaxspeedsCollector::Merge(CollectorInterface const & collector) void MaxspeedsCollector::MergeInto(MaxspeedsCollector & collector) const { + CHECK(!m_stream.is_open() || !collector.m_stream.is_open(), ("Finish() has not been called.")); base::AppendFileToFile(GetTmpFilename(), collector.GetTmpFilename()); } } // namespace generator diff --git a/generator/maxspeeds_collector.hpp b/generator/maxspeeds_collector.hpp index 4a959dc704..c144a51cb0 100644 --- a/generator/maxspeeds_collector.hpp +++ b/generator/maxspeeds_collector.hpp @@ -58,6 +58,6 @@ private: // with ParseMaxspeedTag() function. That means all macro like RU:urban or GE:rural // are converted to an appropriate speed value and macro "none" and "walk" are converted // to |kNoneMaxSpeed| and |kWalkMaxSpeed|. - std::ofstream m_writer; + std::ofstream m_stream; }; } // namespace generator diff --git a/generator/restriction_writer.cpp b/generator/restriction_writer.cpp index 0f9e2a0173..b41ded1169 100644 --- a/generator/restriction_writer.cpp +++ b/generator/restriction_writer.cpp @@ -100,9 +100,9 @@ RestrictionWriter::RestrictionWriter(std::string const & filename, : generator::CollectorInterface(filename) , m_cache(cache) { - m_writer.exceptions(std::fstream::failbit | std::fstream::badbit); - m_writer.open(GetTmpFilename()); - m_writer << std::setprecision(20); + m_stream.exceptions(std::fstream::failbit | std::fstream::badbit); + m_stream.open(GetTmpFilename()); + m_stream << std::setprecision(20); } std::shared_ptr @@ -183,15 +183,15 @@ void RestrictionWriter::CollectRelation(RelationElement const & relationElement) : ViaType::Way; auto const printHeader = [&]() { - m_writer << DebugPrint(type) << "," << DebugPrint(viaType) << ","; + m_stream << DebugPrint(type) << "," << DebugPrint(viaType) << ","; }; if (viaType == ViaType::Way) { printHeader(); - m_writer << fromOsmId << ","; + m_stream << fromOsmId << ","; for (auto const & viaMember : via) - m_writer << viaMember.first << ","; + m_stream << viaMember.first << ","; } else { @@ -202,21 +202,22 @@ void RestrictionWriter::CollectRelation(RelationElement const & relationElement) return; printHeader(); - m_writer << x << "," << y << ","; - m_writer << fromOsmId << ","; + m_stream << x << "," << y << ","; + m_stream << fromOsmId << ","; } - m_writer << toOsmId << '\n'; + m_stream << toOsmId << '\n'; } void RestrictionWriter::Finish() { - if (m_writer.is_open()) - m_writer.close(); + if (m_stream.is_open()) + m_stream.close(); } void RestrictionWriter::Save() { + CHECK(!m_stream.is_open(), ("Finish() has not been called.")); if (Platform::IsFileExistsByFullPath(GetTmpFilename())) CHECK(base::CopyFileX(GetTmpFilename(), GetFilename()), ()); } @@ -228,6 +229,7 @@ void RestrictionWriter::Merge(generator::CollectorInterface const & collector) void RestrictionWriter::MergeInto(RestrictionWriter & collector) const { + CHECK(!m_stream.is_open() || !collector.m_stream.is_open(), ("Finish() has not been called.")); base::AppendFileToFile(GetTmpFilename(), collector.GetTmpFilename()); } diff --git a/generator/restriction_writer.hpp b/generator/restriction_writer.hpp index ae71784d7f..5279fba978 100644 --- a/generator/restriction_writer.hpp +++ b/generator/restriction_writer.hpp @@ -51,7 +51,7 @@ public: static ViaType ConvertFromString(std::string const & str); private: - std::ofstream m_writer; + std::ofstream m_stream; std::shared_ptr m_cache; }; diff --git a/generator/road_access_generator.cpp b/generator/road_access_generator.cpp index 93d49cc439..787186bec4 100644 --- a/generator/road_access_generator.cpp +++ b/generator/road_access_generator.cpp @@ -15,6 +15,7 @@ #include "coding/file_container.hpp" #include "coding/file_writer.hpp" #include "coding/internal/file_data.hpp" +#include "coding/reader.hpp" #include "base/assert.hpp" #include "base/geo_object_id.hpp" @@ -410,7 +411,7 @@ void RoadAccessWriter::CollectFeature(FeatureBuilder const & fb, OsmElement cons if (!routing::IsRoad(fb.GetTypes())) return; - m_waysWriter->Write(&elem.m_id, sizeof(elem.m_id)); + WriteToSink(*m_waysWriter, elem.m_id); rw::WriteVectorOfPOD(*m_waysWriter, elem.m_nodes); } @@ -421,6 +422,7 @@ void RoadAccessWriter::Finish() void RoadAccessWriter::Save() { + CHECK(!m_waysWriter, ("Finish() has not been called.")); ofstream out; out.exceptions(fstream::failbit | fstream::badbit); out.open(GetFilename()); @@ -430,18 +432,13 @@ void RoadAccessWriter::Save() FileReader reader(m_waysFilename); ReaderSource src(reader); - auto const fileSize = reader.Size(); - auto currPos = reader.GetOffset(); - while (currPos < fileSize) + while (src.Size() > 0) { - uint64_t wayId; + uint64_t wayId = ReadPrimitiveFromSource(src); std::vector nodes; - src.Read(&wayId, sizeof(wayId)); rw::ReadVectorOfPOD(src, nodes); for (auto & p : m_tagProcessors) p.WriteBarrierTags(out, wayId, nodes); - - currPos = src.Pos(); } } @@ -458,6 +455,7 @@ void RoadAccessWriter::MergeInto(RoadAccessWriter & collector) const for (size_t i = 0; i < otherProcessors.size(); ++i) otherProcessors[i].Merge(m_tagProcessors[i]); + CHECK(!m_waysWriter || !collector.m_waysWriter, ("Finish() has not been called.")); base::AppendFileToFile(m_waysFilename, collector.m_waysFilename); }