PIDir windows drives support
git-svn-id: svn://db.shs.com.ru/pip@55 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
105
src/io/pidir.cpp
105
src/io/pidir.cpp
@@ -54,14 +54,12 @@ const PIChar PIDir::separator = '/';
|
||||
|
||||
|
||||
PIDir::PIDir(const PIString & dir) {
|
||||
path_ = dir;
|
||||
cleanPath();
|
||||
setDir(dir);
|
||||
}
|
||||
|
||||
|
||||
PIDir::PIDir(const PIFile & file) {
|
||||
path_ = file.path();
|
||||
cleanPath();
|
||||
setDir(file.path());
|
||||
if (isExists()) return;
|
||||
int pos = path_.findLast(separator);
|
||||
path_.cutRight(path_.size_s() - pos);
|
||||
@@ -75,18 +73,42 @@ bool PIDir::operator ==(const PIDir & d) const {
|
||||
|
||||
bool PIDir::isAbsolute() const {
|
||||
if (path_.isEmpty()) return false;
|
||||
#ifdef WINDOWS
|
||||
/*#ifdef WINDOWS
|
||||
if (path_.size_s() < 2) return false;
|
||||
return (path_.mid(1, 2).contains(":"));
|
||||
#else
|
||||
return (path_[0] == separator);
|
||||
#endif*/
|
||||
return (path_[0] == separator);
|
||||
}
|
||||
|
||||
|
||||
PIString PIDir::path() const {
|
||||
#ifdef WINDOWS
|
||||
if (path_.startsWith(separator)) {
|
||||
if (path_.length() == 1)
|
||||
return separator;
|
||||
else
|
||||
return path_.mid(1);
|
||||
} else
|
||||
#endif
|
||||
return path_;
|
||||
}
|
||||
|
||||
|
||||
PIString PIDir::absolutePath() const {
|
||||
if (isAbsolute()) return path_;
|
||||
return PIDir(PIDir::current().path_ + separator + path_).path_;
|
||||
if (isAbsolute()) {
|
||||
#ifdef WINDOWS
|
||||
if (path_.startsWith(separator)) {
|
||||
if (path_.length() == 1)
|
||||
return separator;
|
||||
else
|
||||
return path_.mid(1);
|
||||
} else
|
||||
#endif
|
||||
return path_;
|
||||
}
|
||||
return PIDir(PIDir::current().path() + separator + path_).path();
|
||||
}
|
||||
|
||||
|
||||
@@ -96,13 +118,10 @@ PIDir & PIDir::cleanPath() {
|
||||
path_ = ".";
|
||||
return *this;
|
||||
}
|
||||
path_.replaceAll(PIString(separator) + PIString(separator), PIString(separator));
|
||||
PIString sep = PIString(separator);
|
||||
path_.replaceAll(sep + sep, sep);
|
||||
bool is_abs = isAbsolute();
|
||||
PIStringList l = PIString(p).split(separator);
|
||||
#ifdef WINDOWS
|
||||
PIString letter;
|
||||
if (is_abs) letter = l.take_front();
|
||||
#endif
|
||||
l.removeAll(".");
|
||||
l.removeAll("");
|
||||
for (int i = 0; i < l.size_s() - 1; ++i) {
|
||||
@@ -120,12 +139,8 @@ PIDir & PIDir::cleanPath() {
|
||||
break;
|
||||
}
|
||||
path_ = l.join(separator);
|
||||
if (is_abs) {
|
||||
if (is_abs)
|
||||
path_.prepend(separator);
|
||||
#ifdef WINDOWS
|
||||
path_.prepend(letter);
|
||||
#endif
|
||||
}
|
||||
if (path_.isEmpty()) path_ = ".";
|
||||
return *this;
|
||||
}
|
||||
@@ -149,6 +164,11 @@ PIString PIDir::relative(const PIString & path) const {
|
||||
|
||||
PIDir & PIDir::setDir(const PIString & path) {
|
||||
path_ = path;
|
||||
#ifdef WINDOWS
|
||||
if (path_.length() > 2)
|
||||
if (path_.mid(1, 2).contains(":"))
|
||||
path_.prepend(separator);
|
||||
#endif
|
||||
cleanPath();
|
||||
return *this;
|
||||
}
|
||||
@@ -167,7 +187,7 @@ bool PIDir::make(bool withParents) {
|
||||
PIString tp;
|
||||
bool is_abs = isAbsolute();
|
||||
if (withParents) {
|
||||
PIStringList l = d.path_.split(separator);
|
||||
PIStringList l = d.path().split(separator);
|
||||
for (int i = l.size_s() - 1; i >= 0; --i) {
|
||||
if (i > 1) tp = PIStringList(l).remove(i, l.size_s() - i).join(separator);
|
||||
else {
|
||||
@@ -187,7 +207,7 @@ bool PIDir::make(bool withParents) {
|
||||
}
|
||||
return true;
|
||||
} else
|
||||
if (makeDir(d.path_)) return true;
|
||||
if (makeDir(d.path())) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -201,15 +221,41 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
|
||||
else if (!dp.endsWith(separator)) dp += separator;
|
||||
//piCout << "entries from" << p;
|
||||
#ifdef WINDOWS
|
||||
WIN32_FIND_DATA fd; memset(&fd, 0, sizeof(fd));
|
||||
p += "\\*";
|
||||
void * hf = FindFirstFile((LPCTSTR)(p.data()), &fd);
|
||||
if (!hf) return l;
|
||||
do {
|
||||
l << PIFile::fileInfo(dp + PIString(fd.cFileName));
|
||||
memset(&fd, 0, sizeof(fd));
|
||||
} while (FindNextFile(hf, &fd) != 0);
|
||||
FindClose(hf);
|
||||
if (dp == separator) {
|
||||
char letters[1024];
|
||||
PIFile::FileInfo fi;
|
||||
DWORD ll = GetLogicalDriveStrings(1023, letters);
|
||||
PIString clet;
|
||||
for (int i = 0; i < ll; ++i) {
|
||||
if (letters[i] == '\0') {
|
||||
clet.resize(2);
|
||||
fi.path = clet;
|
||||
fi.flags = PIFile::FileInfo::Dir;
|
||||
l << fi;
|
||||
clet.clear();
|
||||
} else
|
||||
clet += PIChar(letters[i]);
|
||||
}
|
||||
} else {
|
||||
WIN32_FIND_DATA fd; memset(&fd, 0, sizeof(fd));
|
||||
p += "\\*";
|
||||
void * hf = FindFirstFile((LPCTSTR)(p.data()), &fd);
|
||||
if (!hf) return l;
|
||||
bool hdd = false;
|
||||
do {
|
||||
PIString fn(fd.cFileName);
|
||||
if (fn == "..") hdd = true;
|
||||
l << PIFile::fileInfo(dp + fn);
|
||||
memset(&fd, 0, sizeof(fd));
|
||||
} while (FindNextFile(hf, &fd) != 0);
|
||||
FindClose(hf);
|
||||
if (!hdd) {
|
||||
PIFile::FileInfo fi;
|
||||
fi.path = "..";
|
||||
fi.flags = PIFile::FileInfo::Dir | PIFile::FileInfo::DotDot;
|
||||
l.push_front(fi);
|
||||
}
|
||||
}
|
||||
#else
|
||||
dirent ** list;
|
||||
int cnt = scandir(
|
||||
@@ -260,6 +306,7 @@ PIDir PIDir::current() {
|
||||
if (GetCurrentDirectory(1024, (LPTSTR)rc) == 0) return PIString();
|
||||
PIString ret(rc);
|
||||
ret.replaceAll("\\", PIDir::separator);
|
||||
ret.prepend(separator);
|
||||
return PIDir(ret);
|
||||
#else
|
||||
if (getcwd(rc, 1024) == 0) return PIString();
|
||||
@@ -280,6 +327,7 @@ PIDir PIDir::home() {
|
||||
PIString s(rc);
|
||||
s.replaceAll("\\", PIDir::separator);
|
||||
delete[] rc;
|
||||
s.prepend(separator);
|
||||
return PIDir(s);
|
||||
#else
|
||||
rc = getenv("HOME");
|
||||
@@ -302,6 +350,7 @@ PIDir PIDir::temporary() {
|
||||
PIString s(rc);
|
||||
s.replaceAll("\\", PIDir::separator);
|
||||
delete[] rc;
|
||||
s.prepend(separator);
|
||||
return PIDir(s);
|
||||
#else
|
||||
rc = tmpnam(0);
|
||||
|
||||
Reference in New Issue
Block a user