file_helpers.cpp 719 B

12345678910111213141516171819202122232425
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "testing/base/file_helpers.h"
  5. #include <fstream>
  6. #include <sstream>
  7. namespace Carbon::Testing {
  8. auto ReadFile(std::filesystem::path path) -> ErrorOr<std::string> {
  9. std::ifstream file_stream(path);
  10. if (file_stream.fail()) {
  11. return Error(llvm::formatv("Error opening file: {0}", path));
  12. }
  13. std::stringstream buffer;
  14. buffer << file_stream.rdbuf();
  15. if (file_stream.fail()) {
  16. return Error(llvm::formatv("Error reading file: {0}", path));
  17. }
  18. return buffer.str();
  19. }
  20. } // namespace Carbon::Testing