This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfreader.h
98 lines (85 loc) · 2.39 KB
/
confreader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @brief Configuration File Reader
*
* Reads an implementation of the INI "standard". Note that INI
* isn't actually a standard. We support the following:
* - ; comments
* - foo=bar keyword assignment
* - [sections]
*
* @copyright
* This file is part of SiriusOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
* Copyright (C) 2024 Gamma Microsystems
*/
#pragma once
#include <_cheader.h>
#include <sirius/hashmap.h>
_Begin_C_Header
/**
* A configuration file is represented as a hashmap of sections,
* which are themselves hashmaps. You may modify these hashmaps
* to change key values, or add new sections.
*/
typedef struct {
hashmap_t * sections;
} confreader_t;
/**
* confreader_load
*
* Open a configuration file and read its contents.
* Returns NULL if the requested file failed to open.
*/
extern confreader_t * confreader_load(const char * file);
/**
* confreader_get
*
* Retrieve a string value from the config file.
* An empty string for `section` represents the default section.
* If the value is not found, NULL is returned.
*/
extern char * confreader_get(confreader_t * ctx, char * section, char * value);
/**
* confreader_getd
*
* Retrieve a string value from the config file, falling back
* to a default value if the requested key is not found.
*/
extern char * confreader_getd(confreader_t * ctx, char * section, char * value, char * def);
/**
* confreader_int
*
* Retrieve an integer value from the config file.
*
* This is a convenience wrapper that calls atoi().
* If the value is not found, 0 is returned.
*/
extern int confreader_int(confreader_t * ctx, char * section, char * value);
/**
* confreader_intd
*
* Retrieve an integer value from the config file, falling back
* to a default if the requested key is not found.
*/
extern int confreader_intd(confreader_t * ctx, char * section, char * value, int def);
/**
* confreader_free
*
* Free the memory associated with a config file.
*/
extern void confreader_free(confreader_t * conf);
/**
* confreader_write
*
* Write a config file back out to a file.
*/
extern int confreader_write(confreader_t * config, const char * file);
/**
* confreader_create_empty
*
* Create an empty configuration file to be modified directly
* through hashmap values.
*/
extern confreader_t * confreader_create_empty(void);
_End_C_Header