配置文件读写之— —yaml文件
什么是yaml文件,格式是怎样的呢?
yaml是一种可以来用来存储程序比较好的工具,怎么说呢,就是格式简单。
基本语法
大小写敏感
使用缩进表示层级关系
缩进不允许使用tab,只允许空格
缩进的空格数不重要,只要相同层级的元素左对齐即可
'#'表示注释
数据类型
YAML 支持以下几种数据类型:
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值
YAML 对象
对象键值对使用冒号结构表示 key: value,冒号后面要加一个空格。
也可以使用 key:{key1: value1, key2: value2, …}。
还可以使用缩进表示层级关系;
key:
child-key: value
child-key2: value2
较为复杂的对象格式,可以使用问号加一个空格代表一个复杂的 key,配合一个冒号加一个空格代表一个 value:
?
- complexkey1
- complexkey2
:
- complexvalue1
- complexvalue2
意思即对象的属性是一个数组 [complexkey1,complexkey2],对应的值也是一个数组 [complexvalue1,complexvalue2]
YAML 数组
以 - 开头的行表示构成一个数组:
- A
- B
- C
YAML 支持多维数组,可以使用行内表示:
key: [value1, value2, …]
例子:
这个是我的yaml文件的格式
fiber_length: 10000
factor: 770000
input_range: 1
avg_count: 2
fft_count: 256
ring_width: 10
ring_count: 3
close_distance: 5
ignore_ranges:
- [0, 100]
- [9860, 9880]
patterns: |
!6000
---10000
读取yaml文本的信息
bool Model::LoadConfig()
{
std::string config_name = std::string("config_" + std::to_string(m_channel_index + 1) + ".yaml");
std::string path = GetExeDir(config_name);
try
{
YAML::Node config = YAML::LoadFile(path);
//m_channel_index = config["channel_index"].as<int>(0);
m_fiber_length = config["fiber_length"].as<int>(5000);
m_factor = config["factor"].as<int>(782400);
m_input_range = config["input_range"].as<int>(1);
m_avg_count = config["avg_count"].as<int>(10);
m_fft_count = config["fft_count"].as<int>(256);
m_ring_width = config["ring_width"].as<int>(20);
m_ring_count = config["ring_count"].as<int>(3);
m_close_distance = config["close_distance"].as<int>(10);
for (auto range : config["ignore_ranges"])
{
if (range.size() != 2) {
throw std::runtime_error("invalid ignore range");
}
int from = range[0].as<int>();
int to = range[1].as<int>();
m_ignore_ranges.push_back(IgnoreRange{
range[0].as<int>(),
range[1].as<int>()
}
);
}
if (m_pattern.Parse(Trim(config["patterns"].as<std::string>("")), m_fiber_length) == false)
{
LOG_FAT("解析配置文件 pattern 失败, 设置为空值\n");
}
}
catch (const std::exception& e)
{
LOG_FAT("读取配置文件信息失败, %s\n", e.what());
return false;
}
m_sample_count = int(m_fiber_length * double(m_factor) / FA);
return true;
}
保存yaml的信息
void Model::SaveConfig()
{
YAML::Emitter out;
out << YAML::BeginMap;
/*out << YAML::Key << "channel_index";
out << YAML::Value << m_channel_index;*/
out << YAML::Key << "fiber_length";
out << YAML::Value << m_fiber_length;
out << YAML::Key << "factor";
out << YAML::Value << m_factor;
out << YAML::Key << "input_range";
out << YAML::Value << m_input_range;
out << YAML::Key << "avg_count";
out << YAML::Value << m_avg_count;
out << YAML::Key << "fft_count";
out << YAML::Value << m_fft_count;
out << YAML::Key << "ring_width";
out << YAML::Value << m_ring_width;
out << YAML::Key << "ring_count";
out << YAML::Value << m_ring_count;
out << YAML::Key << "close_distance";
out << YAML::Value << m_close_distance;
out << YAML::Key << "ignore_ranges";
out << YAML::BeginSeq;
for (auto range : m_ignore_ranges)
{
out << YAML::Flow;
out << YAML::BeginSeq << range.from << range.to << YAML::EndSeq;
}
out << YAML::EndSeq;
out << YAML::Key << "patterns";
out << YAML::Value << YAML::Literal << m_pattern.Str();
out << YAML::EndMap;
std::string config_name = std::string("config_" + std::to_string(m_channel_index + 1) + ".yaml");
std::string path = GetExeDir(config_name);
std::ofstream file(path.c_str());
file << out.c_str();
m_sample_count = int(m_fiber_length * double(m_factor) / FA);
}