diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-07-06 01:37:34 +0200 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-07-06 01:37:34 +0200 |
| commit | af4b3f8f9bf7b19c169d9fbd0a2b49668af8ad10 (patch) | |
| tree | 3e98fff4bad333affa55e082de0da759bad57229 /modules/core/c++/environment.hpp | |
| parent | 52326efc80dfeae6e830527df2e7803ecf61b00a (diff) | |
| parent | 015e941a91552ce6ea4cde4507ec9451c3708e77 (diff) | |
| download | libs-lbm-af4b3f8f9bf7b19c169d9fbd0a2b49668af8ad10.tar.gz | |
Merge branch 'dev'
Diffstat (limited to 'modules/core/c++/environment.hpp')
| -rw-r--r-- | modules/core/c++/environment.hpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/modules/core/c++/environment.hpp b/modules/core/c++/environment.hpp new file mode 100644 index 0000000..d8aa9ae --- /dev/null +++ b/modules/core/c++/environment.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include <forstio/error.hpp> +#include <filesystem> +#include <cstdlib> + +namespace kel { +namespace lbm { + +struct environment { + std::string name; + + std::filesystem::path lbm_dir; + std::filesystem::path data_dir; +}; + +saw::error_or<environment> setup_lbm_env(const std::string_view name){ + namespace fs = std::filesystem; + + const char* home_dir = std::getenv("HOME"); + if(not home_dir){ + return saw::make_error<saw::err::not_found>("Couldn't find home dir"); + } + + environment env; + + env.lbm_dir = std::filesystem::path{home_dir} / ".lbm"; + + { + // Set default data location to "${lbm_dir}/data" + env.data_dir = env.lbm_dir / "data"; + // LBM Data Location + auto lbm_dir_config = env.lbm_dir / "data_dir_location.txt"; + if( fs::exists(lbm_dir_config) && fs::is_regular_file(lbm_dir_config) ){ + std::ifstream file{lbm_dir_config}; + + if(file.is_open()){ + std::stringstream buffer; + buffer <<file.rdbuf(); + env.data_dir = fs::path{buffer.str()}; + } + } + } + + return env; +} + +/** + * Returns the default output directory. + * Located outside the project dir because dispatching build jobs with output data in the git directory + * also copies simulated data which takes a long time. + */ +saw::error_or<std::filesystem::path> output_directory(){ + namespace fs = std::filesystem; + + const char* home_dir = std::getenv("HOME"); + if(not home_dir){ + return saw::make_error<saw::err::not_found>("Couldn't find home dir"); + } + + auto lbm_dir = std::filesystem::path{home_dir} / ".lbm"; + + { + auto lbm_dir_config = lbm_dir / "data_dir_location.txt"; + if( fs::exists(lbm_dir_config) && fs::is_regular_file(lbm_dir_config) ){ + std::ifstream file{lbm_dir_config}; + + if(file.is_open()){ + std::stringstream buffer; + buffer <<file.rdbuf(); + return fs::path{buffer.str()}; + } + } + } + + return lbm_dir / "data"; +} + +} +} |
