ProductPromotion
Logo

C++ Programming

made by https://0x3d.site

GitHub - Rookfighter/inifile-cpp: A header-only and easy to use Ini file parser for C++.
A header-only and easy to use Ini file parser for C++. - Rookfighter/inifile-cpp
Visit Site

GitHub - Rookfighter/inifile-cpp: A header-only and easy to use Ini file parser for C++.

GitHub - Rookfighter/inifile-cpp: A header-only and easy to use Ini file parser for C++.

inifile-cpp

License CMake codecov

inifile-cpp is a simple and easy to use single header-only ini file en- and decoder for C++.

Install

Install the headers using the CMake build system:

cd <path-to-repo>
mkdir build
cd build
cmake ..
make install

or simply copy the header file into your project and include it directly.

Usage

For examples on how to use and extend inifile-cpp for your custom needs, please have a look at the examples/ directory.

inifile-cpp allows loading data from any std::istream and requires a single function call or use the overloaded constructor.

#include <inicpp.h>

int main()
{
    // create istream object "is" ...

    // use function
    ini::IniFile myFirstIni;
    myFirstIni.decode(is);

    // or use the constructor
    ini::IniFile mySecondIni(is);
}

You can directly load ini-data from files by using the load() function. It requires a file path and automatically parses its contents:

#include <inicpp.h>

int main()
{
    // load an ini file
    ini::IniFile myIni;
    myIni.load("some/ini/path");
}

You 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.

#include <inicpp.h>

int main()
{
    // load an ini file
    ini::IniFile myIni;
    myIni.setMultiLineValues(true);
    myIni.load("some/ini/path");
}

When 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.

#include <inicpp.h>

int main()
{
    // load an ini file
    ini::IniFile myIni;
    myIni.allowOverwriteDuplicateFields(false);
    // throws an exception if the ini file has duplicate fields
    myIni.load("some/ini/path");
}

Sections and fields can be accessed using the index operator []. The values can be converted to various native types:

bool myBool = myIni["Foo"]["myBool"].as<bool>();
char myChar = myIni["Foo"]["myChar"].as<char>();
unsigned char myUChar = myIni["Foo"]["myUChar"].as<unsigned char>();
int myInt = myIni["Foo"]["myInt"].as<int>();
unsigned int myUInt = myIni["Foo"]["myUInt"].as<unsigned int>();
long myLong = myIni["Foo"]["myLong"].as<long>();
unsigned long myULong = myIni["Foo"]["myULong"].as<unsigned long>();
float myFloat = myIni["Foo"]["myFloat"].as<float>();
double myDouble = myIni["Foo"]["myDouble"].as<double>();
std::string myStr = myIni["Foo"]["myStr"].as<std::string>();
const char *myStr2 = myIni["Foo"]["myStr"].as<const char*>();

Natively supported types are:

  • bool
  • char
  • unsigned char
  • short
  • unsigned short
  • int
  • unsigned int
  • long
  • unsigned long
  • float
  • double
  • std::string
  • const char *
  • std::string_view

Custom type conversions can be added by implementing specialized template of the ini::Convert<T> functor (see examples).

Values can be assigned to ini fileds just by using the assignment operator. The content of the inifile can then be written to any std::ostream object.

#include <inicpp.h>

int main()
{
    // create ostream object "os" ...

    ini::IniFile myIni;

    myIni["Foo"]["myInt"] = 1;
    myIni["Foo"]["myStr"] = "Hello world";
    myIni["Foo"]["myBool"] = true;
    myIni["Bar"]["myDouble"] = 1.2;

    myIni.encode(os);
}

You can directly save ini-data to files by using the save() function. It requires a file path and automatically stores the ini file contents:

#include <inicpp.h>

int main()
{
    ini::IniFile myIni;

    myIni["Foo"]["myInt"] = 1;
    myIni["Foo"]["myStr"] = "Hello world";
    myIni["Foo"]["myBool"] = true;
    myIni["Bar"]["myDouble"] = 1.2;

    myIni.save("some/ini/path");
}

You 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):

// the conversion functor must live in the "ini" namespace
namespace ini
{
    /** Conversion functor to parse std::vectors from an ini field-
      * The generic template can be passed down to the vector. */
    template<typename T>
    struct Convert<std::vector<T>>
    {
        /** Decodes a std::vector from a string. */
        void decode(const std::string &value, std::vector<T> &result)
        {
            result.clear();

            // variable to store the decoded value of each element
            T decoded;
            // maintain a start and end pos within the string
            size_t startPos = 0;
            size_t endPos = 0;
            size_t cnt;

            while(endPos != std::string::npos)
            {
                if(endPos != 0)
                    startPos = endPos + 1;
                // search for the next comma as separator
                endPos = value.find(',', startPos);

                // if no comma was found use the rest of the string
                // as input
                if(endPos == std::string::npos)
                    cnt = value.size() - startPos;
                else
                    cnt = endPos - startPos;

                std::string tmp = value.substr(startPos, cnt);
                // use the conversion functor for the type contained in
                // the vector, so the vector can use any type that
                // is compatible with inifile-cpp
                Convert<T> conv;
                conv.decode(tmp, decoded);
                result.push_back(decoded);

            }
        }

        /** Encodes a std::vector to a string. */
        void encode(const std::vector<T> &value, std::string &result)
        {
            // variable to store the encoded element value
            std::string encoded;
            // string stream to build the result stream
            std::stringstream ss;
            for(size_t i = 0; i < value.size(); ++i)
            {
                // use the conversion functor for the type contained in
                // the vector, so the vector can use any type that
                // is compatible with inifile-cp
                Convert<T> conv;
                conv.encode(value[i], encoded);
                ss << encoded;

                // if this is not the last element add a comma as separator
                if(i != value.size() - 1)
                    ss << ',';
            }
            // store the created string in the result
            result = ss.str();
        }
    };
}

Contributing

If you want to contribute new features or bug fixes, simply file a pull request. Make sure all CI checks pass, otherwise PRs will not be merged.

License

inifile-cpp is licensed under the MIT license

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory