# inicpp **Repository Path**: FSJohn/inicpp ## Basic Information - **Project Name**: inicpp - **Description**: 一个头文件(header-file-only)搞定INI文件读写、甚至进行写注释。跨平台,并且用法极其简单。MIT license,从此配置INI文件就像喝水。[ 注:对您有帮助的话,Star或Issues为项目维护提供动力,感谢。] - by offical of JN-inicpp parser project. - **Primary Language**: C++ - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2025-01-04 - **Last Updated**: 2025-01-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Ⅰ、项目 您可以在 [https://github.com/dujingning/inicpp.git](https://github.com/dujingning/inicpp.git) 或者 [https://gitee.com/dujingning/inicpp](https://gitee.com/dujingning/inicpp) 查看该项目. #### * 为了支持开源,请给我们一个⭐。非常感谢! --- ### Ⅱ、描述 仅包含头文件的INI库适用于现代C++,支持**读取**、**写入**,甚至**注释**功能。它易于使用,并简化了与INI文件的工作。 - 新功能 : [非常容易绑定到自定义结构体(读取ini文件) - 见用法第7条](#6非常容易绑定到自定义结构体-读取ini文件). --- ### Ⅲ、用法 #### 0.使用 C++11 或更高版本,非常之简单易用。 ``` git clone https://github.com/dujingning/inicpp.git ``` - 包含 `inicpp.hpp` 头文件,就完成了所有准备工作。 #### 1.读取示例 ```cpp #include "inicpp.hpp" #include 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; } ``` #### 2.写入示例 ```cpp #include "inicpp.hpp" #include int main() { inicpp::IniManager _ini("config.ini"); // Load and parse the INI file. _ini.modify("server","ip","192.168.3.35"); _ini.modify("server","port",554); std::cout << _ini["server"]["port"] << std::endl; } ``` #### 3.注释示例 ```cpp #include "inicpp.hpp" #include int main() { inicpp::IniManager _ini("config.ini"); // Load and parse the INI file. // comment section/key _ini.modifyComment("server", "port", "this is the listen ip for server."); } ``` #### 4.toString()、toInt()、toDouble() 从字符串到其他类型(这几个接口不会异常)。 ```cpp #include "inicpp.hpp" #include int main() { inicpp::IniManager _ini("config.ini"); // Load and parse the INI file. _ini.modify("server","port","554","this is the listen port for server"); std::cout << _ini["server"]["port"] << std::endl; // Convert to string, default is string std::string http_port_s = _ini["server"].toString("port"); std::cout << "to string:\tserver.port = " << http_port_s << std::endl; // Convert to double double http_port_d = _ini["server"].toDouble("port"); std::cout << "to double:\tserver.port = " << http_port_d << std::endl; // Convert to int int http_port_i = _ini["server"].toInt("port"); std::cout << "to int:\t\tserver.port = " << http_port_i << std::endl; } ``` #### 5.isKeyExists()、isSectionExists()、getSectionsList() 可能包含无名的节:当 key/value 于文件开头时。 ```bash #include "inicpp.hpp" #include int main() { inicpp::IniManager _ini("config.ini"); // isKeyExist if (!_ini["server"].isKeyExist("port")) { std::cout << "server.port: not exist!" << "\n"; } // isSectionExists if (!_ini.isSectionExists("math")) { std::cout << "section of math: not exist" << "\n\n"; } // getSectionsList std::list sectionList = _ini.getSectionsList(); for(auto data:sectionList){ // print std::cout << data << std::endl; } } ``` #### 6.非常容易绑定到自定义结构体 (读取INI文件). 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 ```cpp #include "inicpp.hpp" #include #include #define CONFIG_FILE "config.ini" class appConfig { 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() { /** 直接绑定到自定义结构体 */ appConfig::Config config = appConfig::readConfig(); return 0; } ``` #### 7.测试用例 example/main.cpp 用法 可以使用 `example/Makefile` 或任何喜欢的方式进行编译。 如果没有 `make`,请使用以下命令:`g++ -I../ -std=c++11 main.cpp -o iniExample`。 - 编译 `example/main.cpp` ```bash [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 ``` - 运行 `iniExample` ```bash [root@VM-24-13-centos example]# ./iniExample title: config.ini server.port: 8080 server.ip: 127.0.0.1 server.alive: 1 math.PI: 3.141592653589793116 [root@VM-24-13-centos example]# ``` - 生成了配置文件 `config.ini`. ```bash [root@VM-24-13-centos example]# cat 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 [root@VM-24-13-centos example]# ``` --- ### Ⅳ、结尾 The project was created by DuJingning.