Review fixes

This commit is contained in:
Maksim Andrianov 2019-08-07 13:04:26 +03:00 committed by LaGrunge
parent d800f51dc9
commit 62a5a2c496
13 changed files with 65 additions and 63 deletions

View file

@ -19,7 +19,7 @@
#include <vector>
#ifdef OMIM_OS_WINDOWS
#include <io.h>
#include <io.h>
#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, function<bool(string const
return true;
}
void AppendFileToFile(std::string const & fromFilename, std::string const & toFilename)
void AppendFileToFile(string const & fromFilename, string const & toFilename)
{
std::ifstream from;
from.exceptions(std::fstream::failbit | std::fstream::badbit);
from.open(fromFilename, std::ios::binary);
ifstream from;
from.exceptions(fstream::failbit | fstream::badbit);
from.open(fromFilename, ios::binary);
std::ofstream to;
to.exceptions(std::fstream::badbit);
to.open(toFilename, std::ios::binary | std::ios::app);
ofstream to;
to.exceptions(fstream::badbit);
to.open(toFilename, ios::binary | ios::app);
auto * buffer = from.rdbuf();
if (buffer->in_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();

View file

@ -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<CollectorInterface>
@ -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

View file

@ -30,6 +30,6 @@ public:
void MergeInto(CollectorAddresses & collector) const override;
private:
std::ofstream m_writer;
std::ofstream m_stream;
};
} // namespace generator

View file

@ -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<FileReader> 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<uint64_t>(src);
std::vector<uint64_t> 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();
}
}

View file

@ -21,7 +21,7 @@ namespace generator
{
CityAreaCollector::CityAreaCollector(std::string const & filename)
: CollectorInterface(filename),
m_witer(std::make_unique<FeatureBuilderWriter<MaxAccuracy>>(GetTmpFilename())) {}
m_writer(std::make_unique<FeatureBuilderWriter<MaxAccuracy>>(GetTmpFilename())) {}
std::shared_ptr<CollectorInterface>
CityAreaCollector::Clone(std::shared_ptr<cache::IntermediateDataReader> 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

View file

@ -29,6 +29,6 @@ public:
void MergeInto(CityAreaCollector & collector) const override;
private:
std::unique_ptr<feature::FeatureBuilderWriter<feature::serialization_policy::MaxAccuracy>> m_witer;
std::unique_ptr<feature::FeatureBuilderWriter<feature::serialization_policy::MaxAccuracy>> m_writer;
};
} // namespace generator

View file

@ -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<CollectorInterface>
@ -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

View file

@ -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;
};

View file

@ -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

View file

@ -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

View file

@ -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<generator::CollectorInterface>
@ -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());
}

View file

@ -51,7 +51,7 @@ public:
static ViaType ConvertFromString(std::string const & str);
private:
std::ofstream m_writer;
std::ofstream m_stream;
std::shared_ptr<generator::cache::IntermediateDataReader> m_cache;
};

View file

@ -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<FileReader> 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<uint64_t>(src);
std::vector<uint64_t> 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);
}