-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "settings.hpp" | ||
|
||
#include <papki/fs_file.hpp> | ||
|
||
#include <ruisapp/application.hpp> | ||
#include <filesystem> | ||
|
||
using namespace std::string_view_literals; | ||
|
||
using namespace bedsidemon; | ||
|
||
namespace{ | ||
constexpr const auto settings_filename = "settings.tml"sv; | ||
|
||
constexpr const auto sweep_speed_key = "sweep_speed_um_per_sec"sv; | ||
} | ||
|
||
settings_storage::settings_storage() : | ||
filename(utki::cat(ruisapp::application::inst().directory.config, settings_filename)), | ||
settings_v(read(this->filename)) | ||
{} | ||
|
||
settings settings_storage::read(std::string_view filename){ | ||
papki::fs_file fi(filename); | ||
|
||
if(!fi.exists()){ | ||
LOG([](auto&o){o << "settings file not found, use default settings" << std::endl;}) | ||
return {}; | ||
} | ||
|
||
settings setts; | ||
|
||
auto tml = tml::read(fi); | ||
|
||
for(const auto& t : tml){ | ||
if(t.value.to_string() == sweep_speed_key){ | ||
// TODO: | ||
} | ||
} | ||
|
||
return setts; | ||
} | ||
|
||
void settings_storage::set(const settings& s){ | ||
this->settings_v = s; | ||
this->write(); | ||
} | ||
|
||
void settings_storage::write(){ | ||
settings default_values; | ||
|
||
tml::forest tml; | ||
|
||
auto add_setting = [&tml](std::string_view key, tml::leaf val){ | ||
tml.push_back(tml::tree(key, {val})); | ||
}; | ||
|
||
if(this->settings_v.sweep_speed_um_per_sec != default_values.sweep_speed_um_per_sec){ | ||
add_setting(sweep_speed_key, tml::leaf(this->settings_v.sweep_speed_um_per_sec)); | ||
} | ||
|
||
if(tml.empty()){ | ||
return; | ||
} | ||
|
||
std::filesystem::create_directories(ruisapp::application::inst().directory.config); | ||
|
||
papki::fs_file fi(filename); | ||
tml::write(tml, fi); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <tml/tree.hpp> | ||
|
||
namespace bedsidemon{ | ||
|
||
struct settings{ | ||
uint32_t sweep_speed_um_per_sec = 25000; | ||
}; | ||
|
||
class settings_storage{ | ||
std::string filename; | ||
|
||
settings settings_v; | ||
|
||
static settings read(std::string_view filename); | ||
void write(); | ||
|
||
public: | ||
settings_storage(); | ||
~settings_storage(){ | ||
this->write();// TODO: remove? | ||
} | ||
|
||
const settings& get()const noexcept{ | ||
return this->settings_v; | ||
} | ||
|
||
void set(const settings& s); | ||
}; | ||
|
||
} |