Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/DefaultFileSystem.cpp

Issue 29481704: Noissue - Use buffer for FileSystem IO (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created July 6, 2017, 9:19 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/DefaultFileSystem.cpp
===================================================================
--- a/src/DefaultFileSystem.cpp
+++ b/src/DefaultFileSystem.cpp
@@ -61,30 +61,39 @@
// file paths as they are.
std::string NormalizePath(const std::string& path)
{
return path;
}
#endif
}
-std::shared_ptr<std::istream>
+FileSystem::IOBuffer
DefaultFileSystem::Read(const std::string& path) const
{
- std::shared_ptr<std::istream> result(new std::ifstream(NormalizePath(path).c_str()));
- if (result->fail())
+ std::ifstream file(NormalizePath(path).c_str(), std::ios_base::binary);
+ if (file.fail())
throw RuntimeErrorWithErrno("Failed to open " + path);
- return result;
+
+ file.seekg(0, std::ios_base::end);
+ auto dataSize = file.tellg();
+ file.seekg(0, std::ios_base::beg);
+
+ IOBuffer data(dataSize);
+ file.read(reinterpret_cast<std::ifstream::char_type*>(data.data()),
sergei 2017/07/07 07:27:36 I would prefer to rather use static_cast but it se
hub 2017/07/07 12:51:00 static_cast<> is rejected by the compiler: ../src
sergei 2017/07/07 13:19:37 Yeah, one has to firstly cast it to void*, basical
+ data.size());
+ return data;
}
void DefaultFileSystem::Write(const std::string& path,
- std::istream& data)
+ const IOBuffer& data)
{
std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_base::binary);
- file << Utils::Slurp(data);
+ file.write(reinterpret_cast<const std::ofstream::char_type*>(data.data()),
+ data.size());
}
void DefaultFileSystem::Move(const std::string& fromPath,
const std::string& toPath)
{
if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str()))
throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
}

Powered by Google App Engine
This is Rietveld