[47fecbb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Radim Vansa
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @addtogroup libc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file cfg.h
|
---|
| 35 | * @brief Simple interface for configuration files manipulation.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #ifndef LIBC_CFG_H_
|
---|
| 39 | #define LIBC_CFG_H_
|
---|
| 40 |
|
---|
| 41 | #include <adt/list.h>
|
---|
| 42 | #include <sys/types.h>
|
---|
| 43 | #include <bool.h>
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * One key-value pair
|
---|
| 47 | */
|
---|
| 48 | typedef struct {
|
---|
| 49 | link_t link;
|
---|
| 50 |
|
---|
| 51 | const char *key;
|
---|
| 52 | const char *value;
|
---|
| 53 | } cfg_entry_t;
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * One section in the configuration file.
|
---|
| 57 | * Has own title (or is anonymous) and contains entries.
|
---|
| 58 | */
|
---|
| 59 | typedef struct {
|
---|
| 60 | link_t link;
|
---|
| 61 |
|
---|
| 62 | const char *title;
|
---|
| 63 | list_t entries;
|
---|
| 64 | size_t entry_count;
|
---|
| 65 | } cfg_section_t;
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * The configuration file data. The whole file is loaded
|
---|
| 69 | * into memory and strings in sections (titles) or entries
|
---|
| 70 | * (keys and values) are only pointers to this memory.
|
---|
| 71 | * Therefore all the text data are allocated and deallocated
|
---|
| 72 | * only once, if you want to use the strings after unloading
|
---|
| 73 | * the configuration file, you have to copy them.
|
---|
| 74 | */
|
---|
| 75 | typedef struct {
|
---|
| 76 | list_t sections;
|
---|
| 77 | size_t section_count;
|
---|
| 78 | const char *data;
|
---|
| 79 | } cfg_file_t;
|
---|
| 80 |
|
---|
| 81 | #define cfg_file_foreach(file, cur) \
|
---|
| 82 | list_foreach((file)->sections, (cur))
|
---|
| 83 |
|
---|
| 84 | #define cfg_section_instance(cur) \
|
---|
| 85 | list_get_instance((cur), const cfg_section_t, link)
|
---|
| 86 |
|
---|
| 87 | #define cfg_section_foreach(section, cur) \
|
---|
| 88 | list_foreach((section)->entries, (cur))
|
---|
| 89 |
|
---|
| 90 | #define cfg_entry_instance(cur) \
|
---|
| 91 | list_get_instance((cur), const cfg_entry_t, link)
|
---|
| 92 |
|
---|
| 93 | extern int cfg_load(const char *, cfg_file_t *);
|
---|
| 94 | extern int cfg_load_path(const char *, const char *, cfg_file_t *);
|
---|
| 95 | extern void cfg_unload(cfg_file_t *);
|
---|
| 96 | extern bool cfg_empty(const cfg_file_t *);
|
---|
| 97 | extern const cfg_section_t *cfg_find_section(const cfg_file_t *, const char *);
|
---|
| 98 | extern const char *cfg_find_value(const cfg_section_t *, const char *);
|
---|
| 99 | extern const cfg_section_t *cfg_anonymous(const cfg_file_t *);
|
---|
| 100 |
|
---|
| 101 | #endif
|
---|
| 102 |
|
---|
| 103 | /** @}
|
---|
| 104 | */
|
---|