xtd 0.2.0
Loading...
Searching...
No Matches
file_settings.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "../core_export.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
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 auto operator =(const file_settings&) noexcept -> file_settings& = default;
71
73
92 [[nodiscard]] auto auto_save() const noexcept -> bool;
110 auto auto_save(bool value) noexcept -> void;
111
131 [[nodiscard]] auto bottom_file_comment() const noexcept -> xtd::string;
152 auto bottom_file_comment(const xtd::string& value) noexcept -> file_settings&;
153
158 [[nodiscard]] auto file_path() const noexcept -> const xtd::string&;
159
163 [[nodiscard]] auto key_values() const noexcept -> string_dictionary;
168 [[nodiscard]] auto key_values(const xtd::string& section) const noexcept -> string_dictionary;
169
173 [[nodiscard]] auto keys() const noexcept -> string_collection;
178 [[nodiscard]] auto keys(const xtd::string& section) const noexcept -> string_collection;
179
182 [[nodiscard]] auto sections() const noexcept -> string_collection;
183
187 [[nodiscard]] auto stream() const noexcept -> std::optional<xtd::ref<std::iostream>>;
188
210 [[nodiscard]] auto top_file_comment() const noexcept -> xtd::string;
233 auto top_file_comment(const xtd::string& value) noexcept -> file_settings&;
235
237
242 [[nodiscard]] auto equals(const xtd::object& obj) const noexcept -> bool override;
246 [[nodiscard]] auto equals(const file_settings& other) const noexcept -> bool override;
247
252 virtual auto from_string(const xtd::string& text) -> void;
253
271 auto load(const xtd::string& file_path) -> void;
274 auto load(std::istream& stream) -> void;
275
282 [[nodiscard]] auto read(const xtd::string& key, const xtd::string& default_value) noexcept -> xtd::string;
289 template<class type_t>
290 [[nodiscard]] auto read(const xtd::string& key, const type_t& default_value) -> type_t {
291 return xtd::parse<type_t>(read_string(xtd::string::empty_string, key, xtd::string::format("{}", default_value)));
292 }
293
299 [[nodiscard]] auto read(const xtd::string& section, const xtd::string& key, const xtd::string& default_value) noexcept -> xtd::string;
306 template<class type_t>
307 [[nodiscard]] auto read(const xtd::string& section, const xtd::string& key, const type_t& default_value) -> type_t {
308 return xtd::parse<type_t>(read_string(section, key, xtd::string::format("{}", default_value)));
309 }
310
337 auto remove(const xtd::string& key) noexcept -> void;
360 auto remove(const xtd::string& section, const xtd::string& key) noexcept -> void;
361
382 auto remove_all_keys() noexcept -> void;
404 auto remove_all_keys(const xtd::string& section) noexcept -> void;
405
428 auto reset() -> void;
429
451 auto save() -> void;
452
470 auto save_as(const xtd::string& file_path) -> void;
474 auto save_as(std::ostream& stream) -> void;
475
480 [[nodiscard]] auto to_string() const noexcept -> xtd::string override;
481
504 auto write(const xtd::string& key, const xtd::string& value) noexcept -> void;
527 template<class type_t>
528 auto write(const xtd::string& key, type_t&& value) -> void {
529 write_string(xtd::string::empty_string, key, xtd::string::format("{}", value));
530 }
531
554 auto write(const xtd::string& section, const xtd::string& key, const xtd::string& value) noexcept -> void;
579 template<class type_t>
580 auto write(const xtd::string& section, const xtd::string& key, type_t&& value) -> void {
581 write_string(section, key, xtd::string::format("{}", value));
582 }
583
584
586
599 auto operator [](const xtd::string& section) const noexcept -> const string_dictionary&;
611 auto operator [](const xtd::string& section) noexcept -> string_dictionary&;
613
614 protected:
615 virtual auto convert_comment_to_text(const xtd::string& text) const noexcept -> xtd::string;
616 virtual auto convert_text_to_comment(const xtd::string& text) const noexcept -> xtd::string;
617
618 private:
619 auto read_string(const xtd::string& section, const xtd::string& key, const xtd::string& default_value) noexcept -> xtd::string;
620 auto write_string(const xtd::string& section, const xtd::string& key, const xtd::string& value) noexcept -> void;
621
622 bool auto_save_ = false;
627 xtd::string bottom_file_comment_;
628 xtd::string file_path_;
632 std::iostream* stream_ = nullptr;
633 xtd::string top_file_comment_;
634 };
635 }
636}
static const basic_string empty_string
Definition basic_string.hpp:111
auto auto_save() const noexcept -> bool
Gets whether save should be called on the xtd::configuration::file_settings destructor.
auto equals(const xtd::object &obj) const noexcept -> bool override
Indicates whether the current object is equal to another object of the same type.
auto load(const xtd::string &file_path) -> void
Loads settings from specified file.
auto write(const xtd::string &key, const xtd::string &value) noexcept -> void
Writes a specified value for specified key in the global section.
auto read(const xtd::string &section, const xtd::string &key, const type_t &default_value) -> type_t
Reads a value for specified key in the specified section. If not found default value is used.
Definition file_settings.hpp:307
auto write(const xtd::string &section, const xtd::string &key, type_t &&value) -> void
Writes a specified value for specified key in the specified section.
Definition file_settings.hpp:580
auto keys() const noexcept -> string_collection
Gets all keys from global section.
auto bottom_file_comment() const noexcept -> xtd::string
Gets bottom file comment text.
file_settings() noexcept=default
Initialize an xtd::configuration::file_settings without loading a file.
auto remove_all_keys() noexcept -> void
Removes all keys from the global section.
auto save_as(const xtd::string &file_path) -> void
Save current settings in the specified file path.
virtual auto from_string(const xtd::string &text) -> void
Sets the current settings with parsing the xtd::string in INI format.
auto top_file_comment() const noexcept -> xtd::string
Gets top file comment text.
auto read(const xtd::string &key, const xtd::string &default_value) noexcept -> xtd::string
Reads a value for specified key in the global section. If not found default value is used.
auto read(const xtd::string &section, const xtd::string &key, const xtd::string &default_value) noexcept -> xtd::string
Reads a value for specified key in the specified section. If not found default value is used.
auto write(const xtd::string &section, const xtd::string &key, const xtd::string &value) noexcept -> void
Writes a specified value for specified key in the specified section.
auto key_values() const noexcept -> string_dictionary
Gets all key-value pairs from global section.
xtd::collections::specialized::string_collection string_collection
Represents a collection of strings.
Definition file_settings.hpp:41
auto reset() -> void
Reset current settings.
auto save() -> void
Save current settings.
auto remove(const xtd::string &key) noexcept -> void
Removes the specified key from the global section.
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current setting in INI format.
auto remove(const xtd::string &section, const xtd::string &key) noexcept -> void
Removes the specified key from the specified section.
auto file_path() const noexcept -> const xtd::string &
Gets the file path of the current instance.
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.hpp:38
auto sections() const noexcept -> string_collection
Gets all sections.
auto stream() const noexcept -> std::optional< xtd::ref< std::iostream > >
Gets the stream of the current instance.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
object()=default
Create a new instance of the ultimate base class object.
Contains core_export_ keyword.
Contains xtd::io::directory class.
Contains xtd::io::file class.
std::map< key_t, value_t, lesser_t, allocator_t > sorted_dictionary
Represents a collection of key/value pairs that are sorted on the key.
Definition sorted_dictionary.hpp:38
#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
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
xtd::reference_wrapper_object< type_t > ref
The xtd::ref object is a reference wrapper.
Definition ref.hpp:25
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:26
value_t parse(const std::string &str)
Convert a string into a type.
Definition parse.hpp:34
@ other
The operating system is other.
Definition platform_id.hpp:60
Contains xtd::iequatable interface.
Contains the types that provide the programming model for handling configuration data.
Definition file_settings.hpp:17
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::io::path class.
Contains xtd::collections::generic::sorted_dictionary <key_t, value_t> class.
Contains xtd::collections::specialized::string_collection alias.