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/si.cpp
parse/si.hpp
paths.cpp
paths.hpp
platform.hpp
point.cpp
@ -752,6 +753,7 @@ if (TESTS)
parse/value
parse/time
parse/si
paths
point
polynomial
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
* 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
#include <filesystem>
#include <optional>
///////////////////////////////////////////////////////////////////////////////
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
expand (std::filesystem::path const&);
}

View File

@ -16,6 +16,17 @@
#include <wordexp.h>
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
cruft::paths::temp (void)
{
if (char const *tmpdir = getenv ("TMPDIR"); tmpdir)
return tmpdir;
return "/tmp";
}
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
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);
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 ();
}