Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New intefaces call econf_set_opt in order to set libeconf environment. #206

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.12)
# Ensure built-in policies from CMake are used, (e.g. improved policies for macOS)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})

project(libeconf VERSION 0.6.3
project(libeconf VERSION 0.7.0
DESCRIPTION "Enhanced config file parser, which merges config files placed in several locations into one."
LANGUAGES C
)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ as the vendor configuration file.

Optionally, schemes with only drop-ins and without a ‘main’ configuration file will be supported too. In such
schemes many drop-ins are loaded from a common directory in each hierarchy.
For example, /usr/lib/<project>.d/*, /run/<project>.d/* and /etc/<project>.d/c.conf are all loaded and parsed
For example, /usr/lib/_project_.d/*, /run/_project_.d/* and /etc/_project_.d/c.conf are all loaded and parsed
in this scheme.

**Example 1**
Expand Down Expand Up @@ -76,5 +76,5 @@ The API is written in plain C. The description can be found here :https://opensu

## Bindings for other languages

- [Python](https://github.com/openSUSE/libeconf/blob/v0.6.0/bindings/python3/) ([Documentation](https://github.com/openSUSE/libeconf/blob/v0.6.0/bindings/python3/docs/python-libeconf.3)
- [C#](https://github.com/openSUSE/libeconf/blob/v0.6.0/bindings/csharp/) ([Documentation](https://github.com/openSUSE/libeconf/blob/v0.6.0/bindings/csharp/docs/README.md))
- [Python](https://github.com/openSUSE/libeconf/blob/v0.7.0/bindings/python3/) ([Documentation](https://github.com/openSUSE/libeconf/blob/v0.6.0/bindings/python3/docs/python-libeconf.3)
- [C#](https://github.com/openSUSE/libeconf/blob/v0.7.0/bindings/csharp/) ([Documentation](https://github.com/openSUSE/libeconf/blob/v0.6.0/bindings/csharp/docs/README.md))
9 changes: 9 additions & 0 deletions bindings/python3/econf.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,15 @@ def write_file(ef: EconfFile, save_to_dir: str, file_name: str) -> None:
if err:
raise ECONF_EXCEPTION[EconfError(err)](f"write_file failed with error: {err_string(err)}")

def econf_set_opt(option: str) -> None:
"""
Set libeconv environment.

:param option: defined as a string (format <key>=<value>)
:return: Nothing
"""
c_option = _encode_str(option)
LIBECONF.econf_set_opt(c_option)

def get_path(ef: EconfFile) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion bindings/python3/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "python-libeconf"
version = "0.6.0"
version = "0.7.0"
description = "Python bindings for libeconf"
authors = [{name="nkrapp", email="[email protected]"}]

Expand Down
4 changes: 2 additions & 2 deletions doc/man/libeconf.3
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ econf_free (key_file);
.fi
.PP
.PP
Default behaviour if entries have the same name in one file: The first hit will be returned\&. Further entries will be ignored\&. This can be changed by setting the environment variable ECONF_JOIN_SAME_ENTRIES\&. In that case entries with the same name will be joined to one single entry\&.
Default behaviour if entries have the same name in one file: The first hit will be returned\&. Further entries will be ignored\&. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt)\&. In that case entries with the same name will be joined to one single entry\&.

.SS "\fBeconf_err\fP econf_readFileWithCallback (\fBeconf_file\fP ** result, const char * file_name, const char * delim, const char * comment, bool (*callback)(const char *filename, const void *data), const void *callback_data)"

Expand Down Expand Up @@ -744,7 +744,7 @@ econf_free (key_file);
.fi
.PP
.PP
Default behaviour if entries have the same name in one file: The first hit will be returned\&. Further entries will be ignored\&. This can be changed by setting the environment variable ECONF_JOIN_SAME_ENTRIES\&. In that case entries with the same name will be joined to one single entry\&.
Default behaviour if entries have the same name in one file: The first hit will be returned\&. Further entries will be ignored\&. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt)\&. In that case entries with the same name will be joined to one single entry\&.

.SS "\fBeconf_err\fP econf_mergeFiles (\fBeconf_file\fP ** merged_file, \fBeconf_file\fP * usr_file, \fBeconf_file\fP * etc_file)"

Expand Down
6 changes: 3 additions & 3 deletions docs/libeconf_8h.html
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ <h2 class="memtitle"><span class="permalink"><a href="#aa03377bd02882239b3342c09
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>econf_err ECONF_SUCCESS or error code</dd></dl>
<p>Usage: </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="libeconf_8h.html">libeconf.h</a>&quot;</span></div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a0d29e6d5c6c6d151bb3edc54019c8735">econf_file</a> *key_file = NULL;</div><div class="line"><a class="code" href="libeconf_8h.html#a30cb4e6c5ce4daa333646b23b20caa0a">econf_err</a> error;</div><div class="line"></div><div class="line">error = <a class="code" href="libeconf_8h.html#aa03377bd02882239b3342c095f916561">econf_readFile</a> (&amp;key_file, <span class="stringliteral">&quot;/etc/test.conf&quot;</span>, <span class="stringliteral">&quot;=&quot;</span>, <span class="stringliteral">&quot;#&quot;</span>);</div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a27a9d78ae827ef885a4373fa591ded73">econf_free</a> (key_file);</div></div><!-- fragment --><p>Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable ECONF_JOIN_SAME_ENTRIES. In that case entries with the same name will be joined to one single entry. </p>
<p>Usage: </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="libeconf_8h.html">libeconf.h</a>&quot;</span></div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a0d29e6d5c6c6d151bb3edc54019c8735">econf_file</a> *key_file = NULL;</div><div class="line"><a class="code" href="libeconf_8h.html#a30cb4e6c5ce4daa333646b23b20caa0a">econf_err</a> error;</div><div class="line"></div><div class="line">error = <a class="code" href="libeconf_8h.html#aa03377bd02882239b3342c095f916561">econf_readFile</a> (&amp;key_file, <span class="stringliteral">&quot;/etc/test.conf&quot;</span>, <span class="stringliteral">&quot;=&quot;</span>, <span class="stringliteral">&quot;#&quot;</span>);</div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a27a9d78ae827ef885a4373fa591ded73">econf_free</a> (key_file);</div></div><!-- fragment --><p>Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with the same name will be joined to one single entry. </p>

</div>
</div>
Expand Down Expand Up @@ -584,7 +584,7 @@ <h2 class="memtitle"><span class="permalink"><a href="#a6bf61969ee7ea191fdd28b0c
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>econf_err ECONF_SUCCESS or error code</dd></dl>
<p>Usage: </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="libeconf_8h.html">libeconf.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> checkFile(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename, <span class="keyword">const</span> <span class="keywordtype">void</span> *data) {</div><div class="line"> - checking code which returns <span class="keyword">true</span> or <span class="keyword">false</span> -</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a0d29e6d5c6c6d151bb3edc54019c8735">econf_file</a> *key_file = NULL;</div><div class="line"><a class="code" href="libeconf_8h.html#a30cb4e6c5ce4daa333646b23b20caa0a">econf_err</a> error;</div><div class="line"></div><div class="line">error = <a class="code" href="libeconf_8h.html#a6bf61969ee7ea191fdd28b0c8de5761d">econf_readFileWithCallback</a> (&amp;key_file, <span class="stringliteral">&quot;/etc/test.conf&quot;</span>, <span class="stringliteral">&quot;=&quot;</span>, <span class="stringliteral">&quot;#&quot;</span>, checkFile, NULL);</div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a27a9d78ae827ef885a4373fa591ded73">econf_free</a> (key_file);</div></div><!-- fragment --><p>Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable ECONF_JOIN_SAME_ENTRIES. In that case entries with the same name will be joined to one single entry. </p>
<p>Usage: </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="libeconf_8h.html">libeconf.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> checkFile(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename, <span class="keyword">const</span> <span class="keywordtype">void</span> *data) {</div><div class="line"> - checking code which returns <span class="keyword">true</span> or <span class="keyword">false</span> -</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a0d29e6d5c6c6d151bb3edc54019c8735">econf_file</a> *key_file = NULL;</div><div class="line"><a class="code" href="libeconf_8h.html#a30cb4e6c5ce4daa333646b23b20caa0a">econf_err</a> error;</div><div class="line"></div><div class="line">error = <a class="code" href="libeconf_8h.html#a6bf61969ee7ea191fdd28b0c8de5761d">econf_readFileWithCallback</a> (&amp;key_file, <span class="stringliteral">&quot;/etc/test.conf&quot;</span>, <span class="stringliteral">&quot;=&quot;</span>, <span class="stringliteral">&quot;#&quot;</span>, checkFile, NULL);</div><div class="line"></div><div class="line"><a class="code" href="libeconf_8h.html#a27a9d78ae827ef885a4373fa591ded73">econf_free</a> (key_file);</div></div><!-- fragment --><p>Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with the same name will be joined to one single entry. </p>

</div>
</div>
Expand Down Expand Up @@ -1014,7 +1014,7 @@ <h2 class="memtitle"><span class="permalink"><a href="#a94f42f580d8c9dff8cdb01a5
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>econf_err ECONF_SUCCESS or error code</dd></dl>
<p>Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable ECONF_JOIN_SAME_ENTRIES. In that case entries with the same name will be joined to one single entry. </p>
<p>Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with the same name will be joined to one single entry. </p>

</div>
</div>
Expand Down
33 changes: 27 additions & 6 deletions include/libeconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ typedef struct econf_file econf_file;
* Default behaviour if entries have the same name in one file: The
* first hit will be returned. Further entries will be ignored.
* This can be changed by setting the environment variable
* ECONF_JOIN_SAME_ENTRIES. In that case entries with the same name
* will be joined to one single entry.
* JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with
* the same name will be joined to one single entry.
*/
extern econf_err econf_readFile(econf_file **result, const char *file_name,
const char *delim, const char *comment);
Expand Down Expand Up @@ -191,8 +191,8 @@ extern econf_err econf_readFile(econf_file **result, const char *file_name,
* Default behaviour if entries have the same name in one file: The
* first hit will be returned. Further entries will be ignored.
* This can be changed by setting the environment variable
* ECONF_JOIN_SAME_ENTRIES. In that case entries with the same name
* will be joined to one single entry.
* JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with
* the same name will be joined to one single entry.
*/
extern econf_err econf_readFileWithCallback(econf_file **result, const char *file_name,
const char *delim, const char *comment,
Expand Down Expand Up @@ -618,8 +618,8 @@ extern econf_err econf_readDirsHistoryWithCallback(econf_file ***key_files,
* Default behaviour if entries have the same name in one file: The
* first hit will be returned. Further entries will be ignored.
* This can be changed by setting the environment variable
* ECONF_JOIN_SAME_ENTRIES. In that case entries with the same name
* will be joined to one single entry.
* JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with
* the same name will be joined to one single entry.
*/
extern econf_err econf_newKeyFile(econf_file **result, char delimiter, char comment);

Expand Down Expand Up @@ -1034,6 +1034,27 @@ extern void econf_freeArray(char **array);
*/
extern void econf_freeFile(econf_file *key_file);

/** @brief Set libeconv environment.
*
* @param option defined as a string (format "<key>=<value>")
* @return void
*
* Usage:
* @code
* #include "libeconf.h"
*
* econf_set_opt("JOIN_SAME_ENTRIES=1");
* @endcode
*
* Not known options will be ignored. Following options are supported:
* JOIN_SAME_ENTRIES (default 0)
* Parsed entries with the same name will not be replaces but
* will be joined to one entry.
* PYTHON_STYLE (default 0)
* E.G. Identations will be handled like multiline entries.
*/
extern void econf_set_opt(const char *option);

/** @brief All parsed files require this user permission.
* DEPRECATED: Use the callback in econf_readFileWithCallback or
* econf_readConfigWithCallback instead.
Expand Down
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(econf_SRCS libeconf.c
getfilecontents.c
mergefiles.c
helpers.c
options.c
keyfile.c
econf_error.c
get_value_def.c
Expand All @@ -14,6 +15,7 @@ set(econf_HDRS defines.h
getfilecontents.h
mergefiles.h
helpers.h
options.h
keyfile.h
readconfig.h
)
Expand Down
3 changes: 2 additions & 1 deletion lib/getfilecontents.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "defines.h"
#include "getfilecontents.h"
#include "helpers.h"
#include "options.h"

#include <errno.h>
#include <limits.h>
Expand Down Expand Up @@ -529,7 +530,7 @@ read_file(econf_file *ef, const char *file,
if (current_comment_after_value)
free(current_comment_after_value);

if(getenv("ECONF_JOIN_SAME_ENTRIES"))
if(option_join_same_entries() == true)
{
join_same_entries(ef);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/libeconf.map
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ LIBECONF_0.6 {
global:
econf_readConfigWithCallback;
econf_readConfig;
} LIBECONF_0.5;
} LIBECONF_0.5;
LIBECONF_0.7 {
global:
econf_set_opt;
} LIBECONF_0.6;
62 changes: 62 additions & 0 deletions lib/options.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright (C) 2024 SUSE LLC

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.
*/

#include "libeconf.h"
#include "options.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*General options which can be set with econf_set_opt */
static bool join_same_entries = false;
static bool python_style = false;

extern void econf_set_opt(const char *option)
{
if (strcmp(option, "JOIN_SAME_ENTRIES=1") == 0) {
join_same_entries = true;
return;
}
if (strcmp(option, "JOIN_SAME_ENTRIES=0") == 0) {
join_same_entries = false;
return;
}
if (strcmp(option, "PYTHON_STYLE=1") == 0) {
python_style = true;
return;
}
if (strcmp(option, "PYTHON_STYLE=0") == 1) {
python_style = false;
return;
}
}

bool option_join_same_entries(void)
{
return join_same_entries;
}
bool option_python_style(void)
{
return python_style;
}

5 changes: 5 additions & 0 deletions lib/options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#pragma once

bool option_join_same_entries(void);
bool option_python_style(void);
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ project(
'b_pie=true',
'warning_level=3',],
license : 'MIT',
version : '0.6.3',
version : '0.7.0',
)

cc = meson.get_compiler('c')
Expand Down Expand Up @@ -50,6 +50,7 @@ libeconf_src = files(
'lib/libeconf_ext.c',
'lib/readconfig.c',
'lib/mergefiles.c',
'lib/options.c'
)
example_src = ['example/example.c']
econftool_src = ['util/econftool.c']
Expand Down
Loading
Loading