blob: a1def51637bfecfc3d33469de5322ba158329712 (
plain)
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
71
72
73
74
75
76
77
78
79
80
|
#include "file_name_utils.hpp"
#include "std/target_os.hpp"
namespace my
{
void GetNameWithoutExt(string & name)
{
string::size_type const i = name.rfind('.');
if (i != string::npos)
name.erase(i);
}
string FilenameWithoutExt(string name)
{
GetNameWithoutExt(name);
return name;
}
string GetFileExtension(string const & name)
{
size_t const pos = name.find_last_of("./\\");
return ((pos != string::npos && name[pos] == '.') ? name.substr(pos) : string());
}
void GetNameFromFullPath(string & name)
{
string::size_type const i = name.find_last_of("/\\");
if (i != string::npos)
name = name.substr(i+1);
}
string GetDirectory(string const & name)
{
string const sep = GetNativeSeparator();
size_t const sepSize = sep.size();
string::size_type i = name.rfind(sep);
if (i == string::npos)
return ".";
while (i > sepSize && (name.substr(i - sepSize, sepSize) == sep))
i -= sepSize;
return i == 0 ? sep : name.substr(0, i);
}
string GetNativeSeparator()
{
#ifdef OMIM_OS_WINDOWS
return "\\";
#else
return "/";
#endif
}
string JoinFoldersToPath(const string & folder, const string & file)
{
return my::AddSlashIfNeeded(folder) + file;
}
string JoinFoldersToPath(initializer_list<string> const & folders, const string & file)
{
string result;
for (string const & s : folders)
result += AddSlashIfNeeded(s);
result += file;
return result;
}
string AddSlashIfNeeded(string const & path)
{
string const sep = GetNativeSeparator();
string::size_type const pos = path.rfind(sep);
if ((pos != string::npos) && (pos + sep.size() == path.size()))
return path;
return path + sep;
}
} // namespace my
|