-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml_reader.cpp
More file actions
71 lines (60 loc) · 2.46 KB
/
yaml_reader.cpp
File metadata and controls
71 lines (60 loc) · 2.46 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
#include <iostream>
#include "../src/yaml_c_wrapper.h"
// If file name is provided as a command line argument, this will print out the
// expanded contents of the file to the terminal, as well as to expand.pals.yaml.
// Otherwise, it will use the example file ex.pals.yaml
int main(int argc, char* argv[]) {
if (argc != 1) {
char* filename = argv[1];
std::string path = "../lattice_files/";
path += filename;
YAMLNodeHandle handle = parse_file(path.c_str());
lattice_expand(handle);
std::cout << "Printing out contents of file: " << filename << std::endl;
std::cout << yaml_to_string(handle) << std::endl;
write_file(handle, "../lattice_files/expand.pals.yaml");
return 0;
}
// reading a lattice from a yaml file
YAMLNodeHandle handle = parse_file("../lattice_files/ex.pals.yaml");
std::cout << "Printing out contents of file: " << "ex.pals.yaml" << std::endl;
// printing to terminal
std::cout << yaml_to_string(handle) << std::endl << std::endl;
// type checking
// prints "handle is of type sequence: 1", 1 meaning true
std::cout << "handle is of type sequence: " << (is_sequence(handle))
<< "\n";
// accessing sequence
YAMLNodeHandle node = get_index(handle, 0);
/* prints
the first element is:
thingB:
kind: Sextupole
*/
std::cout << "the first element is: \n" << yaml_to_string(node) << "\n";
// accessing map
// prints "the value at key 'thingB' is: kind: Sextupole"
std::cout << "\nthe value at key 'thingB' is: "
<< yaml_to_string(get_key(node, "thingB")) << "\n";
// creating a new node that's a map
YAMLNodeHandle map = create_map();
set_value_int(map, "apples", 5);
// creating a new node that's a sequence
YAMLNodeHandle sequence = create_sequence();
push_string(sequence, "magnet1");
push_string(sequence, "");
YAMLNodeHandle scalar = create_scalar();
set_scalar_string(scalar, "magnet2");
set_at_index(sequence, 1, scalar);
// give sequence a name by putting it in a map:
YAMLNodeHandle magnets = create_map();
set_value_node(magnets, "magnets", sequence);
// adding new nodes to lattice
push_node(handle, map);
push_node(handle, magnets);
// performing lattice expansion
lattice_expand(handle);
// writing modified lattice file to expand.pals.yaml
write_file(handle, "../lattice_files/expand.pals.yaml");
return 0;
}