settings: Add functions to add sections and key/value pairs to a section
[strongswan.git] / src / libstrongswan / settings / settings_types.h
1 /*
2 * Copyright (C) 2010-2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * Internal data types and functions shared between the parser and t.
18 *
19 * @defgroup settings_types settings_types
20 * @{ @ingroup settings
21 */
22
23 #ifndef SETTINGS_TYPES_H_
24 #define SETTINGS_TYPES_H_
25
26 typedef struct kv_t kv_t;
27 typedef struct section_t section_t;
28
29 #include "collections/array.h"
30
31 /**
32 * Key/value pair.
33 */
34 struct kv_t {
35
36 /**
37 * Key string, relative, not the full name.
38 */
39 char *key;
40
41 /**
42 * Value as string.
43 */
44 char *value;
45 };
46
47 /**
48 * Section containing subsections and key value pairs.
49 */
50 struct section_t {
51
52 /**
53 * Name of the section.
54 */
55 char *name;
56
57 /**
58 * Fallback sections, as section_t.
59 */
60 array_t *fallbacks;
61
62 /**
63 * Subsections, as section_t.
64 */
65 array_t *sections;
66
67 /**
68 * Key value pairs, as kv_t.
69 */
70 array_t *kv;
71 };
72
73 /**
74 * Create a key/value pair.
75 *
76 * @param key key (gets adopted)
77 * @param value value (gets adopted)
78 * @return allocated key/value pair
79 */
80 kv_t *settings_kv_create(char *key, char *value);
81
82 /**
83 * Destroy a key/value pair.
84 *
85 * @param this key/value pair to destroy
86 * @param contents optional array to store the value in
87 */
88 void settings_kv_destroy(kv_t *this, array_t *contents);
89
90 /**
91 * Add the given key/value pair to the given section.
92 *
93 * @param section section to add pair to
94 * @param kv key/value pair to add (gets adopted)
95 * @param contents optional array to store replaced values in
96 */
97 void settings_kv_add(section_t *section, kv_t *kv, array_t *contents);
98
99 /**
100 * Create a section with the given name.
101 *
102 * @param name name (gets adopted)
103 * @return allocated section
104 */
105 section_t *settings_section_create(char *name);
106
107 /**
108 * Destroy a section.
109 *
110 * @param this section to destroy
111 * @param contents optional array to store values of removed key/value pairs
112 */
113 void settings_section_destroy(section_t *this, array_t *contents);
114
115 /**
116 * Add the given section to the given parent section.
117 *
118 * @param parent section to add section to
119 * @param section section to add (gets adopted)
120 * @param contents optional array to store replaced values in
121 */
122 void settings_section_add(section_t *parent, section_t *section,
123 array_t *contents);
124
125 /**
126 * Extend the first section with the values and sub-sections of the second
127 * section, from where they are consequently removed.
128 *
129 * @param base base section to extend
130 * @param extension section whose data is extracted
131 * @param contents optional array to store replaced values in
132 */
133 void settings_section_extend(section_t *base, section_t *extension,
134 array_t *contents);
135
136 /**
137 * Callback to find a section by name
138 */
139 int settings_section_find(const void *a, const void *b);
140
141 /**
142 * Callback to sort sections by name
143 */
144 int settings_section_sort(const void *a, const void *b, void *user);
145
146 /**
147 * Callback to find a key/value pair by key
148 */
149 int settings_kv_find(const void *a, const void *b);
150
151 /**
152 * Callback to sort kv pairs by key
153 */
154 int settings_kv_sort(const void *a, const void *b, void *user);
155
156 #endif /** SETTINGS_TYPES_H_ @}*/