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