Add Files::read_all_lines() and Files::write_all_lines()

This commit is contained in:
Alexander Karatarakis 2016-12-15 18:19:22 -08:00
parent 8f397bb8d1
commit e4548a8cf4
2 changed files with 33 additions and 0 deletions

View File

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

View File

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