📄 path.cpp
字号:
//
// Path.cpp
//
// $Id: //poco/Main/Foundation/src/Path.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/Path.h"
#include "Foundation/File.h"
#include "Foundation/Exception.h"
#include "Foundation/StringTokenizer.h"
#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)
#include "Path_WIN32.cpp"
#endif
Foundation_BEGIN
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()
{
}
Path& Path::operator = (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::operator = (const std::string& path)
{
return assign(path);
}
Path& Path::operator = (const char* path)
{
poco_check_ptr(path);
return assign(path);
}
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;
}
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();
}
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;
}
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
}
}
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -