diff options
| -rw-r--r-- | toolsrc/include/vcpkg_Files.h | 4 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg_Files.cpp | 29 |
2 files changed, 33 insertions, 0 deletions
diff --git a/toolsrc/include/vcpkg_Files.h b/toolsrc/include/vcpkg_Files.h index 8b320303d..5e788f7be 100644 --- a/toolsrc/include/vcpkg_Files.h +++ b/toolsrc/include/vcpkg_Files.h @@ -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> diff --git a/toolsrc/src/vcpkg_Files.cpp b/toolsrc/src/vcpkg_Files.cpp index 698579736..889ceb055 100644 --- a/toolsrc/src/vcpkg_Files.cpp +++ b/toolsrc/src/vcpkg_Files.cpp @@ -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; |
