Skip to content

Commit 17ca3ff

Browse files
committed
Add file_settings example
1 parent 4b151ab commit 17ca3ff

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

examples/xtd.core.examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_projects(
99
boxing
1010
collections
1111
console
12+
configuration
1213
convert
1314
date_time
1415
delegates

examples/xtd.core.examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
* [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.
5555
* [wconsole](console/wconsole/README.md) shows how to use [xtd::console](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1console.html) class.
5656

57+
## [Configuration](configuration/README.md)
58+
59+
* [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.
60+
5761
## [Convert](convert/README.md)
5862

5963
* [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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(configuration)
4+
find_package(xtd REQUIRED)
5+
6+
add_projects(
7+
file_settings
8+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Configuration examples
2+
3+
[This folder](.) contains console examples used by [Reference Guide](https://gammasoft71.github.io/xtd/reference_guides/latest/) and more.
4+
5+
* [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.
6+
7+
## Build and run any project
8+
9+
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:
10+
11+
```shell
12+
xtdc run -t any_project_name
13+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(file_settings)
4+
find_package(xtd REQUIRED)
5+
add_sources(README.md src/file_settings.cpp)
6+
target_type(CONSOLE_APPLICATION)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# file_settings
2+
3+
Shows how to use [xtd::consiguration::file_settings](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1configuration_1_1file__settings.html) object.
4+
5+
## Sources
6+
7+
[src/file_settings.cpp](src/file_settings.cpp)
8+
9+
[CMakeLists.txt](CMakeLists.txt)
10+
11+
## Build and run
12+
13+
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:
14+
15+
```cmake
16+
xtdc run
17+
```
18+
19+
## Output
20+
21+
```
22+
----------------------------------------
23+
"exemple.ini" file content :
24+
# Settings file used by file_settings example.
25+
# Copyright (c) 2024 Gammasoft. All rights reserved.
26+
27+
auto_close = true
28+
caption = file_settings example
29+
30+
[Thread "Process"]
31+
timeout_ = 00:00:00.1000000
32+
33+
----------------------------------------
34+
read keys :
35+
auto_close = true
36+
caption = file_settings example
37+
Thread "Process"
38+
----------------------------------------
39+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <xtd/configuration/file_settings>
2+
#include <xtd/console>
3+
4+
using namespace xtd;
5+
using namespace xtd::configuration;
6+
7+
auto write_file_settings() {
8+
auto file = file_settings("example.ini");
9+
file.top_file_comment("Settings file used by file_settings example.\nCopyright (c) 2024 Gammasoft. All rights reserved.");
10+
file.write("auto_close", true);
11+
file.write("caption", "file_settings example");
12+
file.write("Thread \"Process\"", "timeout_", 100_ms);
13+
file.save();
14+
}
15+
16+
auto reset_file_settings() {
17+
auto file = file_settings("example.ini");
18+
file.reset();
19+
}
20+
21+
auto read_file_settings() {
22+
auto file = file_settings("example.ini");
23+
console::write_line("----------------------------------------");
24+
console::write_line("\"exemple.ini\" file content :");
25+
console::write_line(file.to_string());
26+
console::write_line("----------------------------------------");
27+
console::write_line("read keys :");
28+
console::write_line("auto_close = {}", file.read("auto_close", false));
29+
console::write_line("caption = {}", file.read("caption", "example"));
30+
console::write_line("Thread \"Process\"", "time_out = {}", file.read("timeout_", 50_ms));
31+
console::write_line("----------------------------------------");
32+
}
33+
34+
auto main() -> int {
35+
write_file_settings();
36+
// Uncomment the following line to see what happens if the "example.ini" file doesn't exist.
37+
//reset_file_settings();
38+
read_file_settings();
39+
}
40+
41+
// This code produces the following output:
42+
//
43+
// ----------------------------------------
44+
// "exemple.ini" file content :
45+
// # Settings file used by file_settings example.
46+
// # Copyright (c) 2024 Gammasoft. All rights reserved.
47+
//
48+
// auto_close = true
49+
// caption = file_settings example
50+
//
51+
// [Thread "Process"]
52+
// timeout_ = 00:00:00.1000000
53+
//
54+
// ----------------------------------------
55+
// read keys :
56+
// auto_close = true
57+
// caption = file_settings example
58+
// Thread "Process"
59+
// ----------------------------------------

0 commit comments

Comments
 (0)