Skip to content

Commit

Permalink
Add auto_save property
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Jun 11, 2024
1 parent 7df5555 commit 5a146a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/xtd.core/include/xtd/configuration/file_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ namespace xtd {
file_settings(file_settings&&) noexcept = default;
file_settings(const file_settings&) noexcept = default;
file_settings& operator =(const file_settings&) noexcept = default;
~file_settings();
/// @endcond

/// @name Public Properties

/// @{
/// @brief Gets whether save should be called on the xtd::configuration::file_settings destructor.
/// @return true if xtd::configuration::file_settings::save is called on the xtd::configuration::file_settings destructor; otherwise, false.
/// @remarks The default is false.
bool auto_save() const noexcept;
/// @brief Sets whether save should be called on the xtd::configuration::file_settings destructor.
/// @param value true if xtd::configuration::file_settings::save is called on the xtd::configuration::file_settings destructor; otherwise, false.
/// @remarks The default is false.
void auto_save(bool value) noexcept;

/// @brief Gets the file path of the current instance.
/// @return The file path of the current instance.
/// @remarks If no file the property can be return xtd::ustring::empty_string.
Expand Down Expand Up @@ -94,6 +104,11 @@ namespace xtd {
/// @brief Gets all sections.
/// @return The sections vector.
string_vector sections() const noexcept;

/// @brief Gets the stream of the current instance.
/// @return The stream of the current instance.
/// @warning Don't manipulate the stream yourself, otherwise the expected result may be undefined.
std::optional<std::reference_wrapper<std::iostream>> stream() const noexcept;
/// @}

/// @name Public Methods
Expand Down Expand Up @@ -261,8 +276,9 @@ namespace xtd {
xtd::ustring read_string(const xtd::ustring& section, const xtd::ustring& key, const xtd::ustring& default_value) noexcept;
void write_string(const xtd::ustring& section, const xtd::ustring& key, const xtd::ustring& value) noexcept;

std::map<xtd::ustring, string_map> section_key_values_;
bool auto_save_ = false;
xtd::ustring file_path_;
std::map<xtd::ustring, string_map> section_key_values_;
std::iostream* stream_ = nullptr;
};
}
Expand Down
19 changes: 17 additions & 2 deletions src/xtd.core/src/xtd/configuration/file_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ file_settings::file_settings(iostream& stream) : stream_ {&stream} {
load(*stream_);
}

file_settings::~file_settings() {
if (auto_save_) save();
}

bool file_settings::auto_save() const noexcept {
return auto_save_;
}
void file_settings::auto_save(bool value) noexcept {
auto_save_ = value;
}

const xtd::ustring& file_settings::file_path() const noexcept {
return file_path_;
}
Expand Down Expand Up @@ -47,6 +58,10 @@ file_settings::string_vector file_settings::sections() const noexcept {
return sections;
}

optional<reference_wrapper<iostream>> file_settings::stream() const noexcept {
return stream_ ? optional<reference_wrapper<iostream>> {} : optional<reference_wrapper<iostream>> {*stream_};
}

bool file_settings::equals(const file_settings& obj) const noexcept {
return section_key_values_ == obj.section_key_values_;
}
Expand Down Expand Up @@ -102,12 +117,12 @@ void file_settings::remove_all_keys(const ustring& section) noexcept {
void file_settings::reset() {
section_key_values_.clear();
if (!ustring::is_empty(file_path_) && file::exists(file_path_)) file::remove(file_path_);
if (stream_ != nullptr) stream_writer(*stream_).write("");
if (stream_) stream_writer(*stream_).write("");
}

void file_settings::save() {
if (!ustring::is_empty(file_path_)) save_as(file_path_);
if (stream_ != nullptr) save_as(*stream_);
if (stream_) save_as(*stream_);
}

void file_settings::save_as(const xtd::ustring& file_path) {
Expand Down

0 comments on commit 5a146a9

Please sign in to comment.