1 /*
2 * Copyright 2017 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fcp/base/platform.h"
18
19 #include <stdlib.h>
20 #include <sys/stat.h>
21
22 #include <cstdio>
23 #include <fstream>
24 #include <sstream>
25 #include <string>
26
27 #include "absl/status/status.h"
28
29 #ifdef _WIN32
30 #include <direct.h>
31 #endif
32
33 #include "absl/strings/str_cat.h"
34 #include "absl/strings/string_view.h"
35 #include "absl/strings/strip.h"
36 #include "fcp/base/monitoring.h"
37
38 namespace fcp {
39
40 namespace {
41 #ifdef _WIN32
42 constexpr char kPathSeparator[] = "\\";
43 #else
44 constexpr char kPathSeparator[] = "/";
45 #endif
46 } // namespace
47
ConcatPath(absl::string_view path1,absl::string_view path2)48 std::string ConcatPath(absl::string_view path1, absl::string_view path2) {
49 if (path1.empty()) {
50 return std::string(path2);
51 }
52 return absl::StrCat(path1, kPathSeparator, path2);
53 }
54
StripTrailingPathSeparator(absl::string_view path)55 absl::string_view StripTrailingPathSeparator(absl::string_view path) {
56 return absl::StripSuffix(path, kPathSeparator);
57 }
58
59 namespace internal {
60
61 template <typename T>
ReadFile(absl::string_view file_name)62 absl::StatusOr<T> ReadFile(absl::string_view file_name) {
63 auto file_name_str = std::string(file_name);
64 std::ifstream is(file_name_str);
65 if (!is) {
66 return absl::InternalError(
67 absl::StrCat("cannot read file ", file_name_str));
68 }
69 std::ostringstream buffer;
70 buffer << is.rdbuf();
71 if (!is) {
72 return absl::InternalError(
73 absl::StrCat("error reading file ", file_name_str));
74 }
75 return static_cast<T>(buffer.str());
76 }
77
78 } // namespace internal
79
ReadFileToString(absl::string_view file_name)80 absl::StatusOr<std::string> ReadFileToString(absl::string_view file_name) {
81 return internal::ReadFile<std::string>(file_name);
82 }
83
ReadFileToCord(absl::string_view file_name)84 absl::StatusOr<absl::Cord> ReadFileToCord(absl::string_view file_name) {
85 return internal::ReadFile<absl::Cord>(file_name);
86 }
87
WriteStringToFile(absl::string_view file_name,absl::string_view content)88 absl::Status WriteStringToFile(absl::string_view file_name,
89 absl::string_view content) {
90 auto file_name_str = std::string(file_name);
91 std::ofstream os(file_name_str);
92 if (!os) {
93 return absl::InternalError(
94 absl::StrCat("cannot create file ", file_name_str));
95 }
96 os << content;
97 if (!os) {
98 return absl::InternalError(
99 absl::StrCat("error writing to file ", file_name_str));
100 }
101 return absl::OkStatus();
102 }
103
WriteCordToFile(absl::string_view file_name,const absl::Cord & content)104 absl::Status WriteCordToFile(absl::string_view file_name,
105 const absl::Cord& content) {
106 auto file_name_str = std::string(file_name);
107 std::ofstream os(file_name_str);
108 if (!os) {
109 return absl::InternalError(
110 absl::StrCat("cannot create file ", file_name_str));
111 }
112 for (absl::string_view chunk : content.Chunks()) {
113 os << chunk;
114 if (!os) {
115 return absl::InternalError(
116 absl::StrCat("error writing to file ", file_name_str));
117 }
118 }
119 return absl::OkStatus();
120 }
121
FileExists(absl::string_view file_name)122 bool FileExists(absl::string_view file_name) {
123 struct stat info;
124 return stat(std::string(file_name).c_str(), &info) == 0;
125 }
126
GetDataPath(absl::string_view relative_path)127 std::string GetDataPath(absl::string_view relative_path) {
128 return std::string(relative_path);
129 }
130
131 } // namespace fcp
132