Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
filesystem.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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
15
16#include <algorithm>
17#include <exception> // IWYU pragma: keep
18#include <filesystem> // NOLINT(build/c++17)
19#include <regex> // NOLINT
20#include <string>
21#include <string_view>
22#include <system_error> // NOLINT
23#include <vector>
24
25#include "absl/status/status.h"
26#include "absl/strings/str_replace.h"
27#include "ortools/base/file.h"
28
29namespace fs = std::filesystem;
30
31// Converts a absl::string_view into an object compatible with std::filesystem.
32#ifdef ABSL_USES_STD_STRING_VIEW
33#define SV_ABSL_TO_STD(X) X
34#else
35#define SV_ABSL_TO_STD(X) std::string(X)
36#endif
37
38namespace file {
39
40absl::Status Match(std::string_view pattern, std::vector<std::string>* result,
41 const file::Options& options) {
42 try {
43 const auto search_dir = fs::path(SV_ABSL_TO_STD(pattern)).parent_path();
44 const auto filename = fs::path(SV_ABSL_TO_STD(pattern)).filename().string();
45 std::string regexp_filename =
46 absl::StrReplaceAll(filename, {{".", "\\."}, {"*", ".*"}, {"?", "."}});
47 std::regex regexp_pattern(regexp_filename);
48 std::error_code error;
49
50 const fs::directory_iterator path_end;
51 for (auto path = fs::directory_iterator(search_dir, error);
52 !error && path != path_end; path.increment(error)) {
53 if (!fs::is_regular_file(path->path())) {
54 continue;
55 }
56 if (std::regex_match(path->path().filename().string(), regexp_pattern)) {
57 result->push_back(path->path().string());
58 }
59 }
60 if (error) {
61 return absl::InvalidArgumentError(error.message());
62 }
63
64 std::sort(result->begin(), result->end());
65 return absl::OkStatus();
66 } catch (const std::exception& e) {
67 return absl::InvalidArgumentError(e.what());
68 }
69}
70
71absl::Status IsDirectory(std::string_view path, const file::Options& options) {
72 (void)options;
73 if (std::filesystem::is_directory(std::filesystem::path(path))) {
74 return absl::OkStatus();
75 } else {
76 return absl::Status(absl::StatusCode::kFailedPrecondition,
77 absl::StrCat(path, " exists, but is not a directory"));
78 }
79}
80
81absl::Status RecursivelyCreateDir(std::string_view path,
82 const file::Options& options) {
83 (void)options;
84 try {
85 std::filesystem::create_directories(std::filesystem::path(path));
86 return absl::OkStatus();
87 } catch (const std::exception& e) {
88 return absl::InvalidArgumentError(e.what());
89 }
90}
91
92} // namespace file
#define SV_ABSL_TO_STD(X)
Definition filesystem.cc:35
Definition file.cc:327
int Options
Definition file.h:84
absl::Status Match(std::string_view pattern, std::vector< std::string > *result, const file::Options &options)
Definition filesystem.cc:40
absl::Status IsDirectory(std::string_view path, const file::Options &options)
Definition filesystem.cc:71
absl::Status RecursivelyCreateDir(std::string_view path, const file::Options &options)
Definition filesystem.cc:81