LCOV - code coverage report
Current view: top level - include/pairinteraction/utils - paths.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 37 64.9 %
Date: 2026-08-14 15:36:24 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2024 PairInteraction Developers
       2             : // SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : #pragma once
       5             : 
       6             : #include <cstdlib>
       7             : #include <filesystem>
       8             : 
       9             : #ifdef _WIN32
      10             : #else
      11             : #include <pwd.h>
      12             : #include <sys/types.h>
      13             : #include <unistd.h>
      14             : #endif
      15             : 
      16             : namespace pairinteraction::paths {
      17             : 
      18           8 : inline std::filesystem::path get_home_directory() {
      19             : #ifdef _WIN32
      20             :     if (char const *userprofile = getenv("USERPROFILE"); userprofile != nullptr) {
      21             :         std::filesystem::path path = userprofile;
      22             :         return path;
      23             :     }
      24             :     if (char const *hdrive = getenv("HOMEDRIVE"), *hpath = getenv("HOMEPATH");
      25             :         hdrive != nullptr && hpath != nullptr) {
      26             :         std::filesystem::path path = std::string(hdrive) + hpath;
      27             :         return path;
      28             :     }
      29             : #else
      30           8 :     const char *home = std::getenv("HOME");
      31           8 :     if (home != nullptr) {
      32           8 :         std::filesystem::path path = home;
      33           8 :         return path;
      34           8 :     }
      35           0 :     if (struct passwd *pw = getpwuid(getuid()); pw != nullptr) {
      36           0 :         std::filesystem::path path = pw->pw_dir;
      37           0 :         return path;
      38           0 :     }
      39             : #endif
      40           0 :     return {"~"};
      41             : }
      42             : 
      43           5 : inline std::filesystem::path get_cache_directory() {
      44           5 :     std::filesystem::path path;
      45             : 
      46           5 :     char const *pairinteraction_cache_dir = getenv("PAIRINTERACTION_CACHE_DIR");
      47           5 :     if (pairinteraction_cache_dir != nullptr) {
      48           0 :         path = pairinteraction_cache_dir;
      49           0 :         return path;
      50             :     }
      51             : 
      52             : #ifdef _WIN32
      53             :     char const *localappdata = std::getenv("LOCALAPPDATA");
      54             :     if (localappdata != nullptr) {
      55             :         path = localappdata;
      56             :     } else {
      57             :         path = get_home_directory() / "AppData" / "Local";
      58             :     }
      59             :     path /= "pairinteraction";
      60             : #elif __APPLE__
      61             :     path = get_home_directory() / "Library" / "Caches" / "pairinteraction";
      62             : #else
      63           5 :     char const *xdg_cache_home = std::getenv("XDG_CACHE_HOME");
      64           5 :     if (xdg_cache_home != nullptr) {
      65           0 :         path = xdg_cache_home;
      66             :     } else {
      67           5 :         path = get_home_directory() / ".cache";
      68             :     }
      69           5 :     path /= "pairinteraction";
      70             : #endif
      71             : 
      72           5 :     return path;
      73           0 : }
      74             : 
      75           3 : inline std::filesystem::path get_config_directory() {
      76           3 :     std::filesystem::path path;
      77             : 
      78           3 :     char const *pairinteraction_config_dir = getenv("PAIRINTERACTION_CONFIG_DIR");
      79           3 :     if (pairinteraction_config_dir != nullptr) {
      80           0 :         path = pairinteraction_config_dir;
      81           0 :         return path;
      82             :     }
      83             : 
      84             : #ifdef _WIN32
      85             :     char const *appdata = std::getenv("APPDATA");
      86             :     if (appdata != nullptr) {
      87             :         path = appdata;
      88             :     } else {
      89             :         path = get_home_directory() / "AppData" / "Roaming";
      90             :     }
      91             :     path /= "pairinteraction";
      92             : #elif __APPLE__
      93             :     path = get_home_directory() / "Library" / "Preferences" / "pairinteraction";
      94             : #else
      95           3 :     char const *xdg_config_home = std::getenv("XDG_CONFIG_HOME");
      96           3 :     if (xdg_config_home != nullptr) {
      97           0 :         path = xdg_config_home;
      98             :     } else {
      99           3 :         path = get_home_directory() / ".config";
     100             :     }
     101           3 :     path /= "pairinteraction";
     102             : #endif
     103             : 
     104           3 :     return path;
     105           0 : }
     106             : 
     107             : } // namespace pairinteraction::paths

Generated by: LCOV version 1.16