Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit e6f9d04

Browse files
committed
Support both slashes on Windows when making output dirs
1 parent 84986af commit e6f9d04

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/disk_interface.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "disk_interface.h"
1616

17+
#include <algorithm>
18+
1719
#include <errno.h>
1820
#include <stdio.h>
1921
#include <string.h>
@@ -31,15 +33,16 @@ namespace {
3133

3234
string DirName(const string& path) {
3335
#ifdef _WIN32
34-
const char kPathSeparator = '\\';
36+
const char kPathSeparators[] = "\\/";
3537
#else
36-
const char kPathSeparator = '/';
38+
const char kPathSeparators[] = "/";
3739
#endif
38-
39-
string::size_type slash_pos = path.rfind(kPathSeparator);
40+
string::size_type slash_pos = path.find_last_of(kPathSeparators);
4041
if (slash_pos == string::npos)
4142
return string(); // Nothing to do.
42-
while (slash_pos > 0 && path[slash_pos - 1] == kPathSeparator)
43+
const char* const kEnd = kPathSeparators + strlen(kPathSeparators);
44+
while (slash_pos > 0 &&
45+
std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd)
4346
--slash_pos;
4447
return path.substr(0, slash_pos);
4548
}

src/disk_interface_test.cc

+12-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ TEST_F(DiskInterfaceTest, ReadFile) {
9393
}
9494

9595
TEST_F(DiskInterfaceTest, MakeDirs) {
96-
EXPECT_TRUE(disk_.MakeDirs("path/with/double//slash/"));
96+
string path = "path/with/double//slash/";
97+
EXPECT_TRUE(disk_.MakeDirs(path.c_str()));
98+
FILE* f = fopen((path + "a_file").c_str(), "w");
99+
EXPECT_TRUE(f);
100+
EXPECT_EQ(0, fclose(f));
101+
#ifdef _WIN32
102+
string path2 = "another\\with\\back\\\\slashes\\";
103+
EXPECT_TRUE(disk_.MakeDirs(path2.c_str()));
104+
FILE* f2 = fopen((path2 + "a_file").c_str(), "w");
105+
EXPECT_TRUE(f2);
106+
EXPECT_EQ(0, fclose(f2));
107+
#endif
97108
}
98109

99110
TEST_F(DiskInterfaceTest, RemoveFile) {

0 commit comments

Comments
 (0)