summaryrefslogtreecommitdiff
path: root/include/hybbx/config.h
blob: c2083ab4469a6942cfc0920a8803deb9eb256d56 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef HYBBX_CONFIG_H
#define HYBBX_CONFIG_H

#include "hybbx/types.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct hybbx_config_entry {
    char *section;
    char *key;
    char *value;
} hybbx_config_entry_t;

typedef struct hybbx_config {
    hybbx_config_entry_t *entries;
    size_t count;
} hybbx_config_t;

/** Load configuration from an INI file. */
hybbx_result_t hybbx_config_load(hybbx_config_t *config, const char *path);

/** Release all memory held by @p config. */
void hybbx_config_free(hybbx_config_t *config);

/**
 * Return the value for @p key in @p section, or @p default_value if missing.
 * Both @p section and @p key are case-sensitive.
 */
const char *hybbx_config_get(const hybbx_config_t *config,
                             const char *section,
                             const char *key,
                             const char *default_value);

/** Parse HyBBX boolean tokens (yes/no and aliases; see hybbx_parse_bool). */
int hybbx_config_get_bool(const hybbx_config_t *config,
                          const char *section,
                          const char *key,
                          int default_value);

/**
 * Parse an unsigned decimal integer in [@p min_value, @p max_value].
 * Returns @p default_value when missing or invalid.
 */
unsigned hybbx_config_get_uint(const hybbx_config_t *config,
                               const char *section,
                               const char *key,
                               unsigned default_value,
                               unsigned min_value,
                               unsigned max_value);

/**
 * Build a semicolon-separated key=value string for all keys in @p section.
 * Caller must free the returned string.
 */
char *hybbx_config_format_section(const hybbx_config_t *config,
                                  const char *section);

/**
 * Format all @c transport.<plugin> and @c transport.<plugin>N
 * sections (in file order) into one start string. Multiple sections are
 * joined with @ref HYBBX_PACKET_RADIO_INSTANCE_SEP (packet_radio only).
 * Caller must free the returned string.
 */
char *hybbx_config_format_transport_sections(const hybbx_config_t *config,
                                             const char *plugin_name);

/**
 * Like @ref hybbx_config_format_transport_sections for @c packet_radio, but
 * prepends a `[max25]` key chunk when that INI section exists.
 */
char *hybbx_config_format_packet_radio_start(const hybbx_config_t *config);

/** Like packet_radio start — prepends `[max25]` keys for baycom transport. */
char *hybbx_config_format_baycom_start(const hybbx_config_t *config);

/**
 * Prepend @c [max25] keys as a packet_radio start prefix (before instance sep).
 * Caller must free the returned string.
 */
char *hybbx_config_prepend_packet_radio_max25(const hybbx_config_t *config,
                                              const char *body);

/**
 * Resolve INI section for a transport plugin: @c transport.<plugin> first,
 * else the first @c transport.<plugin><digits> section with keys
 * (e.g. @c transport.packet_radio1). Writes the chosen name to @p out_section.
 * Returns 1 when a numbered (or legacy) section with keys was found, else 0.
 */
int hybbx_config_resolve_transport_section(const hybbx_config_t *config,
                                           const char *plugin_name,
                                           char *out_section,
                                           size_t out_size);

typedef void (*hybbx_config_iter_fn)(const char *section, const char *key,
                                     const char *value, void *ctx);

typedef void (*hybbx_config_section_iter_fn)(const char *section, void *ctx);

/** Invoke @p fn for every entry in @p config (in file order). */
void hybbx_config_foreach(const hybbx_config_t *config,
                          hybbx_config_iter_fn fn, void *ctx);

/** Invoke @p fn once per unique section name (first occurrence order). */
void hybbx_config_foreach_section(const hybbx_config_t *config,
                                  hybbx_config_section_iter_fn fn, void *ctx);

/** Set or replace a key in @p config (in-memory only until saved). */
hybbx_result_t hybbx_config_set(hybbx_config_t *config,
                               const char *section,
                               const char *key,
                               const char *value);

/** Remove all keys in @p section from @p config. */
void hybbx_config_remove_section(hybbx_config_t *config, const char *section);

/** Write @p config to @p path (INI format). */
hybbx_result_t hybbx_config_save(const hybbx_config_t *config,
                                 const char *path);

#ifdef __cplusplus
}
#endif

#endif /* HYBBX_CONFIG_H */
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com