buildroot中C语言使用libconfig的实例

首先在buildroot中开启libconfig

在config中添加

BR2_PACKAGE_LIBCONFIG=y

下面是官方给出来的3个实例

/* ----------------------------------------------------------------------------libconfig - A library for processing structured configuration filesCopyright (C) 2005-2010  Mark A LindnerThis file is part of libconfig.This library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General Public Licenseas published by the Free Software Foundation; either version 2.1 ofthe License, or (at your option) any later version.This library is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with this library; if not, see<http://www.gnu.org/licenses/>.----------------------------------------------------------------------------
*/#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>/* This example reads the configuration file 'example.cfg' and displays* some of its contents.*/int main(int argc, char **argv)
{config_t cfg;config_setting_t *setting;const char *str;config_init(&cfg);/* Read the file. If there is an error, report it and exit. */if(! config_read_file(&cfg, "example.cfg")){fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),config_error_line(&cfg), config_error_text(&cfg));config_destroy(&cfg);return(EXIT_FAILURE);}/* Get the store name. */if(config_lookup_string(&cfg, "name", &str))printf("Store name: %s\n\n", str);elsefprintf(stderr, "No 'name' setting in configuration file.\n");/* Output a list of all books in the inventory. */setting = config_lookup(&cfg, "inventory.books");if(setting != NULL){int count = config_setting_length(setting);int i;printf("%-30s  %-30s   %-6s  %s\n", "TITLE", "AUTHOR", "PRICE", "QTY");for(i = 0; i < count; ++i){config_setting_t *book = config_setting_get_elem(setting, i);/* Only output the record if all of the expected fields are present. */const char *title, *author;double price;int qty;if(!(config_setting_lookup_string(book, "title", &title)&& config_setting_lookup_string(book, "author", &author)&& config_setting_lookup_float(book, "price", &price)&& config_setting_lookup_int(book, "qty", &qty)))continue;printf("%-30s  %-30s  $%6.2f  %3d\n", title, author, price, qty);}putchar('\n');}/* Output a list of all movies in the inventory. */setting = config_lookup(&cfg, "inventory.movies");if(setting != NULL){unsigned int count = config_setting_length(setting);unsigned int i;printf("%-30s  %-10s   %-6s  %s\n", "TITLE", "MEDIA", "PRICE", "QTY");for(i = 0; i < count; ++i){config_setting_t *movie = config_setting_get_elem(setting, i);/* Only output the record if all of the expected fields are present. */const char *title, *media;double price;int qty;if(!(config_setting_lookup_string(movie, "title", &title)&& config_setting_lookup_string(movie, "media", &media)&& config_setting_lookup_float(movie, "price", &price)&& config_setting_lookup_int(movie, "qty", &qty)))continue;printf("%-30s  %-10s  $%6.2f  %3d\n", title, media, price, qty);}putchar('\n');}config_destroy(&cfg);return(EXIT_SUCCESS);
}/* eof */
/* ----------------------------------------------------------------------------libconfig - A library for processing structured configuration filesCopyright (C) 2005-2010  Mark A LindnerThis file is part of libconfig.This library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General Public Licenseas published by the Free Software Foundation; either version 2.1 ofthe License, or (at your option) any later version.This library is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with this library; if not, see<http://www.gnu.org/licenses/>.----------------------------------------------------------------------------
*/#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>/* This example reads the configuration file 'example.cfg', adds a new* movie record to the movies list, and writes the updated configuration to* 'updated.cfg'.*/int main(int argc, char **argv)
{static const char *output_file = "updated.cfg";config_t cfg;config_setting_t *root, *setting, *movie;config_init(&cfg);/* Read the file. If there is an error, report it and exit. */if(! config_read_file(&cfg, "example.cfg")){fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),config_error_line(&cfg), config_error_text(&cfg));config_destroy(&cfg);return(EXIT_FAILURE);}/* Find the 'movies' setting. Add intermediate settings if they don't yet* exist.*/root = config_root_setting(&cfg);setting = config_setting_get_member(root, "inventory");if(!setting)setting = config_setting_add(root, "inventory", CONFIG_TYPE_GROUP);setting = config_setting_get_member(setting, "movies");if(!setting)setting = config_setting_add(setting, "movies", CONFIG_TYPE_LIST);/* Create the new movie entry. */movie = config_setting_add(setting, NULL, CONFIG_TYPE_GROUP);setting = config_setting_add(movie, "title", CONFIG_TYPE_STRING);config_setting_set_string(setting, "Buckaroo Banzai");setting = config_setting_add(movie, "media", CONFIG_TYPE_STRING);config_setting_set_string(setting, "DVD");setting = config_setting_add(movie, "price", CONFIG_TYPE_FLOAT);config_setting_set_float(setting, 12.99);setting = config_setting_add(movie, "qty", CONFIG_TYPE_INT);config_setting_set_float(setting, 20);config_set_options(&cfg, 0);/* Write out the updated configuration. */if(! config_write_file(&cfg, output_file)){fprintf(stderr, "Error while writing file.\n");config_destroy(&cfg);return(EXIT_FAILURE);}fprintf(stderr, "Updated configuration successfully written to: %s\n",output_file);config_destroy(&cfg);return(EXIT_SUCCESS);
}/* eof */
/* ----------------------------------------------------------------------------libconfig - A library for processing structured configuration filesCopyright (C) 2005-2010  Mark A LindnerThis file is part of libconfig.This library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General Public Licenseas published by the Free Software Foundation; either version 2.1 ofthe License, or (at your option) any later version.This library is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with this library; if not, see<http://www.gnu.org/licenses/>.----------------------------------------------------------------------------
*/#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>/* This example constructs a new configuration in memory and writes it to* 'newconfig.cfg'.*/int main(int argc, char **argv)
{static const char *output_file = "newconfig.cfg";config_t cfg;config_setting_t *root, *setting, *group, *array;int i;config_init(&cfg);root = config_root_setting(&cfg);/* Add some settings to the configuration. */group = config_setting_add(root, "address", CONFIG_TYPE_GROUP);setting = config_setting_add(group, "street", CONFIG_TYPE_STRING);config_setting_set_string(setting, "1 Woz Way");setting = config_setting_add(group, "city", CONFIG_TYPE_STRING);config_setting_set_string(setting, "San Jose");setting = config_setting_add(group, "state", CONFIG_TYPE_STRING);config_setting_set_string(setting, "CA");setting = config_setting_add(group, "zip", CONFIG_TYPE_INT);config_setting_set_int(setting, 95110);array = config_setting_add(root, "numbers", CONFIG_TYPE_ARRAY);for(i = 0; i < 10; ++i){setting = config_setting_add(array, NULL, CONFIG_TYPE_INT);config_setting_set_int(setting, 10 * i);}/* Write out the new configuration. */if(! config_write_file(&cfg, output_file)){fprintf(stderr, "Error while writing file.\n");config_destroy(&cfg);return(EXIT_FAILURE);}fprintf(stderr, "New configuration successfully written to: %s\n",output_file);config_destroy(&cfg);return(EXIT_SUCCESS);
}/* eof */

使用的时候需要链接库Makefile中添加 -lconfig

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/76246.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

科技云报道:AI时代,对构建云安全提出了哪些新要求?

科技云报道原创。 随着企业上云的提速&#xff0c;一系列云安全问题也逐渐暴露出来&#xff0c;云安全问题得到重视&#xff0c;市场不断扩大。 Gartner 发布“2022 年中国 ICT 技术成熟度曲线”显示&#xff0c;云安全已处于技术萌芽期高点&#xff0c;预期在2-5年内有望达到…

Material Design系列探究之LinearLayoutCompat

谷歌Material Design推出了许多非常好用的控件&#xff0c;所以我决定写一个专题来讲述MaterialDesign&#xff0c;今天带来Material Design系列的第一弹 LinearLayoutCompat。 以前要在LinearLayout布局之间的子View之间添加分割线&#xff0c;还需要自己去自定义控件进行添加…

自动驾驶多任务框架Hybridnets——同时处理车辆检测、可驾驶区域分割、车道线分割模型部署(C++/Python)

一、多感知任务 在移动机器人的感知系统&#xff0c;包括自动驾驶汽车和无人机&#xff0c;会使用多种传感器来获取关键信息&#xff0c;从而实现对环境的感知和物体检测。这些传感器包括相机、激光雷达、雷达、惯性测量单元&#xff08;IMU&#xff09;、全球导航卫星系统&am…

Spark 增量抽取 Mysql To Hive

题目要求&#xff1a; 抽取ds_db01库中customer_inf的增量数据进入Hive的ods库中表customer_inf。根据ods.user_info表中modified_time作为增量字段&#xff0c;只将新增的数据抽入&#xff0c;字段名称、类型不变&#xff0c;同时添加静态分区&#xff0c;分区字段为etl_date&…

SpringCloud(二)

1.Nacos配置管理 Nacos除了可以做注册中心&#xff0c;同样可以做配置管理来使用。 1.1.统一配置管理 当微服务部署的实例越来越多&#xff0c;达到数十、数百时&#xff0c;逐个修改微服务配置就会让人抓狂&#xff0c;而且很容易出错。我们需要一种统一配置管理方案&#…

NIFI实现数据库数据增量同步

说明 nifi版本&#xff1a;1.23.2&#xff08;docker镜像&#xff09; 需求背景 将数据库中的数据同步到另一个数据库中&#xff0c;要求对于新增的数据和历史有修改的数据进行增量同步 模拟数据 建表语句 源数据库和目标数据库结构要保持一致&#xff0c;这样可以避免后…

固定资产管理数据怎么算?

在企业的运营中&#xff0c;固定资产的管理是一个至关重要的环节。然而&#xff0c;对于许多企业来说&#xff0c;理解和管理这些资产的数据却常常是一团迷雾。那么&#xff0c;固定资产管理数据究竟应该如何计算呢&#xff1f;这是一个需要我们深入探讨的问题。  我们需要明…

MySQL——命令行客户端的字符集问题

原因&#xff1a;服务器端认为你的客户端的字符集是utf-8&#xff0c;而实际上你的客户端的字符集是GBK。 查看所有字符集&#xff1a;SHOW VARIABLES LIKE character_set_%; 解决方案&#xff0c;设置当前连接的客户端字符集 “SET NAMES GBK;”

Android12之/proc/pid/status参数含义(一百六十五)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

python sorted函数详解2023.9.11

sorted函数详解 1. 输入和输出2. key传入函数 1. 输入和输出 help(sorted) Help on built-in function sorted in module builtins: sorted(iterable, /, *, keyNone, reverseFalse)Return a new list containing all items from the iterable in ascending order.A custom k…

Redis监控工具_RedisLive

Redis监控工具_RedisLive Redis安装请看: MacBook安装Redis redis集群搭建_亲自操作 RedisLive安装 RedisLive是由python编写的并且开源的图形化监控工具&#xff0c;非常轻量级&#xff0c;核心服务部分只包含一个web服务和一个基于redis自带的info命令以及monitor命令的…

php://filter协议在任意文件读取漏洞(附例题)

php://filter php://fiter 中文叫 元器封装&#xff0c;咱也不知道为什么这么翻译&#xff0c;目前我的理解是可以通过这个玩意对上面提到的php IO流进行处理&#xff0c;及现在可以对php的 IO流进行一定操作。 过滤器&#xff1a;及通过php://filter 对php 的IO流进行的具体…

微服务之流控、容错组件sentinel

背景 2012年阿里巴巴研发的流量治理组件&#xff0c;核心功能流控、容错 有什么功能 流量控制 流量控制 网关控制 黑白名单 熔断降级 熔断 保护分布式系统防止因为调用下有服务时产生故障或者请求超时等异常影响上游服务&#xff0c;使用熔断方案&#xff0c;类似断路器…

hive中的索引

使用索引前的配置 在使用Hive索引之前&#xff0c;需要进行一些配置&#xff0c;以确保索引能够正常工作。以下是一些常见的配置步骤&#xff1a; Hive配置 在Hive中启用索引功能&#xff0c;需要在Hive配置文件&#xff08;hive-site.xml&#xff09;中设置以下属性&#x…

T2I-Adapter:增强文本到图像生成的控制能力

链接&#xff1a;GitHub - TencentARC/T2I-Adapter: T2I-Adapter 文本到图像生成 (T2I) 是人工智能领域的一个重要研究方向。近年来&#xff0c;随着深度学习技术的发展&#xff0c;T2I 技术取得了显著进展&#xff0c;生成的图像在视觉效果上已经与真实图像难以区分。 然而&…

ILS解析漏洞复现

搭建好ILS后&#xff0c;访问127.0.0.1:8000 写一个phpinfo的脚本 可以看到。现在是不能访问的 赋予 IIS 解析 phpinfo 能力 打开服务器管理器&#xff0c;打开 IIS 管理器 点击处理程序映射 再次访问&#xff0c;发现程序可以访问 将index.php改为index.png 此时php脚本自然是…

【pdf密码】如何限制他人对PDF文件编辑?

制作好的PDF文件&#xff0c;先要设置一个密码防止他人对文件进行编辑&#xff0c;那么我们可以对PDF文件设置限制编辑&#xff0c;设置方法很简单&#xff0c;我们在PDF编辑器中点击文件 – 属性 – 安全&#xff0c;在权限下拉框中选中【密码保护】 然后在密码保护界面中&…

LeetCode_贪心算法_困难_630.课程表 III

目录 1.题目2.思路3.代码实现&#xff08;Java&#xff09; 1.题目 这里有 n 门不同的在线课程&#xff0c;按从 1 到 n 编号。给你一个数组 courses &#xff0c;其中 courses[i] [durationi, lastDayi] 表示第 i 门课将会持续上 durationi 天课&#xff0c;并且必须在不晚于…

查看创建好的数据库

MySQL从小白到总裁完整教程目录:https://blog.csdn.net/weixin_67859959/article/details/129334507?spm1001.2014.3001.5502 语法格式: show create database 数据库名称; 案列:查看testing数据库信息 mysql> show create database testing; ------------------------…

SpringMVC相关知识点

1.Spring MVC的理解&#xff1f; 首先&#xff0c;MVC模型是模型&#xff0c;视图&#xff0c;控制器的简写&#xff0c;其思想核心是通过将请求处理控制&#xff0c;业务逻辑&#xff0c;数据封装&#xff0c;数据显示等流程节点分离的思想来组织代码。 所以&#xff0c;MVC是…