Merge pull request #668 from michael-loki/remove_cppbackport

Remove cppbackport from vendor dir
pull/674/head
Jeff 5 years ago committed by GitHub
commit 33142d5005
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -246,14 +246,7 @@ else()
set(LIBTUNTAP_SRC ${LIBTUNTAP_SRC_BASE})
endif()
if(NOT WIN32)
add_subdirectory(vendor)
endif()
set(LIBS ${MALLOC_LIB} ${FS_LIB})
if(NOT WIN32)
set(LIBS ${LIBS} uv)
endif()
set(LIBS ${MALLOC_LIB} ${FS_LIB} ${UV_LIB})
add_subdirectory(crypto)
add_subdirectory(libutp)

@ -1,6 +1,8 @@
add_definitions(-DUNIX)
add_definitions(-DPOSIX)
find_library(UV_LIB NAMES uv)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(FS_LIB stdc++fs)
get_filename_component(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-linux.c ABSOLUTE)
@ -16,7 +18,7 @@ elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR ${CMAKE_SYSTEM_NAME} MATCHES "
set(FS_LIB c++experimental)
set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-freebsd.c ${TT_ROOT}/tuntap-unix-bsd.c)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(FS_LIB stdc++fs)
find_library(FS_LIB NAMES c++fs c++experimental stdc++fs)
set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-darwin.c ${TT_ROOT}/tuntap-unix-bsd.c)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-sunos.c)
@ -30,4 +32,3 @@ set(EXE_LIBS ${STATIC_LIB} libutp)
if(RELEASE_MOTTO)
add_definitions(-DLLARP_RELEASE_MOTTO="${RELEASE_MOTTO}")
endif()

@ -56,7 +56,7 @@ target_link_libraries_system(${UTIL_LIB} absl::synchronization absl::hash absl::
# cut back on fluff
if (NOT WIN32)
target_link_libraries_system(${UTIL_LIB} absl::optional absl::variant absl::strings cppbackport)
target_link_libraries_system(${UTIL_LIB} absl::optional absl::variant absl::strings)
endif(NOT WIN32)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

@ -296,11 +296,11 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath)
// write snapp-example.ini
const std::string snappExample_fpath = basepath + "snapp-example.ini";
{
auto f = llarp::util::OpenFileStream< std::ofstream >(snappExample_fpath,
std::ios::binary);
if(!f)
auto stream = llarp::util::OpenFileStream< std::ofstream >(
snappExample_fpath, std::ios::binary);
if(!stream)
return false;
auto &example_f = f.value();
auto &example_f = stream.value();
if(example_f.is_open())
{
// pick ip

@ -1,21 +0,0 @@
set(CXX_COMPAT_SRC
cppbackport-master/lib/fs/rename.cpp
cppbackport-master/lib/fs/filestatus.cpp
cppbackport-master/lib/fs/filetype.cpp
cppbackport-master/lib/fs/cleanpath.cpp
cppbackport-master/lib/fs/perms.cpp
cppbackport-master/lib/fs/equivalent.cpp
cppbackport-master/lib/fs/current_path.cpp
cppbackport-master/lib/fs/basename.cpp
cppbackport-master/lib/fs/tempdir.cpp
cppbackport-master/lib/fs/create_directory.cpp
cppbackport-master/lib/fs/path.cpp
cppbackport-master/lib/fs/remove.cpp
cppbackport-master/lib/fs/diriter.cpp
cppbackport-master/lib/fs/copyfile.cpp
cppbackport-master/lib/fs/absolute.cpp
cppbackport-master/lib/fs/direntry.cpp
)
add_library(cppbackport STATIC ${CXX_COMPAT_SRC})
target_include_directories(cppbackport PUBLIC cppbackport-master/lib)

@ -1,27 +0,0 @@
Copyright (c) 2016, Pollard Banknote Limited
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* 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.
* Neither the name of cppbackport nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
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 HOLDER 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.

@ -1,60 +0,0 @@
This is a backport of the current C++ standard library to C++03/11/14. Obviously not everything
can be backported, but quite a bit can.
Quick Start
===========
There are a couple of small examples to give you an idea of usage. If you have make and g++
you can build things quickly enough:
- clone the repo to /some/place/cppbackport
- make a temp dir /wherever/you/wanna/build
- cd /wherever/you/wanna/build
- make -f /some/place/cppbackport/examples/Makefile
The examples are all setup to compile the cppbackport files as a static library (libcppbackport.a),
and then link it with one of the main example files (ex., gibberish.cpp).
Installation
============
I recommend copying the lib directory into your project, and calling the included Makefile
to create libcppbackport.a. Link that into your project, and add the appropriate include
flags (ex., -iquotecppbackport).
Usage
=====
Basically, #include "filesystem.h" or similar. Then use cpp namespace in place of std namespace.
The headers are all named after their official counterparts, with the addition of the .h
extension.
FAQ
===
**Who is this library suitable for?**
Anyone, really. There are a few use cases:
- The primary use case is to provide some newer features to people who are stuck with/choose to use an older compiler
- Another use would be to soften the requirements of your own project (i.e., so your users can use an older compiler)
- In some cases, this project may provide usable code before compilers support a standard. For example, C++17 at the moment isn't even standardized, *but* we basically know what's in it so we can start supporting it.
**What's the license?**
BSD 3-clause. Use it. Contribute if you like. Don't blame us for things.
**What compiler(s)/platforms are supported**
The development environment is Fedora 24 with GCC 6.1.1. I've used it with earlier versions
of GCC (4.7.4, I think), and a semi-recent version of Clang. I've not tested under Windows, yet.
**Will this use C++11/14/17 if available?**
Yes. Based on the value of the __cplusplus define, the files will simply #include the
system header (as appropriate).
**Why not header-only?**
I actually like the interface/implementation distinction. Header-only implementations (IMO)
get way too large and the files are difficult to navigate. Also, save the compiler some work.
Also, installing a lib isn't really that hard.
**Does it work on Windows?**
Sorry, POSIX mostly. Would love for some Windows devs to help.
**Why not use Boost?**
There's some overlap, but there are differences, too. And none of these things, *individually*
are that big.

@ -1,2 +0,0 @@
// Add predefined macros for your project here. For example:
// #define THE_ANSWER 42

@ -1,144 +0,0 @@
examples/any.cpp
examples/clock.cpp
examples/directory_listing.cpp
examples/fbind.cpp
examples/gibberish.cpp
examples/hash.cpp
examples/histogram.cpp
examples/lshere.cpp
examples/mathfns.cpp
examples/maxalign.cpp
examples/multithreading.cpp
examples/multitype.cpp
examples/now.cpp
examples/optional.cpp
examples/pathiter.cpp
examples/singlell.cpp
examples/smartptr.cpp
examples/tempdir.cpp
examples/testtraits.cpp
examples/two_threads.cpp
lib/algorithm.h
lib/any.h
lib/array.h
lib/atomic.h
lib/chrono.cpp
lib/chrono.h
lib/cmath.cpp
lib/cmath.h
lib/condition_variable.cpp
lib/condition_variable.h
lib/config/arch.h
lib/config/os.h
lib/cstdarg.h
lib/cstddef.h
lib/cstdint.h
lib/cstdlib.h
lib/filesystem.h
lib/forward_list.h
lib/fs/absolute.cpp
lib/fs/absolute.h
lib/fs/basename.cpp
lib/fs/basename.h
lib/fs/cleanpath.cpp
lib/fs/cleanpath.h
lib/fs/copyfile.cpp
lib/fs/copyfile.h
lib/fs/create_directory.cpp
lib/fs/create_directory.h
lib/fs/current_path.cpp
lib/fs/current_path.h
lib/fs/direntry.cpp
lib/fs/direntry.h
lib/fs/diriter.cpp
lib/fs/diriter.h
lib/fs/equivalent.cpp
lib/fs/equivalent.h
lib/fs/filestatus.cpp
lib/fs/filestatus.h
lib/fs/filetype.cpp
lib/fs/filetype.h
lib/fs/path.cpp
lib/fs/path.h
lib/fs/perms.cpp
lib/fs/perms.h
lib/fs/remove.cpp
lib/fs/remove.h
lib/fs/rename.cpp
lib/fs/rename.h
lib/fs/tempdir.cpp
lib/fs/tempdir.h
lib/functional.h
lib/future.h
lib/iterator.h
lib/locale.h
lib/memory.h
lib/mutex.cpp
lib/mutex.h
lib/numeric.h
lib/optional.h
lib/random.cpp
lib/random.h
lib/ratio.h
lib/rvalueref.h
lib/shared_mutex.cpp
lib/shared_mutex.h
lib/string.h
lib/string_view.h
lib/system_error.h
lib/thread.cpp
lib/thread.h
lib/traits/add_cv.h
lib/traits/add_pointer.h
lib/traits/add_reference.h
lib/traits/alignment.h
lib/traits/common_type.h
lib/traits/conditional.h
lib/traits/decay.h
lib/traits/declval.h
lib/traits/enable_if.h
lib/traits/extent.h
lib/traits/integral_constant.h
lib/traits/is_arithmetic.h
lib/traits/is_array.h
lib/traits/is_base_of.h
lib/traits/is_class_or_union.h
lib/traits/is_compound.h
lib/traits/is_const.h
lib/traits/is_convertible.h
lib/traits/is_enum.h
lib/traits/is_floating_point.h
lib/traits/is_function.h
lib/traits/is_fundamental.h
lib/traits/is_integral.h
lib/traits/is_member_pointer.h
lib/traits/is_null_pointer.h
lib/traits/is_object.h
lib/traits/is_pointer.h
lib/traits/is_reference.h
lib/traits/is_same.h
lib/traits/is_scalar.h
lib/traits/is_signed.h
lib/traits/is_unsigned.h
lib/traits/is_void.h
lib/traits/is_volatile.h
lib/traits/make_signed.h
lib/traits/make_unsigned.h
lib/traits/rank.h
lib/traits/remove_cv.h
lib/traits/remove_extent.h
lib/traits/remove_pointer.h
lib/traits/remove_reference.h
lib/traits/result_of.h
lib/traits/underlying_type.h
lib/traits/void_t.h
lib/traits/yesno.h
lib/tuple.h
lib/type_traits.h
lib/unordered_map.h
lib/unordered_set.h
lib/utility.h
lib/variant.h
lib/version.h
LICENSE
README.md

@ -1,56 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CONFIG_ARCH_H
#define PBL_CONFIG_ARCH_H
#include <climits>
/* Whether or not the compiler supports long long and unsigned long long
*/
#ifndef HAS_LONG_LONG
#if __cplusplus >= 201103L
#define HAS_LONG_LONG
#else
#ifdef __GNUG__
#ifdef __SIZEOF_LONG_LONG__
#define HAS_LONG_LONG
#endif
#endif
#endif
#endif
/* The character type that char matches (i.e., signed or unsigned)
*/
#if CHAR_MIN < 0
typedef signed char underlying_char_type;
#else
typedef unsigned char underlying_char_type;
#endif
#endif // PBL_CONFIG_ARCH_H

@ -1,131 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
/** Provide types, macros, and functions for the operating system
*
* This file provides a stable way to work with the OS. It provides
* - namespaces and typedefs that can be used portably between OS's
* - feature macros for enabling specific operating system capabilities
*
* @todo May want to rethink OS_POSIX as feature test macros since posix can
* sorta be in Windows (ex., Cygwin)
*/
#ifndef PBL_CONFIG_OS_H
#define PBL_CONFIG_OS_H
// =============================================================================
// Determine the platform. Ex., Windows or POSIX. Define feature macros
#if ( defined( __unix__ ) || defined( __unix ) || ( defined( __APPLE__ ) && defined( __MACH__ ) ) )
#include <unistd.h>
#ifdef _POSIX_VERSION
// ISO POSIX.1 aka IEEE 1003.1
/// OS_POSIX - unistd.h is included and _POSIX_VERSION is defined
#define OS_POSIX
#if _POSIX_VERSION >= 199506L
// ISO POSIX.1-1996 aka IEEE 1003.1-1996
#define POSIX_THREADS
#if ( _POSIX_VERSION >= 200112L )
// ISO POSIX.1-2001 aka IEEE 1003.1-2001, aka Issue 6, aka SUSv3
#define POSIX_ISSUE_6
#if ( _POSIX_VERSION >= 200809L )
// ISO POSIX.1-2008, aka IEEE 1003.1-2008, aka Issue 7, aka SUSv4
#define POSIX_ISSUE_7
#endif // 2008
#endif // 2001
#endif // 1996
#endif // ifdef _POSIX_VERSION
#endif // if ( defined( __unix__ ) || defined( __unix ) || ( defined( __APPLE__ ) && defined( __MACH__ )))
#if defined( _WIN64 ) || defined( _WIN32 )
#include <windows.h> /* MS Windows operating systems */
#define OS_WINDOWS
#endif
// =============================================================================
// Determine basic types
#ifdef OS_POSIX
#include <sys/types.h>
namespace pbl
{
namespace os
{
// Identifies a user
typedef uid_t user_id_type;
// Identifies a process
typedef pid_t pid_type;
}
}
#else
#ifdef OS_WINDOWS
namespace pbl
{
namespace os
{
typedef DWORD pid_type;
}
}
#endif // Windows
#endif // ifdef OS_POSIX
// =============================================================================
// Determine multithreading types
#ifdef POSIX_THREADS
#include <pthread.h>
namespace pbl
{
namespace os
{
typedef pthread_t thread_type;
typedef pthread_mutex_t mutex_type;
typedef pthread_rwlock_t shared_mutex_type;
typedef pthread_cond_t condition_variable_type;
}
}
#else
#ifdef OS_WINDOWS
namespace pbl
{
namespace os
{
typedef HANDLE thread_type;
typedef CRITICAL_SECTION mutex_type;
}
}
#endif
#endif // ifdef POSIX_THREADS
#endif // PBL_CONFIG_OS_H

@ -1,58 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FILESYSTEM_H
#define PBL_CPP_FILESYSTEM_H
#include "version.h"
#if defined(CPP17) && defined(USE_CXX17_FILESYSTEM)
#include <experimental/filesystem>
#else
// OpenBSD needs this
// MacOS llvm 3.8 needs this
#include "fs/absolute.h"
#include "fs/basename.h"
#include "fs/cleanpath.h"
#include "fs/copyfile.h"
#include "fs/create_directory.h"
#include "fs/current_path.h"
#include "fs/direntry.h"
#include "fs/diriter.h"
#include "fs/equivalent.h"
#include "fs/filestatus.h"
#include "fs/filetype.h"
#include "fs/path.h"
#include "fs/perms.h"
#include "fs/remove.h"
#include "fs/rename.h"
#include "fs/tempdir.h"
#endif // if not cpp17 and not openbsd
#endif // PBL_CPP_FILESYSTEM_H

@ -1,96 +0,0 @@
/* Copyright (c) 2014, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "absolute.h"
#include <climits>
#include <cstdlib>
#include "../config/os.h"
namespace cpp17
{
namespace filesystem
{
path
absolute(const path& filename)
{
if(!filename.empty())
{
#if((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809l) \
|| defined(__GLIBC__)) \
|| defined(ANDROID)
// Preferred - POSIX-2008 and glibc will allocate the path buffer
char* res = ::realpath(filename.c_str(), NULL);
if(res)
{
path s = res;
::free(res);
return s;
}
#else
#ifdef _GNU_SOURCE
// Maybe we can rely on the GNU extension
char* res = ::canonicalize_file_name(filename.c_str());
if(res)
{
std::string s = res;
::free(res);
return s;
}
#elif(((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
|| (defined(_XOPEN_VERSION) && _XOPEN_VERSION >= 500)) \
&& defined(PATH_MAX))
/// @todo PATH_MAX may be huge or -1, according to man pages for
/// realpath
char resolved[PATH_MAX + 1];
char* res = ::realpath(filename.c_str(), resolved);
if(res)
{
return resolved;
}
#else
#error "No way to get absolute file path!"
#endif // if 1
#endif // if ( defined( _POSIX_VERSION ) && _POSIX_VERSION >= 200809l )
}
return path();
}
} // namespace filesystem
} // namespace cpp17

@ -1,50 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_ABSOLUTE_H
#define PBL_CPP_FS_ABSOLUTE_H
#include "path.h"
namespace cpp17
{
namespace filesystem
{
/** Get an absolute path of a file
*
* @param filepath The path of an (existing) file
* @returns An absolute path that corresponds to the same file as filepath, or
* an empty string if there is an error.
*/
path absolute(const path& filepath);
}
}
#endif // PBL_CPP_FS_ABSOLUTE_H

@ -1,207 +0,0 @@
/* Copyright (c) 2014, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "basename.h"
#include "../config/os.h"
#include "cleanpath.h"
namespace
{
// the position following the previous occurrence of c (relative to position
// j), or 0 if not. In this file, the first letter of a path component.
std::size_t
after_last(const std::string& s, char c, std::size_t j)
{
std::size_t i = s.find_last_of(c, j);
return i == std::string::npos ? 0 : i + 1;
}
// (first character, length) of last path component in s or (0,0) for "."
// or (npos, 0) if error
std::pair< std::size_t, std::size_t >
locate_last_path_component(const std::string& s)
{
if(s.empty())
{
// error
return std::pair< std::size_t, std::size_t >(std::string::npos, 0);
}
// j points to the last character in a component
std::size_t j = s.find_last_not_of('/');
unsigned depth = 0;
while(j != std::string::npos)
{
// i points to first character of a component
// i <= j, because j never points to a '/'
const std::size_t i = after_last(s, '/', j);
if(j - i + 1 == 1 && s[i] == '.')
{
// component is ".", basically ignore this component
}
else if(j - i + 1 == 2 && s[i] == '.' && s[i + 1] == '.')
{
// component is "..", ignore the next component
++depth;
}
else
{
// found a "normal" path component
if(depth == 0)
{
return std::pair< std::size_t, std::size_t >(i, j - i + 1);
}
// ..but we're ignoring it
--depth;
}
if(i == 0)
{
if(depth == 0)
{
// "."
return std::pair< std::size_t, std::size_t >(0, 0);
}
else
{
// error, path is malformed
return std::pair< std::size_t, std::size_t >(std::string::npos, 0);
}
}
j = s.find_last_not_of('/', i - 1);
}
// all slashes
return std::pair< std::size_t, std::size_t >(0, 1);
}
std::string
basename_posix(const std::string& s)
{
const std::pair< std::size_t, std::size_t > range =
locate_last_path_component(s);
if(range.first == std::string::npos)
{
return std::string();
}
if(range.first == 0 && range.second == 0)
{
return ".";
}
return s.substr(range.first, range.second);
}
std::string
dirname_posix(const std::string& s)
{
const std::pair< std::size_t, std::size_t > range =
locate_last_path_component(s);
// path was malformed
if(range.first == std::string::npos)
{
return std::string();
}
if(range.first == 0)
{
if(range.second == 0)
{
return "..";
}
// could be '/', or could be a unpathed filename
if(s[0] == '/')
{
return "/";
}
return ".";
}
else
{
// leading directory
return s.substr(0, range.first);
}
}
} // namespace
namespace cpp17
{
namespace filesystem
{
// Calls the basename_xxx appropriate for this platform
std::string
basename(const std::string& s)
{
#ifdef OS_POSIX
return basename_posix(s);
#else
std::string t;
char fname[_MAX_FNAME], ext[_MAX_EXT];
_splitpath(s.c_str(), nullptr, nullptr, fname, ext);
t = fname;
t += ext;
return t;
#endif
}
std::string
dirname(const std::string& s)
{
#ifdef OS_POSIX
const std::string& t = dirname_posix(s);
if(t == ".")
{
return t;
}
return cleanpath(t);
#else
std::string t;
char _dirname[_MAX_DIR];
_splitpath(s.c_str(), nullptr, _dirname, nullptr, nullptr);
t = _dirname;
return t;
#endif
}
} // namespace filesystem
} // namespace cpp17

@ -1,85 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_BASENAME_H
#define PBL_CPP_FS_BASENAME_H
#include <string>
namespace cpp17
{
namespace filesystem
{
/** Return the name of the file system component indicated by path
*
* @param path A file path
* @returns The name of the directory or file that the path points to, or the
* empty string if there's an error
*
* The exact behavior of this function is platform dependent, because paths are.
* However, whatever the platform, it should return the name of the file or
* directory pathed. Ex., on POSIX platforms it should return "readme.txt" for
* "/home/user/documents/readme.txt"; similarly it should return "readme.txt"
* for "C:\Windows\readme.txt" on Windows platforms.
*
* This function does not access the file system. It merely parses the string.
* In particular, it does not check if the path is valid and/or accessible. It
* also does not check that the component before "." or ".." is, in fact, a
* directory.
*
* If the last component cannot be determined for whatever reason, the string is
* considered malformed and the empty string is returned.
*
* @note Although the empty string is considered a malformed path, "." (and
* anything equivalent) will return ".".
*/
std::string basename(const std::string& path);
/** Return the directory which has the last path component
*
* @param path A file path
*
* Determines the last path component as in basename, then returns directory
* which contains that file system object. Ex., the dirname of
* "/home/user/documents/readme.txt" is "/home/user/documents".
*
* As special cases, the dirname of a bare filename is "."; the dirname of
* "." (or equivalent) is ".."; the dirname of the root directory is still the
* root.
*
* If the path is malformed, the empty string is returned.
*
* @sa basename
*/
std::string dirname(const std::string& path);
}
}
#endif // PBL_FS_BASENAME_H

@ -1,144 +0,0 @@
/* Copyright (c) 2014, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "cleanpath.h"
#include <algorithm>
#if defined(_WIN32) && !defined(NOMINMAX)
#define NOMINMAX
#endif
namespace cpp17
{
namespace filesystem
{
/// @todo Probably belongs in path.cpp
std::string
cleanpath(const std::string& s)
{
std::string t;
if(s.empty())
{
// error
return t;
}
const std::size_t n = s.length();
for(std::size_t i = 0; i < n;)
{
if(s[i] == '/')
{
// directory separator
t.push_back('/');
i = s.find_first_not_of('/', i);
}
else
{
// path component
const std::size_t j = std::min(s.find('/', i), n);
if(s.compare(i, j - i, ".", 1) == 0)
{
// handle dot
if(j < n)
{
i = s.find_first_not_of('/', j);
}
else
{
i = n;
}
}
else if(s.compare(i, j - i, "..", 2) == 0)
{
// handle dot-dot
const std::size_t l = t.length();
if(l == 0)
{
// no previous component (ex., "../src")
t.assign("..", 2);
i = j;
}
else
{
// remove previously copied component (unless root)
if(l >= 2)
{
const std::size_t k = t.find_last_of('/', l - 2);
if(k == std::string::npos)
{
t.clear();
}
else
{
t.resize(k + 1);
}
}
if(j < n)
{
i = s.find_first_not_of('/', j);
}
else
{
i = n;
}
}
}
else
{
// append path component
t.append(s, i, j - i);
i = j;
}
}
}
if(t.empty())
{
return ".";
}
// drop trailing slashes
const std::size_t i = t.find_last_not_of('/');
if(i != std::string::npos)
{
t.resize(i + 1);
}
return t;
}
} // namespace filesystem
} // namespace cpp17

@ -1,64 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_CLEANPATH_H
#define PBL_CPP_FS_CLEANPATH_H
#include <string>
namespace cpp17
{
namespace filesystem
{
/** Return a simplified version of path
*
* @param path A file system path
*
* Collapses multiple path separators (ex., as in "/home//user"); replaces
* "name/." with "name" and "parent/child/.." with "parent"; removes
* trailing slashes.
*
* The returned path is equivalent to the original path in the sense that it
* identifies the same file system object. Specifically, relative paths are
* preserved (ex., no simplification is done to the dot-dot in "../here").
*
* If the path is malformed, the empty string is returned. For this
* function, really only the empty string itself is "malformed".
*
* @note This is a textual operation. It does not validate the string
* against the file system or otherwise touch the file system.
* @bug foo/bar/../baz may not point to /foo/baz if bar symlinks to
* flip/flop
*/
std::string
cleanpath(const std::string& path);
} // namespace filesystem
} // namespace cpp17
#endif // PBL_FS_CLEANPATH_H

@ -1,174 +0,0 @@
/* Copyright (c) 2014, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "copyfile.h"
#include <cerrno>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
namespace cpp17
{
namespace filesystem
{
/** Try to do copy the file as safely as possible (esp., gracefully handle
* errors, avoid race conditions).
*
* @bug If there's an error while overwriting a file, the original is lost
*/
bool copy_file(
const path& source,
const path& dest,
copy_options opt
)
{
const int in = ::open(source.c_str(), O_RDONLY | O_CLOEXEC);
if ( in != -1 )
{
struct stat instat;
if ( ::fstat(in, &instat) == 0 && ( S_ISREG(instat.st_mode) || S_ISLNK(instat.st_mode) ) )
{
// Get the destination file
int out = ::open(dest.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, S_IWUSR);
if ( out == -1 && errno == EEXIST )
{
// File already exists -- maybe we will overwrite it
out = ::open(dest.c_str(), O_WRONLY | O_CLOEXEC);
if ( out != -1 )
{
bool err = false;
struct stat outstat;
if ( ( ::fstat(out, &outstat) != 0 ) || ( instat.st_dev == outstat.st_dev && instat.st_ino == outstat.st_ino ) || ( ( opt & 7 ) == 0 ) )
{
// Couldn't stat, Same file, or bad copy_options
err = true;
}
else
{
// Replace file or not, depending on flags
if ( (opt& copy_options::overwrite_existing) || ( (opt& copy_options::update_existing) && ( instat.st_mtime > outstat.st_mtime ) ) )
{
// replace the existing file
if ( ::ftruncate(out, 0) != 0 )
{
err = true;
}
}
else
{
// do nothing
::close(out);
::close(in);
return false;
}
}
if ( err )
{
::close(out);
out = -1;
}
}
}
// Do we have a destination file?
if ( out != -1 )
{
bool err = false;
while ( !err )
{
char buf[4096];
const ssize_t n = ::read( in, buf, sizeof( buf ) );
if ( n == -1 )
{
err = true;
}
else if ( n == 0 )
{
// EOF - copy has been successful
::fchmod(out, instat.st_mode & 0777);
::close(out);
::close(in);
return true;
}
else
{
const char* p = buf;
while ( p - buf < n )
{
const ssize_t m = ::write( out, p, static_cast< std::size_t >( n - ( p - buf ) ) );
if ( m == -1 )
{
err = true;
break;
}
p += m;
}
}
}
// Remove the incomplete file
::unlink( dest.c_str() );
::close(out);
}
}
::close(in);
}
return false;
}
bool copy_file(
const path& source,
const path& dest
)
{
return copy_file(source, dest, ::copy_options::none);
}
}
}

@ -1,81 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_COPYFILE_H
#define PBL_CPP_FS_COPYFILE_H
#include "path.h"
namespace copy_options
{
enum copy_options
{
none = 0,
skip_existing = 1,
overwrite_existing = 2,
update_existing = 4,
recursive = 8,
copy_symlinks = 16,
skip_symlinks = 32,
directories_only = 64,
create_symlinks = 128,
create_hard_links = 256
};
}
namespace cpp17
{
namespace filesystem
{
typedef ::copy_options::copy_options copy_options;
/** Copy the file at source to dest
*
* @param source A file to copy
* @param dest A file (including name) of the file to create
* @returns true iff dest exists and is a copy of source
*
* If source does not exist or is not a file, the copy will fail.
*
* If dest exists, it will be overwritten. Dest will have the same file
* permissions of source, if possible (subject to umask).
*
* This function copies the source file "safely". That is, in the event of an
* error, dest is unaltered (or, if it didn't exist, continues to not exist).
*
* @todo The std::experimental::fs namespace defines copy and copy_file. This
* function should be renamed accordingly.
*/
bool copy_file(const path &source, const path &dest, copy_options);
bool copy_file(const path& source, const path& dest);
}
}
#endif // PBL_CPP_FS_COPYFILE_H

@ -1,77 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "create_directory.h"
#include <sys/stat.h>
#include "perms.h"
#include "filestatus.h"
#ifdef _WIN32
#include <direct.h>
#define mkdir(x, y) mkdir(x)
#endif
namespace cpp17
{
namespace filesystem
{
bool
create_directory(const path& s, __attribute__((unused)) std::error_code& ec)
{
if(s.empty())
{
return false;
}
return ::mkdir(s.c_str(), static_cast< int >(perms::all)) == 0;
}
bool
create_directories(const path& s,
__attribute__((unused)) std::error_code& ec)
{
path p = s.parent_path();
if(!p.empty())
{
std::error_code ec, ec2;
if(!exists(p, ec) && !create_directories(p, ec2))
{
return false;
}
}
std::error_code ec3;
return create_directory(s, ec3);
}
} // namespace filesystem
} // namespace cpp17

@ -1,45 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_CREATE_DIRECTORY_H
#define PBL_CPP_FS_CREATE_DIRECTORY_H
#include "path.h"
#include <system_error>
namespace cpp17
{
namespace filesystem
{
bool create_directory(const path&, std::error_code&);
bool create_directories(const path&, std::error_code&);
}
}
#endif // PBL_CPP_FS_CREATE_DIRECTORY_H

@ -1,83 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "current_path.h"
#include <cerrno>
#include <unistd.h>
namespace cpp17
{
namespace filesystem
{
path
current_path()
{
char buf[4096];
if(::getcwd(buf, sizeof(buf)))
{
return buf;
}
// Dynamically allocate larger buffers until cwd fits
std::size_t size = 2 * sizeof(buf);
while(true)
{
char* q = new char[size];
if(::getcwd(q, size))
{
path p = q;
delete[] q;
return p;
}
else
{
if(errno == ERANGE)
{
delete[] q;
size *= 2;
}
else
{
delete[] q;
break;
}
}
}
return path();
}
} // namespace filesystem
} // namespace cpp17

@ -1,42 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_CURRENT_PATH_H
#define PBL_CPP_FS_CURRENT_PATH_H
#include "path.h"
namespace cpp17
{
namespace filesystem
{
path current_path();
}
}
#endif // PBL_CPP_FS_CURRENT_PATH_H

@ -1,137 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "direntry.h"
namespace cpp17
{
namespace filesystem
{
directory_entry::directory_entry()
{
}
directory_entry::directory_entry(const directory_entry& e)
: path_(e.path_)
{
}
directory_entry::directory_entry(const ::cpp17::filesystem::path& p)
: path_(p)
{
}
directory_entry& directory_entry::operator=(const directory_entry& e)
{
path_ = e.path_;
return *this;
}
void directory_entry::assign(const ::cpp17::filesystem::path& p)
{
path_ = p;
}
void directory_entry::replace_filename(const ::cpp17::filesystem::path& p)
{
path_ = path_.parent_path() / p;
}
const path& directory_entry::path() const
{
return path_;
}
directory_entry::operator const ::cpp17::filesystem::path&() const
{
return path_;
}
file_status directory_entry::status() const
{
return ::cpp17::filesystem::status(path_);
}
file_status directory_entry::symlink_status() const
{
return ::cpp17::filesystem::symlink_status(path_);
}
bool operator==(
const directory_entry& a,
const directory_entry& b
)
{
return a.path() == b.path();
}
bool operator!=(
const directory_entry& a,
const directory_entry& b
)
{
return a.path() != b.path();
}
bool operator<(
const directory_entry& a,
const directory_entry& b
)
{
return a.path() < b.path();
}
bool operator<=(
const directory_entry& a,
const directory_entry& b
)
{
return a.path() <= b.path();
}
bool operator>(
const directory_entry& a,
const directory_entry& b
)
{
return a.path() > b.path();
}
bool operator>=(
const directory_entry& a,
const directory_entry& b
)
{
return a.path() >= b.path();
}
}
}

@ -1,71 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_DIRENTRY_H
#define PBL_CPP_FS_DIRENTRY_H
#include <iosfwd>
#include "path.h"
#include "filestatus.h"
namespace cpp17
{
namespace filesystem
{
/** Reperesents a file system object
*/
class directory_entry
{
public:
directory_entry();
directory_entry(const directory_entry&);
explicit directory_entry(const path&);
directory_entry& operator=(const directory_entry&);
void assign(const path&);
void replace_filename(const path&);
const ::cpp17::filesystem::path& path() const;
operator const ::cpp17::filesystem::path&() const;
file_status status() const;
file_status symlink_status() const;
private:
::cpp17::filesystem::path path_;
};
bool operator==(const directory_entry&, const directory_entry&);
bool operator!=(const directory_entry&, const directory_entry&);
bool operator<(const directory_entry&, const directory_entry&);
bool operator<=(const directory_entry&, const directory_entry&);
bool operator>(const directory_entry&, const directory_entry&);
bool operator>=(const directory_entry&, const directory_entry&);
}
}
#endif // PBL_FS_DIRENTRY_H

@ -1,502 +0,0 @@
/* Copyright (c) 2014, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "diriter.h"
#include <cstdlib>
#include <cstring>
#include <climits>
#include <dirent.h>
#ifdef _WIN32
#include <io.h>
#endif
#include "direntry.h"
#include "path.h"
namespace cpp17
{
namespace filesystem
{
/// @todo Could save the malloc/free (of e) if we mark end-of-directory
class directory_iterator::impl
{
public:
impl() : p(), d(0), e(0), info(), valid_info(false)
{
// printf("directory_iterator::impl::cstr\n");
pos = 0;
}
explicit impl(const path& path_)
: p(path_), d(0), e(0), info(), valid_info(false)
{
// printf("directory_iterator::impl::cstr path[%s]\n", path_.c_str());
this->cPath = path_;
d = ::opendir(path_.c_str());
if(d)
{
e = acquire();
next();
}
}
~impl()
{
// printf("directory_iterator::impl::dstr [%s]\n", this->cPath.c_str());
if(d)
{
release(); // free e
::closedir(d);
d = 0;
}
}
void
rewind()
{
// printf("directory_iterator::impl::rewind\n");
if(d)
{
release();
::closedir(d);
}
d = ::opendir(this->cPath.c_str());
pos = 0;
if(d)
{
e = acquire();
next();
}
}
void
seek(size_t seekPos)
{
// printf("directory_iterator::impl::seek\n");
this->rewind();
for(size_t i = 0; i < seekPos; i++)
{
this->next();
}
}
/** Test if this is an end iterator
*/
bool
is_end() const
{
// printf("directory_iterator::impl::is_end\n");
return !e;
}
bool
next()
{
// printf("directory_iterator::impl::next\n");
if(d)
{
valid_info = false;
do
{
dirent* ptr = 0;
const int res = ::readdir_r(d, e, &ptr);
if(res != 0 || ptr == 0)
{
// error, or end of directory
pos = 0;
release();
return false;
}
} while(std::strcmp(e->d_name, ".") == 0
|| std::strcmp(e->d_name, "..") == 0);
pos++;
return true;
}
return false;
}
path
get_path() const
{
// printf("directory_iterator::impl::get_path\n");
return e ? (p / e->d_name) : path();
}
file_type
type() const
{
// printf("directory_iterator::impl::type\n");
if(e)
{
#ifdef DT_BLK
switch(e->d_type)
{
case DT_FIFO:
return file_type::fifo;
case DT_CHR:
return file_type::character;
case DT_DIR:
return file_type::directory;
case DT_BLK:
return file_type::block;
case DT_REG:
return file_type::regular;
case DT_LNK:
return file_type::symlink;
case DT_SOCK:
return file_type::socket;
default:
break;
} // switch
#endif
}
return file_type::unknown;
}
const directory_entry&
get_reference()
{
// printf("directory_iterator::impl::get_reference\n");
update();
return info;
}
const directory_entry*
get_pointer()
{
// printf("directory_iterator::impl::get_pointer\n");
update();
return &info;
}
/// Position
size_t pos;
// Path to directory
path p;
private:
void
update()
{
// printf("directory_iterator::impl::update\n");
if(!valid_info && e)
{
const path q = p / e->d_name;
info.assign(q);
valid_info = true;
}
}
static dirent*
acquire()
{
// printf("directory_iterator::impl::acquire\n");
/* dirent::d_name is required to be at least NAME_MAX + 1 bytes.
* However, some implementations use the struct hack and make d_name
* 1 byte. Watch for this scenario and adjust accordingly.
*/
if(sizeof(static_cast< dirent* >(0)->d_name) < NAME_MAX + 1)
{
return static_cast< dirent* >(
::malloc(sizeof(dirent) + NAME_MAX + 1));
}
else
{
return static_cast< dirent* >(::malloc(sizeof(dirent)));
}
}
void
release()
{
// printf("directory_iterator::impl::release\n");
::free(e);
e = 0;
}
// constructor path
path cPath;
/// Platform information
DIR* d;
/// Platform information
dirent* e;
directory_entry info;
bool valid_info; // info has been populated
};
directory_iterator::directory_iterator() : pimpl(new impl)
{
}
directory_iterator::directory_iterator(const path& path_)
: pimpl(new impl(path_))
{
}
// is this right
directory_iterator::directory_iterator(directory_iterator const& src)
: pimpl(new impl(src.pimpl->p.c_str()))
{
pimpl->seek(src.pimpl->pos);
}
directory_iterator::~directory_iterator()
{
delete pimpl;
}
directory_iterator::directory_iterator(directory_iterator const& src,
int pos)
{
// printf("directory_iterator::directory_iterator copy at pos - pimpl[%x]
// from[%s]\n", pimpl, src->path().c_str());
pimpl = new impl(src.pimpl->p.c_str());
pimpl->seek(pos);
}
bool
directory_iterator::operator==(const directory_iterator& i) const
{
// only equal if both are end iterators
return pimpl->is_end() && i.pimpl->is_end();
}
bool
directory_iterator::operator!=(const directory_iterator& i) const
{
return !(pimpl->is_end() && i.pimpl->is_end());
}
directory_iterator&
directory_iterator::operator++()
{
pimpl->next();
return *this;
}
const directory_entry& directory_iterator::operator*() const
{
return pimpl->get_reference();
}
const directory_entry* directory_iterator::operator->() const
{
return pimpl->get_pointer();
}
file_type
directory_iterator::type() const
{
return pimpl->type();
}
void
directory_iterator::swap(directory_iterator& d)
{
std::swap(pimpl, d.pimpl);
}
// FIXME: this isn't right
directory_iterator
directory_iterator::begin()
{
return directory_iterator(*this, 0);
/*
printf("directory_iterator::directory_iterator::begin - start\n");
size_t cur = pimpl->pos; // backup state
printf("directory_iterator::directory_iterator::begin - was at
[%zu][%s]\n", cur, this->pimpl->get_path().c_str()); pimpl->rewind();
directory_iterator *ret = new directory_iterator(*this); // copy ourself
pimpl->seek(cur); // restore state
printf("directory_iterator::directory_iterator::begin - end\n");
return *ret;
*/
}
directory_iterator
directory_iterator::end()
{
// return *(directory_iterator *)endPtr;
// printf("directory_iterator::directory_iterator::end - start\n");
size_t cur = pimpl->pos; // backup state
// printf("directory_iterator::directory_iterator::end - was at
// [%zu][%s]\n", cur, this->pimpl->get_path().c_str());
// spool to the end
size_t finalPos = pimpl->pos;
while(!pimpl->is_end())
{
if(pimpl->next())
{
finalPos = pimpl->pos;
}
}
// directory_iterator *ret = new directory_iterator(*this); // copy
// ourself printf("directory_iterator::directory_iterator::end - now at
// [%zu][%s]\n", pimpl->pos, this->pimpl->get_path().c_str());
pimpl->seek(cur); // restore state
return directory_iterator(*this, finalPos);
// printf("directory_iterator::directory_iterator::end - end\n");
// return *ret;
}
//
// recursive_directory_iterator
//
recursive_directory_iterator::recursive_directory_iterator()
{
}
recursive_directory_iterator::recursive_directory_iterator(const path& p)
{
descend(p);
}
recursive_directory_iterator::~recursive_directory_iterator()
{
while(!stack.empty())
{
ascend();
}
}
bool
recursive_directory_iterator::descend(const path& p)
{
directory_iterator it(p), end;
if(it != end)
{
directory_iterator* jt = new directory_iterator();
jt->swap(it);
stack.push(jt);
return true;
}
return false;
}
void
recursive_directory_iterator::ascend()
{
delete stack.top();
stack.pop();
}
const directory_entry& recursive_directory_iterator::operator*() const
{
return *(*stack.top());
}
const directory_entry* recursive_directory_iterator::operator->() const
{
return stack.top()->operator->();
}
recursive_directory_iterator&
recursive_directory_iterator::operator++()
{
if(stack.top()->type() == file_type::directory)
{
// go to directory's first child (if any)
if(descend((*stack.top())->path()))
{
return *this;
}
}
do
{
// move to sibling
stack.top()->operator++();
directory_iterator end;
if(*(stack.top()) != end)
{
return *this;
}
// move to parent
ascend();
} while(!stack.empty());
return *this;
}
bool
recursive_directory_iterator::operator==(
const recursive_directory_iterator& i) const
{
return stack.empty() && i.stack.empty();
}
bool
recursive_directory_iterator::operator!=(
const recursive_directory_iterator& i) const
{
return !(stack.empty() && i.stack.empty());
}
} // namespace filesystem
} // namespace cpp17

@ -1,151 +0,0 @@
/* Copyright (c) 2014, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_DIRITER_H
#define PBL_CPP_FS_DIRITER_H
#include <stack>
#include "direntry.h"
namespace cpp17
{
namespace filesystem
{
/** An iterator for traversing directory contents
*
* This class can be used for iterating over the contents of a directory. The
* parameterized constructor will make a "begin" iterator, and the default
* constructor will make an "end" iterator. One can dereference an iterator
* to get a directory_entry describing the file/subdirectory.
*
* @note The "." and ".." 'files' are ignored by this iterator, since they are
* not 'in' the directory.
*
* @note These objects are not copyable
*
* @note Only end iterators are ever considered equal. In particular, two
* directory_iterator-s constructed with the same argument may not point to the
* same file.
*/
class directory_iterator
{
public:
/** Construct an iterator for a directory
*
* After construction, the iterator will point to the first file in the
* directory.
*/
explicit directory_iterator(const path& path_);
/** Construct an end iterator
*/
directory_iterator();
/** Destructor
*/
~directory_iterator();
directory_iterator begin();
directory_iterator end();
/** Test if two iterators are equal
*
* Only two end iterators are equal
*/
bool operator==(const directory_iterator& i) const;
/** Test if two iterators are not equal
*
* Only two end iterators are equal
*/
bool operator!=(const directory_iterator& i) const;
/** Move to the next file/subdirectory
*/
directory_iterator& operator++();
/** Get information for the file system object
*
* @note Causes a stat. If all you need is the type of the file system object,
* use the cheaper type() member
*/
const directory_entry& operator*() const;
/** Get information for the file system object
*
* @note Causes a stat. If all you need is the type of the file system object,
* use the cheaper type() member
*/
const directory_entry* operator->() const;
file_type type() const;
void swap(directory_iterator&);
// non-copyable
// now it is
directory_iterator(const directory_iterator&);
directory_iterator& operator=(const directory_iterator&);
directory_iterator(const directory_iterator&, int pos);
//void *endPtr;
private:
class impl;
impl* pimpl;
};
class recursive_directory_iterator
{
public:
recursive_directory_iterator();
explicit recursive_directory_iterator(const path& p);
~recursive_directory_iterator();
recursive_directory_iterator& operator++();
const directory_entry& operator*() const;
const directory_entry* operator->() const;
bool operator==(const recursive_directory_iterator& i) const;
bool operator!=(const recursive_directory_iterator& i) const;
private:
// non-copyable
recursive_directory_iterator(const recursive_directory_iterator&);
recursive_directory_iterator& operator=(const recursive_directory_iterator&);
bool descend(const path& p);
void ascend();
std::stack< directory_iterator* > stack;
};
}
}
#endif // PBL_FS_DIRITER_H

@ -1,60 +0,0 @@
/* Copyright (c) 2018, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "equivalent.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace cpp17
{
namespace filesystem
{
bool equivalent(
const path& p1,
const path& p2
)
{
struct stat s1;
if ( ::stat(p1.c_str(), &s1) == 0 )
{
struct stat s2;
if ( ::stat(p2.c_str(), &s2) == 0 )
{
return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
}
}
return false;
}
}
}

@ -1,42 +0,0 @@
/* Copyright (c) 2018, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_EQUIVALENT_H
#define PBL_CPP_FS_EQUIVALENT_H
#include "path.h"
namespace cpp17
{
namespace filesystem
{
bool equivalent(const path&, const path&);
}
}
#endif // PBL_CPP_FS_EQUIVALENT_H

@ -1,303 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "filestatus.h"
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "path.h"
namespace
{
::cpp17::filesystem::file_status
from_mode_t(mode_t m)
{
::cpp17::filesystem::perms p =
static_cast< cpp17::filesystem::perms >(m & 0xFFF);
::cpp17::filesystem::file_type t = file_type::unknown;
if(S_ISREG(m))
{
t = file_type::regular;
}
else if(S_ISDIR(m))
{
t = file_type::directory;
}
else if(S_ISCHR(m))
{
t = file_type::character;
}
else if(S_ISBLK(m))
{
t = file_type::block;
}
else if(S_ISFIFO(m))
{
t = file_type::fifo;
}
#ifndef _WIN32 // these only work on cygnus or msys2!
else if(S_ISLNK(m))
{
t = file_type::symlink;
}
else if(S_ISSOCK(m))
{
t = file_type::socket;
}
#endif
return ::cpp17::filesystem::file_status(t, p);
}
} // namespace
namespace cpp17
{
namespace filesystem
{
file_status::file_status(const file_status& s) : t(s.t), p(s.p)
{
}
file_status::file_status(file_type t_, perms p_) : t(t_), p(p_)
{
}
file_status&
file_status::operator=(const file_status& s)
{
t = s.t;
p = s.p;
return *this;
}
file_type
file_status::type() const
{
return t;
}
void
file_status::type(file_type t_)
{
t = t_;
}
perms
file_status::permissions() const
{
return p;
}
void
file_status::permissions(perms p_)
{
p = p_;
}
std::ostream&
operator<<(std::ostream& os, const file_status& fs)
{
os << fs.type() << "; " << fs.permissions();
return os;
}
file_status
status(const path& path_)
{
if(!path_.empty())
{
struct stat st;
if(::stat(path_.c_str(), &st) == 0)
{
return from_mode_t(st.st_mode);
}
}
return file_status();
}
file_status
symlink_status(const path& path_)
{
if(!path_.empty())
{
struct stat st;
#ifndef _WIN32
if(::lstat(path_.c_str(), &st) == 0)
#else
if(::stat(path_.c_str(), &st) == 0)
#endif
{
return from_mode_t(st.st_mode);
}
}
return file_status();
}
bool
status_known(file_status s)
{
return s.type() != file_type::none;
}
bool
exists(file_status s)
{
return status_known(s) && s.type() != file_type::not_found;
}
bool
exists(const path& p, __attribute__((unused)) std::error_code& ec)
{
return exists(status(p));
}
bool
is_block_file(file_status s)
{
return s.type() == file_type::block;
}
bool
is_block_file(const path& p)
{
return is_block_file(status(p));
}
bool
is_character_file(file_status s)
{
return s.type() == file_type::character;
}
bool
is_character_file(const path& p)
{
return is_character_file(status(p));
}
bool
is_fifo(file_status s)
{
return s.type() == file_type::fifo;
}
bool
is_fifo(const path& p)
{
return is_fifo(status(p));
}
bool
is_other(file_status s)
{
return exists(s) && !is_regular_file(s) && !is_directory(s)
&& !is_symlink(s);
}
bool
is_other(const path& p)
{
return is_other(status(p));
}
bool
is_regular_file(file_status s)
{
return s.type() == file_type::regular;
}
bool
is_regular_file(const path& p)
{
return is_regular_file(status(p));
}
bool
is_socket(file_status s)
{
return s.type() == file_type::socket;
}
bool
is_socket(const path& p)
{
return is_socket(status(p));
}
bool
is_symlink(file_status s)
{
return s.type() == file_type::symlink;
}
bool
is_symlink(const path& p)
{
return is_symlink(status(p));
}
bool
is_directory(file_status s)
{
return s.type() == file_type::directory;
}
bool
is_directory(const path& p)
{
return is_directory(status(p));
}
std::size_t
file_size(const path& p)
{
if(!p.empty())
{
struct stat st;
if(::stat(p.c_str(), &st) == 0)
{
return st.st_size;
}
}
return std::size_t(-1);
}
} // namespace filesystem
} // namespace cpp17

@ -1,92 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_FILESTATUS_H
#define PBL_CPP_FS_FILESTATUS_H
#include <iosfwd>
#include <system_error>
#include "filetype.h"
#include "perms.h"
namespace cpp17
{
namespace filesystem
{
class path;
/** Information about a file's type and permissions
*
* Due to the nature of the file system, the information may not be exactly
* current.
*/
class file_status
{
public:
file_status(const file_status&);
explicit file_status(file_type = file_type::none, perms = perms::unknown);
file_status& operator=(const file_status&);
file_type type() const;
void type(file_type);
perms permissions() const;
void permissions(perms);
private:
file_type t;
perms p;
};
file_status status(const path&);
file_status symlink_status(const path&);
bool status_known(file_status);
bool exists(file_status);
bool exists(const path&, std::error_code&);
bool is_block_file(file_status);
bool is_block_file(const path&);
bool is_character_file(file_status);
bool is_character_file(const path&);
bool is_fifo(file_status);
bool is_fifo(const path&);
bool is_other(file_status);
bool is_other(const path&);
bool is_regular_file(file_status);
bool is_regular_file(const path&);
bool is_socket(file_status);
bool is_socket(const path&);
bool is_symlink(file_status);
bool is_symlink(const path&);
bool is_directory(file_status);
bool is_directory(const path&);
std::size_t file_size(const path&);
std::ostream& operator<<(std::ostream&, const file_status&);
}
}
#endif // PBL_CPP_FS_FILESTATUS_H

@ -1,59 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "filetype.h"
#include <iostream>
static const char* const friendly[] =
{
"Not found", "None", "Regular File", "Directory", "Symbolic Link",
"Block Device", "Character Device", "FIFO", "Socket"
};
namespace file_type
{
std::ostream& operator<<(
std::ostream& os,
file_type t
)
{
if ( -1 <= t && t < 8 )
{
os << friendly[t + 1];
}
else
{
os << "Unknown";
}
return os;
}
}

@ -1,54 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_FILETYPE_H
#define PBL_CPP_FS_FILETYPE_H
#include <iosfwd>
namespace file_type
{
enum file_type
{
not_found = -1, none = 0, regular, directory, symlink, block, character, fifo,
socket, unknown
};
std::ostream& operator<<(std::ostream&, file_type);
}
namespace cpp17
{
namespace filesystem
{
/** Types of file system objects
*/
typedef ::file_type::file_type file_type;
}
}
#endif // PBL_CPP_FS_FILETYPE_H

@ -1,698 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "path.h"
#include <algorithm>
namespace cpp17
{
namespace filesystem
{
path::path()
{
}
path::path(const string_type& s_) : s(s_)
{
}
path::path(const char* s_) : s(s_)
{
}
path::path(const path& p) : s(p.s)
{
}
path&
path::operator=(const path& p)
{
s = p.s;
return *this;
}
void
path::clear()
{
s.clear();
}
void
path::swap(path& p)
{
s.swap(p.s);
}
std::string
path::string() const
{
return s;
}
const char*
path::c_str() const
{
return s.c_str();
}
bool
path::empty() const
{
return s.empty();
}
path
path::extension() const
{
const std::size_t i = s.find_last_of(preferred_separator);
const std::size_t j = (i == std::string::npos ? 0 : i + 1);
if(j < s.length())
{
if(s.compare(j, std::string::npos, ".", 1) != 0
&& s.compare(j, std::string::npos, "..", 2) != 0)
{
const std::size_t k = s.find_last_of('.');
if(k != std::string::npos && j <= k)
{
return s.substr(k);
}
}
}
return path();
}
path
path::filename() const
{
const std::size_t i = s.find_last_of(preferred_separator);
if(i == std::string::npos)
{
return *this;
}
if(i + 1 < s.length())
{
return path(s.substr(i + 1));
}
else
{
return ".";
}
}
path
path::parent_path() const
{
const std::size_t i = s.find_last_of(preferred_separator);
if(i == std::string::npos)
{
return path();
}
return path(s.substr(0, i));
}
path&
path::remove_filename()
{
const std::size_t i = s.find_last_of(preferred_separator);
if(i != std::string::npos)
{
s.resize(i);
}
return *this;
}
path&
path::replace_filename(const path& p)
{
const std::size_t i = s.find_last_of(preferred_separator);
if(i != std::string::npos)
{
s.resize(i);
append(p);
}
else
{
assign(p);
}
return *this;
}
path::operator string_type() const
{
return s;
}
path&
path::append(const path& p)
{
bool sep = true;
if(!s.empty() && s[s.length() - 1] == preferred_separator)
{
sep = false;
}
else if(s.empty())
{
sep = false;
}
else if(p.empty() || p.s[0] == preferred_separator)
{
sep = false;
}
if(sep)
{
s += preferred_separator;
}
s.append(p.native());
return *this;
}
path&
path::operator/=(const path& p)
{
return append(p);
}
const std::string&
path::native() const
{
return s;
}
path
path::lexically_relative(const path& p) const
{
const_iterator first1 = begin(), last1 = end();
const_iterator first2 = p.begin(), last2 = p.end();
const_iterator it = first1;
const_iterator jt = first2;
while(it != last1 && jt != last2 && *it == *jt)
{
++it, ++jt;
}
if(it == first1 || jt == first2)
{
return path();
}
if(it == last1 && jt == last2)
{
return path(".");
}
path r;
for(; jt != last2; ++jt)
{
r /= "..";
}
for(; it != last1; ++it)
{
r /= *it;
}
return r;
}
bool
path::is_absolute() const
{
return !s.empty() && s[0] == preferred_separator;
}
path
path::lexically_normal() const
{
std::string t;
if(s.empty())
{
// error
return t;
}
const std::size_t n = s.length();
for(std::size_t i = 0; i < n;)
{
if(s[i] == '/')
{
// directory separator
t.push_back('/');
i = s.find_first_not_of('/', i);
}
else
{
// path component
const std::size_t j = std::min(s.find('/', i), n);
if(s.compare(i, j - i, ".", 1) == 0)
{
// handle dot
if(j < n)
{
i = s.find_first_not_of('/', j);
}
else
{
i = n;
}
}
else if(s.compare(i, j - i, "..", 2) == 0)
{
// handle dot-dot
const std::size_t l = t.length();
if(l == 0)
{
// no previous component (ex., "../src")
t.assign("..", 2);
i = j;
}
else
{
// remove previously copied component (unless root)
if(l >= 2)
{
const std::size_t k = t.find_last_of('/', l - 2);
if(k == std::string::npos)
{
t.clear();
}
else
{
t.resize(k + 1);
}
}
if(j < n)
{
i = s.find_first_not_of('/', j);
}
else
{
i = n;
}
}
}
else
{
// append path component
t.append(s, i, j - i);
i = j;
}
}
}
if(t.empty())
{
return ".";
}
// drop trailing slashes
const std::size_t i = t.find_last_not_of('/');
if(i != std::string::npos)
{
t.resize(i + 1);
}
return t;
}
int
path::compare(const path& p) const
{
const_iterator first1 = begin(), last1 = end();
const_iterator first2 = p.begin(), last2 = p.end();
while(first1 != last1 && first2 != last2)
{
if(first1->native() < first2->native())
{
return -1;
}
if(first2->native() < first1->native())
{
return 1;
}
++first1;
++first2;
}
if(first1 != last1)
{
return 1;
}
if(first2 != last2)
{
return -1;
}
return 0;
}
path::const_iterator
path::begin() const
{
begin_iterator_tag tag;
return const_iterator(this, tag);
}
path::const_iterator
path::end() const
{
end_iterator_tag tag;
return const_iterator(this, tag);
}
path::const_iterator::const_iterator()
: parent(0), first(std::string::npos), last(std::string::npos), value()
{
}
path::const_iterator::const_iterator(const path* parent_,
begin_iterator_tag)
: parent(parent_), first(0)
{
if(parent->s.empty())
{
first = std::string::npos;
last = std::string::npos;
}
else
{
const std::size_t k = parent->s.find_first_of(preferred_separator);
if(k == std::string::npos)
{
last = parent->s.length();
}
else if(k == 0)
{
last = 1;
}
else
{
last = k;
}
value = parent->s.substr(0, last);
}
}
path::const_iterator::const_iterator(const path* p, end_iterator_tag)
: parent(p), first(std::string::npos), last(std::string::npos), value()
{
}
path::const_iterator&
path::const_iterator::operator++()
{
if(parent)
{
if(last < parent->s.length())
{
// find next component
const std::size_t j =
parent->s.find_first_not_of(preferred_separator, last);
if(j == std::string::npos)
{
if(parent->s[first] == preferred_separator)
{
// path to root
first = std::string::npos;
last = std::string::npos;
value.clear();
}
else
{
// ends with directory
first = parent->s.length();
last = std::string::npos;
value = ".";
}
}
else
{
first = j;
// next path component
const std::size_t k =
parent->s.find_first_of(preferred_separator, j);
if(k == std::string::npos)
{
last = parent->s.length();
}
else
{
last = k;
}
value = parent->s.substr(first, last - first);
}
}
else
{
// go to end iterator
first = std::string::npos;
last = std::string::npos;
value.clear();
}
}
return *this;
}
path::const_iterator
path::const_iterator::operator++(int)
{
const_iterator t = *this;
operator++();
return t;
}
path::const_iterator&
path::const_iterator::operator--()
{
if(parent && first != 0)
{
if(first == std::string::npos)
{
// end iterator
if(!parent->s.empty())
{
const std::size_t j = parent->s.find_last_of(preferred_separator);
if(j == std::string::npos)
{
// single path component
first = 0;
last = parent->s.length();
value = parent->s;
}
else
{
if(j + 1 < parent->s.length())
{
// trailing filename
first = j + 1;
last = parent->s.length();
value = parent->s.substr(first, last - first);
}
else
{
// trailing directory, or root
const std::size_t k =
parent->s.find_last_not_of(preferred_separator, j);
if(k == std::string::npos)
{
first = 0;
last = 1;
value = parent->s.substr(first, last - first);
}
else
{
first = parent->s.length();
last = std::string::npos;
value = ".";
}
}
}
}
}
else
{
// on an actual path component, or the fake "."
const std::size_t j =
parent->s.find_last_not_of(preferred_separator, first - 1);
if(j == std::string::npos)
{
// beginning of absolute path
first = 0;
last = 1;
}
else
{
last = j + 1;
const std::size_t k =
parent->s.find_last_of(preferred_separator, j);
if(k == std::string::npos)
{
first = 0;
}
else
{
first = k + 1;
}
}
value = parent->s.substr(first, last - first);
}
}
return *this;
}
path::const_iterator
path::const_iterator::operator--(int)
{
const_iterator t = *this;
operator--();
return t;
}
bool
path::const_iterator::operator==(const const_iterator& o) const
{
return parent == o.parent && first == o.first;
}
bool
path::const_iterator::operator!=(const const_iterator& o) const
{
return parent != o.parent || first != o.first;
}
const path& path::const_iterator::operator*() const
{
return value;
}
const path* path::const_iterator::operator->() const
{
return &value;
}
path
operator/(const path& lhs, const path& rhs)
{
path res = lhs;
res.append(rhs);
return res;
}
std::ostream&
operator<<(std::ostream& os, const path& p)
{
return os << p.string();
}
bool
operator==(const path& l, const path& r)
{
return l.compare(r) == 0;
}
bool
operator!=(const path& l, const path& r)
{
return l.compare(r) != 0;
}
bool
operator<(const path& l, const path& r)
{
return l.compare(r) < 0;
}
bool
operator<=(const path& l, const path& r)
{
return l.compare(r) <= 0;
}
bool
operator>(const path& l, const path& r)
{
return l.compare(r) > 0;
}
bool
operator>=(const path& l, const path& r)
{
return l.compare(r) >= 0;
}
} // namespace filesystem
} // namespace cpp17

@ -1,191 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_PATH_H
#define PBL_CPP_FS_PATH_H
#include <string>
#include <iosfwd>
#include <iterator>
#include <iostream>
namespace cpp17
{
namespace filesystem
{
/** A file path
*
* A partial implementation of std::experimental::filesystem::path
* @todo Construct from the other basic_string types and pointer-to-char16_t, etc.
* @bug Throughout, preferred_separator is used incorrectly
*/
class path
{
public:
typedef char value_type;
typedef std::basic_string< value_type > string_type;
static const value_type preferred_separator = '/';
class const_iterator;
typedef const_iterator iterator;
/// An empty path
path();
/// Construct a path form the given string
path(const string_type&);
path(const char*);
/// Copy constructor
path(const path&);
/// Copy assignment
path& operator=(const path&);
template< typename Source >
path& operator=(const Source& source)
{
return assign(source);
}
template< typename Source >
path& assign(const Source& source)
{
path t(source);
swap(t);
return *this;
}
template< typename Iterator >
path& assign(
Iterator first,
Iterator last
)
{
path t(first, last);
swap(t);
return *this;
}
/// Clear the path
void clear();
/// Swap the two paths
void swap(path&);
std::string string() const;
const char* c_str() const;
path& operator/=(const path&);
path& append(const path&);
/// Is the path empty (i.e., "")
bool empty() const;
const std::string& native() const;
path extension() const;
path filename() const;
path parent_path() const;
path& remove_filename();
path& replace_filename(const path&);
operator string_type() const;
bool is_absolute() const;
path lexically_normal() const;
path lexically_relative(const path&) const;
int compare(const path&) const;
const_iterator begin() const;
const_iterator end() const;
private:
struct begin_iterator_tag {};
struct end_iterator_tag {};
std::string s;
};
class path::const_iterator
{
public:
typedef const path value_type;
typedef const path& reference;
typedef const path* pointer;
typedef std::ptrdiff_t difference_type;
const_iterator();
const_iterator(const path*, begin_iterator_tag);
const_iterator(const path*, end_iterator_tag);
const_iterator& operator++();
const_iterator operator++(int);
const_iterator& operator--();
const_iterator operator--(int);
bool operator==(const const_iterator&) const;
bool operator!=(const const_iterator&) const;
const path& operator*() const;
const path* operator->() const;
private:
const path* parent;
std::size_t first;
std::size_t last;
path value;
};
path operator/(const path& lhs, const path& rhs);
std::ostream& operator<<(std::ostream&, const path&);
bool operator==(const path&, const path&);
bool operator!=(const path&, const path&);
bool operator<(const path&, const path&);
bool operator<=(const path&, const path&);
bool operator>(const path&, const path&);
bool operator>=(const path&, const path&);
}
}
namespace std
{
template< >
struct iterator_traits< ::cpp17::filesystem::path::const_iterator >
{
typedef std::ptrdiff_t difference_type;
typedef const ::cpp17::filesystem::path value_type;
typedef const ::cpp17::filesystem::path* pointer;
typedef const ::cpp17::filesystem::path& reference;
typedef std::bidirectional_iterator_tag iterator_category;
};
}
#endif // PBL_FS_PATH_H

@ -1,48 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "perms.h"
#include <iostream>
#include <iomanip>
namespace perms
{
std::ostream& operator<<(
std::ostream& os,
perms p
)
{
std::ostream t( os.rdbuf() );
t << "mode: " << std::oct << static_cast< int >( p );
return os;
}
}

@ -1,101 +0,0 @@
/* Copyright (c) 2015, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_PERMS_H
#define PBL_CPP_FS_PERMS_H
#include <iosfwd>
namespace perms
{
/** File permissions
*
* See: std::experimental::filesystem::perms
*/
enum perms
{
none = 0,
owner_read = 0400, // S_IRUSR
owner_write = 0200, // S_IWUSR
owner_exec = 0100, // S_IXUSR
owner_all = 0700, // S_IRWXU
group_read = 040, // S_IRGRP
group_write = 020, // S_IWGRP
group_exec = 010, // S_IXGRP
group_all = 070, // S_IRWXG
others_read = 04, // S_IROTH
others_write = 02, // S_IWOTH
others_exec = 01, // S_IXOTH
others_all = 07, // S_IRWXO
all = 0777,
set_uid = 04000,
set_gid = 02000,
sticky_bit = 01000,
mask = 07777,
unknown = 0xffff,
add_perms = 0x10000,
remove_perms = 0x20000,
resolve_symlinks = 0x40000
};
inline perms operator|(
perms a,
perms b
)
{
return static_cast< perms >( static_cast< int >( a ) | static_cast< int >( b ) );
}
inline perms operator&(
perms a,
perms b
)
{
return static_cast< perms >( static_cast< int >( a ) & static_cast< int >( b ) );
}
inline perms operator^(
perms a,
perms b
)
{
return static_cast< perms >( static_cast< int >( a ) ^ static_cast< int >( b ) );
}
std::ostream& operator<<(std::ostream&, perms);
}
namespace cpp17
{
namespace filesystem
{
typedef ::perms::perms perms;
}
}
#endif // PBL_CPP_FS_PERMS_H

@ -1,62 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "remove.h"
#include <cstdio>
#include "diriter.h"
namespace cpp17
{
namespace filesystem
{
bool remove(const path& p)
{
return ::remove( p.c_str() ) == 0;
}
unsigned long remove_all(const path& p)
{
unsigned long total = 0;
for ( directory_iterator it(p), last; it != last; ++it )
{
total += remove_all( it->path() );
}
if ( remove(p) )
{
++total;
}
return total;
}
}
}

@ -1,43 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_REMOVE_H
#define PBL_CPP_FS_REMOVE_H
#include "path.h"
namespace cpp17
{
namespace filesystem
{
bool remove(const path&);
unsigned long remove_all(const path&);
}
}
#endif // PBL_CPP_FS_REMOVE_H

@ -1,51 +0,0 @@
/* Copyright (c) 2018, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "rename.h"
#include <cstdio>
#include "copyfile.h"
#include "remove.h"
namespace cpp17
{
namespace filesystem
{
/// @bug This is just a quick and dirty implementation
void rename(const path& from, const path& to)
{
if (std::rename(from.c_str(), to.c_str()) != 0)
{
if (copy_file(from, to))
cpp17::filesystem::remove(from);
}
}
}
}

@ -1,42 +0,0 @@
/* Copyright (c) 2018, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_RENAME_H
#define PBL_CPP_FS_RENAME_H
#include "path.h"
namespace cpp17
{
namespace filesystem
{
void rename(const path& from, const path& to);
}
}
#endif // PBL_CPP_FS_RENAME_H

@ -1,70 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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 "tempdir.h"
#include <cstdlib>
namespace cpp17
{
namespace filesystem
{
/* TMPDIR is the POSIX variable for the temporary directory. Other variables
* are sometimes used, however, in non-conforming environments. Those are
* checked as fallback (following Boost's implementation)
*/
path temp_directory_path()
{
const char* p = std::getenv("TMPDIR");
if ( !p )
{
p = std::getenv("TMP");
if ( !p )
{
p = std::getenv("TEMP");
if ( !p )
{
p = std::getenv("TEMPDIR");
if ( !p )
{
p = "/tmp";
}
}
}
}
return path(p);
}
}
}

@ -1,42 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
#ifndef PBL_CPP_FS_TEMPDIR_H
#define PBL_CPP_FS_TEMPDIR_H
#include "path.h"
namespace cpp17
{
namespace filesystem
{
path temp_directory_path();
}
}
#endif // PBL_CPP_FS_TEMPDIR_H

@ -1,116 +0,0 @@
/* Copyright (c) 2016, Pollard Banknote Limited
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. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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 HOLDER 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.
*/
/** @file version.h
* @brief namespaces for C++11 and C++14
*/
#ifndef PBL_CPP_VERSION_H
#define PBL_CPP_VERSION_H
#include <cstddef>
/// @todo Value may not be correct, because as of writing C++17 isn't official
#if __cplusplus >= 201703L
#define CPP17
#endif
#if __cplusplus >= 201402L
#define CPP14
#endif
#if __cplusplus >= 201103L
#define CPP11
#endif
/* Namespace macros. It's sometimes necessary to put something into the "std"
* namespace. For example, a specialization of std::hash. These macros can be
* used to put the code in the correct namespace.
*/
#ifdef CPP17
#define CPP17_NAMESPACE std
#else
#define CPP17_NAMESPACE cpp17
#endif
#ifdef CPP14
#define CPP14_NAMESPACE std
#else
#define CPP14_NAMESPACE cpp14
#endif
#ifdef CPP11
#define CPP11_NAMESPACE std
#else
#define CPP11_NAMESPACE cpp11
#endif
namespace cpp11
{
}
#ifndef CPP11
namespace cpp11
{
class nullptr_t
{
public:
template< class T >
operator T*() const
{
return 0;
}
template< class C, class T >
operator T C::*( ) const
{
return 0;
}
private:
void operator&() const;
};
}
const cpp11::nullptr_t nullptr = {};
#endif // ifndef CPP11
namespace cpp14
{
}
namespace cpp17
{
}
/* cpp points to either std or cpp11/14/17 as appropriate
*/
namespace cpp
{
using namespace ::std;
using namespace ::cpp11;
using namespace ::cpp14;
using namespace ::cpp17;
}
#endif // PBL_CPP_VERSION_H

@ -1,422 +0,0 @@
# This Makefile is intended to be included by another Makefile. It contains a
# number of common functions, variables, recipes. Ideally the calling Makefile
# will only need to define targets for that project.
#
# Features:
# - Shadow building
# - Auto dependency generation
# - debug target uses address sanitizer when available (GCC >= 4.8.0),
# and undefined behavior sanitizer (GCC >=4.9.0)
# - coloured error messages when available (GCC >=4.9.0)
# - pre-configured targets for cleaning .o files, insure leftovers
# - targets for callgrind, memcheck, insure, profiling and code coverage
#
# Organization
# - Supporting functions
# - Definitions of the variables used by this makefile (ex., CXX, WARNINGS)
# - Predefined configurations of variables (ex., DEBUG)
# - File type targets (ex., .o)
# - Named targets (ex., profile)
# - Inclusion of auto-dependencies
#
# TODO Profile guided optimization
# TODO Precompiled headers
# TODO Static analysis
# TODO Clang support
# TODO -fsanitize=integer for clang
# TODO -fsanitize=thread
# TODO ubsan prints messages to the stderr; does not terminate
# TODO dynamic/static libs
# TODO _GLIBCXX_DEBUG and _GLIBCXX_DEBUG_PEDANTIC
# TODO split up makefile into common functions, variables, configurations, recipes
# TODO -B option for TOOLCHAIN
# TODO CONFIG=RELEASE (strip, for example)
# TODO Different debug levels
# TODO CONFIG=HARDENED including some of the following options
# TODO -fstack-protector-strong?
# TODO -Wformat-security
# TODO -pie -fPIE
# TODO full relro -Wl,-z,relro,-z,now
# TODO -pipe
# TODO -flto
# Shadow building ==============================================================
# This gets the directory of the calling Makefile (and, presumably, where the
# source code resides
# Functions ====================================================================
gccversion = $(shell $(CXX) -dumpversion | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/')
gcc48 = $(shell expr $(gccversion) \>= 40800)
gcc49 = $(shell expr $(gccversion) \>= 40900)
gcc51 = $(shell expr $(gccversion) \>= 50100)
# Convert a word to lowercase
lower = $(shell echo $(1) | tr '[:upper:]' '[:lower:]')
# Convert a word to uppercase
upper = $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
# Variables ====================================================================
#
# The basic variables that this makefile uses
# Toolchain --------------------------------------------------------------------
# Which programs to use during compilation
# Path to a different toolchain (ex., ~/bin/gcc-4.9.0)
TOOLCHAIN =
ifndef TOOLCHAIN
# C++ compiler
CXX = g++
# Archive (.a) program
AR = ar
# Linker
LD = $(CXX)
PROFILER = gprof
else
# Use a particular set of tools
CXX = $(TOOLCHAIN)/bin/g++ -B$(TOOLCHAIN)/bin/
AR = $(TOOLCHAIN)/bin/ar
LD = $(CXX)
PROFILER = $(TOOLCHAIN)/bin/gprof
endif
# Misc
VALGRIND = valgrind
# Compiler flags ---------------------------------------------------------------
# Arguments to the toolchain programs
# Which version of C++ to compile for
STANDARD = -ansi -pedantic
# Optimization flags
OPTIMIZATION = -O2
# Architecture flags
TARGET_ARCH =
# Warnings
WARNINGS = -Wall -Wsign-compare -Wconversion -Wpointer-arith \
-Winit-self -Wcast-qual -Wredundant-decls -Wcast-align -Wwrite-strings \
-Wno-long-long -Woverloaded-virtual -Wformat -Wno-unknown-pragmas \
-Wnon-virtual-dtor -Wno-c++0x-compat
# -Wold-style-cast
ifeq "$(gcc51)" "1"
#WARNINGS += -Wsuggest-attribute=const -Wsuggest-attribute=pure
endif
# Level of debugging information to provide
DEBUGGING = -g
# Machine independent options
#-fno-nonansi-builtins
OPTIONS = -fuse-cxa-atexit -ffor-scope
ifeq "$(gcc49)" "1"
OPTIONS += -fdiagnostics-color
endif
# Flags to use when compiling C++ source into object files
CXXFLAGS = $(STANDARD) $(DEBUGGING) $(OPTIMIZATION) $(WARNINGS) $(OPTIONS) $(TARGET_ARCH)
# Directories to search for includes
INCLUDE =
# Any macros that can be defined on the command line
DEFINES = -D_FORTIFY_SOURCE=1
# Preprocessor flags
CPPFLAGS = $(INCLUDE) $(DEFINES)
# How to build an archive
ARFLAGS = crs
# Linker flags
LDFLAGS =
LDLIBS =
ifdef TOOLCHAIN
# Statically link the GCC and
LDFLAGS += -static-libgcc -static-libstdc++
# Dynamic linking
#LDFLAGS += -Wl,-rpath -Wl,$(TOOLCHAIN)/lib
#LDLIBS = -L$(TOOLCHAIN)/lib
endif
# Printing =====================================================================
# If this is not empty, print messages
VERBOSE =
# Predefined configurations ====================================================
# Combinations of the OPTIMIZATION, etc.
ifdef CONFIG
UCONFIG := $(call upper,$(CONFIG))
# Fast -------------------------------------------------------------------------
# A faster build, primarily for testing
ifeq "$(UCONFIG)" "FAST"
OPTIMIZATION = -O0
endif
# Tune -------------------------------------------------------------------------
# Heavily optimized for a fast application
ifeq "$(UCONFIG)" "TUNE"
ifeq "$(gcc48)" "1"
TARGET_ARCH = -march=native
endif
OPTIMIZATION = -O3 -mtune=native
endif
# Debug ------------------------------------------------------------------------
# Programmer is tracking a bug
ifeq "$(UCONFIG)" "DEBUG"
# Set additional warnings and flags
WARNINGS += -Wshadow -Wfloat-equal
ifeq "$(gcc51)" "1"
WARNINGS += -Wstrict-overflow=5
endif
DEBUGGING = -ggdb3 -fno-omit-frame-pointer -fno-default-inline -fno-inline -fno-merge-constants -ffloat-store
# -fno-elide-constructors
# Set the optimization level
ifeq "$(gcc48)" "1"
OPTIMIZATION = -Og
else
OPTIMIZATION = -O0
endif
# Turn on address sanitizer
ifeq "$(gcc48)" "1"
DEBUGGING += -fsanitize=address
LDFLAGS += -fsanitize=address -static-libasan
endif
# Turn on undefined behavior sanitizer and leak sanitizer
ifeq "$(gcc49)" "1"
DEBUGGING += -fsanitize=undefined -fsanitize=leak
LDFLAGS += -fsanitize=undefined -fsanitize=leak -static-libubsan -static-liblsan
endif
endif
# Profile ----------------------------------------------------------------------
# Programmer wants profile information
ifeq "$(UCONFIG)" "PROFILE"
OPTIMIZATION = -O2 -pg
LDFLAGS += -pg
endif
# Coverage ---------------------------------------------------------------------
# Programmer wants code coverage information
ifeq "$(UCONFIG)" "COVERAGE"
DEBUGGING += --coverage -fno-elide-constructors
LDFLAGS += --coverage
OPTIMIZATION = -O0
endif
# Insure -----------------------------------------------------------------------
# Programmer wants to run insure
ifeq "$(UCONFIG)" "INSURE"
CXX = insure
LDLIBS += -lstdc++
OPTIMIZATION = -O0
endif
endif
# Targets ======================================================================
.PHONY: no_default_goal depend clean_insure clean_objects clean_profile clean_depend distclean coverage profile memcheck callgrind cachegrind help help_common examples
no_default_goal:
$(error "The including Makefile hasn't specified a default goal. It should.")
# Dependency generation --------------------------------------------------------
DEPDIR := .deps
# Build object files with automatic dependency generation
# TODO maybe use LINK.cpp from 'make -p'
%.o: %.cpp
@mkdir -p $(@D)/$(DEPDIR)
ifeq ($(VERBOSE),)
@echo [CXX] $@
@$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -MMD -MP -MF $(@D)/$(DEPDIR)/$(*F).d -o $@ $<
else
$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -MMD -MP -MF $(@D)/$(DEPDIR)/$(*F).d -o $@ $<
endif
%.a:
ifeq ($(VERBOSE),)
@echo [AR] $@
@$(AR) $(ARFLAGS) $@ $^ > /dev/null
else
$(AR) $(ARFLAGS) $@ $^ > /dev/null
endif
# A script for combining .a's. Dependency list for the mri should be
# subdirectories containing static libs. Name of the output library
# is inferred from the mri name (abc.mri produces libabc.a)
%.mri:
@echo 'CREATE $(patsubst %.mri,lib%.a,$@)' > $@
@for dir in $^; do echo ADDLIB $$dir >> $@; done
@echo 'SAVE' >> $@
# TODO dynamic libs
# %.so: CXXFLAGS += -fPIC
# %.so: LDFLAGS := -shared
# %.so:
# $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
# optionally add -Wl,-soname,$(SONAME)
# TODO java classes
# %.class: %.java
# $(JC) -cp . $<
# Code coverage ----------------------------------------------------------------
coverage:
ifdef COVERAGE_OUT
@TMPFILE=`mktemp`; lcov --capture --directory . --output-file $$TMPFILE; genhtml $$TMPFILE --output-directory $(COVERAGE_OUT); $(RM) $$TMPFILE
else
$(error You have not set COVERAGE_OUT)
endif
# Profiling --------------------------------------------------------------------
profile:
ifdef PROFILE_TARGET
$(PROFILE_TARGET) $(PROFILE_TARGET_ARGS)
ifdef PROFILE_OUT
@$(PROFILER) -c $(PROFILE_TARGET) > $(PROFILE_OUT)
else
$(error You have not specified PROFILE_OUT)
endif
else
$(error You have not specified PROFILE_TARGET)
endif
# Memcheck ---------------------------------------------------------------------
# User should set MEMCHECK_TARGET, MEMCHECK_TARGET_ARGS, and a recipe like:
# memcheck: $(MEMCHECK_TARGET)
memcheck:
$(VALGRIND) --tool=memcheck -v --read-var-info=yes --track-origins=yes --show-reachable=yes --leak-check=full --read-inline-info=yes --keep-stacktraces=alloc-and-free $(VALGRIND_TARGET) $(VALGRIND_TARGET_ARGS) 2> $(MEMCHECK_OUT)
# Callgrind --------------------------------------------------------------------
# User should set CALLGRIND_TARGET, CALLGRIND_TARGET_ARGS, and a recipe like:
# callgrind: $(CALLGRIND_TARGET)
callgrind:
$(VALGRIND) --tool=callgrind $(VALGRIND_TARGET) $(VALGRIND_TARGET_ARGS)
cachegrind:
$(VALGRIND) --tool=cachegrind --branch-sim=yes $(VALGRIND_TARGET) $(VALGRIND_TARGET_ARGS)
# Clean targets ----------------------------------------------------------------
# remove all .o files
clean_objects:
ifeq ($(VERBOSE),)
@find -name '*.o' -exec $(RM) '{}' \;
else
find -name '*.o' -exec $(RM) '{}' \;
endif
# remove all files left by insure
clean_insure:
ifeq ($(VERBOSE),)
@find -name '*.tcl' -exec $(RM) '{}' \;
@$(RM) tca.map tca.log
else
find -name '*.tcl' -exec $(RM) '{}' \;
$(RM) tca.map tca.log
endif
# remove all files left by gprof
clean_profile:
ifeq ($(VERBOSE),)
@$(RM) profile
@find \( -name '*.gcda' -o -name '*.gcno' \) -exec $(RM) '{}' \;
else
$(RM) profile
find \( -name '*.gcda' -o -name '*.gcno' \) -exec $(RM) '{}' \;
endif
clean_depend:
ifeq ($(VERBOSE),)
@find -depth -name $(DEPDIR) -type d -exec $(RM) -r '{}' \;
else
find -depth -name $(DEPDIR) -type d -exec $(RM) -r '{}' \;
endif
depend: clean clean_depend
distclean: depend
# Documentation ----------------------------------------------------------------
help_common:
@echo "==================================================================="
@echo "Here are some standard Makefile variables and their defaults:"
@echo " CXX ($(CXX))"
@echo " CPPFLAGS ($(CPPFLAGS))"
@echo " LDFLAGS ($(LDFLAGS))"
@echo " LDLIBS ($(LDLIBS))"
@echo " AR ($(AR))"
@echo " ARFLAGS ($(ARFLAGS))"
@echo
@echo "You can set the TOOLCHAIN variable to use a different compiler,"
@echo "linker, profiler. Ex., (~/bin/gcc-4.9.0)"
@echo
@echo "The CXXFLAGS variable is comprised of a number of other variables:"
@echo " STANDARD ($(STANDARD))"
@echo " DEBUGGING ($(DEBUGGING))"
@echo " OPTIMIZATION ($(OPTIMIZATION))"
@echo " WARNINGS ($(WARNINGS))"
@echo " TARGET_ARCH ($(TARGET_ARCH))"
@echo
@echo "For convenience, several preconfigured options are available by"
@echo "setting the CONFIG variale:"
@echo " DEBUG - Turns on debugging info; turns down optimization; enables"
@echo " address sanitizer, etc. if available"
@echo " PROFILE - Keeps optimization for release build; adds debugging"
@echo " and gprof flags"
@echo " COVERAGE - Don't inline functions"
@echo " INSURE - Build the target with the insure compiler"
@echo " TUNE - High optimization level; CPU-specific instructions"
@echo " FAST - A fast compilation with low optimization level"
@echo
@echo "==================================================================="
@echo "Here are some special targets and variables that affect them"
@echo
@echo " profile - First build a target with CONFIG=PROFILE and run it."
@echo " PROFILE_TARGET the name of the program to profile"
@echo " (default: $(PROFILE_TARGET))"
@echo " PROFILE_TARGET_ARGS arguments to the program to profile"
@echo " (default: $(PROFILE_TARGET_ARGS))"
@echo " PROFILE_OUT the file to place the profile data in"
@echo " (default: $(PROFILE_OUT))"
@echo " PROFILER Path to profiler"
@echo " (default: $(PROFILER))"
@echo " coverage - First build a target with CONFIG=COVERAGE and run it."
@echo " COVERAGE_OUT specifies where to put the resulting web pages"
@echo " (default: $(COVERAGE_OUT))"
@echo
@echo " memcheck - Run memcheck for VALGRIND_TARGET, testing memory access"
@echo " callgrind - Run callgrind for VALGRIND_TARGET, testing performance"
@echo " cachegrind - Run cachegrind for VALGRIND_TARGET, testing cache"
@echo " VALGRIND_TARGET The program to check"
@echo " (default: $(VALGRIND_TARGET))"
@echo " VALGRIND_TARGET_ARGS The arguments to VALGRIND_TARGET"
@echo " (default: $(VALGRIND_TARGET_ARGS))"
@echo " MEMCHECK_OUT Where to put the memcheck results"
@echo " (default: $(MEMCHECK_OUT))"
@echo "==================================================================="
help: help_common
# ==============================================================================
# Include dependencies which are stored as .d files in the $(DEPDIR) directories
# TODO prefer -wholename to -path, but old version of find doesn't have wholename
-include $(shell find -path '*/$(DEPDIR)/*.d')
Loading…
Cancel
Save