🔥 1.Aims to make INI file processing simple, convenient, and intuitive.
🔥 2.Advancing the enduring excellence of C++ through collaborative innovation.
🔥 3.Let C++ keep simple,easy and free.
You can view the project at https://github.com/dujingning/inicpp.git or https://gitee.com/dujingning/inicpp.
* To support open source, please give us a star. For any issues, feel free to open an issue. Thank you very much!
The INI header-only library for Modern C++ supports reading, writing, and even commenting. It is easy to use and simplifies working with INI files.
- New Feature : Super Easy Binding to Your Data Structures (For Read).
git clone https://github.com/dujingning/inicpp.git
Include inicpp.hpp
, declare the inicpp::IniManager
class, and you're all set.
Write: Set directly to the file.
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
_ini["server"]["ip"] = "192.168.3.35";
_ini["server"]["port"] = 554;
std::cout << _ini["server"]["ip"] << ":"<< _ini["server"]["port"] << std::endl;
// or set any
_ini.set("server","ip","127.0.0.1");
_ini.set("server","port",8080);
std::cout << _ini["server"]["ip"] << ":"<< _ini["server"]["port"] << std::endl;
}
Convert: From string to type (Exception for error).
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
int port = _ini["server"]["port"];
std::string ip = _ini["server"]["ip"];
std::cout << ip << ":"<< port << std::endl;
// or get any
ip = _ini["server"]["ip"].get<std::string>();
port = _ini["server"]["port"].get<int>();
std::cout << ip << ":"<< port << std::endl;
}
Comment: Write comments for key-value pairs.
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
// comment section/key
_ini.set("math"/*section*/, "PI"/*key*/, "3.1415926535897932"/*key*/, "This is PI in mathematics."/*comment*/);
_ini.setComment("server"/*section*/, "port"/*key*/, "this is the listen ip for server."/*comment*/);
}
Convert: From string to type (No exception for error).
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
_ini.set("server","port","554","this is the listen port for server");
std::cout << _ini["server"]["port"] << std::endl;
// toString
std::string http_port_s = _ini["server"].toString("port");
std::cout << "to string:\tserver.port = " << http_port_s << std::endl;
// toDouble
double http_port_d = _ini["server"].toDouble("port");
std::cout << "to double:\tserver.port = " << http_port_d << std::endl;
// toInt
int http_port_i = _ini["server"].toInt("port");
std::cout << "to int:\t\tserver.port = " << http_port_i << std::endl;
}
May contain unnamed sections: when keys are at the head of the file.
#include "inicpp.hpp"
#include <iostream>
#include <iomanip>
// ANSI escape code for green text
#define GREEN_TEXT "\033[1;32m"
#define RESET_COLOR "\033[0m"
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
for (auto §ionName : _ini.sectionsList())
{
std::cout << GREEN_TEXT << "\nSection: " << RESET_COLOR << sectionName << "\n";
for (auto &kv : _ini.sectionMap(sectionName))
{
std::cout << std::setw(10) << std::left << kv.first
<< " -------> "
<< kv.second << std::endl;
}
}
}
config.ini :
title=config.ini
[server]
isKeepalived=true
;this is the listen ip for server.
port=8080
ip=127.0.0.1
[math]
;Comment: This is pi in mathematics.
PI=3.141592653589793238462643383279502884
config.cpp
#include "inicpp.hpp"
#include <iostream>
#include <iomanip>
#define CONFIG_FILE "config.ini"
class app
{
public:
typedef struct Config
{
typedef struct Server
{
std::string ip;
unsigned short port;
bool isKeepalived;
} Server;
std::string title;
Server server;
double PI;
} Config;
static const Config readConfig()
{
inicpp::IniManager _ini(CONFIG_FILE);
return Config{
title : _ini[""]["title"],
server : {ip : _ini["server"]["ip"],
port : _ini["server"]["port"],
isKeepalived : _ini["server"]["isKeepalived"]},
PI : _ini["math"]["PI"],
};
}
};
int main()
{
/** easy read for app as struct */
app::Config config = app::readConfig();
std::cout << config.server.ip << std::endl;
return 0;
}
You can compile it using example/Makefile
or any other method you prefer.
If make is not available, use the following command: g++ -I../ -std=c++11 main.cpp -o iniExample
.
- Compile
example/main.cpp
jn@jn:~/inicpp/example$ ls
example inicpp.hpp LICENSE README.md
jn@jn:~/inicpp/example$ cd example/
jn@jn:~/inicpp/example$ make
g++ -I../ -std=c++11 main.cpp -o iniExample
jn@jn:~/inicpp/example$ ls
iniExample main.cpp Makefile
- Run example app
iniExample
jn@jn:~/inicpp/example$ ./iniExample
Section:
title -------> config.ini
Section: math
PI -------> 3.141592653589793238462643383279502884
Section: server
info -------> the server socket info.
ip -------> 127.0.0.1
keepalived -------> true
number -------> 1
port -------> 8080
jn@jn:~/inicpp/example$
- Configuration file
config.ini
has been created.
jn@jn:~/inicpp/example$ ls
config.ini iniExample main.cpp Makefile
jn@jn:~/inicpp/example$ cat config.ini
;This is the title.
title=config.ini
[server]
number=1
info=the server socket info.
keepalived=true
;this is the listen ip for server.
port=8080
ip=127.0.0.1
[math]
;Comment: This is pi in mathematics.
PI=3.141592653589793238462643383279502884
jn@jn:~/inicpp/example$
The project was created by DuJingning.