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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 #else 59 #else
60 // POSIX systems: assume that file system encoding is UTF-8 and just use the 60 // POSIX systems: assume that file system encoding is UTF-8 and just use the
61 // file paths as they are. 61 // file paths as they are.
62 std::string NormalizePath(const std::string& path) 62 std::string NormalizePath(const std::string& path)
63 { 63 {
64 return path; 64 return path;
65 } 65 }
66 #endif 66 #endif
67 } 67 }
68 68
69 std::shared_ptr<std::istream> 69 FileSystem::IOBuffer
70 DefaultFileSystem::Read(const std::string& path) const 70 DefaultFileSystem::Read(const std::string& path) const
71 { 71 {
72 std::shared_ptr<std::istream> result(new std::ifstream(NormalizePath(path).c_s tr())); 72 std::ifstream file(NormalizePath(path).c_str(), std::ios_base::binary);
73 if (result->fail()) 73 if (file.fail())
74 throw RuntimeErrorWithErrno("Failed to open " + path); 74 throw RuntimeErrorWithErrno("Failed to open " + path);
75 return result; 75
76 file.seekg(0, std::ios_base::end);
77 auto dataSize = file.tellg();
78 file.seekg(0, std::ios_base::beg);
79
80 IOBuffer data(dataSize);
81 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
82 data.size());
83 return data;
76 } 84 }
77 85
78 void DefaultFileSystem::Write(const std::string& path, 86 void DefaultFileSystem::Write(const std::string& path,
79 std::istream& data) 87 const IOBuffer& data)
80 { 88 {
81 std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_ base::binary); 89 std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_ base::binary);
82 file << Utils::Slurp(data); 90 file.write(reinterpret_cast<const std::ofstream::char_type*>(data.data()),
91 data.size());
83 } 92 }
84 93
85 void DefaultFileSystem::Move(const std::string& fromPath, 94 void DefaultFileSystem::Move(const std::string& fromPath,
86 const std::string& toPath) 95 const std::string& toPath)
87 { 96 {
88 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str())) 97 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str()))
89 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); 98 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
90 } 99 }
91 100
92 void DefaultFileSystem::Remove(const std::string& path) 101 void DefaultFileSystem::Remove(const std::string& path)
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 void DefaultFileSystem::SetBasePath(const std::string& path) 196 void DefaultFileSystem::SetBasePath(const std::string& path)
188 { 197 {
189 basePath = path; 198 basePath = path;
190 199
191 if (*basePath.rbegin() == PATH_SEPARATOR) 200 if (*basePath.rbegin() == PATH_SEPARATOR)
192 { 201 {
193 basePath.resize(basePath.size() - 1); 202 basePath.resize(basePath.size() - 1);
194 } 203 }
195 } 204 }
196 205
OLDNEW

Powered by Google App Engine
This is Rietveld