📄 warpath.h
字号:
return mString.size(); } inline char GetSlash() const { return (char)sys_slash_t::SYS_SLASH; } path_ccstring_t GetPath() const { return mString.c_str(); } WarPath<charT, slashT> GetFilename() const { path_string_t::size_type position = mString.find_last_of( sys_slash_t::SYS_SLASH); return mString.c_str() + (int)((position == mString.npos) ? 0 : (position + 1)); } WarPath<charT, slashT> GetPathname() const { path_string_t::size_type position = mString.find_last_of( sys_slash_t::SYS_SLASH); if (position == mString.npos) { charT buf[sizeof(int)]; buf[0] = '.'; buf[1] = sys_slash_t::SYS_SLASH; buf[2] = 0; return buf; } if (position == 0) { charT buf[sizeof(int)]; buf[0] = sys_slash_t::SYS_SLASH; buf[1] = 0; return buf; } return mString.substr(0, position); } WarPath<charT, slashT> GetExtension() const { path_string_t::size_type index = mString.find_last_of('.'); if (index == path_string_t::npos) return WarPath(); // Blank return mString.c_str() + (index +1); } bool IsEmpty() const { return mString.empty(); } bool IsFirstChSlash() const { if (IsEmpty()) return false; return IsSlash(mString[0]); } path_char_t GetLastCh() const { if (!mString.empty()) return mString[mString.size() -1]; return 0; } bool IsRoot() const { return ((GetLength() == 1) && (mString[0] == '/')); } static bool IsSlash(const path_char_t ch) { if (sys_slash_t::SYS_SLASH != '/') { if (sys_slash_t::SYS_SLASH == ch) return true; } return '/' == ch; } /** Gets the first part of the path. Typically used by lame WIN32 calls that needs the initial C:\ or \\SERVER\SHARE\ to return interesting information. @return Win32: The file system part of the path. UNIX : Returns the full path. */ WarPath<charT, slashT> GetMountPath(bool do_expand = true) const { if (IsEmpty()) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), "empty path"); #ifdef WIN32 //WarUrl MyUrl = mPath; //WarPath RealPath = MyUrl.GetFilePath().GetPath(); WarPath<charT, slashT>RealPath = *this; path_ccstring_t p = RealPath.mString.c_str(); WarPath<charT, slashT> MyPath; if ((p[0] == '\\') && (p[1] == '\\') && ((unsigned)(p[0]) < 0xff) && isalpha((char)p[2])) { // UNC path MyPath.mString += *p++; MyPath.mString += *p++; while(*p && !IsSlash(*p)) MyPath.mString += *p++; if (IsSlash(*p)) MyPath.mString += *p++; while(*p && !IsSlash(*p)) MyPath.mString += *p++; return MyPath; } else if (((unsigned)(p[0]) < 0xff) && isalpha(p[0]) && (p[1] == ':')) { MyPath.mString += *p++; MyPath.mString += ':'; } else if (do_expand) { path_char_t cwd_buf[MAX_PATH]; if (::GetCurrentDirectory(MAX_PATH, cwd_buf)) { WarPath retry_path = cwd_buf; retry_path << WAR_SYSSLASH << MyPath.GetPath(); return retry_path.GetMountPath(false); } else { WarError system_error; system_error.Capture(); WarThrow(system_error, NULL); } } else WarThrow(WarError(WAR_ERR_INVALID_PATH), "funny path"); MyPath.mString += '\\'; return MyPath;#else // WIN32 if (mString[0] != WarSysSlash::SYS_SLASH) WarThrow(WarError(WAR_ERR_INVALID_PATH), "funny path"); return *this;#endif // WIN32 } /** Compare this path with another path.*/ bool CmpPath(const path_t& otherPath, const PathCmpTypeE CmpMode, bool allowOtherToBeLonger = true, int *pCmpResult = NULL) const { size_t cmp_length = GetLength(), other_length = otherPath.GetLength(); bool return_val = false; int cmp_result = 0; if (IsSlash(GetLastCh())) --cmp_length; if (otherPath.IsSlash(otherPath.GetLastCh())) --other_length; if (pCmpResult) { *pCmpResult = 0; } else { if (!allowOtherToBeLonger) { if (cmp_length != other_length) return false; } else if ((other_length != cmp_length) && !IsSlash(otherPath[cmp_length])) return false; } if (CmpMode == PCMP_CASE_INSENSITIVE) { return_val = (cmp_result = Strnicmp( otherPath.GetPath(), cmp_length)) == 0; } else return_val = (cmp_result = memcmp( GetPath(), otherPath.GetPath(), cmp_length *sizeof(path_char_t))) == 0; if (pCmpResult) *pCmpResult = cmp_result; return return_val; }protected:private:};/* INLINE METHODS *//* EXTERNAL REFERENCES */template <class charT, class slashT, class fcT, class fsT>WarPath<charT, slashT>& operator << (WarPath<charT, slashT>&c, const WarPath<fcT, fsT>& from){ WarCollector<charT> &rc = c; rc << (WarCollector<fcT>)from; return c;}template <class charT, class slashT>WarPath<charT, slashT>& operator << (WarPath<charT, slashT>&c, const WarPathE from){ charT buf[4]; charT *p = buf; if (from == WAR_CDIR) c.RemovePaddingCdir(); else c.RemovePaddingSlash(); switch(from) { case WAR_DSYSSLASH: *p++ = (charT)slashT::SYS_SLASH; // Fall trough case WAR_SYSSLASH: *p++ = (charT)slashT::SYS_SLASH; break; case WAR_DSLASH: *p++ = '/'; // Fall trough case WAR_SLASH: *p++ = '/'; break; case WAR_CDIR: *p++ = '.'; *p++ = '/'; break; default: WarThrow(WarException(WAR_ERR_INVALID_CASE_VALUE), NULL); } *p = 0; c.mString += buf; return c;}template <class charT, class slashT, class T>WarPath<charT, slashT>& operator << (WarPath<charT, slashT>&c, const T& from){ WarCollector<charT> &rc = c; rc << from; return c;}template <class slashT>std::ostream& operator << (std::ostream& c, const WarPath<char,slashT>& from){ c << from.GetPath(); return c;}template <class slashT>std::ostream& operator << (std::ostream& c, const WarPath<wchar_t,slashT>& from){ try { WarPath<char,WarSlash> charPath(from.GetPath()); c << charPath; } catch(WarException& ex) { c << "[filename can not be expressed:" << "(" << ex.Explain() << ")]"; } return c;}/** */template <class charT, class fcT, class fsT>WarCollector<charT>& operator << (WarCollector<charT>& c, const WarPath<fcT, fsT>& val){ const WarCollector<fcT>& rc = val; return c << rc.mString;}typedef WarPath<war_sysch_t,WarSysSlash> war_syspath_t;typedef wchar_t war_svrpath_ch_t;typedef WarPath<war_svrpath_ch_t, WarSlash> war_svrpath_t; typedef const war_svrpath_ch_t * war_ccsvrpath_t;typedef std::basic_string<war_svrpath_ch_t> war_svrpath_str_t;typedef war_svrpath_t::path_ccstring_t war_svrpath_ccstr_t;typedef WarPath<war_fsysch_t, WarSysSlash> war_registrypath_t;typedef war_registrypath_t::path_ccstring_t war_registrypath_ccstr_t;typedef std::basic_string<war_registrypath_t::path_char_t> war_regstr_t;typedef const war_registrypath_t::path_char_t * war_regstr_ccstr_t;#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* WAR_PATH_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -