-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirectory.cpp
54 lines (47 loc) · 1.25 KB
/
Directory.cpp
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
#include "Forward.h"
#include "Directory.h"
std::string Directory::DotDot(std::string dir) // Go up one directory from this directory, i.e. "/apple/pear" -> "/apple"
{
size_t lastslash = dir.find_last_of('/');
if (lastslash == std::string::npos || lastslash == 0)
return "/";
return dir.erase(lastslash, std::string::npos);
}
std::string Directory::lastword(std::string dir)
{
size_t lastslashpos = dir.find_last_of('/');
if (lastslashpos == std::string::npos) // If this directory lacks a slash (???)
{
return dir; // return itself
}
else if (dir[lastslashpos] == dir.back()) // If the slash is at the very end
{
return "";
}
return dir.substr(lastslashpos + 1, std::string::npos);
}
bool Directory::is_base_of(std::string base, std::string derived)
{
if (base == "/")
return true;
if (derived == base)
return false;
while (true)
{
derived = Directory::DotDot(derived);
if (derived == base)
return true;
if (base.length() > derived.length())
return false;
}
}
std::string string::replace_all(std::string hay, char needle, char cooler_needle) // Needle vs. the cooler needle
{
size_t pos = hay.find_first_of(needle);
while (pos != std::string::npos)
{
hay[pos] = cooler_needle;
pos = hay.find_first_of(needle);
}
return hay;
}