Read a YAML file using a C script
The second answer to this question has a little bit better example of using the library to parse yaml file into something more useful.
Read a YAML file into C#?
First problem is you are reading filename itself as yaml document, not file contents. Use StreamReader
instead of StringReader
:
using (var reader = new StreamReader(filepath)) {
// Load the stream
var yaml = new YamlStream();
yaml.Load(reader);
// the rest
}
Then, YAML 1.0 is VERY old, so it seems parser does not understand %YAML: 1.0
directive (in later versions the ":" was removed from this directive). So remove it and "---" (not needed). Instead of removing you can change it to %YAML 1.1
(no ":", version 1.1, 1.0 is rejected by parser).
Then, intendation is important. This version (note spaces after ":" in "id" and "corners") will be parsed with your code without issues:
%YAML 1.1
---
aruco_bc_dict: ARUCO
aruco_bc_nmarkers: 24
aruco_bc_mInfoType: 1
aruco_bc_markers:
- { id: 0, corners: [ [ -1.2928584814071655e+00, 8.1286805868148804e-01,
-1.6458697617053986e-01 ], [ -1.1746160984039307e+00,
8.1223398447036743e-01, -1.4413379132747650e-01 ], [
-1.1754947900772095e+00, 6.9224494695663452e-01,
-1.4277370274066925e-01 ], [ -1.2937371730804443e+00,
6.9287902116775513e-01, -1.6322688758373260e-01 ] ] }
- { id: 1, corners: [ [ -7.9834830760955811e-01, 8.1106305122375488e-01,
-9.9434338510036469e-02 ], [ -6.7920655012130737e-01,
8.1078404188156128e-01, -8.5110619664192200e-02 ], [
-6.7947661876678467e-01, 6.9078433513641357e-01,
-8.5201270878314972e-02 ], [ -7.9861837625503540e-01,
6.9106334447860718e-01, -9.9524989724159241e-02 ] ] }
- { id: 2, corners: [ [ -3.0384334921836853e-01, 8.1034839153289795e-01,
-3.8991540670394897e-02 ], [ -1.8399941921234131e-01,
8.1008774042129517e-01, -3.2878942787647247e-02 ], [
-1.8429389595985413e-01, 6.9008994102478027e-01,
-3.2222278416156769e-02 ], [ -3.0413782596588135e-01,
6.9035059213638306e-01, -3.8334876298904419e-02 ] ] }
How to parse a file with yaml-cpp
I guess I underestimated the power of the library. It turns out that doing so solves the problem:
YAML::Node map = YAML::LoadFile(filename);
for(YAML::const_iterator it=map.begin(); it!=map.end(); ++it){
const std::string &key=it->first.as<std::string>();
Eigen::Vector2f pos;
YAML::Node attributes = it->second;
YAML::Node position = attributes["position"];
for(int i=0; i<2; ++i){
pos(i) = position[i].as<float>();
}
...
}
Related Topics
Optimize Template Replacement of a Switch
Is It Safe to Read an Integer Variable That's Being Concurrently Modified Without Locking
How Can It Be Useful to Overload the "Function Call" Operator
How to Improve Link Performance for a Large C++ Application in VS2005
Passing Reference to Stl Vector Over Dll Boundary
Std::This_Thread::Sleep_For() and Gcc
Debug and Release Library Linking with Cmake (Visual Studio)
Sqlite3_Exec() Callback Function Clarification
Are Ieee Floats Valid Key Types for Std::Map and Std::Set
How Much Overhead Is There in Calling a Function in C++
Lru Implementation in Production Code
Why Is Std::Vector::Operator[] 5 to 10 Times Faster Than Std::Vector::At()
Specializing a Template on a Lambda in C++0X
How to Format Date Time Object with Format Dd/Mm/Yyyy