Skip to content
API Documentation
BastionGuard™
Resource.hpp
Go to the documentation of this file.
1/*
2 * BastionGuard™ Recovery Data
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#pragma once
19
20#include <cstdlib>
21#include <filesystem>
22#include <iostream>
23#include <string>
24
25#ifndef DATA_DIR
26#define DATA_DIR "/usr/share/bastionguard-recovery-data/data"
27#endif
28
29inline std::string resource(const std::string& rel)
30{
31 namespace fs = std::filesystem;
32
33 if (const char* env = std::getenv("BASTIONGUARD_DATA"); env && *env) {
34 fs::path p = fs::path(env) / rel;
35 if (fs::exists(p)) {
36 return p.string();
37 }
38 }
39
40 {
41 fs::path system_path = fs::path(DATA_DIR) / rel;
42 if (fs::exists(system_path)) {
43 return system_path.string();
44 }
45 }
46
47 {
48 std::string user_base;
49
50 if (const char* xdg = std::getenv("XDG_DATA_HOME"); xdg && *xdg) {
51 user_base = xdg;
52 } else if (const char* home = std::getenv("HOME"); home && *home) {
53 user_base = std::string(home) + "/.local/share";
54 }
55
56 if (!user_base.empty()) {
57 fs::path user_path = fs::path(user_base) / "bastionguard-recovery-data" / "data" / rel;
58 if (fs::exists(user_path)) {
59 return user_path.string();
60 }
61 }
62 }
63
64 {
65 fs::path cwd_path = fs::current_path() / rel;
66 if (fs::exists(cwd_path)) {
67 return cwd_path.string();
68 }
69 }
70
71 {
72 fs::path local_data_path = fs::current_path() / "data" / fs::path(rel).filename();
73 if (fs::exists(local_data_path)) {
74 return local_data_path.string();
75 }
76 }
77
78 std::cerr << "⚠ Risorsa non trovata: " << rel << std::endl;
79 return (fs::path(DATA_DIR) / rel).string();
80}
std::string resource(const std::string &rel)
Definition Resource.hpp:31
#define DATA_DIR
Definition Resource.hpp:28