36 #if GTEST_OS_WINDOWS_MOBILE
38 #elif GTEST_OS_WINDOWS
44 #endif // GTEST_OS_WINDOWS_MOBILE
49 # define GTEST_PATH_MAX_ _MAX_PATH
50 #elif defined(PATH_MAX)
51 # define GTEST_PATH_MAX_ PATH_MAX
52 #elif defined(_XOPEN_PATH_MAX)
53 # define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
55 # define GTEST_PATH_MAX_ _POSIX_PATH_MAX
56 #endif // GTEST_OS_WINDOWS
67 const char kAlternatePathSeparator =
'/';
68 const char kAlternatePathSeparatorString[] =
"/";
69 # if GTEST_OS_WINDOWS_MOBILE
75 const DWORD kInvalidFileAttributes = 0xffffffff;
78 # endif // GTEST_OS_WINDOWS_MOBILE
82 #endif // GTEST_OS_WINDOWS
86 #if GTEST_HAS_ALT_PATH_SEP_
94 FilePath FilePath::GetCurrentDir() {
95 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
96 GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32
100 #elif GTEST_OS_WINDOWS
102 return FilePath(_getcwd(cwd,
sizeof(cwd)) ==
nullptr ?
"" : cwd);
105 char* result = getcwd(cwd,
sizeof(cwd));
111 # endif // GTEST_OS_NACL
112 return FilePath(result ==
nullptr ?
"" : cwd);
113 #endif // GTEST_OS_WINDOWS_MOBILE
120 FilePath FilePath::RemoveExtension(
const char* extension)
const {
121 const std::string dot_extension = std::string(
".") + extension;
123 return FilePath(pathname_.substr(
124 0, pathname_.length() - dot_extension.length()));
132 const char* FilePath::FindLastPathSeparator()
const {
134 #if GTEST_HAS_ALT_PATH_SEP_
135 const char*
const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);
137 if (last_alt_sep !=
nullptr &&
138 (last_sep ==
nullptr || last_alt_sep > last_sep)) {
151 FilePath FilePath::RemoveDirectoryName()
const {
152 const char*
const last_sep = FindLastPathSeparator();
153 return last_sep ? FilePath(last_sep + 1) : *this;
162 FilePath FilePath::RemoveFileName()
const {
163 const char*
const last_sep = FindLastPathSeparator();
166 dir = std::string(c_str(), static_cast<size_t>(last_sep + 1 - c_str()));
170 return FilePath(dir);
179 FilePath FilePath::MakeFileName(
const FilePath& directory,
180 const FilePath& base_name,
182 const char* extension) {
185 file = base_name.string() +
"." + extension;
190 return ConcatPaths(directory, FilePath(file));
195 FilePath FilePath::ConcatPaths(
const FilePath& directory,
196 const FilePath& relative_path) {
197 if (directory.IsEmpty())
198 return relative_path;
199 const FilePath dir(directory.RemoveTrailingPathSeparator());
200 return FilePath(dir.string() +
kPathSeparator + relative_path.string());
205 bool FilePath::FileOrDirectoryExists()
const {
206 #if GTEST_OS_WINDOWS_MOBILE
207 LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
208 const DWORD attributes = GetFileAttributes(unicode);
210 return attributes != kInvalidFileAttributes;
213 return posix::Stat(pathname_.c_str(), &file_stat) == 0;
214 #endif // GTEST_OS_WINDOWS_MOBILE
219 bool FilePath::DirectoryExists()
const {
224 const FilePath& path(IsRootDirectory() ? *
this :
225 RemoveTrailingPathSeparator());
227 const FilePath& path(*
this);
230 #if GTEST_OS_WINDOWS_MOBILE
231 LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
232 const DWORD attributes = GetFileAttributes(unicode);
234 if ((attributes != kInvalidFileAttributes) &&
235 (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
240 result =
posix::Stat(path.c_str(), &file_stat) == 0 &&
242 #endif // GTEST_OS_WINDOWS_MOBILE
249 bool FilePath::IsRootDirectory()
const {
251 return pathname_.length() == 3 && IsAbsolutePath();
253 return pathname_.length() == 1 &&
IsPathSeparator(pathname_.c_str()[0]);
258 bool FilePath::IsAbsolutePath()
const {
259 const char*
const name = pathname_.c_str();
261 return pathname_.length() >= 3 &&
262 ((name[0] >=
'a' && name[0] <=
'z') ||
263 (name[0] >=
'A' && name[0] <=
'Z')) &&
279 FilePath FilePath::GenerateUniqueFileName(
const FilePath& directory,
280 const FilePath& base_name,
281 const char* extension) {
282 FilePath full_pathname;
285 full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
286 }
while (full_pathname.FileOrDirectoryExists());
287 return full_pathname;
293 bool FilePath::IsDirectory()
const {
294 return !pathname_.empty() &&
301 bool FilePath::CreateDirectoriesRecursively()
const {
302 if (!this->IsDirectory()) {
306 if (pathname_.length() == 0 || this->DirectoryExists()) {
310 const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
311 return parent.CreateDirectoriesRecursively() && this->CreateFolder();
318 bool FilePath::CreateFolder()
const {
319 #if GTEST_OS_WINDOWS_MOBILE
320 FilePath removed_sep(this->RemoveTrailingPathSeparator());
321 LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
322 int result = CreateDirectory(unicode,
nullptr) ? 0 : -1;
324 #elif GTEST_OS_WINDOWS
325 int result = _mkdir(pathname_.c_str());
326 #elif GTEST_OS_ESP8266
330 int result = mkdir(pathname_.c_str(), 0777);
331 #endif // GTEST_OS_WINDOWS_MOBILE
334 return this->DirectoryExists();
342 FilePath FilePath::RemoveTrailingPathSeparator()
const {
344 ? FilePath(pathname_.substr(0, pathname_.length() - 1))
351 void FilePath::Normalize() {
352 std::string normalized_pathname;
353 normalized_pathname.reserve(pathname_.length());
355 for (
const char character : pathname_) {
357 normalized_pathname.push_back(character);
358 }
else if (normalized_pathname.empty() ||
366 pathname_ = normalized_pathname;
static bool EndsWithCaseInsensitive(const std::string &str, const std::string &suffix)
int Stat(const char *path, StatStruct *buf)
const char kCurrentDirectoryString[]
std::string StreamableToString(const T &streamable)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
bool IsDir(const StatStruct &st)
static bool IsPathSeparator(char c)
const char kPathSeparator