aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util/file_posix.cc
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2016-12-19 14:30:52 +0000
committerGravatar Klaus Aehlig <aehlig@google.com>2016-12-19 16:15:50 +0000
commita4d0ea406e8622e305fc3253075cfee60da3d3d2 (patch)
tree3ae018781f294f50a391cd2f3423d919b4cedeb1 /src/main/cpp/util/file_posix.cc
parent17380ac67da7860188f778209f144b4c401127a8 (diff)
Bazel client: SplitPath works with Windows paths
This allows correct behavior of Dirname and Basename on Windows. See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 142441234 MOS_MIGRATED_REVID=142441234
Diffstat (limited to 'src/main/cpp/util/file_posix.cc')
-rw-r--r--src/main/cpp/util/file_posix.cc12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc
index e197a09453..9e835d759e 100644
--- a/src/main/cpp/util/file_posix.cc
+++ b/src/main/cpp/util/file_posix.cc
@@ -169,6 +169,18 @@ IPipe* CreatePipe() {
}
#endif // __CYGWIN__
+pair<string, string> SplitPath(const string &path) {
+ size_t pos = path.rfind('/');
+
+ // Handle the case with no '/' in 'path'.
+ if (pos == string::npos) return std::make_pair("", path);
+
+ // Handle the case with a single leading '/' in 'path'.
+ if (pos == 0) return std::make_pair(string(path, 0, 1), string(path, 1));
+
+ return std::make_pair(string(path, 0, pos), string(path, pos + 1));
+}
+
bool ReadFile(const string &filename, string *content, int max_size) {
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) return false;