steam-min-cpp
Loading...
Searching...
No Matches
vdf.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <string>
10#include <unordered_map>
11#include <variant>
12#include <vector>
13#include <cstdint>
14
15namespace Steam::Utils::VDF {
16
17 struct VDFNode;
18
22 using Object = std::unordered_map<std::string, VDFNode>;
23
27 using Value = std::variant<
28 std::string,
29 int32_t,
30 uint64_t,
31 float,
32 Object
33 >;
34
40 struct VDFNode {
41
44
49 const VDFNode* get(const std::string& key) const {
50 if (auto obj = std::get_if<Object>(&value)) {
51 auto it = obj->find(key);
52 if (it != obj->end())
53 return &it->second;
54 }
55 return nullptr;
56 }
57
59 VDFNode* get(const std::string& key) {
60 if (auto obj = std::get_if<Object>(&value)) {
61 auto it = obj->find(key);
62 if (it != obj->end())
63 return &it->second;
64 }
65 return nullptr;
66 }
67
73 std::string get_string_or(const std::string& key, std::string def) const {
74 if (auto node = get(key)) {
75 if (auto val = std::get_if<std::string>(&node->value))
76 return *val;
77 }
78 return def;
79 }
80
82 int32_t get_int_or(const std::string& key, int32_t def) const {
83 if (auto node = get(key)) {
84 if (auto val = std::get_if<int32_t>(&node->value))
85 return *val;
86 }
87 return def;
88 }
89
91 uint64_t get_uint64_or(const std::string& key, uint64_t def) const {
92 if (auto node = get(key)) {
93 if (auto val = std::get_if<uint64_t>(&node->value))
94 return *val;
95 }
96 return def;
97 }
98
100 float get_float_or(const std::string& key, float def) const {
101 if (auto node = get(key)) {
102 if (auto val = std::get_if<float>(&node->value))
103 return *val;
104 }
105 return def;
106 }
107
117 const VDFNode* get_path(std::initializer_list<std::string> keys) const {
118 const VDFNode* current = this;
119
120 for (const auto& key : keys) {
121 if (!current)
122 return nullptr;
123
124 auto obj = std::get_if<Object>(&current->value);
125 if (!obj)
126 return nullptr;
127
128 auto it = obj->find(key);
129 if (it == obj->end())
130 return nullptr;
131
132 current = &it->second;
133 }
134
135 return current;
136 }
137
142 return std::get<Object>(value);
143 }
144
146 const Object& as_object() const {
147 return std::get<Object>(value);
148 }
149
153 std::string& as_string() {
154 return std::get<std::string>(value);
155 }
156
158 const std::string& as_string() const {
159 return std::get<std::string>(value);
160 }
161
163 int32_t as_int() const {
164 return std::get<int32_t>(value);
165 }
166
168 uint64_t as_uint64() const {
169 return std::get<uint64_t>(value);
170 }
171
173 float as_float() const {
174 return std::get<float>(value);
175 }
176 };
177
182 Object parse_text(std::string_view text);
183
189 Object parse_binary(const uint8_t* data, size_t size);
190
194 static void stringify_object(const Object& obj, std::string& out, int indent = 0);
195
199 inline std::string stringify(const Object& obj)
200 {
201 std::string out;
202 stringify_object(obj, out, 0);
203 return out;
204 }
205}
Node in a VDF tree.
Definition vdf.hpp:40
VDFNode * get(const std::string &key)
Find a child node by key.
Definition vdf.hpp:59
std::string & as_string()
Access the node as a string.
Definition vdf.hpp:153
const VDFNode * get(const std::string &key) const
Find a child node by key.
Definition vdf.hpp:49
float get_float_or(const std::string &key, float def) const
Retrieve a float value by key with fallback.
Definition vdf.hpp:100
uint64_t as_uint64() const
Access the node as a uint64_t.
Definition vdf.hpp:168
Object & as_object()
Access the node as an object.
Definition vdf.hpp:141
int32_t as_int() const
Access the node as an int32_t.
Definition vdf.hpp:163
std::string get_string_or(const std::string &key, std::string def) const
Retrieve a string value by key with a default fallback.
Definition vdf.hpp:73
int32_t get_int_or(const std::string &key, int32_t def) const
Retrieve an int32_t value by key with fallback.
Definition vdf.hpp:82
uint64_t get_uint64_or(const std::string &key, uint64_t def) const
Retrieve a uint64_t value by key with fallback.
Definition vdf.hpp:91
const std::string & as_string() const
Access the node as a string.
Definition vdf.hpp:158
Value value
Stored node value.
Definition vdf.hpp:43
const VDFNode * get_path(std::initializer_list< std::string > keys) const
Find a nested node using a path of keys.
Definition vdf.hpp:117
float as_float() const
Access the node as a float.
Definition vdf.hpp:173
const Object & as_object() const
Access the node as an object.
Definition vdf.hpp:146
std::variant< std::string, int32_t, uint64_t, float, Object > Value
Value stored in a VDF node.
Definition vdf.hpp:27
Object parse_text(std::string_view text)
Parse VDF text into an object tree.
Object parse_binary(const uint8_t *data, size_t size)
Parse binary VDF data.
std::string stringify(const Object &obj)
Convert a VDF object into a human-readable string.
Definition vdf.hpp:199
std::unordered_map< std::string, VDFNode > Object
Object node type.
Definition vdf.hpp:22