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_GNU_FS_EXPERIMENTAL
39#include <experimental/filesystem>
40namespace fs = std::experimental::filesystem;
41#else
42#include <filesystem>
43namespace fs = std::filesystem;
44#endif
45
46#include <algorithm>
47#include <chrono>
48#include <random>
49
51{
52
53/**
54 * \brief create portable path on different platforms for std::filesystem path.
55 */
56static inline std::string PortablePath(const fs::path &path)
57{
58 return fs::path(path).make_preferred().string();
59}
60
61/**
62 * @brief Create a unique (random) path, based on an input stem string. The
63 * returned string is a filename or directory that does not exist.
64 *
65 * Given @p specFileStem as a parameter, this returns a string in the form
66 * `tmp_<stem>_abcdef` where the final 6 characters are random characters
67 * and digits.
68 */
69static inline fs::path UniquePath(std::string specFileStem)
70{
71 std::random_device rd;
72 std::mt19937 generator(rd());
73 fs::path tmp;
74
75 do
76 {
77 std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
78 "abcdefghijklmnopqrstuvwxyz";
79
80 std::shuffle(chars.begin(), chars.end(), generator);
81 tmp = fs::path("tmp_" + specFileStem + "_" + chars.substr(0, 6));
82 } while (fs::exists(tmp));
83
84 return tmp;
85}
86
87} // namespace Nektar::LibUtilities
88
89#endif
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:56
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:69