Skip to content

Commit

Permalink
Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
StormBytePP committed Feb 19, 2025
1 parent 32a4dc5 commit 40d2ecb
Showing 1 changed file with 190 additions and 25 deletions.
215 changes: 190 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ StormByte is a comprehensive, cross-platform C++ library aimed at easing system
- [Installation](#Installation)
- [Modules](#Modules)
- [System](#System)
- [Config](#Config)
- [Config](#Configuration)
- [Log](#Log)
- [Database](#Database)
- [Contributing](#Contributing)
Expand Down Expand Up @@ -87,49 +87,214 @@ int main() {
}
```

### Config
### Configuration

The `Config` module provides a flexible and easy-to-use API for configuration management. It supports reading and writing configuration files with various types including groups, strings, integers, and doubles.
#### Overview

#### Example: Config
Example `example.cfg`:
The StormByte configuration format is designed to be flexible and human-readable. It supports various data types, including integers, floating-point numbers, strings, booleans, groups (nested configurations), and lists. The format also allows for comments to enhance readability and documentation.

#### Basic Structure

Configuration files consist of key-value pairs. Keys are strings that follow a specific naming convention, while values can be of different data types.

##### Example

```plaintext
username = "example_user"
timeout = 30
enable_feature = true
```

#### Naming Convention

- Keys and group names must consist of alphanumeric characters, underscores (`_`), and hyphens (`-`).
- Strings must be enclosed in double quotes (`"`).
- Special characters like newline (`\n`), tab (`\t`), and backslash (`\\`) are supported within strings.
- Boolean values are represented as `true` or `false`.

#### Groups

Groups are nested configurations that can contain other key-value pairs, groups, or lists. Groups are denoted by curly braces `{}`.

##### Example

```plaintext
settings = {
username = "example_user"
timeout = 30
enable_feature = true
}
```

#### Lists

Lists are sequences of values enclosed in square brackets `[]`. Lists can contain comments, values, and even nested groups.

##### Example

```plaintext
# Configuration file
favorite_numbers = [
3
14
42
]
```

#### Comments

Comments can be added using the `#` symbol. Comments can appear on their own line or after a value.

##### Example

```plaintext
# This is a comment
username = "example_user" # This is an inline comment
```

#### Advanced Examples

##### Group with Nested List and Comments

```plaintext
settings = {
username = "example_user"
timeout = 30
enable_feature = true
permissions = [
"read"
"write"
"admin"
]
username = "example_user" # This is a comment
timeout = 30
enable_feature = true
favorite_numbers = [ # List of favorite numbers
3
14
42
]
}
```

##### Complex Nested Structure

```plaintext
Group1 = {
Group2 = {
SubTestInt = 99
SubTestStr = "Sub Hello"
}
}
```

##### Special Characters in Strings

```plaintext
special_string = "This is a test string with special characters: \n, \t, \\"
```

##### Floating Point Precision

```plaintext
float_precise = 1.123456789012345
```

##### Large Numbers

```plaintext
large_number = 9223372036854775807 # INT64_MAX
small_number = -9223372036854775807 # INT64_MIN
```

#### Real Usage Example

Consider a real usage example where a main file reads a configuration file (`config.cfg`) and parses its values.

##### Configuration File (`config.cfg`)

```plaintext
settings = {
username = "example_user"
timeout = 30
enable_feature = true
}
database = {
host = "localhost"
port = 5432
user = "db_user"
password = "secret"
}
log_levels = [
"info"
"debug"
"error"
]
features = {
enable_new_feature = true
feature_timeout = 60.5
}
```

##### Main File (`main.cpp`)

```cpp
#include <StormByte/config/config.hxx>
#include <fstream>
#include <StormByte/system/system.hxx>
#include <iostream>
#include <fstream>

using namespace StormByte::Config;

// Example usage
int main() {
Config config;
std::fstream input_file;
input_file.open("example.cfg", std::ios::in);
config << input_file;
input_file.close();
if (config.Exists("settings/username")) {
std::cout << "Username: " << config["settings/username"].Value<std::string>() << std::endl;
}
return 0;
Config config;

// Read configuration from file
std::ifstream config_file("config.cfg");
if (!config_file.is_open()) {
std::cerr << "Failed to open config file." << std::endl;
return 1;
}

config_file >> config;
config_file.close();

// Access configuration values
try {
const Item& username = config["settings/username"];
const Item& timeout = config["settings/timeout"];
const Item& enable_feature = config["settings/enable_feature"];
const Item& db_host = config["database/host"];
const Item& db_port = config["database/port"];
const Item& db_user = config["database/user"];
const Item& db_password = config["database/password"];
const List& log_levels = config["log_levels"].Value<List>();
const Item& enable_new_feature = config["features/enable_new_feature"];
const Item& feature_timeout = config["features/feature_timeout"];

// Print configuration values
std::cout << "Username: " << username.Value<std::string>() << std::endl;
std::cout << "Timeout: " << timeout.Value<int>() << std::endl;
std::cout << "Enable Feature: " << enable_feature.Value<bool>() << std::endl;
std::cout << "Database Host: " << db_host.Value<std::string>() << std::endl;
std::cout << "Database Port: " << db_port.Value<int>() << std::endl;
std::cout << "Database User: " << db_user.Value<std::string>() << std::endl;
std::cout << "Database Password: " << db_password.Value<std::string>() << std::endl;
std::cout << "Log Levels: ";
for (const auto& level : log_levels) {
std::cout << level.Value<std::string>() << " ";
}
std::cout << std::endl;
std::cout << "Enable New Feature: " << enable_new_feature.Value<bool>() << std::endl;
std::cout << "Feature Timeout: " << feature_timeout.Value<double>() << std::endl;

} catch (const StormByte::Config::Exception& e) {
std::cerr << "Error accessing configuration: " << e.what() << std::endl;
return 1;
}

return 0;
}
```

#### Summary

The StormByte configuration format is designed to be flexible and easy to use. It supports various data types, including nested structures, lists, and comments. By following the provided examples and guidelines, you can create comprehensive and readable configuration files for your StormByte projects. The real usage example demonstrates how to read and parse a configuration file in a main program, providing a practical application of the configuration format.

### Log

The `Log` module provides a comprehensive logging framework with support for different logging levels and outputs.
Expand Down

0 comments on commit 40d2ecb

Please sign in to comment.