Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
file.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#include "ortools/port/file.h"
15
16#include <string>
17
18#include "absl/status/status.h"
20
21#if !defined(__PORTABLE_PLATFORM__)
22#if !defined(_MSC_VER)
23#include <unistd.h>
24#endif
25
26#include "absl/strings/str_format.h"
27#include "absl/time/clock.h"
28#include "ortools/base/file.h"
31#endif // !defined(__PORTABLE_PLATFORM__)
32
33namespace operations_research {
34
35::absl::Status PortableFileSetContents(absl::string_view file_name,
36 absl::string_view content) {
37#if defined(__PORTABLE_PLATFORM__)
38 return absl::Status(absl::StatusCode::kUnimplemented,
39 "File io is not implemented for this platform.");
40#else // defined(__PORTABLE_PLATFORM__)
41 return file::SetContents(file_name, content, file::Defaults());
42#endif // !defined(__PORTABLE_PLATFORM__)
43}
44
45::absl::Status PortableFileGetContents(absl::string_view file_name,
46 std::string* output) {
47#if defined(__PORTABLE_PLATFORM__)
48 return absl::Status(absl::StatusCode::kUnimplemented,
49 "File io is not implemented for this platform.");
50#else // defined(__PORTABLE_PLATFORM__)
51 return file::GetContents(file_name, output, file::Defaults());
52#endif // !defined(__PORTABLE_PLATFORM__)
53}
54
55bool PortableTemporaryFile(const char* directory_prefix,
56 std::string* filename_out) {
57#if defined(__PORTABLE_PLATFORM__)
58 LOG(ERROR) << "Temporary files are not implemented for this platform.";
59 return false;
60#else // defined(__PORTABLE_PLATFORM__)
61#if defined(__linux)
62 int32_t tid = static_cast<int32_t>(pthread_self());
63#else // defined(__linux__)
64 int32_t tid = 123;
65#endif // defined(__linux__)
66#if !defined(_MSC_VER)
67 int32_t pid = static_cast<int32_t>(getpid());
68#else // _MSC_VER
69 int32_t pid = 456;
70#endif // _MSC_VER
71 int64_t now = absl::GetCurrentTimeNanos();
72 std::string filename =
73 absl::StrFormat("/tmp/parameters-tempfile-%x-%d-%llx", tid, pid, now);
74 return true;
75#endif // !defined(__PORTABLE_PLATFORM__)
76}
77
78::absl::Status PortableDeleteFile(absl::string_view file_name) {
79#if defined(__PORTABLE_PLATFORM__)
80 return absl::Status(absl::StatusCode::kUnimplemented,
81 "File io is not implemented for this platform.");
82#else // defined(__PORTABLE_PLATFORM__)
83 return file::Delete(file_name, file::Defaults());
84#endif // !defined(__PORTABLE_PLATFORM__)
85}
86} // namespace operations_research
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
Definition file.cc:191
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
Definition file.cc:242
Options Defaults()
Definition file.h:109
absl::Status Delete(absl::string_view path, Options options)
Definition file.cc:371
In SWIG mode, we don't want anything besides these top-level includes.
bool PortableTemporaryFile(const char *directory_prefix, std::string *filename_out)
Returns true if successful. Outputs temp file to filename.
Definition file.cc:55
::absl::Status PortableDeleteFile(absl::string_view file_name)
Definition file.cc:78
::absl::Status PortableFileGetContents(absl::string_view file_name, std::string *output)
Definition file.cc:45
::absl::Status PortableFileSetContents(absl::string_view file_name, absl::string_view content)
Definition file.cc:35