-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
70 lines (64 loc) · 2.1 KB
/
Copy pathFileSystem.cpp
File metadata and controls
70 lines (64 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! ========================================================================================
//! ExecutionGraph
//! Copyright (C) 2014 by Gabriel Nützi <gnuetzi (at) gmail (døt) com>
//!
//! @date Sat Feb 17 2018
//! @author Gabriel Nützi, gnuetzi (at) gmail (døt) com
//!
//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this
//! file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! ========================================================================================
#include "executionGraph/common/FileSystem.hpp"
namespace executionGraph
{
//! Split all leading slashes from `path`.
std::path splitLeadingSlashes(const std::path& path)
{
std::path res;
auto itC = path.begin();
if(itC != path.end())
{
if(*itC == "/")
{
++itC;
}
while(itC != path.end())
{
res /= *itC++;
}
}
return res;
}
//! Split the prefix `prefix` from the path `path`
std::optional<std::path> splitPrefixFromPath(const std::path& path, const std::path& prefix)
{
// path e.g. "////host/folderA/folderB/file.ext"
std::path tempPath = executionGraph::splitLeadingSlashes(path);
// e.g. tempPath : "host/folderA/folderB/file.ext""
// Split pathPrefix from front (e.g prefix := "host/folderA")
auto it = tempPath.begin();
auto itEnd = tempPath.end();
auto itPref = prefix.begin();
auto itPrefEnd = prefix.end();
for(; it != itEnd && itPref != itPrefEnd; ++it, ++itPref)
{
if(*itPref != *it)
{
break;
}
}
if(itPref != itPrefEnd)
{
// Could not split pathPrefix
return {};
}
// Concat the tempPath together
std::path filePath;
while(it != itEnd)
{
filePath /= *it++;
}
return filePath;
}
} // namespace executionGraph