summaryrefslogtreecommitdiff
path: root/lib/core/c++/environment.hpp
blob: 27d8f3ac8fe2108419e9877c64951cc1ec521a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once

#include <forstio/error.hpp>
#include <filesystem>
#include <cstdlib>

namespace kel {
namespace lbm {

struct environment {
	std::filesystem::path lbm_dir;
	std::filesystem::path data_dir;
};

saw::error_or<environment> lbm_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");
	}

	environment env;

	env.lbm_dir = std::filesystem::path{home_dir} / ".lbm";

	{
		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";
}

}
}