LCOV - code coverage report
Current view: top level - include/pairinteraction/utils - paths.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 33 72.7 %
Date: 2025-06-06 09:03:14 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             : namespace pairinteraction::paths {
      10             : 
      11          27 : inline std::filesystem::path get_home_directory() {
      12             : #ifdef _WIN32
      13             :     char const *hdrive = getenv("HOMEDRIVE");
      14             :     char const *hpath = getenv("HOMEPATH");
      15             :     if (hdrive != nullptr && hpath != nullptr) {
      16             :         std::filesystem::path path = std::string(hdrive) + hpath;
      17             :         return path;
      18             :     }
      19             : #else
      20          27 :     const char *home = std::getenv("HOME");
      21          27 :     if (home != nullptr) {
      22          27 :         std::filesystem::path path = home;
      23          27 :         return path;
      24          27 :     }
      25             : #endif
      26           0 :     return {"~"};
      27             : }
      28             : 
      29          14 : inline std::filesystem::path get_cache_directory() {
      30          14 :     std::filesystem::path path;
      31             : 
      32          14 :     char const *pairinteraction_cache_dir = getenv("PAIRINTERACTION_CACHE_DIR");
      33          14 :     if (pairinteraction_cache_dir != nullptr) {
      34           0 :         path = pairinteraction_cache_dir;
      35           0 :         return path;
      36             :     }
      37             : 
      38             : #ifdef _WIN32
      39             :     char const *localappdata = std::getenv("LOCALAPPDATA");
      40             :     if (localappdata != nullptr) {
      41             :         path = localappdata;
      42             :     } else {
      43             :         path = get_home_directory() / "AppData" / "Local";
      44             :     }
      45             :     path /= "pairinteraction";
      46             : #elif __APPLE__
      47             :     path = get_home_directory() / "Library" / "Caches" / "pairinteraction";
      48             : #else
      49          14 :     char const *xdg_cache_home = std::getenv("XDG_CACHE_HOME");
      50          14 :     if (xdg_cache_home != nullptr) {
      51           0 :         path = xdg_cache_home;
      52             :     } else {
      53          14 :         path = get_home_directory() / ".cache";
      54             :     }
      55          14 :     path /= "pairinteraction";
      56             : #endif
      57             : 
      58          14 :     return path;
      59           0 : }
      60             : 
      61          13 : inline std::filesystem::path get_config_directory() {
      62          13 :     std::filesystem::path path;
      63             : 
      64          13 :     char const *pairinteraction_config_dir = getenv("PAIRINTERACTION_CONFIG_DIR");
      65          13 :     if (pairinteraction_config_dir != nullptr) {
      66           0 :         path = pairinteraction_config_dir;
      67           0 :         return path;
      68             :     }
      69             : 
      70             : #ifdef _WIN32
      71             :     char const *appdata = std::getenv("APPDATA");
      72             :     if (appdata != nullptr) {
      73             :         path = appdata;
      74             :     } else {
      75             :         path = get_home_directory() / "AppData" / "Roaming";
      76             :     }
      77             :     path /= "pairinteraction";
      78             : #elif __APPLE__
      79             :     path = get_home_directory() / "Library" / "Preferences" / "pairinteraction";
      80             : #else
      81          13 :     char const *xdg_config_home = std::getenv("XDG_CONFIG_HOME");
      82          13 :     if (xdg_config_home != nullptr) {
      83           0 :         path = xdg_config_home;
      84             :     } else {
      85          13 :         path = get_home_directory() / ".config";
      86             :     }
      87          13 :     path /= "pairinteraction";
      88             : #endif
      89             : 
      90          13 :     return path;
      91           0 : }
      92             : 
      93             : } // namespace pairinteraction::paths

Generated by: LCOV version 1.16