xtd 0.2.0
file_settings.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "../core_export.hpp"
6#include "../collections/generic/sorted_dictionary.hpp"
7#include "../collections/specialized/string_collection.hpp"
8#include "../io/directory.hpp"
9#include "../io/file.hpp"
10#include "../io/path.hpp"
11#include "../iequatable.hpp"
12#include <map>
13
15namespace xtd {
17 namespace configuration {
32 class core_export_ file_settings : public object, public iequatable<file_settings> {
33 public:
35
38 using string_dictionary = std::map<xtd::string, xtd::string>;
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
242 bool equals(const xtd::object& obj) const noexcept override;
246 bool equals(const file_settings& other) const noexcept override;
247
252 virtual void from_string(const xtd::string& text);
253
271 void load(const xtd::string& file_path);
274 void load(std::istream& stream);
275
282 xtd::string read(const xtd::string& key, const xtd::string& default_value) noexcept;
289 template<class type_t>
290 type_t read(const xtd::string& key, const type_t& default_value) {
291 return xtd::parse<type_t>(read_string(xtd::string::empty_string, key, xtd::string::format("{}", default_value)));
292 }
299 xtd::string read(const xtd::string& section, const xtd::string& key, const xtd::string& default_value) noexcept;
306 template<class type_t>
307 type_t read(const xtd::string& section, const xtd::string& key, const type_t& default_value) {
308 return xtd::parse<type_t>(read_string(section, key, xtd::string::format("{}", default_value)));
309 }
310
337 void remove(const xtd::string& key) noexcept;
360 void remove(const xtd::string& section, const xtd::string& key) noexcept;
361
382 void remove_all_keys() noexcept;
404 void remove_all_keys(const xtd::string& section) noexcept;
405
428 void reset();
429
451 void save();
452
470 void save_as(const xtd::string& file_path);
474 void save_as(std::ostream& stream);
475
480 xtd::string to_string() const noexcept override;
481
504 void write(const xtd::string& key, const xtd::string& value) noexcept;
527 template<class type_t>
528 void write(const xtd::string& key, type_t&& value) {
529 write_string(xtd::string::empty_string, key, xtd::string::format("{}", value));
530 }
554 void write(const xtd::string& section, const xtd::string& key, const xtd::string& value) noexcept;
579 template<class type_t>
580 void write(const xtd::string& section, const xtd::string& key, type_t&& value) {
581 write_string(section, key, xtd::string::format("{}", value));
582 }
584
586
599 const string_dictionary& operator [](const xtd::string& section) const noexcept;
611 string_dictionary& operator [](const xtd::string& section) noexcept;
613
614 protected:
615 virtual xtd::string convert_comment_to_text(const xtd::string& text) const noexcept;
616 virtual xtd::string convert_text_to_comment(const xtd::string& text) const noexcept;
617
618 private:
619 xtd::string read_string(const xtd::string& section, const xtd::string& key, const xtd::string& default_value) noexcept;
620 void write_string(const xtd::string& section, const xtd::string& key, const xtd::string& value) noexcept;
621
622 bool auto_save_ = false;
623 std::map<xtd::string, string_dictionary> after_key_value_comment_;
624 std::map<xtd::string, xtd::string> after_section_comment_;
625 std::map<xtd::string, string_dictionary> before_key_value_comment_;
626 std::map<xtd::string, xtd::string> before_section_comment_;
627 xtd::string bottom_file_comment_;
628 xtd::string file_path_;
629 std::map<xtd::string, string_dictionary> key_value_comment_;
630 std::map<xtd::string, xtd::string> section_comment_;
631 std::map<xtd::string, string_dictionary> section_key_values_;
632 std::iostream* stream_ = nullptr;
633 xtd::string top_file_comment_;
634 };
635 }
636}
Represents text as a sequence of character units.
Definition basic_string.hpp:71
static const basic_string empty_string
Represents the empty basic_string.
Definition basic_string.hpp:116
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
Represents an object for storing and retrieving configuration information using text files in INI for...
Definition file_settings.hpp: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.hpp:580
void remove_all_keys() noexcept
Removes all keys from the global section.
std::map< 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.hpp: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.hpp:307
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
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 ...
#define core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::collections::generic::list< xtd::string > string_collection
Represents a collection of strings.
Definition string_collection.hpp:27
std::optional< type_t > optional
Represents the null_opt alias on std::nullopt_t.
Definition optional.hpp:175
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10