Debian Codegraph @

ini/ini_configobj.h

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Header file for the ini configuration interface.
5 THIS IS THE PREFERRED INTERFACE TO USE.
6
7 Copyright (C) Dmitri Pal <[email protected]> 2010 - 2012
8
9 INI Library is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 INI Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #ifndef INI_CONFIGOBJ_H
25 #define INI_CONFIGOBJ_H
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include "ref_array.h"
33
34 struct value_obj;
35
36 #ifndef DING_ATTR_FORMAT
37 # if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)))
38 # define DING_ATTR_FORMAT(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
39 # else
40 # define DING_ATTR_FORMAT(fmt, args)
41 # endif
42 #endif
43
44 /** @mainpage The INI configuration interface
45 *
46 * The goal of the this interface is to allow applications
47 * to read configuration from an INI file.
48 *
49 * So why yet another library to read data from an INI file?
50 * As we started the SSSD project we looked around for a
51 * open source library that would meet the following
52 * requirements:
53 * - Is written in C (not C++)
54 * - Is lightweight.
55 * - Has an live community.
56 * - Supported on multiple platforms .
57 * - Can evolve as we build SSSD solution.
58 * - Can deal with different types of values including arrays.
59 * - Can deal with sections that are related to each other
60 * and can form a hierarchy of sections.
61 * - Has a compatible license we can use.
62 *
63 * We have seen several solutions but none was able to fully address
64 * our requirements. As a result we started developing our own
65 * INI parsing library.
66 *
67 * Currently INI parser allows reading and merging INI files
68 * and getting a resulting configuration in one object.
69 *
70 * One of the main advantages of this interface is that
71 * the library is created with the idea of reading the configuration
72 * data, not managing it. Thus currently you will not find
73 * any function that alters the configuration data read from the files.
74 * There is a set of proposed enhancements to be able to manipulate
75 * the configuration data and save it back but there has been no real
76 * driver for it. This API is focused on letting applications read data
77 * from a file (or files) and interpret it, not to generate configuration
78 * files. There are all sorts of different tools that already do that.
79 *
80 * The INI configuration interface uses COLLECTION (see libcollection
81 * interface) to store data internally.
82 *
83 * Concepts:
84 * - INI file consists of the key value pairs.
85 * - Keys and values are separated by the equal sign.
86 * Spaces around equal sign are trimmed. Everything before the equal
87 * sign is the key, everything after is the value.
88 * - Comments are the lines that start with ";" or "#" in the first
89 * position of the line.
90 * - Library now supports multi-line values. Values that span across multiple
91 * lines should start with a single space on every new line.
92 * - After being read both keys and values are stored in the internal
93 * objects.
94 * - Application configuration can consist from multiple files.
95 * For example, there can be a generic file in /etc containing
96 * configuration for all applications of a particular class running
97 * on a box and then there might be a special file
98 * with parameters specific for each application in the
99 * /etc/whatever.d directory. Interface does not allow reading
100 * multiple files in one call. Instead files need to be read separately
101 * and then merged together. A helper function to do so might be added
102 * later.
103 * - If there is no section in the file or there are key value pairs
104 * declared before the first section, those pairs will be placed into
105 * the default section with the name "default".
106 * - All values are treated as strings. Spaces are trimmed at the beginning
107 * and the end of the value. The value ends at the end of the line.
108 * If values is too long, an error will be returned.
109 * - Parsing of a value happens when a caller tries to interpret
110 * the value. The caller can use different functions to do this.
111 * The value can be treated as numeric, logical, string, binary,
112 * array of strings or array of numbers. In case of arrays parsing functions
113 * accept separators that will be used to slice the value into the array
114 * of elements.
115 * - If there is any error parsing section and key values it can be
116 * intercepted by the caller. There are different modes that the library
117 * supports regarding error handling. See details in the description
118 * of individual functions.
119 * - The library allows ini files with fragmented sections. This means that
120 * a section can be scattered across the file. Flags control what to
121 * do in such situation.
122 * - The library allows storing and retrieving multiple key value pairs with
123 * the same key in one section.
124 *
125 * <i>Previously deprecated interface ini_config.h isn't supported anymore.</i>
126 */
127
128 /**
129 * @defgroup structures Structures
130 *
131 * All structures used in the interface should be treated as internal opaque objects.
132 *
133 * @{
134 *
135 * @}
136 */
137
138
139 /**
140 * @defgroup bomType Types of configutration file encodings
141 *
142 * Constants that define how configuration file is encoded.
143 *
144 * @{
145 */
146 /** Enumeration of the encoding types. */
147
148 enum index_utf_t {
149 INDEX_UTF32BE = 0, /**< The file is encoded in 'big-endian' 32-bit */
150 INDEX_UTF32LE = 1, /**< The file is encoded in 'little-endian' 32-bit */
151 INDEX_UTF16BE = 2, /**< The file is encoded in 'big-endian' 16-bit */
152 INDEX_UTF16LE = 3, /**< The file is encoded in 'little-endian' 16-bit */
153 INDEX_UTF8 = 4, /**< The file is encoded in standard UTF8 but has BOM */
154 INDEX_UTF8NOBOM = 5 /**< The file is encoded in standard UTF8 without BOM */
155 };
156
157 /**
158 * @}
159 */
160
161
162 /**
163 * @defgroup errorlevel Error tolerance constants
164 *
165 * Constants in this section define what to do if
166 * error or warning encountered while parsing the INI file.
167 *
168 * @{
169 */
170 /** Enumeration of error levels. */
171 enum ERR_LEVEL {
172 INI_STOP_ON_ANY = 0, /**< Fail if any problem is detected. */
173 INI_STOP_ON_NONE = 1, /**< Best effort - do not fail. */
174 INI_STOP_ON_ERROR = 2 /**< Fail on errors only. */
175 };
176
177 /**
178 * @}
179 */
180
181 /**
182 * @defgroup parseerr Parsing errors and warnings
183 *
184 * Parsing errors and warnings
185 *
186 * @{
187 */
188 /** Enumeration of parsing errors. */
189 enum ERR_PARSE {
190 ERR_LONGDATA = 1, /**< Line is too long (Error). */
191 ERR_NOCLOSESEC, /**< No closing bracket in section
192 definition (Error). */
193 ERR_NOSECTION, /**< Section name is missing (Error). */
194 ERR_SECTIONLONG, /**< Section name too long (Error). */
195 ERR_NOEQUAL, /**< No equal sign (Error). */
196 ERR_NOKEY, /**< No key before equal sign (Error). */
197 ERR_LONGKEY, /**< Key is too long (Error). */
198 ERR_READ, /**< Failed to read line (Error). */
199 ERR_SPACE, /**< Line starts with space when it
200 should not (Error). */
201 ERR_DUPKEY, /**< Duplicate key is not allowed (Error). */
202 ERR_DUPKEYSEC, /**< Duplicate key is detected while merging
203 sections (Error). */
204 ERR_DUPSECTION, /**< Duplicate section is not allowed (Error). */
205 ERR_SPECIAL, /**< Line contains invalid characters (Error). */
206 ERR_TAB, /**< Line starts with tab when it
207 should not (Error). */
208 ERR_BADCOMMENT, /**< End of file while processing comment (Error). */
209 ERR_MAXPARSE = ERR_BADCOMMENT /**< Special value. Size of the error array. */
210 };
211
212 /**
213 * @}
214 */
215
216 /**
217 * @defgroup metacollect Constants that define what meta data to collect
218 *
219 * Constants in this section define what meta data to collect
220 *
221 *
222 * @{
223 */
224 /** @brief Do not collect any data. */
225 #define INI_META_NONE 0
226 /** @brief Collect file stats. */
227 #define INI_META_STATS 1
228
229 /**
230 * @}
231 */
232
233 /**
234 * @defgroup accesscheck Access control check flags
235 *
236 * @{
237 */
238
239 /**
240 * @brief Validate access mode
241 *
242 * If this flag is specified the mode parameter
243 * will be matched against the permissions set on the file
244 * using the provided mask.
245 */
246 #define INI_ACCESS_CHECK_MODE 0x00000001
247
248 /**
249 * @brief Validate uid
250 *
251 * Provided uid will be checked against uid
252 * of the file.
253 */
254 #define INI_ACCESS_CHECK_UID 0x00000002
255
256 /**
257 * @brief Validate gid
258 *
259 * Provided gid will be checked against gid
260 * of the file.
261 */
262 #define INI_ACCESS_CHECK_GID 0x00000004
263
264 /**
265 * @}
266 */
267
268 /**
269 * @defgroup collisionflags Flags that define collision resolution logic.
270 *
271 * @{
272 */
273
274 /**
275 * @defgroup onesecvalue Colliding values come from one section
276 *
277 * Flags that define collision resolution logic for values in
278 * the same section.
279 * These flags should be used during parsing to handle duplicate
280 * keys in the same section of the ini file.
281 *
282 * @{
283 */
284
285 /** @brief Value with same key is overwritten */
286 #define INI_MV1S_OVERWRITE 0x0000
287 /** @brief Collision causes error */
288 #define INI_MV1S_ERROR 0x0001
289 /** @brief Second value is discarded */
290 #define INI_MV1S_PRESERVE 0x0002
291 /** @brief Duplicates are allowed */
292 #define INI_MV1S_ALLOW 0x0003
293 /** @brief Duplicates are allowed but errors are logged */
294 #define INI_MV1S_DETECT 0x0004
295
296 /**
297 * @}
298 */
299
300 /**
301 * @defgroup twosecvalue Colliding values come from two sections
302 *
303 * Flags that define collision resolution logic between two values
304 * that come from two sections with the same name.
305 * These flags should be used during parsing to handle duplicate
306 * keys coming from the same section scattered across the ini file.
307 * These flags also can be used to specify the rules of merging
308 * values that come from two different configuration files.
309 *
310 * @{
311 */
312 /** @brief Value with same key is overwritten */
313 #define INI_MV2S_OVERWRITE 0x0000
314 /** @brief Collision causes error */
315 #define INI_MV2S_ERROR 0x0010
316 /** @brief Second value is discarded */
317 #define INI_MV2S_PRESERVE 0x0020
318 /** @brief Duplicates are allowed */
319 #define INI_MV2S_ALLOW 0x0030
320 /** @brief Duplicates are allowed but errors are logged */
321 #define INI_MV2S_DETECT 0x0040
322
323 /**
324 * @}
325 */
326
327 /**
328 * @defgroup mergesec Collision in two sections
329 *
330 * Flags that define collision resolution logic between two sections.
331 * These flags should be used during parsing to handle duplicate
332 * sections scattered across the ini file.
333 * These flags also can be used to specify the rules of merging
334 * sections that come from two different configuration files.
335 *
336 * @{
337 */
338 /** @brief Sections are merged */
339 #define INI_MS_MERGE 0x0000
340 /** @brief Collision causes error */
341 #define INI_MS_ERROR 0x0100
342 /** @brief First section is discarded */
343 #define INI_MS_OVERWRITE 0x0200
344 /** @brief Second section is discarded */
345 #define INI_MS_PRESERVE 0x0300
346 /** @brief Log errors if duplicate sections are detected; non-exclusive */
347 /** This defaults to MERGE, but can be used with OVERWRITE and PRESERVE **/
348 #define INI_MS_DETECT 0x0400
349
350 /**
351 * @}
352 */
353
354 /**
355 * @}
356 */
357
358 /**
359 * @defgroup parseflags Flags that define parsing rules
360 *
361 * Flags that define how the file should be parsed.
362 *
363 * @{
364 */
365
366 /** @brief Suppress multi line value wrapping */
367 #define INI_PARSE_NOWRAP 0x0001
368 /** @brief No spaces are allowed to the left of the key */
369 #define INI_PARSE_NOSPACE 0x0002
370 /** @brief No tabs are allowed to the left of the key */
371 #define INI_PARSE_NOTAB 0x0004
372 /** @brief Do not allow C-style comments */
373 #define INI_PARSE_NO_C_COMMENTS 0x0008
374 /** @brief Skip lines that are not KVPs */
375 #define INI_PARSE_IGNORE_NON_KVP 0x0010
376
377 /**
378 * @}
379 */
380
381 /**
382 * @defgroup searchmode Constants that define how to look for a value
383 *
384 * Configuration file can allow several keys with the same name
385 * in one section. Use the constants below to define which
386 * value you are looking for.
387 * You can search for the next value only if you are looking
388 * for the same section and key as in the previous search. If you
389 * specify INI_GET_NEXT_VALUE but the section or key is
390 * different from the values that were used in the previous search
391 * the value will be ignored and the function will act as if
392 * INI_GET_FIRST_VALUE is specified.
393 * This functionality allows creating an attribute list and
394 * actually fetching every value including duplicate values
395 * in a single loop.
396 *
397 * @{
398 */
399 /** Enumeration of parsing errors. */
400 enum INI_GET {
401 INI_GET_FIRST_VALUE, /**< Get the first value (default). */
402 INI_GET_NEXT_VALUE, /**< Look for the next value in the section */
403 INI_GET_LAST_VALUE /**< Look for the last value in the section */
404 };
405
406 /**
407 * @}
408 */
409
410
411 /**
412 * @defgroup augment Constants and structures related to augmentation.
413 *
414 * @{
415 */
416
417 /** Structure to pass access check parameters to augmentation function.
418 *
419 */
420 struct access_check {
421 uint32_t flags; /**< Define what to check.
422 * One can check file
423 * permissions with mask,
424 * uid, and gid of the file.
425 * See \ref accesscheck constants.
426 */
427 uid_t uid; /**< Expected uid of the file. */
428 gid_t gid; /**< Expected gid of the file.*/
429 mode_t mode; /**< Expected mode of the file. */
430 mode_t mask; /**< Mask to use in the mode check.
431 * Mask is always adjusted to
432 * include at least S_IRWXU,
433 * S_IRWXG and S_IRWXO.
434 */
435 };
436
437 /** Enumeration of augmentation modes. */
438 enum augmode {
439 INI_AUG_ANY = 0, /**< Allow any augmentation. */
440 INI_AUG_ADD = 1, /**< Allow only new sections. */
441 INI_AUG_OVER = 2 /**< Allow section updates. */
442 };
443
444 /**
445 * @}
446 */
447
448 /**
449 * @brief Name of the default section.
450 *
451 * This is the name of the implied section where orphan key-value
452 * pairs will be put.
453 */
454 #define INI_DEFAULT_SECTION "default"
455
456 /**
457 * @defgroup structures Structures
458 * @{
459 */
460
461
462 struct ini_cfgobj;
463 struct ini_cfgfile;
464
465 /** @brief Structure that holds error number and
466 * line number for the encountered error.
467 */
468 struct ini_parse_error;
469
470
471 /**
472 * @}
473 */
474
475
476 /**
477 * @defgroup ini_core Core interface functions
478 *
479 * Functions in this section allow manipulation with the configuration file,
480 * parsing data from the configuration file and storing it in a configuration
481 * object, merging configuration objects and other operations.
482 *
483 * For functions to create or alter configuration object in memory
484 * see \ref ini_mod.
485 *
486 * @{
487 *
488 */
489
490 /**
491 * @brief Create a configuration object.
492 *
493 * Allocates an object that will store configuration data.
494 * Configuration object is populated by parsing a file.
495 *
496 * @param[out] ini_config Configuration object.
497 *
498 * @return 0 - Success.
499 * @return EINVAL - Invalid parameter.
500 * @return ENOMEM - No memory.
501 */
502 int ini_config_create(struct ini_cfgobj **ini_config);
503
504 /**
505 * @brief Destroy a configuration object.
506 *
507 * Frees configuration data.
508 *
509 * @param[in] ini_config Configuration object.
510 *
511 */
512 void ini_config_destroy(struct ini_cfgobj *ini_config);
513
514 /**
515 * @brief Flush cached search data.
516 *
517 * Frees cached search data. This will cause
518 * any iteration over the same keys to start over.
519 *
520 * @param[in] ini_config Configuration object.
521 *
522 */
523 void ini_config_clean_state(struct ini_cfgobj *ini_config);
524
525 /**
526 * @brief Create a configuration file object.
527 *
528 * Create a file object for parsing a configuration file.
529 *
530 * A "configuration file object" is different from
531 * a "configuration object". The former stores metadata
532 * about a file the configuration data is read from,
533 * while the latter holds the configuration itself.
534 *
535 * @param[in] filename Name or path to the ini file.
536 * This argument can contain
537 * a short or a fully qualified
538 * file name. If a short name is
539 * specified the full path
540 * will be resolved internally.
541 * @param[in] metadata_flags Flags that specify what additional
542 * data if any needs to be collected
543 * about the ini file. See \ref metacollect.
544 * @param[out] file_ctx Configuration file object.
545 *
546 * @return 0 - Success.
547 * @return EINVAL - Invalid parameter.
548 * @return ENOMEM - No memory.
549 */
550 int ini_config_file_open(const char *filename,
551 uint32_t metadata_flags,
552 struct ini_cfgfile **file_ctx);
553
554 /**
555 * @brief Create a configuration file object using memory buffer.
556 *
557 * Create a file object for parsing a configuration file.
558 * Configuration will use provided memory instead of the actual file.
559 *
560 * A "configuration file object" is different from
561 * a "configuration object". The former stores metadata
562 * about a file the configuration data is read from,
563 * while the latter holds the configuration itself.
564 *
565 * @param[in] data_buf In memory configuration data.
566 * Needs to be NULL terminated.
567 * @param[in] data_len Length of memory data
568 * not including terminating NULL.
569 * @param[out] file_ctx Configuration file object.
570 *
571 * @return 0 - Success.
572 * @return EINVAL - Invalid parameter.
573 * @return ENOMEM - No memory.
574 */
575 int ini_config_file_from_mem(void *data_buf,
576 uint32_t data_len,
577 struct ini_cfgfile **file_ctx);
578
579 /**
580 * @brief Close configuration file after parsing
581 *
582 * Closes file but keeps the context. File can be reopened
583 * and reread using \ref ini_config_file_reopen function.
584 *
585 * @param[in] file_ctx Configuration file object.
586 *
587 */
588 void ini_config_file_close(struct ini_cfgfile *file_ctx);
589
590
591 /**
592 * @brief Reopen the configuration file
593 *
594 * Creates a new file object from the original one.
595 * The file configuration objects then can be compared
596 * to determine whether the file actually changed.
597 *
598 * @param[in] file_ctx_in Original configuration file object.
599 * @param[out] file_ctx_out A new configuration file object.
600 *
601 * @return 0 - Success.
602 * @return EINVAL - Invalid parameter.
603 * @return ENOMEM - No memory.
604 */
605 int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in,
606 struct ini_cfgfile **file_ctx_out);
607
608
609 /**
610 * @brief Close configuration file and free all data
611 *
612 * Closes file and frees the context.
613 *
614 * @param[in] file_ctx Configuration file object.
615 *
616 */
617 void ini_config_file_destroy(struct ini_cfgfile *file_ctx);
618
619 /**
620 * @brief Save configuration in a backup configuration file
621 *
622 * Creates a backup version of the data in a given configuration file.
623 * It is expected that file context was created by some open or reopen
624 * function first. Then the caller can make this call to save the data
625 * aside before parsing the configuration file and making changes to it.
626 *
627 * The caller can specify a backup directory to save the file in.
628 * If directory is not specified then a current working directory will
629 * be used. If the directory is invalid or caller does not have access to it
630 * an error will be returned.
631 *
632 *>The template specifies the file name to use for the backup.
633 *>For example:
634 *> my_file.conf.%d.save
635 *> my_file%d.conf.bak
636 *>The template can contain only one '%d' placeholder. This placeholder
637 * will be replaced by a number. If previously created backup files
638 * are found in the given directory. The function will start with 1 and will
639 * try to find an available unused file name in the given directory
640 * up until it reaches the limit specified in the max_num argument.
641 * Function will return EEXIST if it runs out of attempts to save the file.
642 *
643 * The caller can optionally pass an access structure. The access structure
644 * would specify what mode and ownership to use for the newly created file.
645 * If the access structure is not specified the access data of the original
646 * file context will be used. If file object does not have stats explicitly
647 * read at the time when the object is created then the stats will be collected
648 * but not saved. If the file was a memory mapped file and no access
649 * structure is passed in, the function will use effective UID and GID of the
650 * running process and mode will be set to: S_IFREG | S_IRUSR | S_IWUSR
651 *
652 * @param[in] file_ctx File context of the file to backup.
653 * @param[in] backup_dir Path to backup directory. Can be NULL.
654 * @param[in] backup_tpl File name template with %d placeholder.
655 * @param[in] backup_access Optional access overwrite structure.
656 * See \ref access_check for more details.
657 * @param[in] max_num Maximum number of retries to try to create
658 * a specific backup file.
659 *
660 * @return 0 - Success.
661 * @return EINVAL - Invalid parameter.
662 * @return EIXIST - All possible backup file names are already taken.
663 * @return ENOMEM - No memory.
664 *
665 * Function can return other errors that standard libc functions line open,
666 * chmod, and chown return.
667 */
668 int ini_config_file_backup(struct ini_cfgfile *file_ctx,
669 const char *backup_dir,
670 const char *backup_tpl,
671 struct access_check *backup_access,
672 unsigned max_num);
673
674 /**
675 * @brief Change permissions and ownership of the file
676 *
677 * Function changes access mode and permissions of the file associated
678 * with the given context. If there is no file associated with the context
679 * because the context is created using a memory buffer, then the function
680 * will return an error EINVAL.
681 *
682 * @param[in] file_ctx File context of the file to change access.
683 * @param[in] new_access Structure that defines what access should
684 * be set on the file.
685 * See \ref access_check for more details.
686 *
687 * @return 0 - Success.
688 * @return EINVAL - Invalid parameter.
689 *
690 * Function can return other errors that standard chmod and chown
691 * functions return.
692 */
693 int ini_config_change_access(struct ini_cfgfile *file_ctx,
694 struct access_check *new_access);
695
696 /**
697 * @brief Save configuration in a file
698 *
699 * Function is a wrapper around \ref ini_config_save_as with
700 * argument filename as NULL.
701 *
702 * For more information see \ref ini_config_save_as.
703 */
704 int ini_config_save(struct ini_cfgfile *file_ctx,
705 struct access_check *new_access,
706 struct ini_cfgobj *ini_config);
707
708
709 /* Save configuration in a file using existing context but with a new name */
710 /**
711 * @brief Save configuration with a new name.
712 *
713 * Function uses an existing file context but a new file name.
714 * The file context will be used to perform operation to save file.
715 * By default the ownership, mode and BOM of the new file will be derived
716 * from the existing context. The rest of the context will be reinitialized.
717 * Configuration will be serialized and saved in the file using encoding
718 * specified by BOM type. The BOM prefix will also be added if needed.
719 * After saving the file the function initializes the context and reads the
720 * file back. At this moment the file context is ready for the parsing
721 * again.
722 *
723 * @param[in] file_ctx File context to use for saving.
724 * @param[in] filename Name of the file to save into. If NULL
725 * the file name of the context will be used.
726 * If the context was originally created
727 * as a memory mapped configuration buffer
728 * and filename is NULL the function will
729 * return error EINVAL.
730 * @param[in] new_access Structure that defines what access should
731 * be set on the file.
732 * See \ref access_check for more details.
733 * @param[in] ini_config Configuration to save.
734 *
735 * @return 0 - Success.
736 * @return EINVAL - Invalid parameter.
737 *
738 * Function can return other errors that standard open, chmod and chown
739 * functions return.
740 */
741 int ini_config_save_as(struct ini_cfgfile *file_ctx,
742 const char *filename,
743 struct access_check *new_access,
744 struct ini_cfgobj *ini_config);
745
746
747
748 /**
749 * @brief Return the encoding indicator.
750 *
751 * When the file object is created the configuration data is inspected for
752 * encoding indicator called BOM. This function returns a constant that
753 * indicates what type of BOM was detected.
754 *
755 * @param[in] file_ctx File context.
756 *
757 * Function returns the constant of type enum index_utf_t.
758 */
759 enum index_utf_t ini_config_get_bom(struct ini_cfgfile *file_ctx);
760
761 /**
762 * @brief Set the encoding indicator.
763 *
764 * When the file object is created the configuration data is inspected for
765 * encoding indicator called BOM. The BOM indicator is recorded in the file
766 * object and used when the file is saved. If the caller wants to change
767 * the encoding before saving he can use this function to alter the BOM type.
768 *
769 * @param[in] file_ctx File context.
770 * @param[in] bom BOM type indicator to set.
771 *
772 *
773 * @return 0 - Success.
774 * @return EINVAL - Invalid parameter.
775 */
776 int ini_config_set_bom(struct ini_cfgfile *file_ctx, enum index_utf_t bom);
777
778
779 /**
780 * @brief Check parsing errors count
781 *
782 * Query the configuration file object about
783 * how many parsing errors were found during last
784 * parsing operation.
785 *
786 * @param[in] ini_config Configuration object.
787 *
788 * @return Number of errors.
789 */
790 unsigned ini_config_error_count(struct ini_cfgobj *ini_config);
791
792 /**
793 * @brief Get array of parsing errors
794 *
795 * Function returns a newly allocated array of strings
796 * that should be later freed by the \ref ini_config_free_errors
797 * function.
798 * Array can be referenced as a normal array of strings.
799 * The NULL entry indicates the end of the array.
800 *
801 * @param[in] ini_config Configuration object.
802 * @param[out] errors Array of error strings.
803 *
804 * @return 0 - Success.
805 * @return EINVAL - Invalid parameter.
806 * @return ENOMEM - No memory.
807 */
808 int ini_config_get_errors(struct ini_cfgobj *ini_config,
809 char ***errors);
810
811 /**
812 * @brief Free array of parsing errors
813 *
814 * Free array of parsing errors previously allocated
815 * by using \ref ini_config_get_errors function.
816 *
817 * @param[in] errors Array of error strings.
818 *
819 */
820 void ini_config_free_errors(char **errors);
821
822 /**
823 * @brief Print errors to a file
824 *
825 * Prints array of parsing errors previously allocated
826 * by using \ref ini_config_get_errors function into
827 * a provided file.
828 *
829 * @param[in] file File or stream to send errors to.
830 * @param[in] error_list Array of error strings.
831 *
832 */
833 void ini_config_print_errors(FILE *file, char **error_list);
834
835 /**
836 * @brief Get the fully resolved file name
837 *
838 * Returns the full name to the configuration file
839 * that was resolved by the library.
840 *
841 * @param[in] file_ctx Configuration file object.
842 *
843 * @return Full file name.
844 */
845 const char *ini_config_get_filename(struct ini_cfgfile *file_ctx);
846
847 /**
848 * @brief Get pointer to collected stat data
849 *
850 * Returns the pointer to the internal stat structure.
851 * If stat data was not collected when the file was open
852 * function would return NULL.
853 *
854 * @param[in] file_ctx Configuration file object.
855 *
856 * @return Pointer to the stat structure.
857 */
858 const struct stat *ini_config_get_stat(struct ini_cfgfile *file_ctx);
859
860
861
862 /**
863 * @brief Print file context
864 *
865 * Function is useful for debugging purposes only.
866 *
867 * @param[in] file_ctx Configuration file object.
868 *
869 */
870 void ini_config_file_print(struct ini_cfgfile *file_ctx);
871
872 /**
873 * @brief Check file properties
874 *
875 * Before parsing it makes sense to make sure
876 * that the file you are trying to read is properly
877 * owned and has proper permissions.
878 *
879 * @param[in] file_ctx Configuration file object.
880 * @param[in] flags Define what to check.
881 * One can check file
882 * permissions with mask,
883 * uid, and gid of the file.
884 * See \ref accesscheck.
885 * @param[in] uid Expected uid of the file.
886 * @param[in] gid Expected gid of the file.
887 * @param[in] mode Expected mode of the file.
888 * @param[in] mask Mask to use in the mode check.
889 * Mask is always adjusted to
890 * include at least S_IRWXU,
891 * S_IRWXG and S_IRWXO
892 *
893 * @return 0 - Success.
894 * @return EINVAL - Invalid parameter.
895 * @return EACCES - File attributes do no match expectations.
896 */
897 int ini_config_access_check(struct ini_cfgfile *file_ctx,
898 uint32_t flags,
899 uid_t uid,
900 gid_t gid,
901 mode_t mode,
902 mode_t mask);
903
904 /**
905 * @brief Check if file has changed
906 *
907 * Compares two configuration file objects.
908 * Determines if two objects are different
909 * by comparing:
910 * - time stamp
911 * - device ID
912 * - i-node
913 *
914 * Function can be used to check if the file
915 * has changed since last time the it was read.
916 *
917 * <i> Note:</i> If the file was deleted and quickly
918 * re-created the kernel seems to restore the same i-node.
919 * The stat structure keeps time granularity of seconds.
920 * As a result if the file is quickly recreated
921 * with the same contents like in the unit test the check
922 * would assume that file did not change.
923 * This is why the unit test has a one second delay.
924 *
925 * @param[in] file_ctx1 First configuration file object.
926 * @param[in] file_ctx2 Second configuration file object.
927 * @param[out] changed A value will be set to 0 if
928 * the objects are same and to 1
929 * if they are different.
930 *
931 * @return 0 - Success.
932 * @return EINVAL - Invalid parameter.
933 * @return EACCES - File attributes do no match expectations.
934 */
935 int ini_config_changed(struct ini_cfgfile *file_ctx1,
936 struct ini_cfgfile *file_ctx2,
937 int *changed);
938
939 /**
940 * @brief Parse the file and populate a configuration object
941 *
942 * Function parses the file. It is assumed that
943 * the configuration object was just created.
944 * Using a non empty configuration object in
945 * a parsing operation would fail with EINVAL.
946 *
947 * @param[in] file_ctx Configuration file object.
948 * @param[in] error_level Flags that control actions
949 * in case of parsing error.
950 * See \ref errorlevel.
951 * @param[in] collision_flags Flags that control handling
952 * of the duplicate sections or keys.
953 * See \ref collisionflags.
954 * @param[in] parse_flags Flags that control parsing process,
955 * for example how to handle spaces at
956 * the beginning of the line.
957 * See \ref parseflags.
958 * @param[out] ini_config Configuration object.
959 *
960 * @return 0 - Success.
961 * @return EINVAL - Invalid parameter.
962 * @return ENOMEM - No memory.
963 */
964 int ini_config_parse(struct ini_cfgfile *file_ctx,
965 int error_level,
966 uint32_t collision_flags,
967 uint32_t parse_flags,
968 struct ini_cfgobj *ini_config);
969
970 /**
971 * @brief Create a copy of the configuration object
972 *
973 * Function creates a deep copy of all the configuration data.
974 * Error list created during parsing is not copied over.
975 *
976 * @param[in] ini_config Original configuration object.
977 * @param[out] ini_new A new configuration object.
978 *
979 * @return 0 - Success.
980 * @return EINVAL - Invalid parameter.
981 * @return ENOMEM - No memory.
982 */
983 int ini_config_copy(struct ini_cfgobj *ini_config,
984 struct ini_cfgobj **ini_new);
985
986 /**
987 * @brief Merge two configuration objects
988 *
989 * Function merges configuration objects and creates
990 * a new resulting object out of the two.
991 * IMPORTANT: Use same collision flags for reading
992 * of the files and then merging.
993 * Mixing the flags would lead to strange results
994 * that would be hard to debug.
995 *
996 *
997 * @param[in] first A base object
998 * the other object will
999 * be merged with.
1000 * @param[in] second The object that will
1001 * be merged to the first one.
1002 * @param[in] collision_flags Flags that control handling
1003 * of the duplicate sections or keys.
1004 * See \ref collisionflags.
1005 * @param[out] result A new configuration object,
1006 * the result of the merge.
1007 *
1008 * @return 0 - Success.
1009 * @return EINVAL - Invalid parameter.
1010 * @return ENOMEM - No memory.
1011 */
1012 int ini_config_merge(struct ini_cfgobj *first,
1013 struct ini_cfgobj *second,
1014 uint32_t collision_flags,
1015 struct ini_cfgobj **result);
1016
1017
1018 /**
1019 * @brief Augment configuration
1020 *
1021 * Function merges the main configuration file
1022 * with the configuration file snippets
1023 * read from a specified directory
1024 * ordered according to the locale setting.
1025 *
1026 * @param[in] base_cfg A configuration object
1027 * that will be augmented.
1028 * @param[in] path Path to a directory where
1029 * configuration snippets
1030 * will be read from.
1031 * @param[in] patterns List of regular expressions
1032 * that the name of a snippet file
1033 * has to match to be considered
1034 * for merge. These use POSIX Basic Regular
1035 * Expressions.
1036 * @param[in] sections List of regular expressions
1037 * that the section names in the snippet
1038 * file need to match. If file contains
1039 * sections that do not match any patterns
1040 * the file is skipped and error is recorded.
1041 * These use POSIX Basic Regular Expressions.
1042 * @param[in] check_perm Pointer to structure that
1043 * holds criteria for the
1044 * access check.
1045 * @param[in] error_level Flags that control actions
1046 * in case of parsing error in a snippet file.
1047 * See \ref errorlevel.
1048 * @param[in] collision_flags These flags control how the potential
1049 * collisions between keys and sections
1050 * within the snippet file will be handled.
1051 * For more information
1052 * see \ref collisionflags.
1053 * @param[in] parse_flags Flags that control parsing process,
1054 * for example how to handle spaces at
1055 * the beginning of the line.
1056 * See \ref parseflags.
1057 * @param[in] merge_flags Flags that control handling
1058 * of the duplicate sections or keys
1059 * during merging of the snippets.
1060 * They are different from the collision flags
1061 * because duplicate sections and keys inside
1062 * are snippets most likely will be handled as
1063 * 'last value wins' while during merge
1064 * the attempt to overwrite
1065 * a specific section might be treated as
1066 * an error.
1067 * See \ref mergesec.
1068 * @param[out] result_cfg A new configuration object,
1069 * the result of the merge.
1070 * @param[out] error_list List of strings that
1071 * contains all encountered
1072 * errors.
1073 * It can be NULL, in this case list of errors
1074 * is not populated.
1075 * @param[out] success_list List of strings that
1076 * contains file names of snippets that were
1077 * successfully merged.
1078 * It can be NULL, in this case list of files
1079 * is not populated.
1080 *
1081 * @return 0 - Success.
1082 * @return EINVAL - Invalid parameter.
1083 * @return ENOMEM - No memory.
1084 */
1085 int ini_config_augment(struct ini_cfgobj *base_cfg,
1086 const char *path,
1087 const char *patterns[],
1088 const char *sections[],
1089 struct access_check *check_perm,
1090 int error_level,
1091 uint32_t collision_flags,
1092 uint32_t parse_flags,
1093 uint32_t merge_flags,
1094 struct ini_cfgobj **result_cfg,
1095 struct ref_array **error_list,
1096 struct ref_array **success_list);
1097
1098 /**
1099 * @brief Set the folding boundary
1100 *
1101 * Set the folding boundary for multiline values.
1102 * Use before serializing and saving to a file if the
1103 * default boundary of 80 characters does not work for you.
1104 *
1105 * @param[in] ini_config Configuration object.
1106 * @param[in] boundary Wrapping boundary.
1107 *
1108 * @return 0 - Success.
1109 * @return EINVAL - Invalid parameter.
1110 */
1111 int ini_config_set_wrap(struct ini_cfgobj *ini_config,
1112 uint32_t boundary);
1113
1114 /* Functions that add, modify or delete sections and values in
1115 * the configuration object can be found in section \ref ini_mod.
1116 */
1117
1118 /**
1119 * @}
1120 */
1121
1122
1123 /**
1124 * @defgroup ini_section_and_attr Section and attribute management
1125 *
1126 * Functions in this section allow getting the lists of
1127 * sections in the configuration file and keys in a section
1128 * as arrays of strings.
1129 *
1130 * @{
1131 *
1132 */
1133
1134 /**
1135 * @brief Get list of sections.
1136 *
1137 * Get list of sections from the configuration object
1138 * as an array of strings.
1139 * Function allocates memory for the array of the sections.
1140 * Use \ref ini_free_section_list() to free allocated memory.
1141 *
1142 * @param[in] ini_config Configuration object.
1143 * @param[out] size If not NULL parameter will
1144 * receive number of sections
1145 * in the configuration.
1146 * @param[out] error If not NULL parameter will
1147 * receive the error code.
1148 * 0 - Success.
1149 * EINVAL - Invalid parameter.
1150 * ENOMEM - No memory.
1151 *
1152 * @return Array of strings.
1153 */
1154 char **ini_get_section_list(struct ini_cfgobj *ini_config,
1155 int *size,
1156 int *error);
1157
1158 /**
1159 * @brief Free list of sections.
1160 *
1161 * The section array created by \ref ini_get_section_list()
1162 * should be freed using this function.
1163 *
1164 * @param[in] section_list Array of strings returned by
1165 * \ref ini_get_section_list() function.
1166 */
1167 void ini_free_section_list(char **section_list);
1168
1169 /**
1170 * @brief Get list of attributes.
1171 *
1172 * Get list of attributes in a section as an array of strings.
1173 * Function allocates memory for the array of attributes.
1174 * Use \ref ini_free_attribute_list() to free allocated memory.
1175 *
1176 * @param[in] ini_config Configuration object.
1177 * @param[in] section Section name.
1178 * @param[out] size If not NULL parameter will
1179 * receive number of attributes
1180 * in the section.
1181 * @param[out] error If not NULL parameter will
1182 * receive the error code.
1183 * 0 - Success.
1184 * EINVAL - Invalid parameter.
1185 * ENOMEM - No memory.
1186 *
1187 * @return Array of strings.
1188 */
1189 char **ini_get_attribute_list(struct ini_cfgobj *ini_config,
1190 const char *section,
1191 int *size,
1192 int *error);
1193
1194 /**
1195 * @brief Free list of attributes.
1196 *
1197 * The attribute array created by \ref ini_get_attribute_list()
1198 * should be freed using this function.
1199 *
1200 * @param[in] attr_list Array of strings returned by
1201 * \ref ini_get_attribute_list() function.
1202 */
1203 void ini_free_attribute_list(char **attr_list);
1204
1205 /**
1206 * @}
1207 */
1208
1209 /**
1210 * @defgroup ini_value Value management
1211 *
1212 * This section contains value management functions. These functions
1213 * can be used to interpret values that are stored in the configuration
1214 * object in memory.
1215 *
1216 * @{
1217 *
1218 */
1219
1220
1221 /**
1222 * @brief Retrieve a value object form the configuration.
1223 *
1224 * Check return error code first. If the function returns
1225 * an error there is a serious problem.
1226 * Then check if object is found. Function will set
1227 * vo parameter to NULL if no attribute with
1228 * provided name is found in the collection.
1229 *
1230 * @param[in] section Section name.
1231 * If NULL assumed default.
1232 * @param[in] name Attribute name to find.
1233 * @param[in] ini_config Configuration object to search.
1234 * @param[in] mode See \ref searchmode "search mode"
1235 * section for more info.
1236 * @param[out] vo Value object.
1237 * Will be set to NULL if
1238 * element with the given name
1239 * is not found.
1240 * @return 0 - Success.
1241 * @return EINVAL - Invalid parameter.
1242 * @return ENOMEM - No memory.
1243 *
1244 */
1245
1246 int ini_get_config_valueobj(const char *section,
1247 const char *name,
1248 struct ini_cfgobj *ini_config,
1249 int mode,
1250 struct value_obj **vo);
1251
1252
1253
1254 /**
1255 * @brief Convert value to integer number.
1256 *
1257 * This is a conversion function.
1258 * It converts the value read from the INI file
1259 * and stored in the configuration element
1260 * into an int number. Any of the conversion
1261 * functions can be used to try to convert the value
1262 * stored as a string inside the value object.
1263 * The result can be different depending upon
1264 * how the caller tries to interpret the value.
1265 * If "strict" parameter is non zero the function will fail
1266 * if there are more characters after the last digit.
1267 * The value range is from INT_MIN to INT_MAX.
1268 *
1269 * @param[in] vo Value object to interpret.
1270 * It must be retrieved using
1271 * \ref ini_get_config_valueobj().
1272 * @param[in] strict Fail the function if
1273 * the symbol after last digit
1274 * is not valid.
1275 * @param[in] def Default value to use if
1276 * conversion failed.
1277 * @param[out] error Variable will get the value
1278 * of the error code if
1279 * error happened.
1280 * Can be NULL. In this case
1281 * function does not set
1282 * the code.
1283 * Codes:
1284 * - 0 - Success.
1285 * - EINVAL - Argument is invalid.
1286 * - EIO - Conversion failed due
1287 * invalid characters.
1288 * - ERANGE - Value is out of range.
1289 *
1290 * @return Converted value.
1291 * In case of failure the function returns default value and
1292 * sets error code into the provided variable.
1293 */
1294 int ini_get_int_config_value(struct value_obj *vo,
1295 int strict,
1296 int def,
1297 int *error);
1298
1299 /**
1300 * @brief Convert value object to a unsigned number.
1301 *
1302 * This is a conversion function.
1303 * It converts the value read from the INI file
1304 * and stored in the configuration element
1305 * into an unsigned number. Any of the conversion
1306 * functions can be used to try to convert the value
1307 * stored as a string inside the value object.
1308 * The result can be different depending upon
1309 * how the caller tries to interpret the value.
1310 * If "strict" parameter is non zero the function will fail
1311 * if there are more characters after the last digit.
1312 * The value range is from 0 to UINT_MAX.
1313 *
1314 * @param[in] vo Value object to interpret.
1315 * It must be retrieved using
1316 * \ref ini_get_config_valueobj().
1317 * @param[in] strict Fail the function if
1318 * the symbol after last digit
1319 * is not valid.
1320 * @param[in] def Default value to use if
1321 * conversion failed.
1322 * @param[out] error Variable will get the value
1323 * of the error code if
1324 * error happened.
1325 * Can be NULL. In this case
1326 * function does not set
1327 * the code.
1328 * Codes:
1329 * - 0 - Success.
1330 * - EINVAL - Argument is invalid.
1331 * - EIO - Conversion failed due
1332 * invalid characters.
1333 * - ERANGE - Value is out of range.
1334 *
1335 * @return Converted value.
1336 * In case of failure the function returns default value and
1337 * sets error code into the provided variable.
1338 */
1339
1340 unsigned ini_get_unsigned_config_value(struct value_obj *vo,
1341 int strict,
1342 unsigned def,
1343 int *error);
1344
1345 /**
1346 * @brief Convert value to long number.
1347 *
1348 * This is a conversion function.
1349 * It converts the value read from the INI file
1350 * and stored in the configuration element
1351 * into a long number. Any of the conversion
1352 * functions can be used to try to convert the value
1353 * stored as a string inside the value object.
1354 * The result can be different depending upon
1355 * how the caller tries to interpret the value.
1356 * If "strict" parameter is non zero the function will fail
1357 * if there are more characters after the last digit.
1358 * The value range is from LONG_MIN to LONG_MAX.
1359 *
1360 * @param[in] vo Value object to interpret.
1361 * It must be retrieved using
1362 * \ref ini_get_config_valueobj().
1363 * @param[in] strict Fail the function if
1364 * the symbol after last digit
1365 * is not valid.
1366 * @param[in] def Default value to use if
1367 * conversion failed.
1368 * @param[out] error Variable will get the value
1369 * of the error code if
1370 * error happened.
1371 * Can be NULL. In this case
1372 * function does not set
1373 * the code.
1374 * Codes:
1375 * - 0 - Success.
1376 * - EINVAL - Argument is invalid.
1377 * - EIO - Conversion failed due
1378 * invalid characters.
1379 * - ERANGE - Value is out of range.
1380 *
1381 * @return Converted value.
1382 * In case of failure the function returns default value and
1383 * sets error code into the provided variable.
1384 */
1385
1386 long ini_get_long_config_value(struct value_obj *vo,
1387 int strict,
1388 long def,
1389 int *error);
1390
1391 /**
1392 * @brief Convert value to unsigned long number.
1393 *
1394 * This is a conversion function.
1395 * It converts the value read from the INI file
1396 * and stored in the configuration element
1397 * into an unsigned long number. Any of the conversion
1398 * functions can be used to try to convert the value
1399 * stored as a string inside the value object.
1400 * The result can be different depending upon
1401 * how the caller tries to interpret the value.
1402 * If "strict" parameter is non zero the function will fail
1403 * if there are more characters after the last digit.
1404 * The value range is from 0 to ULONG_MAX.
1405 *
1406 * @param[in] vo Value object to interpret.
1407 * It must be retrieved using
1408 * \ref ini_get_config_valueobj().
1409 * @param[in] strict Fail the function if
1410 * the symbol after last digit
1411 * is not valid.
1412 * @param[in] def Default value to use if
1413 * conversion failed.
1414 * @param[out] error Variable will get the value
1415 * of the error code if
1416 * error happened.
1417 * Can be NULL. In this case
1418 * function does not set
1419 * the code.
1420 * Codes:
1421 * - 0 - Success.
1422 * - EINVAL - Argument is invalid.
1423 * - EIO - Conversion failed due
1424 * invalid characters.
1425 * - ERANGE - Value is out of range.
1426 *
1427 * @return Converted value.
1428 * In case of failure the function returns default value and
1429 * sets error code into the provided variable.
1430 */
1431
1432 unsigned long ini_get_ulong_config_value(struct value_obj *vo,
1433 int strict,
1434 unsigned long def,
1435 int *error);
1436
1437
1438 /**
1439 * @brief Convert value to int32_t number.
1440 *
1441 * This is a conversion function.
1442 * It converts the value read from the INI file
1443 * and stored in the configuration element
1444 * into an int32_t number. Any of the conversion
1445 * functions can be used to try to convert the value
1446 * stored as a string inside the value object.
1447 * The result can be different depending upon
1448 * how the caller tries to interpret the value.
1449 * If "strict" parameter is non zero the function will fail
1450 * if there are more characters after the last digit.
1451 * The value range is from INT_MIN to INT_MAX.
1452 *
1453 * @param[in] vo Value object to interpret.
1454 * It must be retrieved using
1455 * \ref ini_get_config_valueobj().
1456 * @param[in] strict Fail the function if
1457 * the symbol after last digit
1458 * is not valid.
1459 * @param[in] def Default value to use if
1460 * conversion failed.
1461 * @param[out] error Variable will get the value
1462 * of the error code if
1463 * error happened.
1464 * Can be NULL. In this case
1465 * function does not set
1466 * the code.
1467 * Codes:
1468 * - 0 - Success.
1469 * - EINVAL - Argument is invalid.
1470 * - EIO - Conversion failed due
1471 * invalid characters.
1472 * - ERANGE - Value is out of range.
1473 *
1474 * @return Converted value.
1475 * In case of failure the function returns default value and
1476 * sets error code into the provided variable.
1477 */
1478 int32_t ini_get_int32_config_value(struct value_obj *vo,
1479 int strict,
1480 int32_t def,
1481 int *error);
1482
1483 /**
1484 * @brief Convert value to uint32_t number.
1485 *
1486 * This is a conversion function.
1487 * It converts the value read from the INI file
1488 * and stored in the configuration value
1489 * into an uint32_t number. Any of the conversion
1490 * functions can be used to try to convert the value
1491 * stored as a string inside the value object.
1492 * The result can be different depending upon
1493 * how the caller tries to interpret the value.
1494 * If "strict" parameter is non zero the function will fail
1495 * if there are more characters after the last digit.
1496 * The value range is from 0 to ULONG_MAX.
1497 *
1498 * @param[in] vo Value object to interpret.
1499 * It must be retrieved using
1500 * \ref ini_get_config_valueobj().
1501 * @param[in] strict Fail the function if
1502 * the symbol after last digit
1503 * is not valid.
1504 * @param[in] def Default value to use if
1505 * conversion failed.
1506 * @param[out] error Variable will get the value
1507 * of the error code if
1508 * error happened.
1509 * Can be NULL. In this case
1510 * function does not set
1511 * the code.
1512 * Codes:
1513 * - 0 - Success.
1514 * - EINVAL - Argument is invalid.
1515 * - EIO - Conversion failed due
1516 * invalid characters.
1517 * - ERANGE - Value is out of range.
1518 *
1519 * @return Converted value.
1520 * In case of failure the function returns default value and
1521 * sets error code into the provided variable.
1522 */
1523 uint32_t ini_get_uint32_config_value(struct value_obj *vo,
1524 int strict,
1525 uint32_t def,
1526 int *error);
1527
1528 /**
1529 * @brief Convert value to integer number.
1530 *
1531 * This is a conversion function.
1532 * It converts the value read from the INI file
1533 * and stored in the configuration element
1534 * into an int64_t number. Any of the conversion
1535 * functions can be used to try to convert the value
1536 * stored as a string inside the value object.
1537 * The result can be different depending upon
1538 * how the caller tries to interpret the value.
1539 * If "strict" parameter is non zero the function will fail
1540 * if there are more characters after the last digit.
1541 * The value range is from LLONG_MIN to LLONG_MAX.
1542 *
1543 * @param[in] vo Value object to interpret.
1544 * It must be retrieved using
1545 * \ref ini_get_config_valueobj().
1546 * @param[in] strict Fail the function if
1547 * the symbol after last digit
1548 * is not valid.
1549 * @param[in] def Default value to use if
1550 * conversion failed.
1551 * @param[out] error Variable will get the value
1552 * of the error code if
1553 * error happened.
1554 * Can be NULL. In this case
1555 * function does not set
1556 * the code.
1557 * Codes:
1558 * - 0 - Success.
1559 * - EINVAL - Argument is invalid.
1560 * - EIO - Conversion failed due
1561 * invalid characters.
1562 * - ERANGE - Value is out of range.
1563 *
1564 * @return Converted value.
1565 * In case of failure the function returns default value and
1566 * sets error code into the provided variable.
1567 */
1568 int64_t ini_get_int64_config_value(struct value_obj *vo,
1569 int strict,
1570 int64_t def,
1571 int *error);
1572
1573 /**
1574 * @brief Convert value to integer number.
1575 *
1576 * This is a conversion function.
1577 * It converts the value read from the INI file
1578 * and stored in the configuration element
1579 * into an uint64_t number. Any of the conversion
1580 * functions can be used to try to convert the value
1581 * stored as a string inside the value object.
1582 * The result can be different depending upon
1583 * how the caller tries to interpret the value.
1584 * If "strict" parameter is non zero the function will fail
1585 * if there are more characters after the last digit.
1586 * The value range is from 0 to ULLONG_MAX.
1587 *
1588 * @param[in] vo Value object to interpret.
1589 * It must be retrieved using
1590 * \ref ini_get_config_valueobj().
1591 * @param[in] strict Fail the function if
1592 * the symbol after last digit
1593 * is not valid.
1594 * @param[in] def Default value to use if
1595 * conversion failed.
1596 * @param[out] error Variable will get the value
1597 * of the error code if
1598 * error happened.
1599 * Can be NULL. In this case
1600 * function does not set
1601 * the code.
1602 * Codes:
1603 * - 0 - Success.
1604 * - EINVAL - Argument is invalid.
1605 * - EIO - Conversion failed due
1606 * invalid characters.
1607 * - ERANGE - Value is out of range.
1608 *
1609 * @return Converted value.
1610 * In case of failure the function returns default value and
1611 * sets error code into the provided variable.
1612 */
1613 uint64_t ini_get_uint64_config_value(struct value_obj *vo,
1614 int strict,
1615 uint64_t def,
1616 int *error);
1617
1618 /**
1619 * @brief Convert value to floating point number.
1620 *
1621 * This is a conversion function.
1622 * It converts the value read from the INI file
1623 * and stored in the configuration element
1624 * into a floating point number. Any of the conversion
1625 * functions can be used to try to convert the value
1626 * stored as a string inside the value object.
1627 * The result can be different depending upon
1628 * how the caller tries to interpret the value.
1629 * If "strict" parameter is non zero the function will fail
1630 * if there are more characters after the last digit.
1631 *
1632 * @param[in] vo Value object to interpret.
1633 * It must be retrieved using
1634 * \ref ini_get_config_valueobj().
1635 * @param[in] strict Fail the function if
1636 * the symbol after last digit
1637 * is not valid.
1638 * @param[in] def Default value to use if
1639 * conversion failed.
1640 * @param[out] error Variable will get the value
1641 * of the error code if
1642 * error happened.
1643 * Can be NULL. In this case
1644 * function does not set
1645 * the code.
1646 * Codes:
1647 * - 0 - Success.
1648 * - EINVAL - Argument is invalid.
1649 * - EIO - Conversion failed due
1650 * invalid characters.
1651 *
1652 * @return Converted value.
1653 * In case of failure the function returns default value and
1654 * sets error code into the provided variable.
1655 */
1656 double ini_get_double_config_value(struct value_obj *vo,
1657 int strict,
1658 double def,
1659 int *error);
1660
1661 /**
1662 * @brief Convert value into a logical value.
1663 *
1664 * This is a conversion function.
1665 * It converts the value read from the INI file
1666 * and stored in the configuration element
1667 * into a Boolean. Any of the conversion
1668 * functions can be used to try to convert the value
1669 * stored as a string inside the value object.
1670 * The result can be different depending upon
1671 * how the caller tries to interpret the value.
1672 *
1673 * @param[in] vo Value object to interpret.
1674 * It must be retrieved using
1675 * \ref ini_get_config_valueobj().
1676 * @param[in] def Default value to use if
1677 * conversion failed.
1678 * @param[out] error Variable will get the value
1679 * of the error code if
1680 * error happened.
1681 * Can be NULL. In this case
1682 * function does not set
1683 * the code.
1684 * Codes:
1685 * - 0 - Success.
1686 * - EINVAL - Argument is invalid.
1687 * - EIO - Conversion failed due
1688 * invalid characters.
1689 *
1690 * @return Converted value.
1691 * In case of failure the function returns default value and
1692 * sets error code into the provided variable.
1693 */
1694 unsigned char ini_get_bool_config_value(struct value_obj *vo,
1695 unsigned char def,
1696 int *error);
1697
1698 /**
1699 * @brief Get the copy of string stored in the configuration value
1700 *
1701 * Function creates a copy of the string value stored in
1702 * the configuration element.
1703 * Returned value needs to be freed after use.
1704 * If error occurred the returned value will be NULL.
1705 *
1706 * @param[in] vo Value object to use.
1707 * It must be retrieved using
1708 * \ref ini_get_config_valueobj().
1709 * @param[out] error Variable will get the value
1710 * of the error code if
1711 * error happened.
1712 * Can be NULL. In this case
1713 * function does not set
1714 * the code.
1715 * Codes:
1716 * - 0 - Success.
1717 * - EINVAL - Argument is invalid.
1718 * - ENOMEM - No memory.
1719 *
1720 * @return Copy of the string or NULL.
1721 */
1722 char *ini_get_string_config_value(struct value_obj *vo,
1723 int *error);
1724 /**
1725 * @brief Get the string stored in the configuration value
1726 *
1727 * Function returns a reference to the string value
1728 * stored inside the configuration element. This string can't be altered.
1729 * The string will go out of scope if the value object is deleted.
1730 *
1731 * @param[in] vo Value object to use.
1732 * It must be retrieved using
1733 * \ref ini_get_config_valueobj().
1734 * @param[out] error Variable will get the value
1735 * of the error code if
1736 * error happened.
1737 * Can be NULL. In this case
1738 * function does not set
1739 * the code.
1740 * Codes:
1741 * - 0 - Success.
1742 * - EINVAL - Argument is invalid.
1743 *
1744 * @return String from the value object.
1745 */
1746 const char *ini_get_const_string_config_value(struct value_obj *vo,
1747 int *error);
1748
1749 /**
1750 * @brief Convert value into a binary sequence.
1751 *
1752 * This is a conversion function.
1753 * It converts the value read from the INI file
1754 * and stored in the configuration element
1755 * into a sequence of bytes.
1756 * Any of the conversion functions
1757 * can be used to try to convert the value
1758 * stored as a string inside the value object.
1759 * The result can be different depending upon
1760 * how the caller tries to interpret the value.
1761 *
1762 * The function allocates memory.
1763 * It is the responsibility of the caller to free it after use.
1764 * Use \ref ini_free_bin_config_value() for this purpose.
1765 * Functions will return NULL if conversion failed.
1766 *
1767 * Function assumes that the value being interpreted
1768 * has a special format.
1769 * The string should be taken in single quotes
1770 * and consist of hex encoded value represented by
1771 * two hex digits per byte.
1772 * Case does not matter.
1773 *
1774 * Example: '0a2BFeCc'
1775 *
1776 * @param[in] vo Value object to interpret.
1777 * It must be retrieved using
1778 * \ref ini_get_config_valueobj().
1779 * @param[out] length Variable that optionally receives
1780 * the length of the binary
1781 * sequence.
1782 * @param[out] error Variable will get the value
1783 * of the error code if
1784 * error happened.
1785 * Can be NULL. In this case
1786 * function does not set
1787 * the code.
1788 * Codes:
1789 * - 0 - Success.
1790 * - EINVAL - Argument is invalid.
1791 * - EIO - Conversion failed due
1792 * invalid characters.
1793 * - ENOMEM - No memory.
1794 *
1795 * @return Converted value.
1796 * In case of failure the function returns NULL.
1797 */
1798 char *ini_get_bin_config_value(struct value_obj *vo,
1799 int *length,
1800 int *error);
1801
1802 /**
1803 * @brief Free binary buffer
1804 *
1805 * Free binary value returned by \ref ini_get_bin_config_value().
1806 *
1807 * @param[in] bin Binary buffer to free.
1808 *
1809 */
1810 void ini_free_bin_config_value(char *bin);
1811
1812 /**
1813 * @brief Convert value to an array of strings.
1814 *
1815 * This is a conversion function.
1816 * It converts the value read from the INI file
1817 * and stored in the configuration value object
1818 * into an array of strings. Any of the conversion
1819 * functions can be used to try to convert the value
1820 * stored as a string inside the value object.
1821 * The result can be different depending upon
1822 * how the caller tries to interpret the value.
1823 *
1824 * Separator string includes up to three different separators.
1825 * If separator NULL, comma is assumed.
1826 * The spaces are trimmed automatically around separators
1827 * in the string.
1828 * The function drops empty tokens from the list.
1829 * This means that the string like this: "apple, ,banana, ,orange ,"
1830 * will be translated into the list of three items:
1831 * "apple","banana" and "orange".
1832 *
1833 * The length of the allocated array is returned in "size".
1834 * Size and error parameters can be NULL.
1835 * Use \ref ini_free_string_config_array() to free the array after use.
1836 *
1837 * The array is always NULL terminated so
1838 * it is safe not to get size and just loop until
1839 * array element is NULL.
1840 *
1841 * @param[in] vo Value object to interpret.
1842 * It must be retrieved using
1843 * \ref ini_get_config_valueobj().
1844 * @param[in] sep String cosisting of separator
1845 * symbols. For example: ",.;" would mean
1846 * that comma, dot and semicolon
1847 * should be treated as separators
1848 * in the value.
1849 * @param[out] size Variable that optionally receives
1850 * the size of the array.
1851 * @param[out] error Variable will get the value
1852 * of the error code if
1853 * error happened.
1854 * Can be NULL. In this case
1855 * function does not set
1856 * the code.
1857 * Codes:
1858 * - 0 - Success.
1859 * - EINVAL - Argument is invalid.
1860 * - EIO - Conversion failed.
1861 * - ENOMEM - No memory.
1862 *
1863 * @return Array of strings.
1864 * In case of failure the function returns NULL.
1865 */
1866 char **ini_get_string_config_array(struct value_obj *vo,
1867 const char *sep,
1868 int *size,
1869 int *error);
1870
1871 /**
1872 * @brief Convert value to an array of strings.
1873 *
1874 * This is a conversion function.
1875 * It converts the value read from the INI file
1876 * and stored in the configuration element
1877 * into an array of strings. Any of the conversion
1878 * functions can be used to try to convert the value
1879 * stored as a string inside the value object.
1880 * The result can be different depending upon
1881 * how the caller tries to interpret the value.
1882 *
1883 * Separator string includes up to three different separators.
1884 * If separator NULL, comma is assumed.
1885 * The spaces are trimmed automatically around separators
1886 * in the string.
1887 * The function does not drop empty tokens from the list.
1888 * This means that the string like this: "apple, ,banana, ,orange ,"
1889 * will be translated into the list of five items:
1890 * "apple", "", "banana", "" and "orange".
1891 *
1892 * The length of the allocated array is returned in "size".
1893 * Size and error parameters can be NULL.
1894 * Use \ref ini_free_string_config_array() to free the array after use.
1895 *
1896 * The array is always NULL terminated so
1897 * it is safe not to get size and just loop until
1898 * array element is NULL.
1899 *
1900 * @param[in] vo Value object to interpret.
1901 * It must be retrieved using
1902 * \ref ini_get_config_valueobj().
1903 * @param[in] sep String cosisting of separator
1904 * symbols. For example: ",.;" would mean
1905 * that comma, dot and semicolon
1906 * should be treated as separators
1907 * in the value.
1908 * @param[out] size Variable that optionally receives
1909 * the size of the array.
1910 * @param[out] error Variable will get the value
1911 * of the error code if
1912 * error happened.
1913 * Can be NULL. In this case
1914 * function does not set
1915 * the code.
1916 * Codes:
1917 * - 0 - Success.
1918 * - EINVAL - Argument is invalid.
1919 * - EIO - Conversion failed.
1920 * - ENOMEM - No memory.
1921 *
1922 * @return Array of strings.
1923 * In case of failure the function returns NULL.
1924 */
1925 char **ini_get_raw_string_config_array(struct value_obj *vo,
1926 const char *sep,
1927 int *size,
1928 int *error);
1929
1930 /**
1931 * @brief Convert value to an array of long values.
1932 *
1933 * This is a conversion function.
1934 * It converts the value read from the INI file
1935 * and stored in the configuration element
1936 * into an array of long values. Any of the conversion
1937 * functions can be used to try to convert the value
1938 * stored as a string inside the value object.
1939 * The result can be different depending upon
1940 * how the caller tries to interpret the value.
1941 *
1942 * Separators inside the string are detected automatically.
1943 * The spaces are trimmed automatically around separators
1944 * in the string.
1945 *
1946 * The length of the allocated array is returned in "size".
1947 * Size parameter can't be NULL.
1948 *
1949 * Use \ref ini_free_long_config_array() to free the array after use.
1950 *
1951 * @param[in] vo Value object to interpret.
1952 * It must be retrieved using
1953 * \ref ini_get_config_valueobj().
1954 * @param[out] size Variable that receives
1955 * the size of the array.
1956 * @param[out] error Variable will get the value
1957 * of the error code if
1958 * error happened.
1959 * Can be NULL. In this case
1960 * function does not set
1961 * the code.
1962 * Codes:
1963 * - 0 - Success.
1964 * - EINVAL - Argument is invalid.
1965 * - EIO - Conversion failed.
1966 * - ERANGE - Value is out of range.
1967 * - ENOMEM - No memory.
1968 *
1969 * @return Array of long values.
1970 * In case of failure the function returns NULL.
1971 */
1972 long *ini_get_long_config_array(struct value_obj *vo,
1973 int *size,
1974 int *error);
1975
1976 /**
1977 * @brief Convert value to an array of floating point values.
1978 *
1979 * This is a conversion function.
1980 * It converts the value read from the INI file
1981 * and stored in the configuration element
1982 * into an array of floating point values. Any of the conversion
1983 * functions can be used to try to convert the value
1984 * stored as a string inside the value object.
1985 * The result can be different depending upon
1986 * how the caller tries to interpret the value.
1987 *
1988 * Separators inside the string are detected automatically.
1989 * The spaces are trimmed automatically around separators
1990 * in the string.
1991 *
1992 * The length of the allocated array is returned in "size".
1993 * Size parameter can't be NULL.
1994 *
1995 * Use \ref ini_free_double_config_array() to free the array after use.
1996 *
1997 * @param[in] vo Value object to interpret.
1998 * It must be retrieved using
1999 * \ref ini_get_config_valueobj().
2000 * @param[out] size Variable that receives
2001 * the size of the array.
2002 * @param[out] error Variable will get the value
2003 * of the error code if
2004 * error happened.
2005 * Can be NULL. In this case
2006 * function does not set
2007 * the code.
2008 * Codes:
2009 * - 0 - Success.
2010 * - EINVAL - Argument is invalid.
2011 * - EIO - Conversion failed.
2012 * - ENOMEM - No memory.
2013 *
2014 * @return Array of floating point values.
2015 * In case of failure the function returns NULL.
2016 */
2017 double *ini_get_double_config_array(struct value_obj *vo,
2018 int *size,
2019 int *error);
2020
2021 /**
2022 * @brief Free array of string values.
2023 *
2024 * Use this function to free the array returned by
2025 * \ref ini_get_string_config_array() or by
2026 * \ref ini_get_raw_string_config_array().
2027 *
2028 * @param[in] str_config Array of string values.
2029 */
2030 void ini_free_string_config_array(char **str_config);
2031
2032 /**
2033 * @brief Free array of long values.
2034 *
2035 * Use this function to free the array returned by
2036 * \ref ini_get_long_config_array().
2037 *
2038 * @param[in] array Array of long values.
2039 */
2040 void ini_free_long_config_array(long *array);
2041 /**
2042 * @brief Free array of floating pointer values.
2043 *
2044 * Use this function to free the array returned by
2045 * \ref ini_get_double_config_array().
2046 *
2047 * @param[in] array Array of floating pointer values.
2048 */
2049 void ini_free_double_config_array(double *array);
2050
2051 /** @brief Structure that holds error messages
2052 * generated by validators.
2053 */
2054 struct ini_errobj;
2055
2056 /**
2057 * @brief Create structure to hold error messages.
2058 *
2059 * This function initiates structure that can be used to
2060 * hold error messages from generators. To add messages to
2061 * the structure use \ref ini_errobj_add_msg.
2062 *
2063 * @param[out] _errobj container for errors.
2064 *
2065 * @return Zero on success, nonzero value in case of error.
2066 */
2067 int ini_errobj_create(struct ini_errobj **_errobj);
2068
2069 /**
2070 * @brief Free structure that holds error messages.
2071 *
2072 * This function is used to free structure
2073 * previously created by \ref ini_errobj_create.
2074 *
2075 * @param[in] errobj container for errors.
2076 */
2077 void ini_errobj_destroy(struct ini_errobj **errobj);
2078
2079 /**
2080 * @brief Add new printf formated message to errobj.
2081 *
2082 * This function initiates structure that can be used to
2083 * hold error messages from generators. To add messages to
2084 * the structure use \ref ini_errobj_add_msg.
2085 *
2086 * @param[in] errobj container for errors previously
2087 * created by \ref ini_errobj_create.
2088 * @param[in] format printf format string
2089 *
2090 * @return Zero on success, nonzero value in case of error.
2091 */
2092 int ini_errobj_add_msg(struct ini_errobj *errobj,
2093 const char *format, ...)
2094 DING_ATTR_FORMAT(2, 3);
2095
2096 /**
2097 * @brief Reset iterator in errobj.
2098 *
2099 * After calling this function, the iterator in errobj
2100 * will point to the first error message. Use this if
2101 * you need to accesss the list multiple times in a loop.
2102 *
2103 * @param[in] errobj container for errors previously
2104 * created by \ref ini_errobj_create.
2105 */
2106 void ini_errobj_reset(struct ini_errobj *errobj);
2107
2108 /**
2109 * @brief Get pointer to current message in errobj.
2110 *
2111 * This function returns pointer to current message
2112 * pointed by the internal iterator. The returned string can
2113 * not be changed and will point to valid data only
2114 * until \ref ini_errobj_destroy is called.
2115 *
2116 * @param[in] errobj container for errors previously
2117 * created by \ref ini_errobj_create.
2118 * @return String inside the errobj structure. String
2119 * is valid until errobj is destroyed.
2120 */
2121 const char *ini_errobj_get_msg(struct ini_errobj *errobj);
2122
2123 /**
2124 * @brief Move to the next message in errobj.
2125 *
2126 * This function moves the internal iterator of errobj
2127 * to the next message in list.
2128 *
2129 * @param[in] errobj container for errors previously
2130 * created by \ref ini_errobj_create.
2131 */
2132 void ini_errobj_next(struct ini_errobj *errobj);
2133
2134 /**
2135 * @brief Check if errobj has more messages.
2136 *
2137 * This function returns true if errobj's internal iterator
2138 * reached end of list and no longer points to a message
2139 *
2140 * @param[in] errobj container for errors previously
2141 * created by \ref ini_errobj_create.
2142 * @return True if internal iterator reached end of list.
2143 */
2144 int ini_errobj_no_more_msgs(struct ini_errobj *errobj);
2145
2146 /**
2147 * @brief Return number of messages in errobj
2148 *
2149 * This function returns number of messages inside errobj
2150 *
2151 * @param[in] errobj container for errors previously
2152 * created by \ref ini_errobj_create.
2153 * @return Number of messages stored in errobj.
2154 */
2155 size_t ini_errobj_count(struct ini_errobj *errobj);
2156
2157 typedef int (ini_validator_func)(const char *rule_name,
2158 struct ini_cfgobj *rules_obj,
2159 struct ini_cfgobj *config_obj,
2160 struct ini_errobj *errobj,
2161 void **data);
2162
2163 typedef int (ini_schema_validator_func)(const char *rule_name,
2164 struct ini_cfgobj *rules_obj,
2165 struct ini_errobj *errobj,
2166 void **data);
2167
2168 /** @brief Structure used to define application specific
2169 * (external to libini) validator
2170 */
2171 struct ini_validator {
2172 const char *name;
2173 ini_validator_func *func;
2174 /* currently unused, for future expansion */
2175 ini_schema_validator_func *schema_validator;
2176 };
2177
2178 /**
2179 * @brief Read rules from INI file
2180 *
2181 * This function is used to read rules from INI file
2182 * and store them in config object. This special
2183 * config object is passed to \ref ini_rules_check
2184 * together with config object representing the
2185 * configuration that will be checked.
2186 *
2187 * @param[in] filename Name of file with rules
2188 * @param[out] _rules_obj config object representing the rules
2189 * @return Zero on success. Non zero value on error.
2190 */
2191 int ini_rules_read_from_file(const char *filename,
2192 struct ini_cfgobj **_rules_obj);
2193
2194 /**
2195 * @brief Check configuration file using rules
2196 *
2197 * This function is used to check if configuration
2198 * file applies to rules previously loaded by
2199 * \ref ini_rules_read_from_file. Any errors
2200 * detected in the configuration are stored in the
2201 * errobj structure. Error code returned by this
2202 * function indicates some internal error with
2203 * validators or memory allocation error (not
2204 * rule violation).
2205 *
2206 * @param[in] rules_obj config object representing the rules
2207 * @param[in] config_obj config object representing the
2208 * configuration
2209 * @param[in] extra_validators NULL terminated array of external
2210 * validators. Can be NULL if no external
2211 * validators are used.
2212 *
2213 * @param[in] errobj errobj to store generated errors
2214 * from validators.
2215 *
2216 * @return Zero on success. Non zero value on error.
2217 */
2218 int ini_rules_check(struct ini_cfgobj *rules_obj,
2219 struct ini_cfgobj *config_obj,
2220 struct ini_validator **extra_validators,
2221 struct ini_errobj *errobj);
2222
2223 /**
2224 * @brief Free the rules
2225 *
2226 * This function is just wrapper around ini_config_destroy
2227 */
2228 void ini_rules_destroy(struct ini_cfgobj *ini_config);
2229
2230 /**
2231 * @}
2232 */
2233
2234 #endif