From 74700363ab19c30d66893c1bd8abfac957922fd5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 19 Jan 2022 11:01:56 +1000 Subject: [PATCH] paths: add system path queries --- CMakeLists.txt | 2 ++ paths.cpp | 3 +++ paths.hpp | 11 ++++++++++- paths_posix.cpp | 13 ++++++++++++- test/paths.cpp | 15 +++++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 paths.cpp create mode 100644 test/paths.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8be9bc7d..1b039498 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/paths.cpp b/paths.cpp new file mode 100644 index 00000000..f37a46e1 --- /dev/null +++ b/paths.cpp @@ -0,0 +1,3 @@ +#include "./paths.hpp" + + diff --git a/paths.hpp b/paths.hpp index ae022a4c..6bffeef4 100644 --- a/paths.hpp +++ b/paths.hpp @@ -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 + * Copyright 2022, Danny Robson */ #pragma once #include +#include +/////////////////////////////////////////////////////////////////////////////// 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&); } \ No newline at end of file diff --git a/paths_posix.cpp b/paths_posix.cpp index a9960b9a..6100d1c2 100644 --- a/paths_posix.cpp +++ b/paths_posix.cpp @@ -16,6 +16,17 @@ #include +/////////////////////////////////////////////////////////////////////////////// +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]; -} +} \ No newline at end of file diff --git a/test/paths.cpp b/test/paths.cpp new file mode 100644 index 00000000..a757647c --- /dev/null +++ b/test/paths.cpp @@ -0,0 +1,15 @@ +#include + +#include +#include +#include + + +int main (int, char**) +{ + cruft::TAP::logger tap; + + tap.expect (!cruft::paths::temp ().empty (), "temp directory is not empty"); + + return tap.status (); +} \ No newline at end of file