fixup: add minimal filesystem implementation

Implement the minimum possible surface of std::experimental::filesystem
in terms of POSIX APIs so that we can compile under mingw
This commit is contained in:
Danny Robson 2016-11-17 18:36:53 +11:00
parent cb32823248
commit 6a8b78cdd7
5 changed files with 206 additions and 0 deletions

View File

@ -344,6 +344,13 @@ if HAVE_CAPTURESTACKBACKTRACE
__BACKTRACE_FILES += backtrace_stackwalk.cpp backtrace_win32.cpp
endif
if !HAVE_STD_EXPERIMENTAL_FILESYSTEM
AM_CXXFLAGS += -isystem ${top_srcdir}/fixup
UTIL_FILES += \
fixup/experimental/filesystem.hpp \
fixup/experimental/filesystem.cpp
endif
BACKTRACE_FILES=$(firstword $(__BACKTRACE_FILES) backtrace_null.cpp)

View File

@ -52,6 +52,9 @@ AS_IF([test "x${host_os}" != "xmingw32"],[
AC_FUNC_MMAP
AC_CHECK_HEADERS([experimental/filesystem])
AM_CONDITIONAL([HAVE_STD_EXPERIMENTAL_FILESYSTEM], [test "x$ac_cv_header_experimental_filesystem" == "xyes"])
AC_SEARCH_LIBS([backtrace], [execinfo])
AC_CHECK_FUNC([backtrace])
AM_CONDITIONAL([HAVE_EXECINFO], [test "x$ac_cv_func_backtrace" == "xyes"])

View File

@ -0,0 +1 @@
#include "./filesystem.hpp"

View File

@ -0,0 +1,121 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#include "./filesystem.hpp"
#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))
::util::errno_error::throw_code ();
return S_ISDIR (buf.st_mode);
}

View File

@ -0,0 +1,74 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_FIXUP_EXPERIMENTAL_FILESYSTEM_HPP
#define CRUFT_UTIL_FIXUP_EXPERIMENTAL_FILESYSTEM_HPP
#include <string>
///////////////////////////////////////////////////////////////////////////////
namespace std::experimental::filesystem {
class path {
public:
using value_type = char;
using string_type = std::basic_string<value_type>;
static constexpr value_type preferred_separator = '/';
path ();
path (const path&);
template <class Source>
path (const Source &s):
m_path (s)
{ ; }
template <class InputT>
path (InputT first, InputT last):
m_path (first, last)
{ ; }
std::string string (void) const;
const string_type& native (void) const;
const value_type* c_str (void) const;
path filename (void) const;
path stem (void) const;
path& operator/= (const path&);
private:
string_type m_path;
};
path operator/ (const path&, const path&);
bool operator== (const path&, const path&);
//bool is_directory (file_status);
bool is_directory (const path&);
//bool is_directory (const path&, error_code&);
template <class CharT, class Traits>
std::basic_ostream<CharT,Traits>&
operator<< (std::basic_ostream<CharT,Traits> &os, const path &p)
{ return os << p.native (); }
}
#endif