📄 path.cpp
字号:
//
// Path.cpp
//
// $Id: //poco/1.3/Foundation/src/Path.cpp#4 $
//
// Library: Foundation
// Package: Filesystem
// Module: Path
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Path.h"
#include "Poco/File.h"
#include "Poco/Exception.h"
#include "Poco/StringTokenizer.h"
#if defined(_WIN32) && defined(POCO_WIN32_UTF8)
#include "Poco/UnicodeConverter.h"
#include "Poco/Buffer.h"
#endif
#include <algorithm>
#if defined(POCO_OS_FAMILY_VMS)
#include "Path_VMS.cpp"
#elif defined(POCO_OS_FAMILY_UNIX)
#include "Path_UNIX.cpp"
#elif defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
#include "Path_WIN32U.cpp"
#elif defined(POCO_OS_FAMILY_WINDOWS)
#include "Path_WIN32.cpp"
#endif
namespace Poco {
Path::Path(): _absolute(false)
{
}
Path::Path(bool absolute): _absolute(absolute)
{
}
Path::Path(const std::string& path)
{
assign(path);
}
Path::Path(const std::string& path, Style style)
{
assign(path, style);
}
Path::Path(const char* path)
{
poco_check_ptr(path);
assign(path);
}
Path::Path(const char* path, Style style)
{
poco_check_ptr(path);
assign(path, style);
}
Path::Path(const Path& path):
_node(path._node),
_device(path._device),
_name(path._name),
_version(path._version),
_dirs(path._dirs),
_absolute(path._absolute)
{
}
Path::Path(const Path& parent, const std::string& fileName):
_node(parent._node),
_device(parent._device),
_name(parent._name),
_version(parent._version),
_dirs(parent._dirs),
_absolute(parent._absolute)
{
makeDirectory();
_name = fileName;
}
Path::Path(const Path& parent, const char* fileName):
_node(parent._node),
_device(parent._device),
_name(parent._name),
_version(parent._version),
_dirs(parent._dirs),
_absolute(parent._absolute)
{
makeDirectory();
_name = fileName;
}
Path::Path(const Path& parent, const Path& relative):
_node(parent._node),
_device(parent._device),
_name(parent._name),
_version(parent._version),
_dirs(parent._dirs),
_absolute(parent._absolute)
{
resolve(relative);
}
Path::~Path()
{
}
Path& Path::operator = (const Path& path)
{
return assign(path);
}
Path& Path::operator = (const std::string& path)
{
return assign(path);
}
Path& Path::operator = (const char* path)
{
poco_check_ptr(path);
return assign(path);
}
void Path::swap(Path& path)
{
std::swap(_node, path._node);
std::swap(_device, path._device);
std::swap(_name, path._name);
std::swap(_version, path._version);
std::swap(_dirs, path._dirs);
std::swap(_absolute, path._absolute);
}
Path& Path::assign(const Path& path)
{
if (&path != this)
{
_node = path._node;
_device = path._device;
_name = path._name;
_version = path._version;
_dirs = path._dirs;
_absolute = path._absolute;
}
return *this;
}
Path& Path::assign(const std::string& path)
{
#if defined(POCO_OS_FAMILY_VMS)
parseVMS(path);
#elif defined(POCO_OS_FAMILY_WINDOWS)
parseWindows(path);
#else
parseUnix(path);
#endif
return *this;
}
Path& Path::assign(const std::string& path, Style style)
{
switch (style)
{
case PATH_UNIX:
parseUnix(path);
break;
case PATH_WINDOWS:
parseWindows(path);
break;
case PATH_VMS:
parseVMS(path);
break;
case PATH_NATIVE:
assign(path);
break;
case PATH_GUESS:
parseGuess(path);
break;
default:
poco_bugcheck();
}
return *this;
}
Path& Path::assign(const char* path)
{
return assign(std::string(path));
}
std::string Path::toString() const
{
#if defined(POCO_OS_FAMILY_UNIX)
return buildUnix();
#elif defined(POCO_OS_FAMILY_WINDOWS)
return buildWindows();
#else
return buildVMS();
#endif
}
std::string Path::toString(Style style) const
{
switch (style)
{
case PATH_UNIX:
return buildUnix();
case PATH_WINDOWS:
return buildWindows();
case PATH_VMS:
return buildVMS();
case PATH_NATIVE:
case PATH_GUESS:
return toString();
default:
poco_bugcheck();
}
return std::string();
}
bool Path::tryParse(const std::string& path)
{
try
{
Path p;
p.parse(path);
assign(p);
return true;
}
catch (...)
{
return false;
}
}
bool Path::tryParse(const std::string& path, Style style)
{
try
{
Path p;
p.parse(path, style);
assign(p);
return true;
}
catch (...)
{
return false;
}
}
Path& Path::parseDirectory(const std::string& path)
{
assign(path);
return makeDirectory();
}
Path& Path::parseDirectory(const std::string& path, Style style)
{
assign(path, style);
return makeDirectory();
}
Path& Path::makeDirectory()
{
#if defined(POCO_OS_FAMILY_VMS)
pushDirectory(getBaseName());
#else
pushDirectory(_name);
#endif
_name.clear();
_version.clear();
return *this;
}
Path& Path::makeFile()
{
if (!_dirs.empty() && _name.empty())
{
_name = _dirs.back();
_dirs.pop_back();
#if defined(POCO_OS_FAMILY_VMS)
setExtension("DIR");
#endif
}
return *this;
}
Path& Path::makeAbsolute()
{
return makeAbsolute(current());
}
Path& Path::makeAbsolute(const Path& base)
{
if (!_absolute)
{
Path tmp = base;
tmp.makeDirectory();
for (StringVec::const_iterator it = _dirs.begin(); it != _dirs.end(); ++it)
{
tmp.pushDirectory(*it);
}
_node = tmp._node;
_device = tmp._device;
_dirs = tmp._dirs;
_absolute = base._absolute;
}
return *this;
}
Path Path::absolute() const
{
Path result(*this);
if (!result._absolute)
{
result.makeAbsolute();
}
return result;
}
Path Path::absolute(const Path& base) const
{
Path result(*this);
if (!result._absolute)
{
result.makeAbsolute(base);
}
return result;
}
Path Path::parent() const
{
Path p(*this);
return p.makeParent();
}
Path& Path::makeParent()
{
if (_name.empty())
{
if (_dirs.empty())
{
if (!_absolute)
_dirs.push_back("..");
}
else
{
if (_dirs.back() == "..")
_dirs.push_back("..");
else
_dirs.pop_back();
}
}
else
{
_name.clear();
_version.clear();
}
return *this;
}
Path& Path::append(const Path& path)
{
makeDirectory();
_dirs.insert(_dirs.end(), path._dirs.begin(), path._dirs.end());
_name = path._name;
_version = path._version;
return *this;
}
Path& Path::resolve(const Path& path)
{
if (path.isAbsolute())
{
assign(path);
}
else
{
for (int i = 0; i < path.depth(); ++i)
pushDirectory(path[i]);
_name = path._name;
}
return *this;
}
void Path::setNode(const std::string& node)
{
_node = node;
_absolute = _absolute || !node.empty();
}
void Path::setDevice(const std::string& device)
{
_device = device;
_absolute = _absolute || !device.empty();
}
const std::string& Path::directory(int n) const
{
poco_assert (0 <= n && n <= _dirs.size());
if (n < _dirs.size())
return _dirs[n];
else
return _name;
}
const std::string& Path::operator [] (int n) const
{
poco_assert (0 <= n && n <= _dirs.size());
if (n < _dirs.size())
return _dirs[n];
else
return _name;
}
void Path::pushDirectory(const std::string& dir)
{
if (!dir.empty() && dir != ".")
{
#if defined(POCO_OS_FAMILY_VMS)
if (dir == ".." || dir == "-")
{
if (!_dirs.empty() && _dirs.back() != ".." && _dirs.back() != "-")
_dirs.pop_back();
else if (!_absolute)
_dirs.push_back(dir);
}
else _dirs.push_back(dir);
#else
if (dir == "..")
{
if (!_dirs.empty() && _dirs.back() != "..")
_dirs.pop_back();
else if (!_absolute)
_dirs.push_back(dir);
}
else _dirs.push_back(dir);
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -