xtd 0.2.0
Loading...
Searching...
No Matches
xtd::configuration::file_settings Class Reference
Inheritance diagram for xtd::configuration::file_settings:
xtd::object xtd::iequatable< file_settings > xtd::interface xtd::equality_operators< type_t, equatable_t >

Definition

Represents an object for storing and retrieving configuration information using text files in INI format.

Header
#include <xtd/configuration/file_settings>
Namespace
xtd
Library
xtd.core
Remarks
An INI file is a configuration file that consists of a text-based content with a structure and syntax comprising key–value pairs for properties, and sections that organize the properties.
Examples
The following code example demonstrates the use of xtd::configuration::file_settings class.
#include <xtd/configuration/file_settings>
#include <xtd/console>
using namespace xtd;
using namespace xtd::configuration;
auto write_file_settings() {
auto file = file_settings {"example.ini"};
file.top_file_comment("Settings file used by file_settings example.\nCopyright (c) 2024 Gammasoft. All rights reserved.");
file.write("auto_close", true);
file.write("caption", "file_settings example");
file.write("Thread \"Process\"", "timeout", 100_ms);
file.save();
}
auto reset_file_settings() {
auto file = file_settings {"example.ini"};
file.reset();
}
auto read_file_settings() {
auto file = file_settings {"example.ini"};
console::write_line("----------------------------------------");
console::write_line("\"exemple.ini\" file content :");
console::write_line(file.to_string());
console::write_line("----------------------------------------");
console::write_line("read keys :");
console::write_line("auto_close = {}", file.read("auto_close", false));
console::write_line("caption = {}", file.read("caption", "example"));
console::write_line("Thread \"Process\"", "time_out = {}", file.read("timeout", 50_ms));
console::write_line("----------------------------------------");
}
auto main() -> int {
write_file_settings();
// Uncomment the following line to see what happens if the "example.ini" file doesn't exist.
//reset_file_settings();
read_file_settings();
}
// This code produces the following output :
//
// ----------------------------------------
// "exemple.ini" file content :
// # Settings file used by file_settings example.
// # Copyright (c) 2024 Gammasoft. All rights reserved.
//
// auto_close = true
// caption = file_settings example
//
// [Thread "Process"]
// timeout = 00:00:00.1000000
//
// ----------------------------------------
// read keys :
// auto_close = true
// caption = file_settings example
// Thread "Process"
// ----------------------------------------
xtd::string top_file_comment() const noexcept
Gets top file comment text.
void reset()
Reset current settings.
Represents an object for storing and retrieving configuration information using text files in INI for...
Definition file_settings.h:32
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Contains the types that provide the programming model for handling configuration data.
Definition file_settings.h:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
file_settings.cpp.

Protected Member Functions

virtual xtd::string convert_comment_to_text (const xtd::string &text) const noexcept
 
virtual xtd::string convert_text_to_comment (const xtd::string &text) const noexcept
 

Public Aliass

using string_dictionary = xtd::collections::generic::sorted_dictionary< xtd::string, xtd::string >
 Represents a xtd::collections::generic::sorted_dictionary with the key and the value strongly typed to be strings.
 
using string_collection = xtd::collections::specialized::string_collection
 Represents a collection of strings.
 

Public Constructors

 file_settings () noexcept=default
 Initialize an xtd::configuration::file_settings without loading a file.
 
 file_settings (const xtd::string &file_path)
 Initialize an xtd::configuration::file_settings with file path to load and save.
 
 file_settings (std::iostream &stream)
 Initialize an xtd::configuration::file_settings with a stream to load and save.
 

Public Properties

bool auto_save () const noexcept
 Gets whether save should be called on the xtd::configuration::file_settings destructor.
 
void auto_save (bool value) noexcept
 Sets whether save should be called on the xtd::configuration::file_settings destructor.
 
xtd::string bottom_file_comment () const noexcept
 Gets bottom file comment text.
 
file_settingsbottom_file_comment (const xtd::string &value) noexcept
 Sets bottom file comment text.
 
const xtd::stringfile_path () const noexcept
 Gets the file path of the current instance.
 
string_dictionary key_values () const noexcept
 Gets all key-value pairs from global section.
 
string_dictionary key_values (const xtd::string &section) const noexcept
 Gets all key-value pairs from a specified section.
 
string_collection keys () const noexcept
 Gets all keys from global section.
 
string_collection keys (const xtd::string &section) const noexcept
 Gets all keys from a specified section.
 
string_collection sections () const noexcept
 Gets all sections.
 
std::optional< std::reference_wrapper< std::iostream > > stream () const noexcept
 Gets the stream of the current instance.
 
xtd::string top_file_comment () const noexcept
 Gets top file comment text.
 
file_settingstop_file_comment (const xtd::string &value) noexcept
 Sets top file comment text.
 

Public Methods

bool equals (const file_settings &obj) const noexcept override
 Indicates whether the current object is equal to another object of the same type.
 
virtual void from_string (const xtd::string &text)
 Sets the current settings with parsing the xtd::string in INI format.
 
void load (const xtd::string &file_path)
 Loads settings from specified file.
 
void load (std::istream &stream)
 Loads settings from specified stream.
 
xtd::string read (const xtd::string &key, const xtd::string &default_value) noexcept
 Reads a value for specified key in the global section. If not found default value is used.
 
template<typename type_t >
type_t read (const xtd::string &key, const type_t &default_value)
 Reads a value for specified key in the global section. If not found default value is used.
 
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.
 
template<typename type_t >
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.
 
void remove (const xtd::string &key) noexcept
 Removes the specified key from the global section.
 
void remove (const xtd::string &section, const xtd::string &key) noexcept
 Removes the specified key from the specified section.
 
void remove_all_keys () noexcept
 Removes all keys from the global section.
 
void remove_all_keys (const xtd::string &section) noexcept
 Removes all key from the specified section.
 
void reset ()
 Reset current settings.
 
void save ()
 Save current settings.
 
void save_as (const xtd::string &file_path)
 Save current settings in the specified file path.
 
void save_as (std::ostream &stream)
 Save current settings in the specified stream.
 
xtd::string to_string () const noexcept override
 Returns a xtd::string that represents the current setting in INI format.
 
void write (const xtd::string &key, const xtd::string &value) noexcept
 Writes a specified value for specified key in the global section.
 
template<typename type_t >
void write (const xtd::string &key, type_t &&value)
 Writes a specified value for specified key in the global section.
 
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.
 
template<typename type_t >
void write (const xtd::string &section, const xtd::string &key, type_t &&value)
 Writes a specified value for specified key in the specified section.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 

Public Operators

const string_dictionaryoperator[] (const xtd::string &section) const noexcept
 Gets key-value pairs of the specified section.
 
string_dictionaryoperator[] (const xtd::string &section) noexcept
 Gets key-value pairs of the specified section.
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
- Public Member Functions inherited from xtd::iequatable< file_settings >
virtual bool equals (const file_settings &) const noexcept=0
 Indicates wheth er the current object is equal to another object of the same type.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

The documentation for this class was generated from the following file: