Nektar++
Filesystem.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Filesystem.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Header file to simplify use of <filesystem>
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIBUTILITIES_FILESYSTEM_HPP
36#define NEKTAR_LIBUTILITIES_FILESYSTEM_HPP
37
38#ifdef NEKTAR_USE_BOOST_FILESYSTEM
39#include <boost/filesystem.hpp>
40namespace fs = boost::filesystem;
41namespace fserrc = boost::system::errc;
42#else
43#ifdef NEKTAR_USE_GNU_FS_EXPERIMENTAL
44#include <experimental/filesystem>
45#include <system_error>
46namespace fs = std::experimental::filesystem;
47using fserrc = std::errc;
48#else
49#include <filesystem>
50#include <system_error>
51namespace fs = std::filesystem;
52using fserrc = std::errc;
53#endif
54#endif
55
56#include <algorithm>
57#include <chrono>
58#include <random>
59
61{
62
63/**
64 * \brief create portable path on different platforms for std::filesystem path.
65 */
66static inline std::string PortablePath(const fs::path &path)
67{
68 return fs::path(path).make_preferred().string();
69}
70
71/**
72 * @brief Create a unique (random) path, based on an input stem string. The
73 * returned string is a filename or directory that does not exist.
74 *
75 * Given @p specFileStem as a parameter, this returns a string in the form
76 * `tmp_<stem>_abcdef` where the final 6 characters are random characters
77 * and digits.
78 */
79static inline fs::path UniquePath(std::string specFileStem)
80{
81 std::random_device rd;
82 std::mt19937 generator(rd());
83 fs::path tmp;
84
85 do
86 {
87 std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
88 "abcdefghijklmnopqrstuvwxyz";
89
90 std::shuffle(chars.begin(), chars.end(), generator);
91 tmp = fs::path("tmp_" + specFileStem + "_" + chars.substr(0, 6));
92 } while (fs::exists(tmp));
93
94 return tmp;
95}
96
97} // namespace Nektar::LibUtilities
98
99#endif
std::errc fserrc
Definition: Filesystem.hpp:52
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:66
static fs::path UniquePath(std::string specFileStem)
Create a unique (random) path, based on an input stem string. The returned string is a filename or di...
Definition: Filesystem.hpp:79