Repository: conan-io/conan-center-index

Issue:
[request] inih/48
### Package Details
  * Package Name/Version: **inih/48**
  * Website: **https://github.com/benhoyt/inih**
  * Source code: **https://github.com/benhoyt/inih**


### Description Of The Library / Tool

Simple .INI file parser in C, good for embedded systems 


Discussion:

Resolving patch:
diff --git a/recipes/inih/all/conanfile.py b/recipes/inih/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/inih/all/conanfile.py
@@ -0,0 +1,78 @@
+from conans import ConanFile, Meson, tools
+from conans.errors import ConanInvalidConfiguration
+import os
+
+
+class InihConan(ConanFile):
+    name = "inih"
+    description = "Simple .INI file parser in C, good for embedded systems "
+    license = "BSD-3-Clause"
+    topics = ("conan", "inih", "ini", "configuration", "parser")
+    homepage = "https://github.com/benhoyt/inih"
+    url = "https://github.com/conan-io/conan-center-index"
+    generators = "cmake"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {
+        "shared": [True, False],
+        "fPIC": [True, False],
+    }
+
+    default_options = {
+        "shared": False,
+        "fPIC": True,
+    }
+
+    _meson = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _build_subfolder(self):
+        return "build_subfolder"
+
+    def build_requirements(self):
+        self.build_requires("meson/0.54.2")
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+            if self.settings.os == "Windows":
+                raise ConanInvalidConfiguration("Shared inih is not supported")
+        del self.settings.compiler.cppstd
+        del self.settings.compiler.libcxx
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version])
+        os.rename("{}-r{}".format(self.name, self.version), self._source_subfolder)
+
+    def _configure_meson(self):
+        if self._meson:
+            return self._meson
+        self._meson = Meson(self)
+        self._meson.options["distro_install"] = True
+        self._meson.configure(source_folder=self._source_subfolder, build_folder=self._build_subfolder)
+        return self._meson
+
+    def build(self):
+        meson = self._configure_meson()
+        meson.build()
+
+    def package(self):
+        self.copy("LICENSE.txt", dst="licenses", src=self._source_subfolder)
+        meson = self._configure_meson()
+        meson.install()
+        tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
+
+        if self.settings.compiler == "Visual Studio":
+            # https://github.com/mesonbuild/meson/issues/7378
+            os.rename(os.path.join(self.package_folder, "lib", "libinih.a"),
+                      os.path.join(self.package_folder, "lib", "inih.lib"))
+
+    def package_info(self):
+        self.cpp_info.libs = ["inih"]


Test patch:
diff --git a/recipes/inih/all/test_package/CMakeLists.txt b/recipes/inih/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/inih/all/test_package/CMakeLists.txt
@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 2.8.11)
+project(test_package)
+
+include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
+conan_basic_setup()
+
+add_executable(${PROJECT_NAME} test_package.c)
+target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
+set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 11)
diff --git a/recipes/inih/all/test_package/conanfile.py b/recipes/inih/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/inih/all/test_package/conanfile.py
@@ -0,0 +1,26 @@
+from conans import ConanFile, CMake, tools
+import os
+import textwrap
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        with open("test.ini", "w") as fn:
+            fn.write(textwrap.dedent("""\
+                [protocol]
+                version = 1337
+                [user]
+                name = conan-center-index
+                email = info@conan.io
+                """))
+        if not tools.cross_building(self.settings):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)
diff --git a/recipes/inih/all/test_package/test_package.c b/recipes/inih/all/test_package/test_package.c
new file mode 100644
--- /dev/null
+++ b/recipes/inih/all/test_package/test_package.c
@@ -0,0 +1,43 @@
+#include "ini.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct
+{
+    int version;
+    const char* name;
+    const char* email;
+} configuration;
+
+static int handler(void* user, const char* section, const char* name,
+                   const char* value)
+{
+    configuration* pconfig = (configuration*)user;
+
+    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
+    if (MATCH("protocol", "version")) {
+        pconfig->version = atoi(value);
+    } else if (MATCH("user", "name")) {
+        pconfig->name = strdup(value);
+    } else if (MATCH("user", "email")) {
+        pconfig->email = strdup(value);
+    } else {
+        return 0;  /* unknown section/name, error */
+    }
+    return 1;
+}
+
+int main(int argc, char* argv[])
+{
+    configuration config;
+
+    if (ini_parse("test.ini", handler, &config) < 0) {
+        printf("Can't load 'test.ini'\n");
+        return 1;
+    }
+    printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n",
+        config.version, config.name, config.email);
+    return 0;
+}