Code to handle the apt-alike configuration entries
[project/opkg-lede.git] / libopkg / cksum_list.c
1 /* cksum_lis.c - the opkg package management system
2
3 Copyright (C) 2010,2011 Javier Palacios
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or (at
8 your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14 */
15
16 #include "config.h"
17
18 #include <stdio.h>
19
20 #include "cksum_list.h"
21 #include "libbb/libbb.h"
22
23
24 int cksum_init(cksum_t *cksum, char **itemlist)
25 {
26 cksum->value = xstrdup(*itemlist++);
27 cksum->size = atoi(*itemlist++);
28 cksum->name = xstrdup(*itemlist++);
29
30 return 0;
31 }
32
33 void cksum_deinit(cksum_t *cksum)
34 {
35 free (cksum->name);
36 cksum->name = NULL;
37
38 free (cksum->value);
39 cksum->value = NULL;
40 }
41
42 void cksum_list_init(cksum_list_t *list)
43 {
44 void_list_init((void_list_t *) list);
45 }
46
47 void cksum_list_deinit(cksum_list_t *list)
48 {
49 cksum_list_elt_t *iter, *n;
50 cksum_t *cksum;
51
52 list_for_each_entry_safe(iter, n, &list->head, node) {
53 cksum = (cksum_t *)iter->data;
54 cksum_deinit(cksum);
55
56 /* malloced in cksum_list_append */
57 free(cksum);
58 iter->data = NULL;
59 }
60 void_list_deinit((void_list_t *) list);
61 }
62
63 cksum_t *cksum_list_append(cksum_list_t *list, char **itemlist)
64 {
65 /* freed in cksum_list_deinit */
66 cksum_t *cksum = xcalloc(1, sizeof(cksum_t));
67 cksum_init(cksum, itemlist);
68
69 void_list_append((void_list_t *) list, cksum);
70
71 return cksum;
72 }
73
74 const cksum_t *cksum_list_find(cksum_list_t *list, const char *name)
75 {
76 cksum_list_elt_t *iter;
77 cksum_t *cksum;
78
79 list_for_each_entry(iter, &list->head, node) {
80 cksum = (cksum_t *)iter->data;
81 if (strcmp(cksum->name, name) == 0) {
82 return cksum;
83 }
84 }
85 return NULL;
86 }
87