]> pilppa.org Git - uci.git/blobdiff - ucimap-example.c
ucimap: add example for using the alloc callback
[uci.git] / ucimap-example.c
index d401624df9695abb853e1cb22093dd9513c66842..9fc71e2e104e0e1d6c1fc8023356aad5640ce7ce 100644 (file)
@@ -20,6 +20,7 @@
 struct list_head ifs;
 
 struct uci_network {
+       struct ucimap_section_data map;
        struct list_head list;
        struct list_head alias;
 
@@ -33,6 +34,7 @@ struct uci_network {
 };
 
 struct uci_alias {
+       struct ucimap_section_data map;
        struct list_head list;
 
        const char *name;
@@ -43,7 +45,7 @@ static int
 network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
 {
        struct uci_network *net = section;
-       unsigned char *target = data->s;
+       unsigned char *target = (unsigned char *) data->s;
        unsigned int tmp[4];
        int i;
 
@@ -99,6 +101,14 @@ network_add_alias(struct uci_map *map, void *section)
        return 0;
 }
 
+static struct ucimap_section_data *
+network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
+{
+       struct uci_network *p = malloc(sizeof(struct uci_network));
+       memset(p, 0, sizeof(struct uci_network));
+       return &p->map;
+}
+
 struct my_optmap {
        struct uci_optmap map;
        int test;
@@ -155,8 +165,9 @@ static struct my_optmap network_interface_options[] = {
 };
 
 static struct uci_sectionmap network_interface = {
+       UCIMAP_SECTION(struct uci_network, map),
        .type = "interface",
-       .alloc_len = sizeof(struct uci_network),
+       .alloc = network_allocate,
        .init = network_init_interface,
        .add = network_add_interface,
        .options = &network_interface_options[0].map,
@@ -173,9 +184,9 @@ static struct uci_optmap network_alias_options[] = {
 };
 
 static struct uci_sectionmap network_alias = {
+       UCIMAP_SECTION(struct uci_alias, map),
        .type = "alias",
        .options = network_alias_options,
-       .alloc_len = sizeof(struct uci_network),
        .init = network_init_alias,
        .add = network_add_alias,
        .n_options = ARRAY_SIZE(network_alias_options),