simpleini开源库使用

news/2024/7/10 19:51:26 标签: 开源

源码下载:GitHub - brofield/simpleini: Cross-platform C++ library providing a simple API to read and write INI-style configuration files

1.加载simpleini库

下载后解压

2.simpleini库的简单使用

(1)加载ini文件

// 定义ini文档对象
CSimpleIniA ini;
 
// 加载ini文件
SI_Error rc;
rc = ini.LoadFile(FILE_NAME);    // 另一种方式:SI_Error LoadFile(FILE * a_fpFile);
if (rc < 0) { 
    printf("加载 %s ini 文件失败!\n", FILE_NAME);
    return -1;
}

rc返回值有以下这些:

using SI_Error = int;
constexpr int SI_OK = 0;        //!< No error
constexpr int SI_UPDATED = 1;   //!< An existing value was updated
constexpr int SI_INSERTED = 2;  //!< A new value was inserted

// note: test for any error with (retval < 0)
constexpr int SI_FAIL = -1;     //!< Generic failure
constexpr int SI_NOMEM = -2;    //!< Out of memory error
constexpr int SI_FILE = -3;     //!< File error (see errno for detail error)

(2)简单配置

// 设置INI数据的存储格式,参数为true时保存为UTF-8格式,否则为本地编码格式
ini.SetUnicode(true);
// 是否允许一个关键字对应多个值,默认为允许;若不允许,则将最后一个值作为此关键字关联的值
ini.SetMultiKey(false);

(3)增

①添加一个新的节点(section)

// 添加一个新的 section
rc = ini.SetValue("section1", nullptr, nullptr);
if (rc < 0) { 
    printf("添加section1失败!\n");
    return -1;
}

②添加一个新的 key和value

// 添加一个新的 key和value
rc = ini.SetValue("section1", "name", "张三");
if (rc < 0) {
    printf("添加name失败!\n");
    return -1;
}
//const char *name = ini.GetValue("section1", "name", "");
//printf("name = %s\n", name);
 
ini.SetValue("section1", "age", "24");
ini.SetValue("section1", "sex", "男");

注意:

如果name存在,则会将name键(key)对应的值(value)修改为张三;

还可以使用SetLongValue、SetDoubleValue、SetBoolValue去添加:

ini.SetLongValue("server", "length", 173);

ini.SetDoubleValue("server", "weight", 53.5);

ini.SetBoolValue("server", "vip", true);

(4)改

①修改值(value)

// 修改value,如果键(name)不存在则添加该 key和value
rc = ini.SetValue("section1", "name", "李四");
if (rc < 0) { 
    printf("修改name失败!\n");
    return -1;
}
//const char *name = ini.GetValue("section1", "name");
//printf("name = %s\n", name);

注意:

如果要修改的值对应的键不存在,则会添加改键和值到section1节点中!

还可以使用SetLongValue、SetDoubleValue、SetBoolValue去添加:

ini.SetLongValue("server", "length", 1000);

ini.SetDoubleValue("server", "weight", 66.66);

ini.SetBoolValue("server", "vip", false);

(5)删

①删除 key 和 value

// 删除 key
// 如果最后一个key也被删除了,那么section也会被一起删除掉
done = ini.Delete("section1", "name");
if (false == done) {
    printf("删除 section1 - name 失败!\n");
    return -1;
}

②删除整个节点(section)和其下的所有键(key)

// 删除整个section和其中的所有键
done = ini.Delete("section1", nullptr);
if (false == done) {
    printf("删除整个section和其中的所有键 失败 !\n");
    return -1;
}

(6)查

①将下图中的ini文件内容读取打印显示

int _int = std::stoi(ini.GetValue("section", "_int", "-1"));
printf("_int = %d\n", _int);
long long _long = std::stoll(ini.GetValue("section", "_long", "-1"));
printf("_long = %lld\n", _long);
double _double = std::stod(ini.GetValue("section", "_double", "0.0"));
printf("_double = %lf\n", _double);
float _float = std::stof(ini.GetValue("section", "_float", "0.0"));
printf("_float = %f\n", _float);
bool _bool = ini.GetBoolValue("section", "_bool", false);
printf("_bool = %s\n", _bool ? "true" : "false");
std::string _string = ini.GetValue("section", "_string", "");
printf("_string = %s\n", _string.c_str());
std::string _string2 = ini.GetValue("section", "_string2", "");
printf("_string2 = %s\n", _string2.c_str());
char _char = ini.GetValue("section", "_char", "")[0];
printf("_char = %c\n", _char);
std::string ip = ini.GetValue("server", "ip", "0.0.0.0");
printf("ip = %s\n", ip.c_str());
int port = std::stoi(ini.GetValue("server", "port", "-1"));
printf("port = %d\n", port);
std::string name1 = ini.GetValue("server", "name", "");
printf("name = %s\n", name1.c_str());

还可以使用GetLongValue、GetDoubleValue、GetBoolValue去查:

int lenght = ini.GetLongValue("server", "length", -1);
double weight = ini.GetDoubleValue("server", "weight", -1);
bool vip = ini.GetBoolValue("server", "vip", false);

②遍历ini文件的所有内容

GetAllSections:获取所有节点,参数一引用返回list链表;

GetSection:根据参数字符串,获取节点,返回multimap容器;

CSimpleIniA::TNamesDepend sections;
// get all sections
ini.GetAllSections(sections);    
// 遍历所有 section 的 key 和 value
for (const auto &it : sections) {
    const CSimpleIniA::TKeyVal *pKeyVal = ini.GetSection(it.pItem);
    if (nullptr != pKeyVal) {
        for (const auto& it : *pKeyVal) {
            std::cout << it.first.pItem << " = " << it.second << std::endl;
        }
    }
}

③遍历所有节点(section)

CSimpleIniA::TNamesDepend sections1;
// 获取所有section
ini.GetAllSections(sections1);
// 遍历所有 sections
for (const auto &it : sections1) {
    std::cout << it.pItem << std::endl;
}

④遍历指定节点的键(key)

GetAllKeys:获取所有键,参数二引用返回list链表;

CSimpleIniA::TNamesDepend keys;
// get all keys in a section
ini.GetAllKeys("section", keys);    
// 遍历 section 指定的所有 key
for (const auto &it : keys) {
    std::cout << it.pItem << std::endl;
}

⑤获取一个键对应多个值

首先,ini.SetMultiKey(true)得设置为true,否则只会获取到最后一个值,其他会被删除掉;在ini文件中的server节点添加多几个name键,使用以下代码获取:

CSimpleIniA::TNamesDepend values;
// 获取 key 所对应的多个 value;ini.SetMultiKey(true);一定要设置为true,
// 否则就只会获取到最后一个,其他删除
ini.GetAllValues("server", "name", values);
// 遍历一个 key 对应多个 value;
for (const auto &it : values) {
    printf("name = %s\n", it.pItem);    
}

⑥获取指定节点(section)里有多少键值

// 获取section里有多少值
int size = ini.GetSectionSize("section");
printf("section 的 key 个数:%d\n", size);

(7)保存

注意:以上增、删、改,只有执行保存代码后,才会在文件做出相应的修改!

①保存到文件

/* 保存到文件中 */ 
rc = ini.SaveFile(FILE_NAME); 
if (rc < 0) { 
    printf("保存 %s ini文件失败\n", FILE_NAME); 
}

②保存到C++字符串

std::string strIni = ""; 
ini.Save(strIni); 
printf("%s\n", strIni.c_str());


http://www.niftyadmin.cn/n/5392217.html

相关文章

分布式知识整理

分布式锁 以商场系统超卖现象举例 超卖现象一 现象&#xff1a; 商品卖出数量超出了库存数量。 产生原因&#xff1a; 扣减库存的动作在程序中进行&#xff0c;在程序中计算剩余库存&#xff0c;在并发场景下&#xff0c;导致库存计算错误。 代码复现 es.shutdown(); cycl…

C++ //练习 8.13 重写本节的电话号码程序,从一个命名文件而非cin读取数据。

C Primer&#xff08;第5版&#xff09; 练习 8.13 练习 8.13 重写本节的电话号码程序&#xff0c;从一个命名文件而非cin读取数据。 环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09; 工具&#xff1a;vim 代码块 /***************************************…

计算机网络面经-TCP三次握手一文说清

目录 说一下TCP的三次握手&#xff1f; 为什么要三次握手&#xff1f;两次行不行&#xff1f;四次呢&#xff1f; 为什么建立连接是三次握手&#xff0c;关闭连接确是四次挥手呢&#xff1f; TCP四次挥手的过程&#xff1f; 如果已经建立了连接&#xff0c;但是客户端突然出…

Python爬虫进阶:爬取在线电视剧信息与高级检索

简介&#xff1a; 本文将向你展示如何使用Python创建一个能够爬取在线电视剧信息的爬虫&#xff0c;并介绍如何实现更高级的检索功能。我们将使用requests和BeautifulSoup库来爬取数据&#xff0c;并使用pandas库来处理和存储检索结果。 目录 一、爬取在线电视剧信息 …

Opencv3.2 ubuntu20.04安装过程

##1、更新源 sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" sudo apt update##2、安装依赖库 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavfor…

消息中间件篇之RabbitMQ-延时队列

一、延时队列 延迟队列&#xff1a;进入队列的消息会被延迟消费的队列。 场景&#xff1a;超时订单、限时优惠、定时发布。 延迟队列死信交换机TTL&#xff08;生存时间&#xff09;。 二、死信交换机 当一个队列中的消息满足下列情况之一时&#xff0c;可以成为死信&#xf…

【Java万花筒】服务网格:微服务世界的交通管制中心

解密服务网格&#xff1a;探索Istio、Envoy、Consul、Nacos和Spring Cloud的特点和应用 前言 服务网格是现代微服务架构中的重要概念&#xff0c;它提供了强大的流量管理、安全性、监控和故障恢复等功能。本文将介绍一些常用的服务网格平台和相关的Java库&#xff0c;包括Ist…

[设计模式Java实现附plantuml源码~行为型]对象间的联动~观察者模式

前言&#xff1a; 为什么之前写过Golang 版的设计模式&#xff0c;还在重新写Java 版&#xff1f; 答&#xff1a;因为对于我而言&#xff0c;当然也希望对正在学习的大伙有帮助。Java作为一门纯面向对象的语言&#xff0c;更适合用于学习设计模式。 为什么类图要附上uml 因为很…