src/cx/properties.h

changeset 924
3c90dfc35f06
parent 923
45da884269c8
equal deleted inserted replaced
923:45da884269c8 924:3c90dfc35f06
36 #ifndef UCX_PROPERTIES 36 #ifndef UCX_PROPERTIES
37 #define UCX_PROPERTIES 37 #define UCX_PROPERTIES
38 38
39 #include "common.h" 39 #include "common.h"
40 #include "string.h" 40 #include "string.h"
41 #include "array_list.h"
41 42
42 struct cx_properties_config_s { 43 struct cx_properties_config_s {
43 /** 44 /**
44 * The key/value delimiter that shall be used. 45 * The key/value delimiter that shall be used.
45 * This is '=' by default. 46 * This is '=' by default.
48 49
49 /** 50 /**
50 * The character, when appearing at the end of a line, continues that line. 51 * The character, when appearing at the end of a line, continues that line.
51 * This is '\' by default. 52 * This is '\' by default.
52 */ 53 */
53 char continuation; 54 // char continuation; // TODO: line continuation in properties
54 55
55 /** 56 /**
56 * The first comment character. 57 * The first comment character.
57 * This is '#' by default. 58 * This is '#' by default.
58 */ 59 */
102 CX_PROPERTIES_INCOMPLETE_DATA, 103 CX_PROPERTIES_INCOMPLETE_DATA,
103 /** 104 /**
104 * Input buffer is \c NULL. 105 * Input buffer is \c NULL.
105 */ 106 */
106 CX_PROPERTIES_NULL_INPUT, 107 CX_PROPERTIES_NULL_INPUT,
108 /**
109 * The line contains a delimiter, but no key.
110 */
111 CX_PROPERTIES_INVALID_EMPTY_KEY,
112 /**
113 * The line contains data, but no delimiter.
114 */
115 CX_PROPERTIES_INVALID_MISSING_DELIMITER,
116 /**
117 * More internal buffer was needed, but could not be allocated.
118 */
119 CX_PROPERTIES_BUFFER_ALLOC_FAILED,
107 }; 120 };
108 121
109 /** 122 /**
110 * Interface for working with properties data. 123 * Interface for working with properties data.
111 */ 124 */
119 * The text buffer. 132 * The text buffer.
120 */ 133 */
121 const char *text; 134 const char *text;
122 135
123 /** 136 /**
124 * Length of the text buffer. 137 * Size of the text buffer.
125 */ 138 */
126 size_t text_len; 139 size_t text_size;
127 140
128 /** 141 /**
129 * Position in the text buffer. 142 * Position in the text buffer.
130 */ 143 */
131 size_t text_pos; 144 size_t text_pos;
145
146 /**
147 * Temporary internal buffer.
148 */
149 char *buf;
150
151 /**
152 * Size of the internal buffer.
153 */
154 size_t buf_size;
155
156 /**
157 * Capacity of the internal buffer.
158 */
159 size_t buf_capacity;
160
161 /**
162 * Internal flags.
163 */
164 int flags;
132 }; 165 };
133 166
134 /** 167 /**
135 * Typedef for the properties interface. 168 * Typedef for the properties interface.
136 */ 169 */
137 typedef struct cx_properties_s CxProperties; 170 typedef struct cx_properties_s CxProperties;
138 171
139 172
140 /** 173 /**
141 * Initialize a properties parser. 174 * Initialize a properties interface.
142 * 175 *
143 * @param prop the properties interface 176 * @param prop the properties interface
144 * @param config the properties configuration 177 * @param config the properties configuration
145 * @see cxPropertiesInitDefault() 178 * @see cxPropertiesInitDefault()
146 */ 179 */
147 __attribute__((__nonnull__)) 180 __attribute__((__nonnull__))
148 static inline void cxPropertiesInit( 181 void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config);
149 CxProperties *prop, 182
150 CxPropertiesConfig config 183 /**
151 ) { 184 * Destroys the properties interface.
152 prop->config = config; 185 *
153 prop->text = NULL; 186 * \note Even when you are certain that you did not use the interface in a
154 } 187 * way that caused a memory allocation, you should call this function anyway.
188 * Future versions of the library might add features that need additional memory
189 * and you really don't want to search the entire code where you might need
190 * add call to this function.
191 *
192 * @param prop the properties interface
193 */
194 __attribute__((__nonnull__))
195 void cxPropertiesDestroy(CxProperties *prop);
155 196
156 /** 197 /**
157 * Initialize a properties parser with the default configuration. 198 * Initialize a properties parser with the default configuration.
158 * 199 *
159 * @param prop the properties interface 200 * @param prop the properties interface
171 * @param prop the properties interface 212 * @param prop the properties interface
172 * @param buf a pointer to data 213 * @param buf a pointer to data
173 * @param len the length of the data 214 * @param len the length of the data
174 */ 215 */
175 __attribute__((__nonnull__)) 216 __attribute__((__nonnull__))
176 static inline void cxPropertiesInput( 217 void cxPropertiesInput(
177 CxProperties *prop, 218 CxProperties *prop,
178 const void *buf, 219 const char *buf,
179 size_t len 220 size_t len
180 ) { 221 );
181 prop->text = buf; 222
182 prop->text_len = len; 223 /**
183 prop->text_pos = 0; 224 * Sets a new input buffer after copying the current unprocessed data
184 } 225 * to a temporary buffer.
226 *
227 * This temporary buffer is allocated on the heap, unless you specified
228 * a buffer on the stack with #cxPropertiesUseStack().
229 * In that case, the stack buffer is used, until the capacity is not sufficient
230 * anymore.
231 *
232 * When this function is called without any unprocessed data that needs to be
233 * copied, it behaves exactly as #cxPropertiesInput().
234 *
235 * @param prop the properties interface
236 * @param buf a pointer to data
237 * @param len the length of the data
238 * @return non-zero when a memory allocation was necessary but failed
239 */
240 __attribute__((__nonnull__))
241 int cxPropertiesFill(
242 CxProperties *prop,
243 const char *buf,
244 size_t len
245 );
246
247 /**
248 * Specifies stack memory that shall be used by #cxPropertiesFill().
249 *
250 * @param prop the properties interface
251 * @param buf a pointer to stack memory
252 * @param capacity the capacity of the stack memory
253 */
254 void cxPropertiesUseStack(
255 CxProperties *prop,
256 char *buf,
257 size_t capacity
258 );
185 259
186 /** 260 /**
187 * Retrieves the next key/value-pair. 261 * Retrieves the next key/value-pair.
188 * 262 *
189 * This function returns zero as long as there are key/value-pairs 263 * This function returns zero as long as there are key/value-pairs found.
190 * found. If no more key/value-pairs are found, #CX_PROPERTIES_NO_DATA 264 * If no more key/value-pairs are found, #CX_PROPERTIES_NO_DATA is returned.
191 * is returned. You may refill the input buffer with cxPropertiesInput(). 265 *
192 * 266 * When an incomplete line is encountered, #CX_PROPERTIES_INCOMPLETE_DATA is
193 * When an invalid line is encountered, #CX_PROPERTIES_INVALID_LINE is returned 267 * returned and the position of the input buffer will be the start of the
194 * and the position of the input buffer will be the start of the affected line. 268 * affected line. You can then add more data with #cxPropertiesFill().
195 * 269 *
196 * @param prop the properties interface 270 * \attention The returned strings will point into a buffer that might not be
197 * @param name a pointer to the cxstring that shall contain the property name 271 * available later. It is strongly recommended to copy the strings for further
272 * use.
273 *
274 * @param prop the properties interface
275 * @param key a pointer to the cxstring that shall contain the property name
198 * @param value a pointer to the cxstring that shall contain the property value 276 * @param value a pointer to the cxstring that shall contain the property value
199 * @return Nonzero, if a key/value-pair was successfully retrieved 277 * @return the status code as defined above
200 * @see ucx_properties_fill() 278 * @see cxPropertiesFill()
201 */ 279 */
202 enum cx_properties_status cxPropertiesNext( 280 enum cx_properties_status cxPropertiesNext(
203 CxProperties *prop, 281 CxProperties *prop,
204 cxstring *name, 282 cxstring *key,
205 cxstring *value 283 cxstring *value
206 ); 284 );
207 285
208 #endif // UCX_PROPERTIES 286 #endif // UCX_PROPERTIES

mercurial