ucimap: move the section data to a dedicated field inside the managed structs
[project/uci.git] / ucimap.h
1 /*
2 * ucimap - library for mapping uci sections into data structures
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include <stdbool.h>
15 #include "uci_list.h"
16 #include "uci.h"
17
18 #ifndef ARRAY_SIZE
19 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
20 #endif
21
22 #define BITFIELD_SIZE(_fields) (((_fields) / 8) + 1)
23
24 #define CLR_BIT(_name, _bit) do { \
25 _name[(_bit) / 8] &= ~(1 << ((_bit) % 8)); \
26 } while (0)
27
28 #define SET_BIT(_name, _bit) do { \
29 _name[(_bit) / 8] |= (1 << ((_bit) % 8)); \
30 } while (0)
31
32 #define TEST_BIT(_name, _bit) \
33 (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
34
35 #define UCIMAP_OPTION(_type, _field) \
36 .type = UCIMAP_CUSTOM, \
37 .name = #_field, \
38 .offset = offsetof(_type, _field)
39
40
41 #define UCIMAP_SECTION(_name, _field) \
42 .alloc_len = sizeof(_name), \
43 .smap_offset = offsetof(_name, _field)
44
45 struct uci_sectionmap;
46 struct uci_optmap;
47 struct ucimap_list;
48
49 struct uci_map {
50 struct uci_sectionmap **sections;
51 unsigned int n_sections;
52 struct list_head sdata;
53 struct list_head fixup;
54
55 void *priv; /* user data */
56 };
57
58 enum ucimap_type {
59 /* types */
60 UCIMAP_SIMPLE = 0x00,
61 UCIMAP_LIST = 0x10,
62 UCIMAP_TYPE = 0xf0, /* type mask */
63
64 /* subtypes */
65 UCIMAP_STRING = 0x0,
66 UCIMAP_BOOL = 0x1,
67 UCIMAP_INT = 0x2,
68 UCIMAP_SECTION = 0x3,
69 UCIMAP_CUSTOM = 0x4,
70 UCIMAP_SUBTYPE = 0xf, /* subtype mask */
71 };
72
73 union ucimap_data {
74 int i;
75 bool b;
76 char *s;
77 void *section;
78 struct ucimap_list *list;
79 };
80
81 struct ucimap_section_data {
82 struct list_head list;
83 struct uci_map *map;
84 struct uci_sectionmap *sm;
85 const char *section_name;
86
87 /* list of allocations done by ucimap */
88 struct uci_alloc *allocmap;
89 unsigned long allocmap_len;
90
91 /* map for changed fields */
92 unsigned char *cmap;
93 bool done;
94 };
95
96
97 struct uci_listmap {
98 struct list_head list;
99 union ucimap_data data;
100 };
101
102 struct uci_sectionmap {
103 /* type string for the uci section */
104 const char *type;
105
106 /* length of the struct to map into */
107 unsigned int alloc_len;
108
109 /* sectionmap offset */
110 unsigned int smap_offset;
111
112 /* give the caller time to initialize the preallocated struct */
113 int (*init)(struct uci_map *map, void *section, struct uci_section *s);
114
115 /* pass the fully processed struct to the callback after the section end */
116 int (*add)(struct uci_map *map, void *section);
117
118 /* let the callback clean up its own stuff in the section */
119 int (*free)(struct uci_map *map, void *section);
120
121 /* list of option mappings for this section */
122 struct uci_optmap *options;
123 unsigned int n_options;
124 unsigned int options_size;
125 };
126
127 struct uci_optmap {
128 unsigned int offset;
129 const char *name;
130 enum ucimap_type type;
131 int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
132 union {
133 struct {
134 int base;
135 int min;
136 int max;
137 } i;
138 struct {
139 int maxlen;
140 } s;
141 struct uci_sectionmap *sm;
142 } data;
143 };
144
145 struct ucimap_list {
146 int n_items;
147 union ucimap_data item[];
148 };
149
150 extern int ucimap_init(struct uci_map *map);
151 extern void ucimap_cleanup(struct uci_map *map);
152 extern void ucimap_set_changed(struct ucimap_section_data *sd, void *field);
153 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section);
154 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
155