cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
prepare_directory.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <cstddef>
23
24#ifdef _WIN32
25
26#include <Windows.h> // GetFileAttributesA
27#include <direct.h> // _mkdir
28#include <fileapi.h> // GetFileAttributesA
29
30namespace stat_bench {
31namespace util {
32
33[[nodiscard]] static auto path_exists(const std::string& path) -> bool {
34 return ::GetFileAttributesA(path.c_str()) != INVALID_FILE_ATTRIBUTES;
35}
36
37void prepare_directory(const std::string& path) {
38 if (path_exists(path)) {
39 return;
40 }
41
42 std::size_t pos = 0;
43 while (true) {
44 pos = path.find_first_of("/\\", pos);
45 if (pos == std::string::npos) {
46 pos = path.size();
47 }
48 const auto parent = path.substr(0, pos);
49 ++pos;
50
51 if (parent.empty() || path_exists(parent)) {
52 continue;
53 }
54 ::_mkdir(parent.c_str());
55 if (pos >= path.size()) {
56 return;
57 }
58 }
59}
60
61void prepare_directory_for(const std::string& path) {
62 const std::size_t pos = path.find_last_of("/\\");
63 if (pos == std::string::npos) {
64 return;
65 }
66 prepare_directory(path.substr(0, pos));
67}
68
69} // namespace util
70} // namespace stat_bench
71
72#else
73
74#include <sys/stat.h> // mkdir, stat
75#include <sys/types.h>
76
77namespace stat_bench {
78namespace util {
79
80namespace {
81
82[[nodiscard]] auto path_exists(const std::string& path) -> bool {
83 struct stat buf{};
84 return ::stat(path.c_str(), &buf) == 0;
85}
86
87} // namespace
88
89void prepare_directory(const std::string& path) {
90 if (path_exists(path)) {
91 return;
92 }
93
94 std::size_t pos = 0;
95 while (true) {
96 pos = path.find_first_of('/', pos);
97 if (pos == std::string::npos) {
98 pos = path.size();
99 }
100 const auto parent = path.substr(0, pos);
101 ++pos;
102
103 if (parent.empty() || path_exists(parent)) {
104 continue;
105 }
106 // NOLINTNEXTLINE(google-readability-casting): This is constructor.
107 const auto mode = ::mode_t(0755);
108 ::mkdir(parent.c_str(), mode);
109 if (pos >= path.size()) {
110 return;
111 }
112 }
113}
114
115void prepare_directory_for(const std::string& path) {
116 const std::size_t pos = path.find_last_of('/');
117 if (pos == std::string::npos) {
118 return;
119 }
120 prepare_directory(path.substr(0, pos));
121}
122
123} // namespace util
124} // namespace stat_bench
125
126#endif
Namespace of utility functions and classes.
void prepare_directory(const std::string &path)
Prepare a directory.
void prepare_directory_for(const std::string &path)
Prepare a directory for a file.
Namespace of stat_bench source codes.
Declaration of prepare_directory function.