mirror of
https://github.com/microsoft/vcpkg.git
synced 2024-11-29 12:19:07 +08:00
Add Files::read_all_lines() and Files::write_all_lines()
This commit is contained in:
parent
8f397bb8d1
commit
e4548a8cf4
@ -14,6 +14,10 @@ namespace vcpkg {namespace Files
|
||||
|
||||
expected<std::string> get_contents(const fs::path& file_path) noexcept;
|
||||
|
||||
expected<std::vector<std::string>> read_all_lines(const fs::path& file_path);
|
||||
|
||||
void write_all_lines(const fs::path& file_path, const std::vector<std::string>& lines);
|
||||
|
||||
fs::path find_file_recursively_up(const fs::path& starting_dir, const std::string& filename);
|
||||
|
||||
template <class Pred>
|
||||
|
@ -42,6 +42,35 @@ namespace vcpkg {namespace Files
|
||||
return std::move(output);
|
||||
}
|
||||
|
||||
expected<std::vector<std::string>> read_all_lines(const fs::path& file_path)
|
||||
{
|
||||
std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary);
|
||||
if (file_stream.fail())
|
||||
{
|
||||
return std::errc::no_such_file_or_directory;
|
||||
}
|
||||
|
||||
std::vector<std::string> output;
|
||||
std::string line;
|
||||
while (std::getline(file_stream, line))
|
||||
{
|
||||
output.push_back(line);
|
||||
}
|
||||
file_stream.close();
|
||||
|
||||
return std::move(output);
|
||||
}
|
||||
|
||||
void write_all_lines(const fs::path& file_path, const std::vector<std::string>& lines)
|
||||
{
|
||||
std::fstream output(file_path, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
|
||||
for (const std::string& line : lines)
|
||||
{
|
||||
output << line << "\n";
|
||||
}
|
||||
output.close();
|
||||
}
|
||||
|
||||
fs::path find_file_recursively_up(const fs::path& starting_dir, const std::string& filename)
|
||||
{
|
||||
fs::path current_dir = starting_dir;
|
||||
|
Loading…
Reference in New Issue
Block a user