📄 path.cpp
字号:
}} void Path::popDirectory(){ poco_assert (!_dirs.empty()); _dirs.pop_back();} void Path::setFileName(const std::string& name){ _name = name;}void Path::setBaseName(const std::string& name){ std::string ext = getExtension(); _name = name; if (!ext.empty()) { _name.append("."); _name.append(ext); }}std::string Path::getBaseName() const{ std::string::size_type pos = _name.rfind('.'); if (pos != std::string::npos) return _name.substr(0, pos); else return _name;}void Path::setExtension(const std::string& extension){ _name = getBaseName(); if (!extension.empty()) { _name.append("."); _name.append(extension); }} std::string Path::getExtension() const{ std::string::size_type pos = _name.rfind('.'); if (pos != std::string::npos) return _name.substr(pos + 1); else return std::string();}void Path::clear(){ _node.clear(); _device.clear(); _name.clear(); _dirs.clear(); _version.clear(); _absolute = false;}std::string Path::current(){ return PathImpl::currentImpl();} std::string Path::home(){ return PathImpl::homeImpl();} std::string Path::temp(){ return PathImpl::tempImpl();}std::string Path::null(){ return PathImpl::nullImpl();} std::string Path::expand(const std::string& path){ return PathImpl::expandImpl(path);}void Path::listRoots(std::vector<std::string>& roots){ PathImpl::listRootsImpl(roots);}bool Path::find(StringVec::const_iterator it, StringVec::const_iterator end, const std::string& name, Path& path){ while (it != end) { Path p(*it); p.makeDirectory(); p.resolve(Path(name)); File f(p); if (f.exists()) { path = p; return true; } ++it; } return false;}bool Path::find(const std::string& pathList, const std::string& name, Path& path){ StringTokenizer st(pathList, std::string(1, pathSeparator()), StringTokenizer::TOK_IGNORE_EMPTY + StringTokenizer::TOK_TRIM); return find(st.begin(), st.end(), name, path);}void Path::parseUnix(const std::string& path){ clear(); std::string::const_iterator it = path.begin(); std::string::const_iterator end = path.end(); if (it != end) { if (*it == '/') { _absolute = true; ++it; } else if (*it == '~') { ++it; if (it == end || *it == '/') { Path cwd(home()); _dirs = cwd._dirs; _absolute = true; } else --it; } while (it != end) { std::string name; while (it != end && *it != '/') name += *it++; if (it != end) { if (_dirs.empty()) { if (!name.empty() && *(name.rbegin()) == ':') _device.assign(name, 0, name.length() - 1); else pushDirectory(name); } else pushDirectory(name); } else _name = name; if (it != end) ++it; } }}void Path::parseWindows(const std::string& path){ clear(); std::string::const_iterator it = path.begin(); std::string::const_iterator end = path.end(); if (it != end) { if (*it == '\\' || *it == '/') { _absolute = true; ++it; } if (_absolute && it != end && (*it == '\\' || *it == '/')) // UNC { ++it; while (it != end && *it != '\\' && *it != '/') _node += *it++; if (it != end) ++it; } else if (it != end) { char d = *it++; if (it != end && *it == ':') // drive letter { if (_absolute || !(d >= 'a' && d <= 'z' || d >= 'A' && d <= 'Z')) throw PathSyntaxException(path); _absolute = true; _device += d; ++it; if (it == end || *it != '\\' && *it != '/') throw PathSyntaxException(path); ++it; } else --it; } while (it != end) { std::string name; while (it != end && *it != '\\' && *it != '/') name += *it++; if (it != end) pushDirectory(name); else _name = name; if (it != end) ++it; } } if (!_node.empty() && _dirs.empty() && !_name.empty()) makeDirectory();}void Path::parseVMS(const std::string& path){ clear(); std::string::const_iterator it = path.begin(); std::string::const_iterator end = path.end(); if (it != end) { std::string name; while (it != end && *it != ':' && *it != '[' && *it != ';') name += *it++; if (it != end) { if (*it == ':') { ++it; if (it != end && *it == ':') { _node = name; ++it; } else _device = name; _absolute = true; name.clear(); } if (it != end) { if (_device.empty() && *it != '[') { while (it != end && *it != ':' && *it != ';') name += *it++; if (it != end) { if (*it == ':') { _device = name; _absolute = true; name.clear(); ++it; } } } } if (name.empty()) { if (it != end && *it == '[') { ++it; if (it != end) { _absolute = true; if (*it == '.') { _absolute = false; ++it; } else if (*it == ']' || *it == '-') _absolute = false; while (it != end && *it != ']') { name.clear(); if (*it == '-') name = "-"; else while (it != end && *it != '.' && *it != ']') name += *it++; if (!name.empty()) { if (name == "-") { if (_dirs.empty() || _dirs.back() == "..") _dirs.push_back(".."); else _dirs.pop_back(); } else _dirs.push_back(name); } if (it != end && *it != ']') ++it; } if (it == end) throw PathSyntaxException(path); ++it; if (it != end && *it == '[') { if (!_absolute) throw PathSyntaxException(path); ++it; if (it != end && *it == '.') throw PathSyntaxException(path); int d = int(_dirs.size()); while (it != end && *it != ']') { name.clear(); if (*it == '-') name = "-"; else while (it != end && *it != '.' && *it != ']') name += *it++; if (!name.empty()) { if (name == "-") { if (_dirs.size() > d) _dirs.pop_back(); } else _dirs.push_back(name); } if (it != end && *it != ']') ++it; } if (it == end) throw PathSyntaxException(path); ++it; } } _name.clear(); } while (it != end && *it != ';') _name += *it++; } else _name = name; if (it != end && *it == ';') { ++it; while (it != end) _version += *it++; } } else _name = name; }}void Path::parseGuess(const std::string& path){ bool hasBackslash = false; bool hasSlash = false; bool hasOpenBracket = false; bool hasClosBracket = false; bool isWindows = path.length() > 2 && path[1] == ':' && (path[2] == '/' || path[2] == '\\'); std::string::const_iterator end = path.end(); std::string::const_iterator semiIt = end; if (!isWindows) { for (std::string::const_iterator it = path.begin(); it != end; ++it) { switch (*it) { case '\\': hasBackslash = true; break; case '/': hasSlash = true; break; case '[': hasOpenBracket = true; case ']': hasClosBracket = hasOpenBracket; case ';': semiIt = it; break; } } } if (hasBackslash || isWindows) { parseWindows(path); } else if (hasSlash) { parseUnix(path); } else { bool isVMS = hasClosBracket; if (!isVMS && semiIt != end) { isVMS = true; ++semiIt; while (semiIt != end) { if (*semiIt < '0' || *semiIt > '9') { isVMS = false; break; } ++semiIt; } } if (isVMS) parseVMS(path); else parseUnix(path); }}std::string Path::buildUnix() const{ std::string result; if (!_device.empty()) { result.append("/"); result.append(_device); result.append(":/"); } else if (_absolute) { result.append("/"); } for (StringVec::const_iterator it = _dirs.begin(); it != _dirs.end(); ++it) { result.append(*it); result.append("/"); } result.append(_name); return result;}std::string Path::buildWindows() const{ std::string result; if (!_node.empty()) { result.append("\\\\"); result.append(_node); result.append("\\"); } else if (!_device.empty()) { result.append(_device); result.append(":\\"); } else if (_absolute) { result.append("\\"); } for (StringVec::const_iterator it = _dirs.begin(); it != _dirs.end(); ++it) { result.append(*it); result.append("\\"); } result.append(_name); return result;}std::string Path::buildVMS() const{ std::string result; if (!_node.empty()) { result.append(_node); result.append("::"); } if (!_device.empty()) { result.append(_device); result.append(":"); } if (!_dirs.empty()) { result.append("["); if (!_absolute && _dirs[0] != "..") result.append("."); for (StringVec::const_iterator it = _dirs.begin(); it != _dirs.end(); ++it) { if (it != _dirs.begin() && *it != "..") result.append("."); if (*it == "..") result.append("-"); else result.append(*it); } result.append("]"); } result.append(_name); if (!_version.empty()) { result.append(";"); result.append(_version); } return result;}} // namespace Poco
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -