{"id":13494746,"url":"https://github.com/Rookfighter/inifile-cpp","last_synced_at":"2025-03-28T14:31:43.175Z","repository":{"id":38817869,"uuid":"48951728","full_name":"Rookfighter/inifile-cpp","owner":"Rookfighter","description":"A header-only and easy to use Ini file parser for C++.","archived":false,"fork":false,"pushed_at":"2025-02-11T18:32:43.000Z","size":138,"stargazers_count":378,"open_issues_count":3,"forks_count":71,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-02-11T19:35:33.162Z","etag":null,"topics":["cpp","cpp-library","ini-parser","inifile"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Rookfighter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-03T16:00:47.000Z","updated_at":"2025-02-11T18:32:48.000Z","dependencies_parsed_at":"2024-10-31T09:31:29.912Z","dependency_job_id":"6d5770fd-fab5-4396-9e63-d6eac0708637","html_url":"https://github.com/Rookfighter/inifile-cpp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rookfighter%2Finifile-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rookfighter%2Finifile-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rookfighter%2Finifile-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rookfighter%2Finifile-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rookfighter","download_url":"https://codeload.github.com/Rookfighter/inifile-cpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246046058,"owners_count":20714893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp-library","ini-parser","inifile"],"created_at":"2024-07-31T19:01:27.737Z","updated_at":"2025-03-28T14:31:42.779Z","avatar_url":"https://github.com/Rookfighter.png","language":"C++","readme":"# inifile-cpp\n![License](https://img.shields.io/packagist/l/doctrine/orm.svg)\n[![CMake](https://github.com/Rookfighter/inifile-cpp/workflows/CMake/badge.svg)](https://github.com/Rookfighter/inifile-cpp/actions/workflows/cmake.yml)\n[![codecov](https://codecov.io/gh/Rookfighter/inifile-cpp/graph/badge.svg?token=39FL0C8NRK)](https://codecov.io/gh/Rookfighter/inifile-cpp)\n\n```inifile-cpp``` is a simple and easy to use single header-only ini file en- and decoder for C++.\n\n## Install\n\nInstall the headers using the CMake build system:\n\n```sh\ncd \u003cpath-to-repo\u003e\nmkdir build\ncd build\ncmake ..\nmake install\n```\n\nor simply copy the header file into your project and include it directly.\n\n## Usage\n\nFor examples on how to use and extend ```inifile-cpp``` for your custom needs, please have a look at the ```examples/``` directory.\n\n```inifile-cpp``` allows loading data from any ```std::istream``` and requires a\nsingle function call or use the overloaded constructor.\n\n```cpp\n#include \u003cinicpp.h\u003e\n\nint main()\n{\n    // create istream object \"is\" ...\n\n    // use function\n    ini::IniFile myFirstIni;\n    myFirstIni.decode(is);\n\n    // or use the constructor\n    ini::IniFile mySecondIni(is);\n}\n```\n\nYou can directly load ini-data from files by using the  ```load()``` function. It requires a file path\nand automatically parses its contents:\n\n```cpp\n#include \u003cinicpp.h\u003e\n\nint main()\n{\n    // load an ini file\n    ini::IniFile myIni;\n    myIni.load(\"some/ini/path\");\n}\n```\n\nYou can enable decoding of multi-line values using the  ```setMultiLineValues(true)``` function.  If you do this, field values may be continued on the next line, after indentation.  Each line will be separated by the `\\n` character in the final value, and the indentation will be removed.\n\n```cpp\n#include \u003cinicpp.h\u003e\n\nint main()\n{\n    // load an ini file\n    ini::IniFile myIni;\n    myIni.setMultiLineValues(true);\n    myIni.load(\"some/ini/path\");\n}\n```\n\nWhen duplicate fields are decoded the previous value is simply overwritten by default. You can disallow duplicate fields from being overwritten by using the ```allowOverwriteDuplicateFields(false)``` function. If you do this, an exception will be thrown if a duplicate field is found inside a section.\n\n```cpp\n#include \u003cinicpp.h\u003e\n\nint main()\n{\n    // load an ini file\n    ini::IniFile myIni;\n    myIni.allowOverwriteDuplicateFields(false);\n    // throws an exception if the ini file has duplicate fields\n    myIni.load(\"some/ini/path\");\n}\n```\n\nSections and fields can be accessed using the index operator ```[]```.\nThe values can be converted to various native types:\n\n```cpp\nbool myBool = myIni[\"Foo\"][\"myBool\"].as\u003cbool\u003e();\nchar myChar = myIni[\"Foo\"][\"myChar\"].as\u003cchar\u003e();\nunsigned char myUChar = myIni[\"Foo\"][\"myUChar\"].as\u003cunsigned char\u003e();\nint myInt = myIni[\"Foo\"][\"myInt\"].as\u003cint\u003e();\nunsigned int myUInt = myIni[\"Foo\"][\"myUInt\"].as\u003cunsigned int\u003e();\nlong myLong = myIni[\"Foo\"][\"myLong\"].as\u003clong\u003e();\nunsigned long myULong = myIni[\"Foo\"][\"myULong\"].as\u003cunsigned long\u003e();\nfloat myFloat = myIni[\"Foo\"][\"myFloat\"].as\u003cfloat\u003e();\ndouble myDouble = myIni[\"Foo\"][\"myDouble\"].as\u003cdouble\u003e();\nstd::string myStr = myIni[\"Foo\"][\"myStr\"].as\u003cstd::string\u003e();\nconst char *myStr2 = myIni[\"Foo\"][\"myStr\"].as\u003cconst char*\u003e();\n```\n\nNatively supported types are:\n\n* ```bool```\n* ```char```\n* ```unsigned char```\n* ```short```\n* ```unsigned short```\n* ```int```\n* ```unsigned int```\n* ```long```\n* ```unsigned long```\n* ```float```\n* ```double```\n* ```std::string```\n* ```const char *```\n* ```std::string_view```\n\nCustom type conversions can be added by implementing specialized template of the ```ini::Convert\u003cT\u003e``` functor (see examples).\n\nValues can be assigned to ini fileds just by using the assignment operator.\nThe content of the inifile can then be written to any ```std::ostream``` object.\n\n```cpp\n#include \u003cinicpp.h\u003e\n\nint main()\n{\n    // create ostream object \"os\" ...\n\n    ini::IniFile myIni;\n\n    myIni[\"Foo\"][\"myInt\"] = 1;\n    myIni[\"Foo\"][\"myStr\"] = \"Hello world\";\n    myIni[\"Foo\"][\"myBool\"] = true;\n    myIni[\"Bar\"][\"myDouble\"] = 1.2;\n\n    myIni.encode(os);\n}\n```\n\nYou can directly save ini-data to files by using the  ```save()``` function. It requires a file path\nand automatically stores the ini file contents:\n\n```cpp\n#include \u003cinicpp.h\u003e\n\nint main()\n{\n    ini::IniFile myIni;\n\n    myIni[\"Foo\"][\"myInt\"] = 1;\n    myIni[\"Foo\"][\"myStr\"] = \"Hello world\";\n    myIni[\"Foo\"][\"myBool\"] = true;\n    myIni[\"Bar\"][\"myDouble\"] = 1.2;\n\n    myIni.save(\"some/ini/path\");\n}\n```\n\nYou can define custom type conversions for inifile-cpp which will be automatically used by the assignment operator and the ```as()``` method of ini fields, e.g. you can add support for ```std::vector``` (see also examples):\n\n```cpp\n// the conversion functor must live in the \"ini\" namespace\nnamespace ini\n{\n    /** Conversion functor to parse std::vectors from an ini field-\n      * The generic template can be passed down to the vector. */\n    template\u003ctypename T\u003e\n    struct Convert\u003cstd::vector\u003cT\u003e\u003e\n    {\n        /** Decodes a std::vector from a string. */\n        void decode(const std::string \u0026value, std::vector\u003cT\u003e \u0026result)\n        {\n            result.clear();\n\n            // variable to store the decoded value of each element\n            T decoded;\n            // maintain a start and end pos within the string\n            size_t startPos = 0;\n            size_t endPos = 0;\n            size_t cnt;\n\n            while(endPos != std::string::npos)\n            {\n                if(endPos != 0)\n                    startPos = endPos + 1;\n                // search for the next comma as separator\n                endPos = value.find(',', startPos);\n\n                // if no comma was found use the rest of the string\n                // as input\n                if(endPos == std::string::npos)\n                    cnt = value.size() - startPos;\n                else\n                    cnt = endPos - startPos;\n\n                std::string tmp = value.substr(startPos, cnt);\n                // use the conversion functor for the type contained in\n                // the vector, so the vector can use any type that\n                // is compatible with inifile-cpp\n                Convert\u003cT\u003e conv;\n                conv.decode(tmp, decoded);\n                result.push_back(decoded);\n\n            }\n        }\n\n        /** Encodes a std::vector to a string. */\n        void encode(const std::vector\u003cT\u003e \u0026value, std::string \u0026result)\n        {\n            // variable to store the encoded element value\n            std::string encoded;\n            // string stream to build the result stream\n            std::stringstream ss;\n            for(size_t i = 0; i \u003c value.size(); ++i)\n            {\n                // use the conversion functor for the type contained in\n                // the vector, so the vector can use any type that\n                // is compatible with inifile-cp\n                Convert\u003cT\u003e conv;\n                conv.encode(value[i], encoded);\n                ss \u003c\u003c encoded;\n\n                // if this is not the last element add a comma as separator\n                if(i != value.size() - 1)\n                    ss \u003c\u003c ',';\n            }\n            // store the created string in the result\n            result = ss.str();\n        }\n    };\n}\n```\n\n## Contributing\n\nIf you want to contribute new features or bug fixes, simply file a pull request.\nMake sure all CI checks pass, otherwise PRs will not be merged.\n\n## License\n\n`inifile-cpp` is licensed under the [MIT license](https://github.com/Rookfighter/inifile-cpp/blob/main/LICENSE.txt)\n","funding_links":[],"categories":["Configuration","C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRookfighter%2Finifile-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRookfighter%2Finifile-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRookfighter%2Finifile-cpp/lists"}