Skip to content

Commit dbf0e71

Browse files
committed
Fix key value comments
1 parent 66c31ff commit dbf0e71

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,13 @@ namespace xtd {
398398
void write_string(const xtd::ustring& section, const xtd::ustring& key, const xtd::ustring& value) noexcept;
399399

400400
bool auto_save_ = false;
401-
std::map<xtd::ustring, xtd::ustring> after_key_comment_;
401+
std::map<xtd::ustring, string_map> after_key_comment_;
402402
std::map<xtd::ustring, xtd::ustring> after_section_comment_;
403-
std::map<xtd::ustring, xtd::ustring> before_key_comment_;
403+
std::map<xtd::ustring, string_map> before_key_comment_;
404404
std::map<xtd::ustring, xtd::ustring> before_section_comment_;
405405
xtd::ustring bottom_file_comment_;
406406
xtd::ustring file_path_;
407-
std::map<xtd::ustring, xtd::ustring> key_comment_;
407+
std::map<xtd::ustring, string_map> key_comment_;
408408
std::map<xtd::ustring, xtd::ustring> section_comment_;
409409
std::map<xtd::ustring, string_map> section_key_values_;
410410
std::iostream* stream_ = nullptr;

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,16 @@ void file_settings::from_string(const xtd::ustring& text) {
137137
//comment = ustring::empty_string;
138138
auto key_value = line.split({key_value_separator});
139139
if (key_value.size() == 1 ) {
140-
if (!ustring::is_empty(comment)) before_key_comment_[unescaping(key_value[0].trim().trim(string_delimiter))] = comment;
140+
if (!ustring::is_empty(comment)) before_key_comment_[section][unescaping(key_value[0].trim().trim(string_delimiter))] = comment;
141141
section_key_values_[section][unescaping(key_value[0].trim().trim(string_delimiter))] = "";
142142
} else {
143143
auto key_comment = ustring::empty_string;
144144
auto value = separate_comment(ustring::join(ustring::format("{}", key_value_separator), key_value, 1), key_comment);
145145
if (value.starts_with(string_delimiter) && value.ends_with(string_delimiter)) value = value.trim(string_delimiter);
146146
if (value.starts_with(alt_string_delimiter) && value.ends_with(alt_string_delimiter)) value = value.trim(alt_string_delimiter);
147-
if (!ustring::is_empty(key_comment)) key_comment_[unescaping(key_value[0].trim().trim(string_delimiter))] = key_comment;
147+
if (!ustring::is_empty(key_comment)) key_comment_[section][unescaping(key_value[0].trim().trim(string_delimiter))] = key_comment;
148148
section_key_values_[section][unescaping(key_value[0].trim().trim(string_delimiter))] = unescaping(value);
149-
if (!ustring::is_empty(comment)) before_key_comment_[unescaping(key_value[0].trim().trim(string_delimiter))] = comment;
149+
if (!ustring::is_empty(comment)) before_key_comment_[section][unescaping(key_value[0].trim().trim(string_delimiter))] = comment;
150150
}
151151
comment = ustring::empty_string;
152152
}
@@ -233,12 +233,21 @@ ustring file_settings::to_string() const noexcept {
233233
auto as_it = after_section_comment_.find(section);
234234
if (as_it != after_section_comment_.end() && !ustring::is_empty(as_it->second)) text += split_comment(as_it->second);
235235
for (auto [key, value] : key_value) {
236-
auto bk_it = before_key_comment_.find(key);
237-
if (bk_it != before_key_comment_.end() && !ustring::is_empty(bk_it->second)) text += split_comment(bk_it->second);
238-
auto k_it = key_comment_.find(key);
239-
text += ustring::format("{} {} {}{}\n", key, key_value_separator, value.starts_with(' ') || value.starts_with('\t') || value.ends_with(' ') || value.ends_with('\t') || value.contains(comment_delimiter) || value.contains(alt_comment_delimiter) || value.contains(key_value_separator) ? ustring::format("\"{}\"", value) : value, k_it != key_comment_.end() && !ustring::is_empty(k_it->second) ? ustring::format(" {}", k_it->second) : "");
240-
auto ak_it = after_key_comment_.find(key);
241-
if (ak_it != after_key_comment_.end() && !ustring::is_empty(ak_it->second)) text += split_comment(ak_it->second);
236+
if (before_key_comment_.find(section) != before_key_comment_.end()) {
237+
auto bk_it = before_key_comment_.at(section).find(key);
238+
if (bk_it != before_key_comment_.at(section).end() && !ustring::is_empty(bk_it->second)) text += split_comment(bk_it->second);
239+
}
240+
if (key_comment_.find(section) == key_comment_.end())
241+
text += ustring::format("{} {} {}\n", key, key_value_separator, value.starts_with(' ') || value.starts_with('\t') || value.ends_with(' ') || value.ends_with('\t') || value.contains(comment_delimiter) || value.contains(alt_comment_delimiter) || value.contains(key_value_separator) ? ustring::format("\"{}\"", value) : value);
242+
else {
243+
auto k_it = key_comment_.at(section).find(key);
244+
text += ustring::format("{} {} {}{}\n", key, key_value_separator, value.starts_with(' ') || value.starts_with('\t') || value.ends_with(' ') || value.ends_with('\t') || value.contains(comment_delimiter) || value.contains(alt_comment_delimiter) || value.contains(key_value_separator) ? ustring::format("\"{}\"", value) : value, k_it != key_comment_.at(section).end() && !ustring::is_empty(k_it->second) ? ustring::format(" {}", k_it->second) : "");
245+
}
246+
247+
if (after_key_comment_.find(section) != after_key_comment_.end()) {
248+
auto ak_it = after_key_comment_.at(section).find(key);
249+
if (ak_it != after_key_comment_.at(section).end() && !ustring::is_empty(ak_it->second)) text += split_comment(ak_it->second);
250+
}
242251
}
243252
}
244253
if (!ustring::is_empty(bottom_file_comment_)) text += environment::new_line() + split_comment(bottom_file_comment_);

0 commit comments

Comments
 (0)