libeconf: include/libeconf.h Source File

libeconf 0.4.0

libeconf.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2019 SUSE LLC
3 Author: Pascal Arlt <parlt@suse.com>
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 SOFTWARE.
22 */
23
24 #pragma once
25
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
40 enum econf_err {
42 ECONF_SUCCESS = 0,
44 ECONF_ERROR = 1,
46 ECONF_NOMEM = 2,
48 ECONF_NOFILE = 3,
50 ECONF_NOGROUP = 4,
52 ECONF_NOKEY = 5,
54 ECONF_EMPTYKEY = 6,
56 ECONF_WRITEERROR = 7,
58 ECONF_PARSE_ERROR = 8,
60 ECONF_MISSING_BRACKET = 9,
62 ECONF_MISSING_DELIMITER = 10,
64 ECONF_EMPTY_SECTION_NAME = 11,
66 ECONF_TEXT_AFTER_SECTION = 12
67 };
68
69 typedef enum econf_err econf_err;
70
81 #define econf_setValue(kf, group, key, value) (( \
82 _Generic((value), \
83 int: econf_setIntValue, \
84 long: econf_setInt64Value, \
85 unsigned int: econf_setUIntValue, \
86 unsigned long: econf_setUInt64Value, \
87 float: econf_setFloatValue, \
88 double: econf_setDoubleValue, \
89 char*: econf_setStringValue, void*: econf_setStringValue)) \
90 (kf, group, key, value))
91
98 #define econf_free(value) (( \
99 _Generic((value), \
100 econf_file*: econf_freeFile , \
101 char**: econf_freeArray)) \
102 (value))
103
104 typedef struct econf_file econf_file;
105
132 extern econf_err econf_readFile(econf_file **result, const char *file_name,
133 const char *delim, const char *comment);
134
161 extern econf_err econf_mergeFiles(econf_file **merged_file,
162 econf_file *usr_file, econf_file *etc_file);
163
194 extern econf_err econf_readDirs(econf_file **key_file,
195 const char *usr_conf_dir,
196 const char *etc_conf_dir,
197 const char *project_name,
198 const char *config_suffix,
199 const char *delim,
200 const char *comment);
201
218 extern econf_err econf_readDirsHistory(econf_file ***key_files,
219 size_t *size,
220 const char *usr_conf_dir,
221 const char *etc_conf_dir,
222 const char *project_name,
223 const char *config_suffix,
224 const char *delim,
225 const char *comment);
226
227 /* The API/ABI of the following three functions (econf_newKeyFile,
228 econf_newIniFile and econf_writeFile) are not stable and will change */
229
243 extern econf_err econf_newKeyFile(econf_file **result, char delimiter, char comment);
244
252 extern econf_err econf_newIniFile(econf_file **result);
253
254
263 extern econf_err econf_writeFile(econf_file *key_file, const char *save_to_dir,
264 const char *file_name);
265
266 /* --------------- */
267 /* --- GETTERS --- */
268 /* --------------- */
269
277 extern char *econf_getPath(econf_file *kf);
278
287 extern econf_err econf_getGroups(econf_file *kf, size_t *length, char ***groups);
288
299 extern econf_err econf_getKeys(econf_file *kf, const char *group, size_t *length, char ***keys);
300
310 extern econf_err econf_getIntValue(econf_file *kf, const char *group, const char *key, int32_t *result);
311
321 extern econf_err econf_getInt64Value(econf_file *kf, const char *group, const char *key, int64_t *result);
322
332 extern econf_err econf_getUIntValue(econf_file *kf, const char *group, const char *key, uint32_t *result);
333
343 extern econf_err econf_getUInt64Value(econf_file *kf, const char *group, const char *key, uint64_t *result);
344
354 extern econf_err econf_getFloatValue(econf_file *kf, const char *group, const char *key, float *result);
355
365 extern econf_err econf_getDoubleValue(econf_file *kf, const char *group, const char *key, double *result);
366
376 extern econf_err econf_getStringValue(econf_file *kf, const char *group, const char *key, char **result);
377
387 extern econf_err econf_getBoolValue(econf_file *kf, const char *group, const char *key, bool *result);
388
400 extern econf_err econf_getIntValueDef(econf_file *kf, const char *group, const char *key, int32_t *result, int32_t def);
401
413 extern econf_err econf_getInt64ValueDef(econf_file *kf, const char *group, const char *key, int64_t *result, int64_t def);
414
426 extern econf_err econf_getUIntValueDef(econf_file *kf, const char *group, const char *key, uint32_t *result, uint32_t def);
427
439 extern econf_err econf_getUInt64ValueDef(econf_file *kf, const char *group, const char *key, uint64_t *result, uint64_t def);
440
452 extern econf_err econf_getFloatValueDef(econf_file *kf, const char *group, const char *key, float *result, float def);
453
465 extern econf_err econf_getDoubleValueDef(econf_file *kf, const char *group, const char *key, double *result, double def);
466
478 extern econf_err econf_getStringValueDef(econf_file *kf, const char *group, const char *key, char **result, char *def);
479
491 extern econf_err econf_getBoolValueDef(econf_file *kf, const char *group, const char *key, bool *result, bool def);
492
493 /* --------------- */
494 /* --- SETTERS --- */
495 /* --------------- */
496
506 extern econf_err econf_setIntValue(econf_file *kf, const char *group, const char *key, int32_t value);
507
517 extern econf_err econf_setInt64Value(econf_file *kf, const char *group, const char *key, int64_t value);
518
528 extern econf_err econf_setUIntValue(econf_file *kf, const char *group, const char *key, uint32_t value);
529
539 extern econf_err econf_setUInt64Value(econf_file *kf, const char *group, const char *key, uint64_t value);
540
550 extern econf_err econf_setFloatValue(econf_file *kf, const char *group, const char *key, float value);
551
561 extern econf_err econf_setDoubleValue(econf_file *kf, const char *group, const char *key, double value);
562
572 extern econf_err econf_setStringValue(econf_file *kf, const char *group, const char *key, const char *value);
573
583 extern econf_err econf_setBoolValue(econf_file *kf, const char *group, const char *key, const char *value);
584
585 /* --------------- */
586 /* --- HELPERS --- */
587 /* --------------- */
588
595 extern const char *econf_errString (const econf_err error);
596
603 extern void econf_errLocation (char **filename, uint64_t *line_nr);
604
611 extern void econf_freeArray(char **array);
612
619 extern void econf_freeFile(econf_file *key_file);
620
621 #ifdef __cplusplus
622 }
623 #endif
ECONF_EMPTY_SECTION_NAME
Empty section name.
Definition: libeconf.h:64
ECONF_PARSE_ERROR
General syntax error in input file.
Definition: libeconf.h:58
ECONF_MISSING_DELIMITER
Missing delimiter.
Definition: libeconf.h:62
econf_file
struct econf_file econf_file
Definition: libeconf.h:104
econf_setFloatValue
econf_err econf_setFloatValue(econf_file *kf, const char *group, const char *key, float value)
Set float value for given group/key.
econf_readDirs
econf_err econf_readDirs(econf_file **key_file, const char *usr_conf_dir, const char *etc_conf_dir, const char *project_name, const char *config_suffix, const char *delim, const char *comment)
Evaluating key/values of a given configuration by reading and merging all needed/available files in t...
ECONF_TEXT_AFTER_SECTION
Text after section.
Definition: libeconf.h:66
econf_setDoubleValue
econf_err econf_setDoubleValue(econf_file *kf, const char *group, const char *key, double value)
Set double value for given group/key.
ECONF_NOFILE
Config file not found.
Definition: libeconf.h:48
econf_getFloatValueDef
econf_err econf_getFloatValueDef(econf_file *kf, const char *group, const char *key, float *result, float def)
Evaluating float value for given group/key.
econf_getUInt64Value
econf_err econf_getUInt64Value(econf_file *kf, const char *group, const char *key, uint64_t *result)
Evaluating uint64 value for given group/key.
econf_errLocation
void econf_errLocation(char **filename, uint64_t *line_nr)
Info about where the error has happened.
econf_getPath
char * econf_getPath(econf_file *kf)
Evaluating path name.
econf_getUInt64ValueDef
econf_err econf_getUInt64ValueDef(econf_file *kf, const char *group, const char *key, uint64_t *result, uint64_t def)
Evaluating uint64 value for given group/key.
econf_getUIntValue
econf_err econf_getUIntValue(econf_file *kf, const char *group, const char *key, uint32_t *result)
Evaluating uint32 value for given group/key.
econf_freeArray
void econf_freeArray(char **array)
Free an array of type char** created by econf_getGroups() or econf_getKeys().
econf_freeFile
void econf_freeFile(econf_file *key_file)
Free memory allocated by e.g.
ECONF_NOGROUP
Group not found.
Definition: libeconf.h:50
econf_getBoolValue
econf_err econf_getBoolValue(econf_file *kf, const char *group, const char *key, bool *result)
Evaluating bool value for given group/key.
econf_getGroups
econf_err econf_getGroups(econf_file *kf, size_t *length, char ***groups)
Evaluating all group entries.
econf_getInt64ValueDef
econf_err econf_getInt64ValueDef(econf_file *kf, const char *group, const char *key, int64_t *result, int64_t def)
Evaluating int64 value for given group/key.
econf_getDoubleValueDef
econf_err econf_getDoubleValueDef(econf_file *kf, const char *group, const char *key, double *result, double def)
Evaluating double value for given group/key.
econf_readDirsHistory
econf_err econf_readDirsHistory(econf_file ***key_files, size_t *size, const char *usr_conf_dir, const char *etc_conf_dir, const char *project_name, const char *config_suffix, const char *delim, const char *comment)
Evaluating key/values for every given configuration files in two different directories (normally in /...
econf_getStringValue
econf_err econf_getStringValue(econf_file *kf, const char *group, const char *key, char **result)
Evaluating string value for given group/key.
ECONF_EMPTYKEY
Key has empty value.
Definition: libeconf.h:54
econf_getKeys
econf_err econf_getKeys(econf_file *kf, const char *group, size_t *length, char ***keys)
Evaluating all keys.
econf_getFloatValue
econf_err econf_getFloatValue(econf_file *kf, const char *group, const char *key, float *result)
Evaluating float value for given group/key.
econf_getStringValueDef
econf_err econf_getStringValueDef(econf_file *kf, const char *group, const char *key, char **result, char *def)
Evaluating string value for given group/key.
econf_writeFile
econf_err econf_writeFile(econf_file *key_file, const char *save_to_dir, const char *file_name)
Write content of a econf_file struct to specified location.
econf_setInt64Value
econf_err econf_setInt64Value(econf_file *kf, const char *group, const char *key, int64_t value)
Set int64 value for given group/key.
ECONF_SUCCESS
General purpose success code.
Definition: libeconf.h:42
econf_getInt64Value
econf_err econf_getInt64Value(econf_file *kf, const char *group, const char *key, int64_t *result)
Evaluating int64 value for given group/key.
econf_setUIntValue
econf_err econf_setUIntValue(econf_file *kf, const char *group, const char *key, uint32_t value)
Set uint32 value for given group/key.
ECONF_NOMEM
Out of memory.
Definition: libeconf.h:46
econf_setStringValue
econf_err econf_setStringValue(econf_file *kf, const char *group, const char *key, const char *value)
Set string value for given group/key.
econf_newKeyFile
econf_err econf_newKeyFile(econf_file **result, char delimiter, char comment)
Create a new econf_file object.
econf_setUInt64Value
econf_err econf_setUInt64Value(econf_file *kf, const char *group, const char *key, uint64_t value)
Set uint64 value for given group/key.
econf_getUIntValueDef
econf_err econf_getUIntValueDef(econf_file *kf, const char *group, const char *key, uint32_t *result, uint32_t def)
Evaluating uint32 value for given group/key.
econf_setBoolValue
econf_err econf_setBoolValue(econf_file *kf, const char *group, const char *key, const char *value)
Set bool value for given group/key.
econf_getIntValueDef
econf_err econf_getIntValueDef(econf_file *kf, const char *group, const char *key, int32_t *result, int32_t def)
Evaluating int32 value for given group/key.
econf_getBoolValueDef
econf_err econf_getBoolValueDef(econf_file *kf, const char *group, const char *key, bool *result, bool def)
Evaluating bool value for given group/key.
econf_readFile
econf_err econf_readFile(econf_file **result, const char *file_name, const char *delim, const char *comment)
Process the file of the given file_name and save its contents into key_file object.
econf_newIniFile
econf_err econf_newIniFile(econf_file **result)
Create a new econf_file object in IniFile format.
ECONF_MISSING_BRACKET
Missing closing section bracket.
Definition: libeconf.h:60
econf_errString
const char * econf_errString(const econf_err error)
Convert an econf_err type to a string.
econf_err
econf_err
libeconf error codes
Definition: libeconf.h:40
econf_getIntValue
econf_err econf_getIntValue(econf_file *kf, const char *group, const char *key, int32_t *result)
Evaluating int32 value for given group/key.
econf_mergeFiles
econf_err econf_mergeFiles(econf_file **merged_file, econf_file *usr_file, econf_file *etc_file)
Merge the contents of two key_files objects.
ECONF_NOKEY
Key not found.
Definition: libeconf.h:52
econf_getDoubleValue
econf_err econf_getDoubleValue(econf_file *kf, const char *group, const char *key, double *result)
Evaluating double value for given group/key.
ECONF_ERROR
Generic Error.
Definition: libeconf.h:44
econf_setIntValue
econf_err econf_setIntValue(econf_file *kf, const char *group, const char *key, int32_t value)
Set int32 value for given group/key.
ECONF_WRITEERROR
Error creating or writing to a file.
Definition: libeconf.h:56