2016-11-17 18:36:53 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2016-11-17 18:36:53 +11:00
|
|
|
*
|
|
|
|
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "filesystem.hpp"
|
2016-11-17 18:36:53 +11:00
|
|
|
|
|
|
|
#include "../../except.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace ns = std::experimental::filesystem;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
ns::path::path ()
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
ns::path::path (const path &p):
|
|
|
|
m_path (p.m_path)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
std::string
|
|
|
|
ns::path::string (void) const
|
|
|
|
{
|
|
|
|
return m_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
const ns::path::string_type&
|
|
|
|
ns::path::native (void) const
|
|
|
|
{
|
|
|
|
return m_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const ns::path::value_type*
|
|
|
|
ns::path::c_str (void) const
|
|
|
|
{
|
|
|
|
return m_path.c_str ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
ns::path
|
|
|
|
ns::path::filename (void) const
|
|
|
|
{
|
|
|
|
auto slash = m_path.find_last_of (preferred_separator);
|
|
|
|
if (slash == decltype(m_path)::npos)
|
|
|
|
return m_path;
|
|
|
|
return ns::path (m_path.cbegin () + slash, m_path.cend ());
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
ns::path
|
|
|
|
ns::path::stem (void) const
|
|
|
|
{
|
|
|
|
auto name = filename ();
|
|
|
|
|
|
|
|
auto first = name.m_path.cbegin ();
|
|
|
|
auto last = std::find_if (first, name.m_path.cend (), [] (auto c) { return c == '.'; });
|
|
|
|
return path (first, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
ns::path
|
|
|
|
ns::operator/ (const ns::path &a, const ns::path &b)
|
|
|
|
{
|
|
|
|
return ns::path (a) /= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
ns::path&
|
|
|
|
ns::path::operator/= (const path &rhs)
|
|
|
|
{
|
|
|
|
m_path += preferred_separator + rhs.m_path;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool
|
|
|
|
ns::operator== (const path &a, const path &b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool
|
|
|
|
ns::is_directory (const path &p)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (stat (p.c_str (), &buf))
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::errno_error::throw_code ();
|
2016-11-17 18:36:53 +11:00
|
|
|
|
|
|
|
return S_ISDIR (buf.st_mode);
|
|
|
|
}
|