OR-Tools  8.2
file_nonport.cc
Go to the documentation of this file.
1// Copyright 2010-2018 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 "absl/status/status.h"
15#include "ortools/port/file.h"
16
17#if !defined(_MSC_VER)
18#include <unistd.h>
19#endif
20
21#include "absl/strings/str_format.h"
22#include "absl/time/clock.h"
23#include "ortools/base/file.h"
24
25namespace operations_research {
26
27::absl::Status PortableFileSetContents(absl::string_view file_name,
28 absl::string_view content) {
29 return file::SetContents(file_name, content, file::Defaults());
30}
31
32::absl::Status PortableFileGetContents(absl::string_view file_name,
33 std::string* output) {
34 return file::GetContents(file_name, output, file::Defaults());
35}
36
37bool PortableTemporaryFile(const char* directory_prefix,
38 std::string* filename_out) {
39#if defined(__linux)
40 int32 tid = static_cast<int32>(pthread_self());
41#else // defined(__linux__)
42 int32 tid = 123;
43#endif // defined(__linux__)
44#if !defined(_MSC_VER)
45 int32 pid = static_cast<int32>(getpid());
46#else // _MSC_VER
47 int32 pid = 456;
48#endif // _MSC_VER
49 int64 now = absl::GetCurrentTimeNanos();
50 std::string filename =
51 absl::StrFormat("/tmp/parameters-tempfile-%x-%d-%llx", tid, pid, now);
52 return true;
53}
54
55::absl::Status PortableDeleteFile(absl::string_view file_name) {
56 return file::Delete(file_name, file::Defaults());
57}
58
59} // namespace operations_research
int int32
int64_t int64
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: file.cc:163
absl::Status Delete(const absl::string_view &path, int flags)
Definition: file.cc:305
int Defaults()
Definition: base/file.h:119
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: file.cc:196
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
::absl::Status PortableDeleteFile(absl::string_view file_name)
Definition: file_nonport.cc:55
bool PortableTemporaryFile(const char *directory_prefix, std::string *filename_out)
Definition: file_nonport.cc:37
::absl::Status PortableFileSetContents(absl::string_view file_name, absl::string_view content)
Definition: file_nonport.cc:27
::absl::Status PortableFileGetContents(absl::string_view file_name, std::string *output)
Definition: file_nonport.cc:32