Skip to content

Commit

Permalink
Add file_settings example
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Jun 21, 2024
1 parent 4b151ab commit 17ca3ff
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/xtd.core.examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_projects(
boxing
collections
console
configuration
convert
date_time
delegates
Expand Down
4 changes: 4 additions & 0 deletions examples/xtd.core.examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
* [redirect_output](console/redirect_output/README.md) shows how to use [xtd::console](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1console.html) class.
* [wconsole](console/wconsole/README.md) shows how to use [xtd::console](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1console.html) class.

## [Configuration](configuration/README.md)

* [file_settings](configuration/file_settings/README.md) shows how to use [xtd::consiguration::file_settings](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1configuration_1_1file__settings.html) object.

## [Convert](convert/README.md)

* [as](convert/as/README.md) shows how to use [xtd::as](https://gammasoft71.github.io/xtd/reference_guides/latest/group__xtd__core.html#ga19379a1158ccd320e208b362f11295b7) operator.
Expand Down
8 changes: 8 additions & 0 deletions examples/xtd.core.examples/configuration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.20)

project(configuration)
find_package(xtd REQUIRED)

add_projects(
file_settings
)
13 changes: 13 additions & 0 deletions examples/xtd.core.examples/configuration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Configuration examples

[This folder](.) contains console examples used by [Reference Guide](https://gammasoft71.github.io/xtd/reference_guides/latest/) and more.

* [file_settings](file_settings/README.md) shows how to use [xtd::consiguration::file_settings](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1configuration_1_1file__settings.html) object.

## Build and run any project

Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:

```shell
xtdc run -t any_project_name
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(file_settings)
find_package(xtd REQUIRED)
add_sources(README.md src/file_settings.cpp)
target_type(CONSOLE_APPLICATION)
39 changes: 39 additions & 0 deletions examples/xtd.core.examples/configuration/file_settings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# file_settings

Shows how to use [xtd::consiguration::file_settings](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1configuration_1_1file__settings.html) object.

## Sources

[src/file_settings.cpp](src/file_settings.cpp)

[CMakeLists.txt](CMakeLists.txt)

## Build and run

Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:

```cmake
xtdc run
```

## 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"
----------------------------------------
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#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"
// ----------------------------------------

0 comments on commit 17ca3ff

Please sign in to comment.