paths: add system path queries

This commit is contained in:
Danny Robson 2022-01-19 11:01:56 +10:00
parent 3d085c4de7
commit 74700363ab
5 changed files with 42 additions and 2 deletions

View File

@ -494,6 +494,7 @@ list (
parse/value.hpp parse/value.hpp
parse/si.cpp parse/si.cpp
parse/si.hpp parse/si.hpp
paths.cpp
paths.hpp paths.hpp
platform.hpp platform.hpp
point.cpp point.cpp
@ -752,6 +753,7 @@ if (TESTS)
parse/value parse/value
parse/time parse/time
parse/si parse/si
paths
point point
polynomial polynomial
pool pool

3
paths.cpp Normal file
View File

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

View File

@ -3,15 +3,24 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2020, Danny Robson <danny@nerdcruft.net> * Copyright 2022, Danny Robson <danny@nerdcruft.net>
*/ */
#pragma once #pragma once
#include <filesystem> #include <filesystem>
#include <optional>
///////////////////////////////////////////////////////////////////////////////
namespace cruft::paths { namespace cruft::paths {
/// Return the path to the system's temporary directory.
std::filesystem::path temp (void);
/// Expand paths that contain platform specific variables
///
/// eg, "$TMPDIR/foo" and "%HOMEPATH%/foo"
std::filesystem::path std::filesystem::path
expand (std::filesystem::path const&); expand (std::filesystem::path const&);
} }

View File

@ -16,6 +16,17 @@
#include <wordexp.h> #include <wordexp.h>
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
cruft::paths::temp (void)
{
if (char const *tmpdir = getenv ("TMPDIR"); tmpdir)
return tmpdir;
return "/tmp";
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
std::filesystem::path std::filesystem::path
cruft::paths::expand (std::filesystem::path const &val) cruft::paths::expand (std::filesystem::path const &val)
@ -29,4 +40,4 @@ cruft::paths::expand (std::filesystem::path const &val)
CHECK_EQ (words.we_offs, 0u); CHECK_EQ (words.we_offs, 0u);
return words.we_wordv[0]; return words.we_wordv[0];
} }

15
test/paths.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <cruft/util/tap.hpp>
#include <cruft/util/paths.hpp>
#include <cruft/util/fs/scoped.hpp>
#include <cruft/util/fs/tmp.hpp>
int main (int, char**)
{
cruft::TAP::logger tap;
tap.expect (!cruft::paths::temp ().empty (), "temp directory is not empty");
return tap.status ();
}