-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_stream.cpp
More file actions
116 lines (97 loc) · 3.1 KB
/
demo_stream.cpp
File metadata and controls
116 lines (97 loc) · 3.1 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
/**
@brief INI Parser Library
A lightweight, single-header, speed and safety focused INI file parsing library written in C with C++ compatibility. Designed for simplicity and portability, this parser provides a low-footprint solution to decode INI format.
@date 2025-05-12
@version 1.0
@author Eray Ozturk | erayozturk1@gmail.com
@url github.com/diffstorm
@license MIT License
*/
#include "ini_parser.h"
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
// Structure to collect parsing results
typedef struct
{
std::map<std::string, std::map<std::string, std::string>> sections;
std::vector<std::string> comments;
std::vector<std::string> errors;
std::string current_section;
} ParserState;
// Handler function prototype
bool parsing_handler(ini_eventtype_t type,
const char *section,
const char *key,
const char *value,
void *userdata);
int main()
{
const char *ini_content =
"; Main configuration file\n"
"[network]\n"
"host = 127.0.0.1\n"
"port = 8080\n"
"[database]\n"
"user = admin\n"
"pass = secret\n"
"[invalid_section\n" // Missing closing bracket
"key = value\n";
ParserState state;
state.current_section = INI_LINE_SECTION;
size_t length = strlen(ini_content);
bool success = ini_parse_stream(ini_content, length, parsing_handler, &state);
// Display results
std::cout << "Parsing " << (success ? "completed" : "aborted") << "\n\n";
std::cout << "Comments (" << state.comments.size() << "):\n";
for(const auto &comment : state.comments)
{
std::cout << " " << comment << "\n";
}
std::cout << "\nErrors (" << state.errors.size() << "):\n";
for(const auto &error : state.errors)
{
std::cout << " " << error << "\n";
}
std::cout << "\nParsed data:\n";
for(const auto& [section, values] : state.sections)
{
std::cout << "[" << section << "]\n";
for(const auto& [key, val] : values)
{
std::cout << " " << key << " = " << val << "\n";
}
}
return 0;
}
bool parsing_handler(ini_eventtype_t type,
const char *section,
const char *key,
const char *value,
void *userdata)
{
ParserState *state = static_cast<ParserState *>(userdata);
switch(type)
{
case INI_EVENT_SECTION:
state->current_section = section;
state->sections[section]; // Create empty section
break;
case INI_EVENT_KEY_VALUE:
{
const char *actual_section = state->current_section.c_str();
state->sections[actual_section][key] = value;
break;
}
case INI_EVENT_COMMENT:
state->comments.push_back(value);
break;
case INI_EVENT_ERROR:
state->errors.push_back(value);
return false; // Abort parsing on first error
default:
break;
}
return true;
}