Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal change #172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions nisaba/port/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <errno.h>
#include <stdlib.h>

#include <filesystem>
#include <fstream>

Expand Down Expand Up @@ -79,8 +80,7 @@ std::string JoinPath(absl::string_view dirname, absl::string_view basename) {
}

std::string TempFilePath(std::string_view filename) {
const std::filesystem::path tmp_dir =
std::filesystem::temp_directory_path();
const std::filesystem::path tmp_dir = std::filesystem::temp_directory_path();
std::filesystem::path file_path = tmp_dir / filename;
return file_path.string();
}
Expand All @@ -90,11 +90,13 @@ absl::StatusOr<std::string> WriteTempTextFile(std::string_view filename,
const std::string &path = TempFilePath(filename);
std::ofstream out(path);
if (!out) {
return absl::PermissionDeniedError(absl::StrCat("Failed to open: ", path));
return absl::PermissionDeniedError(
absl::StrCat("Failed to open: ", std::string(path)));
}
out << contents;
if (!out.good()) {
return absl::InternalError(absl::StrCat("Failed to write to", path));
return absl::InternalError(
absl::StrCat("Failed to write to", std::string(path)));
}
return path;
}
Expand All @@ -104,13 +106,14 @@ absl::Status WriteTextFile(std::string_view file_path,
std::ofstream output;
output.open(std::string(file_path));
if (!output) {
return absl::PermissionDeniedError(absl::StrCat("Failed to open: ",
file_path));
return absl::PermissionDeniedError(
absl::StrCat("Failed to open: ", std::string(file_path)));
}
output << contents;
if (!output) {
return absl::PermissionDeniedError(absl::StrCat(
"Failed to write: ", contents.size(), " bytes to ", file_path));
return absl::PermissionDeniedError(
absl::StrCat("Failed to write: ", contents.size(), " bytes to ",
std::string(file_path)));
}
return absl::OkStatus();
}
Expand All @@ -124,7 +127,8 @@ absl::StatusOr<std::string> ReadFile(std::string_view file_path,
// https://cplusplus.github.io/LWG/issue3430
input.open(std::string(file_path), mode);
if (!input) {
return absl::NotFoundError(absl::StrCat("Failed to open: ", file_path));
return absl::NotFoundError(
absl::StrCat("Failed to open: ", std::string(file_path)));
}
std::string contents;
input.seekg(0, std::ios::end);
Expand All @@ -149,8 +153,8 @@ absl::StatusOr<std::string> ReadTextFile(std::string_view file_path) {
return ReadFile(file_path, std::ifstream::in);
}

absl::StatusOr<std::vector<std::string>> ReadLines(
absl::string_view input_file, int max_line_length) {
absl::StatusOr<std::vector<std::string>> ReadLines(absl::string_view input_file,
int max_line_length) {
std::string::size_type max_length = max_line_length;
if (max_line_length < 0) max_length = kDefaultMaxLineLength;
std::vector<std::string> input_lines;
Expand Down