82bcb230a7e67f27af6c82893ae8cd093364f46c
[strongswan.git] / src / libstrongswan / settings / settings_types.h
1 /*
2 * Copyright (C) 2010-2014 Tobias Brunner
3 * HSR 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 * Subsections in original order, as section_t (pointer to obj in sections).
69 */
70 array_t *sections_order;
71
72 /**
73 * Key value pairs, as kv_t.
74 */
75 array_t *kv;
76
77 /**
78 * Key value pairs in original order, as kv_t (pointer to obj in kv).
79 */
80 array_t *kv_order;
81 };
82
83 /**
84 * Create a key/value pair.
85 *
86 * @param key key (gets adopted)
87 * @param value value (gets adopted)
88 * @return allocated key/value pair
89 */
90 kv_t *settings_kv_create(char *key, char *value);
91
92 /**
93 * Destroy a key/value pair.
94 *
95 * @param this key/value pair to destroy
96 * @param contents optional array to store the value in
97 */
98 void settings_kv_destroy(kv_t *this, array_t *contents);
99
100 /**
101 * Set the value of the given key/value pair.
102 *
103 * @param kv key/value pair
104 * @param value new value (gets adopted), may be NULL
105 * @param contents optional array to store replaced values in
106 */
107 void settings_kv_set(kv_t *kv, char *value, array_t *contents);
108
109 /**
110 * Add the given key/value pair to the given section.
111 *
112 * @param section section to add pair to
113 * @param kv key/value pair to add (gets adopted)
114 * @param contents optional array to store replaced values in
115 */
116 void settings_kv_add(section_t *section, kv_t *kv, array_t *contents);
117
118 /**
119 * Create a section with the given name.
120 *
121 * @param name name (gets adopted)
122 * @return allocated section
123 */
124 section_t *settings_section_create(char *name);
125
126 /**
127 * Destroy a section.
128 *
129 * @param this section to destroy
130 * @param contents optional array to store values of removed key/value pairs
131 */
132 void settings_section_destroy(section_t *this, array_t *contents);
133
134 /**
135 * Add the given section to the given parent section.
136 *
137 * @param parent section to add section to
138 * @param section section to add (gets adopted)
139 * @param contents optional array to store replaced values in
140 */
141 void settings_section_add(section_t *parent, section_t *section,
142 array_t *contents);
143
144 /**
145 * Extend the first section with the values and sub-sections of the second
146 * section, from where they are consequently removed.
147 *
148 * @param base base section to extend
149 * @param extension section whose data is extracted
150 * @param contents optional array to store replaced values in
151 * @param purge TRUE to remove settings and sections not found in the
152 * extension (unless (sub-)sections have/are fallbacks)
153 */
154 void settings_section_extend(section_t *base, section_t *extension,
155 array_t *contents, bool purge);
156
157 /**
158 * Callback to find a section by name
159 */
160 int settings_section_find(const void *a, const void *b);
161
162 /**
163 * Callback to sort sections by name
164 */
165 int settings_section_sort(const void *a, const void *b, void *user);
166
167 /**
168 * Callback to find a key/value pair by key
169 */
170 int settings_kv_find(const void *a, const void *b);
171
172 /**
173 * Callback to sort kv pairs by key
174 */
175 int settings_kv_sort(const void *a, const void *b, void *user);
176
177 #endif /** SETTINGS_TYPES_H_ @}*/