-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml_c_wrapper.cpp
More file actions
432 lines (365 loc) · 13.6 KB
/
yaml_c_wrapper.cpp
File metadata and controls
432 lines (365 loc) · 13.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <yaml-cpp/yaml.h>
#include <string>
#include <cstring>
#include <fstream>
// ======= LATTICE EXPANSION UTILS
// === REPLACE ===
YAML::Node replace_internal(YAML::Node name, std::map<std::string, YAML::Node>* seen) {
std::string str = name.as<std::string>();
if (seen->count(str)) {
return YAML::Clone(seen->at(str));
} else {
return name;
}
}
// === EXPAND ===
YAML::Node expand_internal(YAML::Node node, std::map<std::string, YAML::Node>* seen) {
if (node.IsSequence()) {
for (int i = 0; i < node.size(); i++) {
node[i] = expand_internal(node[i], seen);
}
return node;
} else if (node.IsScalar()) {
return replace_internal(node, seen);
} else if (node.IsMap()) {
for (auto ele : node) {
seen->insert({ele.first.as<std::string>(), ele.second});
node[ele.first.as<std::string>()] = expand_internal(ele.second, seen);
}
return node;
} else {
return node;
}
}
extern "C" {
typedef void* YAMLNodeHandle;
// === CREATION/DELETION ===
YAMLNodeHandle yaml_create_node() {
return new YAML::Node();
}
YAMLNodeHandle yaml_create_map() {
auto node = new YAML::Node();
*node = YAML::Node(YAML::NodeType::Map);
return node;
}
YAMLNodeHandle yaml_create_sequence() {
auto node = new YAML::Node();
*node = YAML::Node(YAML::NodeType::Sequence);
return node;
}
YAMLNodeHandle yaml_create_scalar() {
auto node = new YAML::Node();
*node = YAML::Node(YAML::NodeType::Scalar);
return node;
}
void yaml_delete_node(YAMLNodeHandle handle) {
delete static_cast<YAML::Node*>(handle);
}
// === PARSING ===
YAMLNodeHandle yaml_parse(const char* yaml_str) {
try {
return new YAML::Node(YAML::Load(yaml_str));
} catch (...) {
return nullptr;
}
}
YAMLNodeHandle yaml_parse_file(const char* filename) {
try {
return new YAML::Node(YAML::LoadFile(filename));
} catch (...) {
return nullptr;
}
}
// === TYPE CHECKS ===
bool yaml_is_scalar(YAMLNodeHandle handle) {
return static_cast<YAML::Node*>(handle)->IsScalar();
}
bool yaml_is_sequence(YAMLNodeHandle handle) {
return static_cast<YAML::Node*>(handle)->IsSequence();
}
bool yaml_is_map(YAMLNodeHandle handle) {
return static_cast<YAML::Node*>(handle)->IsMap();
}
bool yaml_is_null(YAMLNodeHandle handle) {
return static_cast<YAML::Node*>(handle)->IsNull();
}
// === ACCESS ===
YAMLNodeHandle yaml_get_key(YAMLNodeHandle handle, const char* key) {
auto node = static_cast<YAML::Node*>(handle);
auto child = (*node)[key];
if (!child.IsDefined()) {
return nullptr;
}
return new YAML::Node(child);
}
YAMLNodeHandle yaml_get_index(YAMLNodeHandle handle, int index) {
auto node = static_cast<YAML::Node*>(handle);
if (index < 0 || index >= node->size()) {
return nullptr;
}
return new YAML::Node((*node)[index]);
}
bool yaml_has_key(YAMLNodeHandle handle, const char* key) {
auto node = static_cast<YAML::Node*>(handle);
return (*node)[key].IsDefined();
}
int yaml_size(YAMLNodeHandle handle) {
return static_cast<YAML::Node*>(handle)->size();
}
char** yaml_get_keys(YAMLNodeHandle handle, int* out_count) {
auto node = static_cast<YAML::Node*>(handle);
if (!node->IsMap()) {
*out_count = 0;
return nullptr;
}
std::vector<std::string> keys;
for (auto it = node->begin(); it != node->end(); ++it) {
keys.push_back(it->first.as<std::string>());
}
*out_count = keys.size();
char** result = new char*[keys.size()];
for (size_t i = 0; i < keys.size(); i++) {
result[i] = new char[keys[i].length() + 1];
strcpy(result[i], keys[i].c_str());
}
return result;
}
// === CONVERT TO C TYPES (caller must free returned strings) ===
char* yaml_as_string(YAMLNodeHandle handle) {
try {
auto str = static_cast<YAML::Node*>(handle)->as<std::string>();
char* result = new char[str.length() + 1];
strcpy(result, str.c_str());
return result;
} catch (...) {
return nullptr;
}
}
int yaml_as_int(YAMLNodeHandle handle) {
try {
return static_cast<YAML::Node*>(handle)->as<int>();
} catch (...) {
return 0;
}
}
double yaml_as_float(YAMLNodeHandle handle) {
try {
return static_cast<YAML::Node*>(handle)->as<double>();
} catch (...) {
return 0.0;
}
}
bool yaml_as_bool(YAMLNodeHandle handle) {
try {
return static_cast<YAML::Node*>(handle)->as<bool>();
} catch (...) {
return false;
}
}
// === MODIFICATION ===
void yaml_set_string(YAMLNodeHandle handle, const char* key, const char* value) {
auto node = static_cast<YAML::Node*>(handle);
(*node)[key] = value;
}
void yaml_set_int(YAMLNodeHandle handle, const char* key, int value) {
auto node = static_cast<YAML::Node*>(handle);
(*node)[key] = value;
}
void yaml_set_float(YAMLNodeHandle handle, const char* key, double value) {
auto node = static_cast<YAML::Node*>(handle);
(*node)[key] = value;
}
void yaml_set_bool(YAMLNodeHandle handle, const char* key, bool value) {
auto node = static_cast<YAML::Node*>(handle);
(*node)[key] = value;
}
void yaml_set_node(YAMLNodeHandle handle, const char* key, YAMLNodeHandle value) {
auto node = static_cast<YAML::Node*>(handle);
auto val_node = static_cast<YAML::Node*>(value);
(*node)[key] = *val_node;
}
void yaml_set_scalar_string(YAMLNodeHandle handle, const char* value) {
if (handle == nullptr || value == nullptr) return;
auto node = static_cast<YAML::Node*>(handle);
*node = value;
}
void yaml_set_scalar_int(YAMLNodeHandle handle, int value) {
if (handle == nullptr) return;
auto node = static_cast<YAML::Node*>(handle);
*node = value;
}
void yaml_set_scalar_float(YAMLNodeHandle handle, double value) {
if (handle == nullptr) return;
auto node = static_cast<YAML::Node*>(handle);
*node = value;
}
void yaml_set_scalar_bool(YAMLNodeHandle handle, bool value) {
if (handle == nullptr) return;
auto node = static_cast<YAML::Node*>(handle);
*node = value;
}
// Set node at index for sequences
void yaml_set_at_index(YAMLNodeHandle handle, int index, YAMLNodeHandle value) {
auto node = static_cast<YAML::Node*>(handle);
auto val_node = static_cast<YAML::Node*>(value);
(*node)[index] = *val_node;
}
void yaml_push_string(YAMLNodeHandle handle, const char* value) {
auto node = static_cast<YAML::Node*>(handle);
node->push_back(value);
}
void yaml_push_int(YAMLNodeHandle handle, int value) {
auto node = static_cast<YAML::Node*>(handle);
node->push_back(value);
}
void yaml_push_float(YAMLNodeHandle handle, double value) {
auto node = static_cast<YAML::Node*>(handle);
node->push_back(value);
}
void yaml_push_node(YAMLNodeHandle handle, YAMLNodeHandle value) {
auto node = static_cast<YAML::Node*>(handle);
auto val_node = static_cast<YAML::Node*>(value);
node->push_back(*val_node);
}
// === WRITE TO FILE WITH EMITTER ===
bool yaml_write_file(YAMLNodeHandle handle, const char* filename) {
try {
auto node = static_cast<YAML::Node*>(handle);
std::ofstream fout(filename);
if (!fout.is_open()) {
return false;
}
YAML::Emitter out;
out << *node;
fout << out.c_str();
fout.close();
return true;
} catch (...) {
return false;
}
}
// Write with emitter control (removed SetWrap)
bool yaml_write_file_formatted(YAMLNodeHandle handle, const char* filename,
int indent, bool flow_maps, bool flow_seqs) {
try {
auto node = static_cast<YAML::Node*>(handle);
std::ofstream fout(filename);
if (!fout.is_open()) {
return false;
}
YAML::Emitter out;
// Set formatting options (only valid ones)
out.SetIndent(indent);
out.SetMapFormat(flow_maps ? YAML::Flow : YAML::Block);
out.SetSeqFormat(flow_seqs ? YAML::Flow : YAML::Block);
out.SetBoolFormat(YAML::TrueFalseBool); // true/false instead of yes/no
out.SetNullFormat(YAML::LowerNull); // null instead of ~
out.SetStringFormat(YAML::Auto); // Auto-detect if quotes needed
out << *node;
fout << out.c_str();
fout.close();
return true;
} catch (...) {
return false;
}
}
// Get YAML string with emitter
char* yaml_emit(YAMLNodeHandle handle, int indent) {
try {
auto node = static_cast<YAML::Node*>(handle);
YAML::Emitter out;
out.SetIndent(indent);
out.SetBoolFormat(YAML::TrueFalseBool);
out.SetNullFormat(YAML::LowerNull);
out << *node;
std::string str = out.c_str();
char* result = new char[str.length() + 1];
strcpy(result, str.c_str());
return result;
} catch (...) {
return nullptr;
}
}
// Advanced version with all available options
bool yaml_write_file_advanced(YAMLNodeHandle handle, const char* filename,
int indent,
bool flow_maps,
bool flow_seqs,
int bool_format, // 0=YesNo, 1=TrueFalse, 2=OnOff
int null_format, // 0=Tilde (~), 1=Null (null), 2=NULL, 3=Null
int string_format) { // 0=Auto, 1=SingleQuoted, 2=DoubleQuoted, 3=Literal
try {
auto node = static_cast<YAML::Node*>(handle);
std::ofstream fout(filename);
if (!fout.is_open()) {
return false;
}
YAML::Emitter out;
out.SetIndent(indent);
out.SetMapFormat(flow_maps ? YAML::Flow : YAML::Block);
out.SetSeqFormat(flow_seqs ? YAML::Flow : YAML::Block);
// Boolean format
switch(bool_format) {
case 0: out.SetBoolFormat(YAML::YesNoBool); break;
case 1: out.SetBoolFormat(YAML::TrueFalseBool); break;
case 2: out.SetBoolFormat(YAML::OnOffBool); break;
default: out.SetBoolFormat(YAML::TrueFalseBool);
}
// Null format
switch(null_format) {
case 0: out.SetNullFormat(YAML::TildeNull); break; // ~
case 1: out.SetNullFormat(YAML::LowerNull); break; // null
case 2: out.SetNullFormat(YAML::UpperNull); break; // NULL
case 3: out.SetNullFormat(YAML::CamelNull); break; // Null
default: out.SetNullFormat(YAML::LowerNull);
}
// String format
switch(string_format) {
case 0: out.SetStringFormat(YAML::Auto); break;
case 1: out.SetStringFormat(YAML::SingleQuoted); break;
case 2: out.SetStringFormat(YAML::DoubleQuoted); break;
case 3: out.SetStringFormat(YAML::Literal); break;
default: out.SetStringFormat(YAML::Auto);
}
out << *node;
fout << out.c_str();
fout.close();
return true;
} catch (...) {
return false;
}
}
// === UTILITY ===
char* yaml_to_string(YAMLNodeHandle handle) {
try {
YAML::Emitter out;
out << *static_cast<YAML::Node*>(handle);
std::string str = out.c_str();
char* result = new char[str.length() + 1];
strcpy(result, str.c_str());
return result;
} catch (...) {
return nullptr;
}
}
// Helper to free strings allocated by this library
void yaml_free_string(char* str) {
delete[] str;
}
void yaml_free_keys(char** keys, int count) {
for (int i = 0; i < count; i++) {
delete[] keys[i];
}
delete[] keys;
}
YAMLNodeHandle yaml_clone(YAMLNodeHandle handle) {
return new YAML::Node(YAML::Clone(*static_cast<YAML::Node*>(handle)));
}
// C interface for expand - handles the map internally
YAMLNodeHandle yaml_expand(YAMLNodeHandle handle) {
auto node = static_cast<YAML::Node*>(handle);
std::map<std::string, YAML::Node> seen;
YAML::Node result = expand_internal(*node, &seen);
return new YAML::Node(result);
}
}