3 Star 10 Fork 2

dujingning / inicpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Ⅰ、Project

You can view the project at https://github.com/dujingning/inicpp.git or https://gitee.com/dujingning/inicpp.


Ⅱ、Description

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.


Ⅲ、Usage

* 0.Simple to use with C++11 or later.

git clone https://github.com/dujingning/inicpp.git

Include inicpp.hpp, declare the inicpp::iniReader class, and you're all set.

* 1.read example

Read: Load file to memory, used directly by the user.

#include "inicpp.hpp"
#include <iostream>

int main()
{
    inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.

    std::cout << _ini["rtsp"]["port"] << std::endl;
}

* 2.write example

Write: Modify directly to the file.

#include "inicpp.hpp"
#include <iostream>

int main()
{
    inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.

    _ini.modify("rtsp","port","554");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}

* 3.or comment

Comment: Write comments for key-value pairs.

#include "inicpp.hpp"
#include <iostream>

int main()
{
    inicpp::iniReader _ini("config.ini"); // Load and parse the INI file.

    _ini.modify("rtsp","port","554","this is the listen port for rtsp server");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}

* 4.toString()、toInt()、toDouble()

Convert: From string to type.

#include "inicpp.hpp"
#include <iostream>

int main()
{
    inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
    _ini.modify("rtsp","port","554","this is the listen port for rtsp server");
    std::cout << _ini["rtsp"]["port"] << std::endl;

    // Convert to string, default is string
    std::string http_port_s = _ini["http"].toString("port");
    std::cout << "to string:\thttp.port = " << http_port_s << std::endl;

    // Convert to double
    double http_port_d = _ini["http"].toDouble("port");
    std::cout << "to double:\thttp.port = " << http_port_d << std::endl;

    // Convert to int
    int http_port_i = _ini["http"].toInt("port");
    std::cout << "to int:\t\thttp.port = " << http_port_i << std::endl;
}

* 5.isKeyExists()

Check: If the key exists.

#include "inicpp.hpp"
#include <iostream>

int main()
{
    inicpp::IniManager _ini("config.ini");

    if (!_ini["rtsp"].isKeyExist("port"))
    {
        std::cout << "rtsp.port: not exist!" << "\n";
    }
    else
    {
        std::cout << "rtsp.port: not exist!" << "\n";
    }

    return 0;
}

* 6.isSectionExists()、getSectionsList()

May contain unnamed sections: when keys are at the head of the file.

#include "inicpp.hpp"
#include <iostream>

int main()
{
    inicpp::IniManager _ini("config.ini");

    // isSectionExists
    if (!_ini.isSectionExists("math"))
    {
        std::cout << "section of math: not exist" << "\n\n";
    }

    // getSectionsList
    std::list<std::string> sectionList = _ini.getSectionsList();

    for(auto data:sectionList){ // print
        std::cout << data << std::endl;
    }
}

* 7.For a full example, see example/main.cpp.

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.

* 8.how to compile example/main.cpp

  • Compile example/main.cpp
[jn@jn inicpp]$ ls
example  inicpp.hpp  LICENSE  README.md
[jn@jn inicpp]$ cd example/
[jn@jn example]$ make
g++ -I../ -std=c++11 main.cpp -o iniExample
[jn@jn example]$ ls
iniExample  main.cpp  Makefile
  • Run example app iniExample
[jn@jn example]$ ./iniExample
get rtsp port:555
to string:      rtsp.port = 554
to string:      math.PI   = 3.1415926
to string:      math.PI   = 3.1415926
to double:      math.PI   = 3.1415926
to int:         math.PI   = 3
to wstring:     other.desc= 你好,世界
[jn@jn example]$
  • then you got config file config.ini
  • Then you will get the config file config.ini.
[jn@jn example]$ cat config.ini
;no section test:add comment later.
noSection=yes
key0=noSectionAndComment
key1=noSectionAndComment
key2=noSectionAndComment
[head]
;thanks for your using inicpp project.
title=inicpp
;Permissive license for open-source software distribution.
license=MIT


[rtsp]
;this is the listen ip for rtsp server.
port=554
ip=127.0.0.1


[math]
;This is pi in mathematics.
PI=3.1415926


[other]
;this test for std::wstring. comment it.
desc=你好,世界
[jn@jn example]$

Ⅳ、End

The project was created by DuJingning.

MIT License Copyright (c) 2023 dujingning Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个头文件(header-file-only)搞定INI文件读写、甚至进行写注释。跨平台,并且用法极其简单。MIT license,从此配置INI文件就像喝水。[ 注:对您有帮助的话,Star或Issues为项目维护提供动力,感谢。] - by offical of JN-inicpp parser project. 展开 收起
C++ 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/dujingning/inicpp.git
git@gitee.com:dujingning/inicpp.git
dujingning
inicpp
inicpp
master

搜索帮助