Skip to content

Commit 57cd488

Browse files
committed
Add from_string method
1 parent 0abf102 commit 57cd488

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/xtd.core/include/xtd/configuration/file_settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ namespace xtd {
8181

8282
void save_as(const xtd::ustring& file_path);
8383

84+
virtual void from_string(const xtd::ustring& text);
85+
8486
xtd::ustring to_string() const noexcept override;
8587

8688
void write(const xtd::ustring& key, const xtd::ustring& value) noexcept;

src/xtd.core/src/xtd/configuration/file_settings.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,7 @@ bool file_settings::equals(const file_settings& rhs) const noexcept {
4343

4444
void file_settings::load(const xtd::ustring& file_path) {
4545
file_path_ = path::get_full_path(file_path);
46-
auto stream = stream_reader {file_path_};
47-
auto section = ustring::empty_string;
48-
while (!stream.end_of_stream()) {
49-
auto line = stream.read_line().trim();
50-
if (ustring::is_empty(line) || line.starts_with(';') || line.starts_with('#')) continue;
51-
if (line.starts_with('[') && line.ends_with(']')) section = line.substring(1, line.size() - 2);
52-
else {
53-
auto key_value = line.split({'='});
54-
section_key_values_[section][key_value[0].trim().trim('"')] = key_value.size() == 1 ? "" : ustring::join("=", key_value, 1).trim().trim('"');
55-
}
56-
}
46+
from_string(stream_reader {file_path_}.read_to_end());
5747
}
5848

5949
ustring file_settings::read(const ustring& key, const ustring& default_value) noexcept {
@@ -100,6 +90,18 @@ void file_settings::save_as(const xtd::ustring& file_path) {
10090
stream.write(to_string());
10191
}
10292

93+
void file_settings::from_string(const xtd::ustring& text) {
94+
auto section = ustring::empty_string;
95+
for (auto line : text.split({10, 13}, string_split_options::remove_empty_entries)) {
96+
if (ustring::is_empty(line) || line.starts_with(';') || line.starts_with('#')) continue;
97+
if (line.starts_with('[') && line.ends_with(']')) section = line.substring(1, line.size() - 2);
98+
else {
99+
auto key_value = line.split({'='});
100+
section_key_values_[section][key_value[0].trim().trim('"')] = key_value.size() == 1 ? "" : ustring::join("=", key_value, 1).trim().trim('"');
101+
}
102+
}
103+
}
104+
103105
ustring file_settings::to_string() const noexcept {
104106
auto text = ustring::empty_string;
105107
for (auto [section, key_value] : section_key_values_) {

0 commit comments

Comments
 (0)