/
utility.cpp
138 lines (111 loc) · 2.89 KB
/
utility.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "utility.h"
#include <QDirIterator>
#if defined(Q_WS_WIN) || defined(Q_OS_WIN)
#define cDoNothing
#elif defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_OS_LINUX) \
|| defined(Q_OS_UNIX) || defined(Q_OS_MAC)
#define cFixRegular
#else
#error "Unknown OS for path manipulation (utility.cpp)"
#endif
bool cCaseInsensitiveCd(QDir &dir, const QString &dirName) {
if (!dir.cd(dirName)) {
#ifdef cFixRegular
// I'm making an assumption that every non-Windows OS CMap is compiled
// for (those that use cFixRegular) will use the typical /-separated
// paths. If not, oh well... no CMap on Haiku or Mac OS 9, I guess :(
QStringList pieces = dirName.split(QRegExp("[\\\\/]"));
foreach (const QString &piece, pieces) {
if (!dir.cd(piece)) {
// let's try to find it manually
QDirIterator di(dir);
bool foundThis = false;
while (di.hasNext()) {
di.next();
if (di.fileName().compare(piece, Qt::CaseInsensitive) == 0) {
dir.cd(di.fileName());
foundThis = true;
break;
}
}
if (!foundThis)
return false;
}
}
// well, we got there in the end
#endif
}
return true;
}
QString cCaseInsensitiveFilePath(QDir &dir, const QString &fileName) {
// Note: this doesn't take into account hierarchical paths!
if (dir.exists(fileName)) {
return dir.filePath(fileName);
} else {
// search the thing
QDirIterator di(dir);
while (di.hasNext()) {
di.next();
if (di.fileName().compare(fileName, Qt::CaseInsensitive) == 0)
return di.filePath();
}
return QString();
}
}
/*QString cFixCaseInsensitiveFilePath(const QString &origPath, bool *didWork) {
if (didWork) *didWork = false;
if (QFile::exists(origPath)) {
if (didWork) *didWork = true;
return origPath;
}
// now, try to normalise it...
#ifdef cFixRegular
// search for the path
QStringList pieces = origPath.split('/');
QDir dir;
if (origPath.at(0) == '/') {
// absolute path
dir = QDir::root();
pieces.removeFirst();
}
QString filename = pieces.takeLast();
foreach (const QString &piece, pieces) {
if (!dir.cd(piece)) {
// oops, let's try to find it manually!
QDirIterator di(dir);
bool foundThis = false;
while (di.hasNext()) {
di.next();
if (di.fileName().compare(piece, Qt::CaseInsensitive) == 0) {
dir.cd(di.fileName());
foundThis = true;
break;
}
}
if (!foundThis) {
return QString();
}
}
}
// if we got here, it means we found the correct dir!
if (dir.exists(filename)) {
// that was easy. nothing to mess with.
if (didWork) *didWork = true;
return dir.filePath(filename);
} else {
// search the directory
QDirIterator di(dir);
bool foundThis = false;
while (di.hasNext()) {
di.next();
if (di.fileName().compare(filename, Qt::CaseInsensitive) == 0) {
// got it
if (didWork) *didWork = true;
return di.filePath();
}
}
// nothing there
}
#endif
return QString();
}*/