xtd 0.2.0
Loading...
Searching...
No Matches
file_settings.h
Go to the documentation of this file.
1
4#pragma once
5#include "../core_export.h"
6#include "../collections/generic/sorted_dictionary.h"
7#include "../collections/specialized/string_collection.h"
8#include "../io/directory.h"
9#include "../io/file.h"
10#include "../io/path.h"
11#include "../iequatable.h"
12#include <map>
13
15namespace xtd {
17 namespace configuration {
32 class core_export_ file_settings : public object, public iequatable<file_settings> {
33 public:
35
39
43
45
50 file_settings() noexcept = default;
56 explicit file_settings(const xtd::string& file_path);
62 explicit file_settings(std::iostream& stream);
64
66 file_settings(file_settings&&) noexcept = default;
67 file_settings(const file_settings&) noexcept = default;
68 file_settings& operator =(const file_settings&) noexcept = default;
71
73
92 bool auto_save() const noexcept;
110 void auto_save(bool value) noexcept;
111
131 xtd::string bottom_file_comment() const noexcept;
152 file_settings& bottom_file_comment(const xtd::string& value) noexcept;
153
158 const xtd::string& file_path() const noexcept;
159
163 string_dictionary key_values() const noexcept;
168 string_dictionary key_values(const xtd::string& section) const noexcept;
169
173 string_collection keys() const noexcept;
178 string_collection keys(const xtd::string& section) const noexcept;
179
182 string_collection sections() const noexcept;
183
187 std::optional<std::reference_wrapper<std::iostream>> stream() const noexcept;
188
210 xtd::string top_file_comment() const noexcept;
233 file_settings& top_file_comment(const xtd::string& value) noexcept;
235
237
239 using object::equals;
243 bool equals(const file_settings& obj) const noexcept override;
244
249 virtual void from_string(const xtd::string& text);
250
268 void load(const xtd::string& file_path);
271 void load(std::istream& stream);
272
279 xtd::string read(const xtd::string& key, const xtd::string& default_value) noexcept;
286 template<typename type_t>
287 type_t read(const xtd::string& key, const type_t& default_value) {
288 return xtd::parse<type_t>(read_string(xtd::string::empty_string, key, xtd::string::format("{}", default_value)));
289 }
296 xtd::string read(const xtd::string& section, const xtd::string& key, const xtd::string& default_value) noexcept;
303 template<typename type_t>
304 type_t read(const xtd::string& section, const xtd::string& key, const type_t& default_value) {
305 return xtd::parse<type_t>(read_string(section, key, xtd::string::format("{}", default_value)));
306 }
307
334 void remove(const xtd::string& key) noexcept;
357 void remove(const xtd::string& section, const xtd::string& key) noexcept;
358
379 void remove_all_keys() noexcept;
401 void remove_all_keys(const xtd::string& section) noexcept;
402
425 void reset();
426
448 void save();
449
467 void save_as(const xtd::string& file_path);
471 void save_as(std::ostream& stream);
472
477 xtd::string to_string() const noexcept override;
478
501 void write(const xtd::string& key, const xtd::string& value) noexcept;
524 template<typename type_t>
525 void write(const xtd::string& key, type_t&& value) {
526 write_string(xtd::string::empty_string, key, xtd::string::format("{}", value));
527 }
551 void write(const xtd::string& section, const xtd::string& key, const xtd::string& value) noexcept;
576 template<typename type_t>
577 void write(const xtd::string& section, const xtd::string& key, type_t&& value) {
578 write_string(section, key, xtd::string::format("{}", value));
579 }
581
583
596 const string_dictionary& operator [](const xtd::string& section) const noexcept;
608 string_dictionary& operator [](const xtd::string& section) noexcept;
610
611 protected:
612 virtual xtd::string convert_comment_to_text(const xtd::string& text) const noexcept;
613 virtual xtd::string convert_text_to_comment(const xtd::string& text) const noexcept;
614
615 private:
616 xtd::string read_string(const xtd::string& section, const xtd::string& key, const xtd::string& default_value) noexcept;
617 void write_string(const xtd::string& section, const xtd::string& key, const xtd::string& value) noexcept;
618
619 bool auto_save_ = false;
620 std::map<xtd::string, string_dictionary> after_key_value_comment_;
621 std::map<xtd::string, xtd::string> after_section_comment_;
622 std::map<xtd::string, string_dictionary> before_key_value_comment_;
623 std::map<xtd::string, xtd::string> before_section_comment_;
624 xtd::string bottom_file_comment_;
625 xtd::string file_path_;
626 std::map<xtd::string, string_dictionary> key_value_comment_;
627 std::map<xtd::string, xtd::string> section_comment_;
628 std::map<xtd::string, string_dictionary> section_key_values_;
629 std::iostream* stream_ = nullptr;
630 xtd::string top_file_comment_;
631 };
632 }
633}
Represents text as a sequence of character units.
Definition basic_string.h:79
static const basic_string empty_string
Represents the empty basic_string.
Definition basic_string.h:124
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
Represents an object for storing and retrieving configuration information using text files in INI for...
Definition file_settings.h:32
xtd::string read(const xtd::string &section, const xtd::string &key, const xtd::string &default_value) noexcept
Reads a value for specified key in the specified section. If not found default value is used.
file_settings() noexcept=default
Initialize an xtd::configuration::file_settings without loading a file.
void write(const xtd::string &section, const xtd::string &key, const xtd::string &value) noexcept
Writes a specified value for specified key in the specified section.
void remove(const xtd::string &key) noexcept
Removes the specified key from the global section.
void write(const xtd::string &section, const xtd::string &key, type_t &&value)
Writes a specified value for specified key in the specified section.
Definition file_settings.h:577
void remove_all_keys() noexcept
Removes all keys from the global section.
xtd::collections::generic::sorted_dictionary< xtd::string, xtd::string > string_dictionary
Represents a xtd::collections::generic::sorted_dictionary with the key and the value strongly typed t...
Definition file_settings.h:38
void remove(const xtd::string &section, const xtd::string &key) noexcept
Removes the specified key from the specified section.
type_t read(const xtd::string &section, const xtd::string &key, const type_t &default_value)
Reads a value for specified key in the specified section. If not found default value is used.
Definition file_settings.h:304
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
std::map< key_t, value_t, helpers::comparer< key_t >, helpers::allocator< std::pair< const key_t, value_t > > > sorted_dictionary
Represents a collection of key/value pairs that are sorted on the key.
Definition sorted_dictionary.h:36
#define core_export_
Define shared library export.
Definition core_export.h:13
xtd::collections::generic::list< xtd::string > string_collection
Represents a collection of strings.
Definition string_collection.h:27
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10