add reorder to lua api
[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 OPTMAP_OPTION(_maptype, _type, _field, ...) \
36         { \
37                 .type = _maptype, \
38                 .name = #_field, \
39                 .offset = offsetof(_type, _field), \
40                 __VA_ARGS__  \
41         }
42
43 struct uci_sectmap;
44 struct uci_optmap;
45
46 struct uci_map {
47         struct uci_sectmap **sections;
48         unsigned int n_sections;
49         struct list_head sdata;
50         struct list_head fixup;
51
52         void *priv; /* user data */
53 };
54
55 enum ucimap_type {
56         /* types */
57         UCIMAP_SIMPLE   = 0x00,
58         UCIMAP_LIST     = 0x10,
59         UCIMAP_TYPE     = 0xf0, /* type mask */
60
61         /* subtypes */
62         UCIMAP_STRING   = 0x0,
63         UCIMAP_BOOL     = 0x1,
64         UCIMAP_INT      = 0x2,
65         UCIMAP_SECTION  = 0x3,
66         UCIMAP_SUBTYPE  = 0xf, /* subtype mask */
67 };
68
69 union uci_datamap {
70         int i;
71         bool b;
72         const char *s;
73         void *section;
74         struct list_head list;
75 };
76
77 struct uci_listmap {
78         struct list_head list;
79         union uci_datamap data;
80 };
81
82 struct uci_sectmap {
83         /* type string for the uci section */
84         const char *type;
85
86         /* length of the struct to map into */
87         unsigned int alloc_len;
88
89         /* give the caller time to initialize the preallocated struct */
90         int (*init_section)(struct uci_map *map, void *section, struct uci_section *s);
91
92         /* pass the fully processed struct to the callback after the section end */
93         int (*add_section)(struct uci_map *map, void *section);
94
95         /* let the callback clean up its own stuff in the section */
96         int (*free_section)(struct uci_map *map, void *section);
97
98         /* list of option mappings for this section */
99         struct uci_optmap *options;
100         unsigned int n_options;
101 };
102
103 struct uci_optmap {
104         unsigned int offset;
105         const char *name;
106         enum ucimap_type type;
107         union {
108                 struct {
109                         int base;
110                         int min;
111                         int max;
112                 } i;
113                 struct {
114                         int maxlen;
115                 } s;
116                 struct uci_sectmap *sm;
117         } data;
118 };
119
120 extern int ucimap_init(struct uci_map *map);
121 extern void ucimap_cleanup(struct uci_map *map);
122 extern void ucimap_set_changed(void *section, void *field);
123 extern int ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section);
124 extern void ucimap_parse(struct uci_map *map, struct uci_package *pkg);
125