-
Notifications
You must be signed in to change notification settings - Fork 0
/
additionalpath.cpp
40 lines (36 loc) · 1.05 KB
/
additionalpath.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
#include <QDir>
#include "additionalpath.h"
QString getAdditonalPath(QString fullPath, int level) {
if (level) {
QString path = QDir::fromNativeSeparators(fullPath);
int max_level = getLevelParentDirs(path);
if (level > max_level)
level = max_level;
int i, level_count = 0;
for (i=path.length() - 1; i>=0; i--) {
if (path[i]=='/')
if (++level_count == level) {
break;
}
}
path.remove(0,i);
return path;
} else {
return QString();
}
}
int getLevelParentDirs(QString path) {
QString stdSepPath = QDir::fromNativeSeparators(path);
int countParentDir = stdSepPath.count('/');
#ifdef Q_OS_WIN
if ((stdSepPath.length() == 3 && stdSepPath[2] == '/') ||
(stdSepPath.length() > 2 &&
stdSepPath[0] == '/' && stdSepPath[1] == '/'))
countParentDir--;
#endif
#ifdef Q_OS_LINUX
if (stdSepPath.length() == 1)
countParentDir--;
#endif
return countParentDir;
}