Google OR-Tools
v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
temp_path.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/base/temp_path.h
"
15
16
#include <utility>
17
18
#include "absl/log/check.h"
19
#include "absl/strings/str_cat.h"
20
#include "absl/time/time.h"
21
#include "
ortools/base/filesystem.h
"
22
23
namespace
file
{
24
25
std::string
TempFile
(absl::string_view prefix) {
26
std::string path;
27
if
(prefix.empty()) {
28
path = absl::StrCat(absl::ToUnixMicros(absl::Now()));
29
}
else
{
30
path = absl::StrCat(prefix,
"_"
, absl::ToUnixMicros(absl::Now()));
31
}
32
return
path;
33
}
34
35
}
// namespace file
36
37
TempPath::TempPath
(absl::string_view prefix) : path_(
file
::TempFile(prefix)) {
38
CHECK_OK(Init(
kDefaultMode
));
39
}
40
41
TempPath::TempPath
(absl::string_view prefix, absl::Status*
status
)
42
: path_(
file
::TempFile(prefix)) {
43
*
status
= Init(
kDefaultMode
);
44
}
45
46
TempPath::TempPath
(
TempPath
&& rhs) : path_(
std
::move(rhs.path_)) {}
47
48
TempPath
&
TempPath::operator=
(
TempPath
&& rhs) {
49
TempPath
tmp(std::move(*
this
));
50
path_ = std::move(rhs.path_);
51
return
*
this
;
52
}
53
54
TempPath::~TempPath
() {}
55
56
TempPath
*
TempPath::Create
(
Location
location) {
57
std::string dirname;
58
switch
(location) {
59
case
Local
:
60
dirname =
file::TempFile
(
""
);
61
}
62
if
(dirname.empty()) {
63
return
nullptr
;
64
}
65
absl::Status
status
;
66
TempPath
* temp_path =
new
TempPath
(dirname, &
status
);
67
if
(!
status
.ok()) {
68
delete
temp_path;
69
return
nullptr
;
70
}
71
return
temp_path;
72
}
73
74
TempPath::TempPath
(
const
std::string& dirname,
file::Options
options,
75
absl::Status*
status
)
76
: path_(dirname) {
77
*
status
= Init(options);
78
}
79
80
absl::Status TempPath::Init(
file::Options
options) {
81
return
file::RecursivelyCreateDir
(
path
(), options);
82
}
TempPath
Definition
temp_path.h:23
TempPath::Create
static TempPath * Create(Location location)
Definition
temp_path.cc:56
TempPath::kDefaultMode
static constexpr int kDefaultMode
default mode to create directories (a+rwx):
Definition
temp_path.h:26
TempPath::Location
Location
Definition
temp_path.h:42
TempPath::Local
@ Local
Definition
temp_path.h:43
TempPath::~TempPath
~TempPath()
Definition
temp_path.cc:54
TempPath::TempPath
TempPath(absl::string_view prefix)
Definition
temp_path.cc:37
TempPath::path
std::string path() const
Returns the path which was created by this object.
Definition
temp_path.h:40
TempPath::operator=
TempPath & operator=(TempPath &&rhs)
Definition
temp_path.cc:48
filesystem.h
status
absl::Status status
Definition
g_gurobi.cc:44
file
Definition
file.cc:169
file::Options
int Options
Definition
file.h:107
file::RecursivelyCreateDir
absl::Status RecursivelyCreateDir(std::string_view path, const file::Options &options)
Definition
filesystem.cc:74
file::TempFile
std::string TempFile(absl::string_view prefix)
Definition
temp_path.cc:25
std
STL namespace.
temp_path.h
ortools
base
temp_path.cc
Generated by
1.12.0