-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoon_example.cpp
More file actions
194 lines (152 loc) · 6.33 KB
/
toon_example.cpp
File metadata and controls
194 lines (152 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Example usage of the TOON parser library
// Demonstrates parsing, value access, and construction
#include "toon_parser.h"
#include <iostream>
#include <iomanip>
// Example 1: Basic file parsing
void example_basic_parsing() {
std::cout << "\n=== Example 1: Basic File Parsing ===\n";
try {
auto result = toon::Parser::parse_file("example.toon");
if (!result) {
std::cerr << "Parse error: " << result.error << "\n";
return;
}
auto config = result.table;
// Access simple values
if (config->contains("title")) {
std::cout << "Title: " << config->get("title").as_string() << "\n";
}
// Access nested table
if (config->contains("database")) {
auto db = config->get("database").as_table();
std::cout << "Database:\n";
std::cout << " Server: " << db->get("server").as_string() << "\n";
std::cout << " Port: " << db->get("port").as_int() << "\n";
std::cout << " Enabled: " << std::boolalpha << db->get("enabled").as_bool() << "\n";
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
// Example 2: Safe value access with defaults
void example_safe_access() {
std::cout << "\n=== Example 2: Safe Value Access ===\n";
try {
auto result = toon::Parser::parse_file("example.toon");
if (!result) {
std::cerr << "Parse error: " << result.error << "\n";
return;
}
auto config = result.table;
// Using value_or for safe access with defaults
std::string title = config->get("title").value_or<std::string>("Untitled");
int64_t port = config->get("database").as_table()->get("port").value_or<int64_t>(8080);
std::cout << "Title: " << title << "\n";
std::cout << "Port: " << port << "\n";
// Access non-existent key safely
std::string missing = config->get("nonexistent").value_or<std::string>("default_value");
std::cout << "Missing key: " << missing << "\n";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
// Example 3: Working with arrays
void example_arrays() {
std::cout << "\n=== Example 3: Array Processing ===\n";
try {
auto result = toon::Parser::parse_file("example.toon");
if (!result) {
std::cerr << "Parse error: " << result.error << "\n";
return;
}
auto config = result.table;
// Access array
if (config->contains("servers")) {
auto servers = config->get("servers").as_array();
std::cout << "Number of servers: " << servers->size() << "\n";
for (size_t i = 0; i < servers->size(); i++) {
auto server = servers->at(i).as_table();
std::cout << "Server " << (i + 1) << ":\n";
std::cout << " IP: " << server->get("ip").as_string() << "\n";
std::cout << " Role: " << server->get("role").as_string() << "\n";
}
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}
// Example 4: Programmatic construction
void example_construction() {
std::cout << "\n=== Example 4: Programmatic Construction ===\n";
// Create a new table
auto config = std::make_shared<toon::Table>();
// Add simple values
config->insert("app_name", toon::Value("MyApp"));
config->insert("version", toon::Value(2));
config->insert("debug", toon::Value(true));
// Create nested table
auto database = std::make_shared<toon::Table>();
database->insert("server", toon::Value("localhost"));
database->insert("port", toon::Value(5432));
database->insert("username", toon::Value("admin"));
config->insert("database", toon::Value(database));
// Create array of servers
auto servers = std::make_shared<toon::Array>();
auto server1 = std::make_shared<toon::Table>();
server1->insert("ip", toon::Value("192.168.1.10"));
server1->insert("role", toon::Value("primary"));
servers->push_back(toon::Value(server1));
auto server2 = std::make_shared<toon::Table>();
server2->insert("ip", toon::Value("192.168.1.11"));
server2->insert("role", toon::Value("backup"));
servers->push_back(toon::Value(server2));
config->insert("servers", toon::Value(servers));
// Display constructed data
std::cout << "App: " << config->get("app_name").as_string() << "\n";
std::cout << "Version: " << config->get("version").as_int() << "\n";
std::cout << "Debug: " << std::boolalpha << config->get("debug").as_bool() << "\n";
std::cout << "Database server: " << config->get("database").as_table()->get("server").as_string() << "\n";
std::cout << "Servers configured: " << config->get("servers").as_array()->size() << "\n";
}
// Example 5: Parsing from string
void example_parse_string() {
std::cout << "\n=== Example 5: Parse from String ===\n";
std::string toon_content = R"(
title: Simple Config
version: 1
database:
host: localhost
port: 3306
features[3]:
fast
secure
scalable
)";
auto result = toon::Parser::parse_string(toon_content);
if (!result) {
std::cerr << "Parse error: " << result.error << "\n";
return;
}
auto config = result.table;
std::cout << "Title: " << config->get("title").as_string() << "\n";
std::cout << "Version: " << config->get("version").as_int() << "\n";
auto db = config->get("database").as_table();
std::cout << "Database host: " << db->get("host").as_string() << "\n";
std::cout << "Database port: " << db->get("port").as_int() << "\n";
auto features = config->get("features").as_array();
std::cout << "Features (" << features->size() << "):\n";
for (size_t i = 0; i < features->size(); i++) {
std::cout << " - " << features->at(i).as_string() << "\n";
}
}
int main() {
std::cout << "TOON Parser Examples\n";
std::cout << "====================\n";
example_basic_parsing();
example_safe_access();
example_arrays();
example_construction();
example_parse_string();
return 0;
}