Add AppFolders class

This commit is contained in:
2021-03-24 19:02:25 +01:00
parent c5a2fb1b02
commit cd60d9ebc3
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "AppFolders.hpp"
#include "PlatformFolders.hpp"
namespace openVulkanoCpp
{
AppFolders AppFolders::INSTANCE = AppFolders("openVulkano");
void AppFolders::Init(std::string_view appName)
{
#ifndef __APPLE__
INSTANCE = AppFolders(appName);
#endif
}
void AppFolders::Init(const std::filesystem::path& dir, std::optional<std::string_view> appName)
{
#ifndef __APPLE__
INSTANCE = AppFolders(dir, appName);
#endif
}
AppFolders::AppFolders(std::string_view appName)
: appDataHome(PlatformFolders::GetDataHomeDir() / appName)
, appConfigHome(PlatformFolders::GetConfigHomeDir() / appName)
, appState(PlatformFolders::GetStateDir() / appName)
, appDataCache(PlatformFolders::GetCacheDir() / appName)
, appTemp(PlatformFolders::GetTempDir() / appName)
{
}
AppFolders::AppFolders(const std::filesystem::path& dir, std::optional<std::string_view> appName)
: appDataHome(dir / appName.value_or(""))
, appConfigHome(dir / appName.value_or(""))
, appState(dir / appName.value_or("") / "state")
, appDataCache(dir / appName.value_or("") / "cache")
, appTemp(dir / appName.value_or("") / "tmp")
{
}
const std::filesystem::path& AppFolders::GetAppDataHomeDir()
{
#ifndef __APPLE__
std::filesystem::create_directories(INSTANCE.appDataHome);
#endif
return INSTANCE.appDataHome;
}
const std::filesystem::path& AppFolders::GetAppConfigHomeDir()
{
#ifndef __APPLE__
std::filesystem::create_directories(INSTANCE.appConfigHome);
#endif
return INSTANCE.appConfigHome;
}
const std::filesystem::path& AppFolders::GetAppStateDir()
{
#ifndef __APPLE__
std::filesystem::create_directories(INSTANCE.appState);
#endif
return INSTANCE.appState;
}
const std::filesystem::path& AppFolders::GetAppCacheDir()
{
#ifndef __APPLE__
std::filesystem::create_directories(INSTANCE.appDataCache);
#endif
return INSTANCE.appDataCache;
}
const std::filesystem::path& AppFolders::GetAppTempDir()
{
#ifndef __APPLE__
std::filesystem::create_directories(INSTANCE.appTemp);
#endif
return INSTANCE.appTemp;
}
}

View File

@@ -0,0 +1,38 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <filesystem>
#include <optional>
namespace openVulkanoCpp
{
class AppFolders
{
static AppFolders INSTANCE;
std::filesystem::path appDataHome, appConfigHome, appState, appDataCache, appTemp;
AppFolders(std::string_view appName);
AppFolders(const std::filesystem::path& dir, std::optional<std::string_view> appName);
public:
static void Init(std::string_view appName);
static void Init(const std::filesystem::path& dir, std::optional<std::string_view> appName);
[[nodiscard]] static const std::filesystem::path& GetAppDataHomeDir();
[[nodiscard]] static const std::filesystem::path& GetAppConfigHomeDir();
[[nodiscard]] static const std::filesystem::path& GetAppStateDir();
[[nodiscard]] static const std::filesystem::path& GetAppCacheDir();
[[nodiscard]] static const std::filesystem::path& GetAppTempDir();
};
}