C/C++ operation ini file (SinpleIni cross-platform library)

When I was studying recently, I found that I can't operate the ini file. I thought that the projects I came into contact with in the past used the ini file more or less to save the data of the initialization program; so I quickly searched the following C/C++ operations on the Internet. What kind of libraries can be played in the ini file; search for:

1. inih: This is a small library in C language, more suitable for embedded development;

2. iniparser: This is a library of C language, very convenient to use, open source, two .h files and two .c files, but it can only be used in Linux;

3. simpleini: This is a C++ library, very convenient to use, cross-platform, open source, only two .h files and one .c file, and supports Chinese;

So in the end I chose the library simpleini to learn and use!


Table of contents

1. Introduction

1. ini introduction

2. Introduction to simpleini

Two, download

Three, use

1. Load the ini file

2. Simple configuration

3. increase

1). Add a new node (section)

2). Add a new key and value

4. change

1). Modify the value (value)

5. delete

1). Delete key and value

2). When the last key is also deleted, section1 will also be deleted

3). Delete the entire node (section) and all keys under it (key) 

6. Check

1). Read and print the contents of the ini file in the figure below

2). Traversing all the contents of the ini file

3). Traverse all nodes (section)

4). Traverse the key of the specified node (key)

5). Get a key corresponding to multiple values 

6). Get how many key values ​​are in the specified node (section)

7. Save

1). Save to file 

2). Save to C++ string

8. Chinese garbled characters

4. Packaging

configdef.h

iniconfig.h

iniconfig.cpp

Test code:

V. Summary


1. Introduction

1. ini introduction

ini file is composed of [ section ] node and key key and value value .

For example a simple ini file looks like this:

[message]
name = 张三
age = 25
height = 173.2

; 这是一个注释

[server]
ip = 127.0.0.1
port = 6666

The message is a node, and its key and value are below the node; the server is also a node.

If you need a comment, use the English semicolon ' ; '.

2. Introduction to simpleini

A cross-platform library that provides a simple API to read and write ini-style configuration files. It supports data files in ASCII, MBCS and Unicode formats. It is explicitly designed to be portable to any platform and has been tested on Windows, WinCE and Linux. Released as open source and free under the MIT license.

Functional Overview

  • MIT license allows free use in all software (including GPL and commercial software)
  • Multi-platform (Windows 95 to Windows 10, Windows CE, Linux, Unix)
  • Load and save ini-style configuration files
  • On all platforms, configuration files can use any newline format
  • Liberal acceptance of file formats
  • key/value without section, key without value
  • Remove whitespace around sections, keys and values
  • Support for multi-line values ​​(values ​​with embedded newline characters)
  • Optional support for multiple keys with the same name
  • Optional case-insensitive sections and keys (only for ASCII characters)
  • Preserve sections and keys in the same order on file load
  • Preserve comments on files, sections and keys as much as possible
  • Both char and wchar_t programming interfaces are supported
  • Supports both MBCS (system locale) and UTF-8 file encoding
  • On Linux/Unix, the system locale does not need to be UTF-8 to load UTF-8 files
  • Support for non-ascii characters in sections, keys, values, and comments
  • Support for non-standard character types or file encodings through user-written converter classes
  • Supports adding/modifying values ​​programmatically
  • should compile without warnings in most compilers

Two, download

GitHub链接:GitHub - brofield/simpleini: Cross-platform C++ library providing a simple API to read and write INI-style configuration fileshttps://github.com/brofield/simpleini

git link:

simpleini: SimpleIni is a cross-platform C++ library that provides a simple API for manipulating ini configuration files (gitee.com) https://gitee.com/mirrors/simpleini

Unzip after downloading

 These three files can be used in Window or Linux environment!


Three, use

The usage described below can be used in both Linux and Window environments! 

 Include the header file:

#include "SimpleIni.h"

#define FILE_NAME    "./test1.ini" 

The content of test1.ini is as follows:

1. Load the ini file

// 定义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;
}

The rc return values ​​are as follows:

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. Simple configuration

// 设置INI数据的存储格式,参数为true时保存为UTF-8格式,否则为本地编码格式
ini.SetUnicode(true);

// 是否允许一个关键字对应多个值,默认为允许;若不允许,则将最后一个值作为此关键字关联的值
ini.SetMultiKey(false);

3. increase

 SetValue

Parameter one: node

Parameter two: key

Parameter three: value

Return value: SI_Error (that is, int type)

1). Add a new node (section)

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

 

2). Add a new key and 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", "男");

Note: If the name exists, the value corresponding to the name key (key) will be changed to Zhang San;

 You can also use SetLongValue, SetDoubleValue, SetBoolValue to add:

ini.SetLongValue("server", "length", 173);
ini.SetDoubleValue("server", "weight", 53.5);
ini.SetBoolValue("server", "vip", true);

4. change

SetValue

Parameter one: node

Parameter two: key

Parameter three: value

Return value: SI_Error (that is, int type)

1). Modify the value (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);

Note: If the key corresponding to the value to be modified does not exist, the modified key and value will be added to the section1 node! 

 It seems that the node (section) and key (key) cannot be modified, and I did not find the relevant api. . .

 You can also use SetLongValue, SetDoubleValue, SetBoolValue to add:

ini.SetLongValue("server", "length", 1000);
ini.SetDoubleValue("server", "weight", 66.66);
ini.SetBoolValue("server", "vip", false);

5. delete

Delete

Parameter one: node

Parameter two: key

return value: bool 

bool done = false

1). Delete key and value

// 删除 key
done = ini.Delete("section1", "name");
if (false == done) {
	printf("删除 section1 - name 失败!\n");
	return -1;
}

2). When the last key is also deleted, section1 will also be deleted

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

At this time, there are still two keys in section1. After the above code is executed, only age will be deleted, and section1 will not be deleted;

 If the third parameter value of Delete is true to delete sex, then section1 will also be deleted!

ini.Delete("section1", "sex", true);

Restore section1 to its original appearance, which is convenient for the operation and deletion of the third point below

3). Delete the entire node (section) and all keys under it (key) 

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

Executing the above code will delete the section1 just restored!

6. Check

GetValue

Parameter one: node

Parameter two: key

Parameter three: If not found, return the default value specified by parameter three

Return value: const char *

1). Read and print the contents of the ini file in the figure below

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());

You can also use GetLongValue, GetDoubleValue, GetBoolValue to check:

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

2). Traversing all the contents of the ini file

 GetAllSections : Get all nodes, parameter one refers to return list linked list;

GetSection : According to the parameter string, get the node and return the multimap container;

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;
		}
	}
}

3). Traverse all nodes (section)

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

4). Traverse the key of the specified node (key)

GetAllKeys: Get all the keys, and the second parameter reference returns the list linked 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;
}

5). Get a key corresponding to multiple values 

First, ini.SetMultiKey(true); must be set to true, otherwise only the last value will be obtained, and the others will be deleted;

Add a few more name keys to the server node in the ini file

 Get it with the following code:

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);	
}

6). Get how many key values ​​are in the specified node (section)

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

7. Save

Note: The above additions, deletions, and changes will only be modified in the file after the code is saved!

1). Save to file 

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

2). Save to C++ string

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

8. Chinese garbled characters

There are garbled characters when writing or reading Chinese in the window environment, just change the file encoding to ANSI encoding!

You can use notepad++ to modify, as shown below:

If there is a Chinese garbled problem in the Linux environment, create a new file, and then manually type in the required information, such as
touch test1.ini     or      vim test1.ini

Remember, don't copy from Window to Linux. Garbled characters will not be displayed in the file, but there will be garbled characters when reading and writing!

The garbled code problem I encountered can be solved by the above method!


4. Packaging

You can package it into a convenient interface to use according to the specific needs of your project!

For example my usage below:

configdef.h

This is the header file that defines the structure, and the data read from the ini file is stored in the structure! 

#ifndef _COMMON_CONFIGDEF_H_
#define _COMMON_CONFIGDEF_H_

#include <string>

typedef struct st_env_config {
    // 对应ini文件

	// section
    int _int;
    long _long;
    double _double;
    float _float;
    bool _bool;
	std::string _string;
	char _char;

	// server
	std::string _ip;
	unsigned short _port;



	// 构造函数
    st_env_config() { }
    st_env_config(int _int, long _long, double _double, float _float, bool _bool, std::string _string, char _char, std::string _ip, unsigned short _port) {
        this->_int = _int;
		this->_long = _long;
		this->_double = _double;
		this->_float = _float;
		this->_bool = _bool;
		this->_string = _string;
		this->_char = _char;

		this->_ip = _ip;
		this->_port = _port;
    }
    
    // 赋值运算符重载
    st_env_config &operator=(const st_env_config &config) {
        if (this != &config) {
			this->_int = config._int;
			this->_long = config._long;
			this->_double = config._double;
			this->_float = config._float;
			this->_bool = config._bool;
			this->_string = config._string;
			this->_char = config._char;

			this->_ip = _ip;
			this->_port = _port;
        }
        
        return *this;
    }
    
}_st_env_config;

#endif	// _COMMON_CONFIGDEF_H_

iniconfig.h

This is the header file that encapsulates simpleini

#ifndef _COMMON_INICONFIG_H_
#define _COMMON_INICONFIG_H_

#include <string>

#include "configdef.h"

#include "../simpleini/SimpleIni.h"



class Iniconfig {
public:
    Iniconfig();
	Iniconfig(const std::string &path, st_env_config &config);
    ~Iniconfig();
    
	// 加载ini文件
    bool loadfile(const std::string &path);
	// 保存ini文件
	bool saveFile(const std::string &fileName);

	// 设置INI数据的存储格式,参数为true时保存为UTF-8格式,否则为本地编码格式
	void setUnicode(const bool utf8 = true);
	// 是否允许一个关键字对应多个值,默认为允许;若不允许,则将最后一个值作为此关键字关联的值,其他删除
	void setMultiKey(const bool multKey = false);

	// 获取ini文件中的数据,保存到结构体中
	bool getData(st_env_config &config);

	// 获取ini文件字符串
	std::string getIniStr();

	// 添加一个新的section
	bool addSection(const std::string &section);
	// 添加一个新的key和value,value可以默认为空
	bool addValue(const std::string &section, const std::string &key, const std::string &value = "");
	bool addLongValue(const std::string &section, const std::string &key, const long value = 0);
	bool addDoubleValue(const std::string &section, const std::string &key, const double value = 0.0);
	bool addBoolValue(const std::string &section, const std::string &key, const bool value = false);

	// 修改value,如果key不存在,则会创建key和value
	bool setValue(const std::string &section, const std::string &key, const std::string &value);
	bool setLongValue(const std::string &section, const std::string &key, const long value = 0);
	bool setDoubleValue(const std::string &section, const std::string &key, const double value = 0.0);
	bool setBoolValue(const std::string &section, const std::string &key, const bool value = false);

	// 删除key
	bool deleteKey(const std::string &section, const std::string &key);
	// 删除key,如果最后一个key也被删除了,那么section也会被一起删除掉
	bool deleteKeys(const std::string &section, const std::string &key, const bool deleteSectionIfEmpty = true);
	// 删除section,整个section和其中的所有键值
	bool deleteSection(const std::string &section);


	// 获取string类型值
	std::string getValue(const std::string &section, const std::string &key, const std::string &defualtValue = "");
	// 获取char类型值
	char getValueC(const std::string &section, const std::string &key, const char &defualtValue = '\0');
	// 获取long、int、short类型
	long getLongValue(const std::string &section, const std::string &key, const short &defualtValue = -1);
	// 获取double、float类型
	double getDoubleValue(const std::string &section, const std::string &key, const double &defualtValue = 0.0);
	// 获取bool类型
	bool getBoolValue(const std::string &section, const std::string &key, const bool &defualtValue = false);

	// 获取section里有多少值
	int getSectionSize(const std::string &section);

	// 遍历所有
	void printAll();
    
private:
    bool _isloaded;		// 是否已经加载
	CSimpleIniA _ini;	// ini操作对象
};

#endif	// _COMMON_INICONFIG_H_

iniconfig.cpp

This is the content of the cpp file that encapsulates simpleini

#include "iniconfig.h"

#include <stdio.h>
#include <iostream>


Iniconfig::Iniconfig() : _isloaded(false) {
	_ini.SetUnicode(true);		// 使用utf8编码
	_ini.SetMultiKey(false);	// 不允许一个key对应多个value

	_isloaded = false;
}

Iniconfig::Iniconfig(const std::string & path, st_env_config &config) {
	_ini.SetUnicode(true);		// 使用utf8编码
	_ini.SetMultiKey(false);	// 不允许一个key对应多个value
	
	_isloaded = false;

	SI_Error rc;
	rc = _ini.LoadFile(path.c_str());	// 另一种方式:SI_Error LoadFile(FILE * a_fpFile);
	if (rc < 0) {
		printf("加载 %s ini 文件失败!\n", path.c_str());
		_isloaded = false;
		return;
	} 

	int _int = getLongValue("section", "_int", -1);
	long _long = getLongValue("section", "_long", -1);
	double _double = getDoubleValue("section", "_double", 0.0);
	float _float = getDoubleValue("section", "_float", 0.0);
	bool _bool = getBoolValue("section", "_bool", false);
	std::string _string = getValue("section", "_string", "");
	char _char = getValueC("section", "_char", '\0');
	std::string ip = getValue("server", "ip", "0.0.0.0");
	unsigned short port = getLongValue("section", "port", -1);

	config = st_env_config(_int, _long, _double, _float, _bool, _string, _char, ip, port);

	_isloaded = true;
}

Iniconfig::~Iniconfig() {
    
}

// 加载ini文件
bool Iniconfig::loadfile(const std::string &path) {
   
    if (false == _isloaded) {
		SI_Error rc;
		rc = _ini.LoadFile(path.c_str());	// 另一种方式:SI_Error LoadFile(FILE * a_fpFile);
		if (rc < 0) {
			printf("加载 %s ini 文件失败!\n", path.c_str());
			_isloaded = false;
			return _isloaded;
		}

		_isloaded = true;

    } 
       
    return _isloaded;
}

bool Iniconfig::saveFile(const std::string & fileName) {

	SI_Error rc = _ini.SaveFile(fileName.c_str());
	if (rc < 0) {
		printf("保存 %s ini文件失败\n", fileName.c_str());
		return false;
	}

	_isloaded = false;

	return true;
}

void Iniconfig::setUnicode(const bool utf8) {
	_ini.SetUnicode(utf8);		// true:使用utf8编码
}

void Iniconfig::setMultiKey(const bool multKey) {
	_ini.SetMultiKey(multKey);	// false:不允许一个key对应多个value
}

bool Iniconfig::getData(st_env_config & config) {
	if (true == _isloaded) {
		int _int = getLongValue("section", "_int", -1);
		long _long = getLongValue("section", "_long", -1);
		double _double = getDoubleValue("section", "_double", 0.0);
		float _float = getDoubleValue("section", "_float", 0.0);
		bool _bool = getBoolValue("section", "_bool", false);
		std::string _string = getValue("section", "_string", "");
		char _char = getValueC("section", "_char", '\0');
		std::string ip = getValue("server", "ip", "0.0.0.0");
		unsigned short port = getLongValue("section", "port", -1);

		config = st_env_config(_int, _long, _double, _float, _bool, _string, _char, ip, port);

		return true;
	}

	return false;
}

std::string Iniconfig::getIniStr() {
	std::string str = "";

	if (true == _isloaded) {
		SI_Error rc = _ini.Save(str);
		if (rc < 0) {
			printf("获取ini文件字符串失败!\n");
			str = "";
		}
	}

	return str;
}

bool Iniconfig::addSection(const std::string & section) {

	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetValue(section.c_str(), nullptr, nullptr);
	if (rc < 0) {
		printf("添加 %s 节点失败!\n", section.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::addValue(const std::string & section, const std::string & key, const std::string & value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetValue(section.c_str(), key.c_str(), value.c_str());
	if (rc < 0) {
		printf("添加 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::addLongValue(const std::string & section, const std::string & key, const long value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetLongValue(section.c_str(), key.c_str(), value);
	if (rc < 0) {
		printf("添加 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::addDoubleValue(const std::string & section, const std::string & key, const double value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetDoubleValue(section.c_str(), key.c_str(), value);
	if (rc < 0) {
		printf("添加 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::addBoolValue(const std::string & section, const std::string & key, const bool value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetBoolValue(section.c_str(), key.c_str(), value);
	if (rc < 0) {
		printf("添加 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::setValue(const std::string & section, const std::string & key, const std::string & value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetValue(section.c_str(), key.c_str(), value.c_str());
	if (rc < 0) {
		printf("修改 %s value失败!\n", value.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::setLongValue(const std::string & section, const std::string & key, const long value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetLongValue(section.c_str(), key.c_str(), value);
	if (rc < 0) {
		printf("修改 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::setDoubleValue(const std::string & section, const std::string & key, const double value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetDoubleValue(section.c_str(), key.c_str(), value);
	if (rc < 0) {
		printf("修改 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::setBoolValue(const std::string & section, const std::string & key, const bool value) {
	if (false == _isloaded) { return false; }

	SI_Error rc = _ini.SetBoolValue(section.c_str(), key.c_str(), value);
	if (rc < 0) {
		printf("修改 %s key失败!\n", key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::deleteKey(const std::string & section, const std::string & key) {
	if (false == _isloaded) { return false; }

	bool done = false;

	// 删除 key
	done = _ini.Delete(section.c_str(), key.c_str());
	if (false == done) {
		printf("删除 %s - %s 失败!\n", section.c_str(), key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::deleteKeys(const std::string & section, const std::string & key, const bool deleteSectionIfEmpty) {
	if (false == _isloaded) { return false; }

	bool done = false;

	done = _ini.Delete(section.c_str(), key.c_str(), deleteSectionIfEmpty);
	if (false == done) {
		printf("删除 %s - %s 失败!\n", section.c_str(), key.c_str());
		return false;
	}

	return true;
}

bool Iniconfig::deleteSection(const std::string & section) {
	if (false == _isloaded) { return false; }

	bool done = false;

	done = _ini.Delete(section.c_str(), nullptr);
	if (false == done) {
		printf("删除整个 %s 和其下的所有 键 失败 !\n", section.c_str());
		return false;
	}

	return true;
}

std::string Iniconfig::getValue(const std::string & section, const std::string & key, const std::string & defualtValue) {
	if (false == _isloaded) { return ""; }

	return  _ini.GetValue(section.c_str(), key.c_str(), defualtValue.c_str());
}

char Iniconfig::getValueC(const std::string & section, const std::string & key, const char & defualtValue) {
	if (false == _isloaded) { return '\0'; }

	std::string str = std::to_string(defualtValue);
	return  _ini.GetValue(section.c_str(), key.c_str(), str.c_str())[0];
}

long Iniconfig::getLongValue(const std::string & section, const std::string & key, const short & defualtValue) {
	if (false == _isloaded) { return -1; }

	return  _ini.GetLongValue(section.c_str(), key.c_str(), defualtValue);
}

double Iniconfig::getDoubleValue(const std::string & section, const std::string & key, const double & defualtValue) {
	if (false == _isloaded) { return -1.0; }

	return  _ini.GetDoubleValue(section.c_str(), key.c_str(), defualtValue);
}

bool Iniconfig::getBoolValue(const std::string & section, const std::string & key, const bool & defualtValue) {
	if (false == _isloaded) { return false; }

	return  _ini.GetBoolValue(section.c_str(), key.c_str(), defualtValue);
}

int Iniconfig::getSectionSize(const std::string & section) {
	if (false == _isloaded) { return -1; }

	return _ini.GetSectionSize(section.c_str());
}

void Iniconfig::printAll() {
	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;
			}
		}
	}
}

Test code:

st_env_config config;
Iniconfig cof(FILE_NAME, config);


cof.addSection("abc");
cof.addValue("abc", "name", "a");
cof.addBoolValue("abc", "vip", true);
cof.addDoubleValue("abc", "length", 175.5);
cof.addLongValue("abc", "weight", 85);

cof.setValue("abc", "name", "b");
cof.setBoolValue("abc", "vip", false);
cof.setDoubleValue("abc", "length", 188.8);
cof.setLongValue("abc", "weight", 90);

//cof.deleteKey("abc", "name");
//cof.deleteKeys("abc", "vip");
//cof.deleteSection("abc");

printf("name = %c\n", cof.getValueC("abc", "name"));
printf("name = %s\n", cof.getValue("abc", "name").c_str());
printf("bool = %d\n", cof.getBoolValue("abc", "vip"));
printf("lenght = %f\n", cof.getDoubleValue("abc", "length"));
printf("weight = %ld\n", cof.getLongValue("abc", "weight"));

printf("%s\n", cof.getIniStr().c_str());
cof.saveFile(FILE_NAME);

V. Summary

The basic usage of the simpleini library is as shown above, and there are some other APIs, which are not used yet, and will be added when they are used!

The simpleini library should not be too difficult, it is nothing more than the use of GetValue and SetValue!

The ini file is often used to initialize the program, such as storing some basic data initialized when the software is started. After learning this library, if you write some small software in the future, you can use ini to initialize it!

Guess you like

Origin blog.csdn.net/cpp_learner/article/details/128780799