From d60e7fad2a0f5fc33a57a3467070aa9b8fe4b14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Wed, 1 Apr 2015 06:41:33 +0000 Subject: [PATCH] PIDir windows drives support git-svn-id: svn://db.shs.com.ru/pip@55 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src/console/piscreendrawer.h | 2 +- src/console/piscreentile.h | 2 +- src/console/piscreentiles.h | 14 ++--- src/io/pidir.cpp | 105 +++++++++++++++++++++++++---------- src/io/pidir.h | 22 ++++---- src/io/pifile.h | 2 +- src/io/pipeer.cpp | 18 +++--- src/io/pipeer.h | 1 + 8 files changed, 111 insertions(+), 55 deletions(-) diff --git a/src/console/piscreendrawer.h b/src/console/piscreendrawer.h index a007104d..14351e05 100644 --- a/src/console/piscreendrawer.h +++ b/src/console/piscreendrawer.h @@ -26,7 +26,7 @@ #include "piscreentypes.h" #include "pistring.h" -class PIScreenDrawer +class PIP_EXPORT PIScreenDrawer { friend class PIScreen; PIScreenDrawer(PIVector > & c): cells(c) {} diff --git a/src/console/piscreentile.h b/src/console/piscreentile.h index 112ca9e8..4bd1becb 100644 --- a/src/console/piscreentile.h +++ b/src/console/piscreentile.h @@ -28,7 +28,7 @@ class PIScreenDrawer; -class PIScreenTile: public PIObject { +class PIP_EXPORT PIScreenTile: public PIObject { friend class PIScreen; PIOBJECT_SUBCLASS(PIScreenTile, PIObject) public: diff --git a/src/console/piscreentiles.h b/src/console/piscreentiles.h index a8efd425..1d647124 100644 --- a/src/console/piscreentiles.h +++ b/src/console/piscreentiles.h @@ -26,7 +26,7 @@ #include "piscreentile.h" -class TileSimple: public PIScreenTile { +class PIP_EXPORT TileSimple: public PIScreenTile { public: TileSimple(const PIString & n = PIString()); typedef PIPair Row; @@ -40,7 +40,7 @@ protected: -class TileList: public PIScreenTile { +class PIP_EXPORT TileList: public PIScreenTile { public: TileList(const PIString & n = PIString()); enum SelectionMode { @@ -68,7 +68,7 @@ protected: -class TileButton: public PIScreenTile { +class PIP_EXPORT TileButton: public PIScreenTile { public: TileButton(const PIString & n = PIString()); enum EventType { @@ -85,7 +85,7 @@ protected: -class TileButtons: public PIScreenTile { +class PIP_EXPORT TileButtons: public PIScreenTile { public: TileButtons(const PIString & n = PIString()); enum EventType { @@ -103,7 +103,7 @@ protected: -class TileCheck: public PIScreenTile { +class PIP_EXPORT TileCheck: public PIScreenTile { public: TileCheck(const PIString & n = PIString()); enum EventType { @@ -121,7 +121,7 @@ protected: -class TileProgress: public PIScreenTile { +class PIP_EXPORT TileProgress: public PIScreenTile { public: TileProgress(const PIString & n = PIString()); PIScreenTypes::CellFormat format; @@ -137,7 +137,7 @@ protected: -class TilePICout: public TileList { +class PIP_EXPORT TilePICout: public TileList { public: TilePICout(const PIString & n = PIString()); PIScreenTypes::CellFormat format; diff --git a/src/io/pidir.cpp b/src/io/pidir.cpp index f3ab01d1..5d383a33 100755 --- a/src/io/pidir.cpp +++ b/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 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); diff --git a/src/io/pidir.h b/src/io/pidir.h index a5f3ced4..b1e888ba 100755 --- a/src/io/pidir.h +++ b/src/io/pidir.h @@ -38,7 +38,7 @@ public: //! Returns if this directory is exists - bool isExists() const {return PIDir::isExists(path_);} + bool isExists() const {return PIDir::isExists(path());} //! Returns if path of this directory is absolute bool isAbsolute() const; @@ -48,7 +48,7 @@ public: //! Returns path of this directory - const PIString & path() const {return path_;} + PIString path() const; //! Returns absolute path of this directory PIString absolutePath() const; @@ -60,7 +60,7 @@ public: PIDir & cleanPath(); //! Returns %PIDir with simplified path of this directory - PIDir cleanedPath() const {PIDir d(path_); d.cleanPath(); return d;} + PIDir cleanedPath() const {PIDir d(path()); d.cleanPath(); return d;} //! Returns relative to this directory path "path" PIString relative(const PIString & path) const; @@ -69,13 +69,14 @@ public: PIDir & setDir(const PIString & path); //! Set this directory path as current for application - bool setCurrent() {return PIDir::setCurrent(path_);} + bool setCurrent() {return PIDir::setCurrent(path());} /** \brief Returns this directory content * \details Scan this directory and returns all directories * and files in one list, sorted alphabetically. This list - * contains also "." and ".." members. + * contains also "." and ".." members. There are absolute + * pathes in returned list. * \attention This function doesn`t scan content of inner * directories! */ PIVector entries(); @@ -83,13 +84,14 @@ public: /** \brief Returns all this directory content * \details Scan this directory recursively and returns all * directories and files in one list, sorted alphabetically. - * This list doesn`t contains "." and ".." members. - * Files placed after directories in this list */ + * This list doesn`t contains "." and ".." members. There + * are absolute pathes in returned list, and + * files placed after directories in this list */ PIVector allEntries(); bool make(bool withParents = true); - bool remove() {return PIDir::remove(path_);} - bool rename(const PIString & new_name) {if (!PIDir::rename(path_, new_name)) return false; path_ = new_name; return true;} + bool remove() {return PIDir::remove(path());} + bool rename(const PIString & new_name) {if (!PIDir::rename(path(), new_name)) return false; setDir(new_name); return true;} PIDir & cd(const PIString & path); PIDir & up() {return cd("..");} @@ -107,7 +109,7 @@ public: static bool remove(const PIString & path) {return removeDir(path);} static bool rename(const PIString & path, const PIString & new_name) {return PIDir::renameDir(path, new_name);} static bool setCurrent(const PIString & path); - static bool setCurrent(const PIDir & dir) {return setCurrent(dir.path_);} + static bool setCurrent(const PIDir & dir) {return setCurrent(dir.path());} private: static bool makeDir(const PIString & path); diff --git a/src/io/pifile.h b/src/io/pifile.h index 582db2ca..863355be 100755 --- a/src/io/pifile.h +++ b/src/io/pifile.h @@ -48,7 +48,7 @@ public: typedef PIFlags Flags; struct Permissions { Permissions(uchar r = 0): raw(r) {} - Permissions(bool r, bool w, bool e): raw(0) {read = w; write = w; exec = e;} + Permissions(bool r, bool w, bool e): raw(0) {read = r; write = w; exec = e;} PIString toString() const {return PIString(read ? "r" : "-") + PIString(write ? "w" : "-") + PIString(exec ? "x" : "-");} operator int() const {return raw;} Permissions & operator =(int v) {raw = v; return *this;} diff --git a/src/io/pipeer.cpp b/src/io/pipeer.cpp index b347a56f..2b07552b 100755 --- a/src/io/pipeer.cpp +++ b/src/io/pipeer.cpp @@ -122,7 +122,10 @@ PIString PIPeer::PeerInfo::fastestAddress() const { } + + PIPeer::PIPeer(const PIString & name_): PIObject() { + destroyed = false; setName(name_); self_info.name = name_; self_info.dist = 0; @@ -131,15 +134,16 @@ PIPeer::PIPeer(const PIString & name_): PIObject() { srand(uint(PISystemTime::current(true).toMicroseconds())); //id_ = self_info.name + "_" + PIString::fromNumber(rand()); CONNECTU(&timer, tickEvent, this, timerEvent); + prev_ifaces = PIEthernet::interfaces(); initNetwork(); sendSelfInfo(); - prev_ifaces = PIEthernet::interfaces(); timer.addDelimiter(5); timer.start(1000); } PIPeer::~PIPeer() { + destroyed = true; timer.stop(); diag_s.stop(); diag_d.stop(); @@ -148,10 +152,10 @@ PIPeer::~PIPeer() { i->stopThreadedRead(); piForeach (PIEthernet * i, eths_bcast) i->stopThreadedRead(); - eth_send.close(); eth_lo.stopThreadedRead(); sendSelfRemove(); destroyMBcasts(); + eth_send.close(); } @@ -170,7 +174,6 @@ void PIPeer::timerEvent(void * data, int delim) { void PIPeer::initEths(PIStringList al) { PIEthernet * ce; - PIEthernet::InterfaceList il = PIEthernet::interfaces(); const PIEthernet::Interface * cint = 0; piForeachC (PIString & a, al) { ce = new PIEthernet(); @@ -183,7 +186,7 @@ void PIPeer::initEths(PIStringList al) { ce->setReadAddress(a, p); if (ce->open()) { eths_traffic << ce; - cint = il.getByAddress(a); + cint = prev_ifaces.getByAddress(a); self_info.addresses << PeerInfo::Address(ce->path(), cint == 0 ? "255.255.255.0" : cint->netmask); CONNECTU(ce, threadedReadEvent, this, dataRead); ce->startThreadedRead(); @@ -204,7 +207,6 @@ void PIPeer::initEths(PIStringList al) { void PIPeer::initMBcasts(PIStringList al) { destroyMBcasts(); PIEthernet * ce; - PIEthernet::InterfaceList il = PIEthernet::interfaces(); const PIEthernet::Interface * cint; PIString nm; al << _PIPEER_MULTICAST_IP; @@ -227,7 +229,7 @@ void PIPeer::initMBcasts(PIStringList al) { ce->setDebug(false); ce->setName("__S__PIPeer_bcast_eth_" + a); ce->setParameters(PIEthernet::Broadcast); - cint = il.getByAddress(a); + cint = prev_ifaces.getByAddress(a); nm = (cint == 0) ? "255.255.255.0" : cint->netmask; ce->setSendAddress(PIEthernet::getBroadcast(a, nm), _PIPEER_BROADCAST_PORT); ce->setReadAddress(a, _PIPEER_BROADCAST_PORT); @@ -240,7 +242,7 @@ void PIPeer::initMBcasts(PIStringList al) { eth_lo.setDebug(false); eth_lo.setName("__S__PIPeer_eth_loopback"); eth_lo.setParameters(0); - cint = il.getByAddress("127.0.0.1"); + cint = prev_ifaces.getByAddress("127.0.0.1"); for (int p = _PIPEER_LOOPBACK_PORT_S; p <= _PIPEER_LOOPBACK_PORT_E; ++p) { eth_lo.setReadAddress("127.0.0.1", p); if (eth_lo.open()) { @@ -345,6 +347,7 @@ void PIPeer::dtReceived(const PIString & from, const PIByteArray & data) { bool PIPeer::dataRead(uchar * readed, int size) { + if (destroyed) return true; if (size < 16) return true; PIByteArray ba(readed, size), sba, pba; int type, cnt; @@ -444,6 +447,7 @@ bool PIPeer::dataRead(uchar * readed, int size) { bool PIPeer::mbcastRead(uchar * data, int size) { + if (destroyed) return true; if (size < 8) return true; int type, dist; PIByteArray ba(data, size); diff --git a/src/io/pipeer.h b/src/io/pipeer.h index d495392d..20d5a8f2 100755 --- a/src/io/pipeer.h +++ b/src/io/pipeer.h @@ -192,6 +192,7 @@ private: PIMap > addresses_map; // map {"to" = list of nearest peers} PIMap > removed; PIDiagnostics diag_s, diag_d; + bool destroyed; //PIString id_; };