opkg: implement active_list_prev and test cases.
[oweals/opkg-lede.git] / libopkg / active_list.c
1 /* active_list.h - the opkg package management system
2
3    Tick Chen <tick@openmoko.com>
4
5    Copyright (C) 2008 Openmoko Inc. 
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18
19 #include "active_list.h"
20 #include <stdio.h>
21
22
23 void active_list_init(struct active_list *ptr) {
24     INIT_LIST_HEAD(&ptr->node);
25     INIT_LIST_HEAD(&ptr->depend);
26     ptr->depended = NULL;
27 }
28
29 /**
30  */ 
31 struct active_list * active_list_next(struct active_list *head, struct active_list *ptr) {
32     struct active_list *next=NULL;
33     if ( !head ) {
34         fprintf(stderr, "active_list_next head = %p, ptr = %p invalid value!!\n", head, ptr);
35         return NULL;
36     }
37     if ( !ptr )
38         ptr = head;
39     next = list_entry(ptr->node.next, struct active_list, node);
40     if ( next == head ) {
41         return NULL;
42     }
43     if ( ptr->depended && &ptr->depended->depend == ptr->node.next ) {
44         return ptr->depended;
45     }
46     while ( next->depend.next != &next->depend ) {
47         next = list_entry(next->depend.next, struct active_list, node); 
48     }
49     return next;
50 }
51
52
53 struct active_list * active_list_prev(struct active_list *head, struct active_list *ptr) {
54     struct active_list *prev=NULL;
55     if ( !head ) {
56         fprintf(stderr, "active_list_prev head = %p, ptr = %p invalid value!!\n", head, ptr);
57         return NULL;
58     }
59     if ( !ptr )
60         ptr = head;
61     if ( ptr->depend.prev != &ptr->depend ) {
62         prev = list_entry(ptr->depend.prev, struct active_list, node);
63         return prev;
64     } 
65     if ( ptr->depended  && ptr->depended != head && &ptr->depended->depend == ptr->node.prev ) {
66         prev = list_entry(ptr->depended->node.prev, struct active_list, node);
67     } else 
68         prev = list_entry(ptr->node.prev, struct active_list, node);
69     if ( prev == head )
70         return NULL;
71     return prev;
72 }
73
74 void active_list_clear(struct active_list *head) {
75 }
76
77 void active_list_add_depend(struct active_list *node, struct active_list *depend) {
78     list_del_init(&depend->node);
79     list_add_tail(&depend->node, &node->depend);
80     depend->depended  = node;
81 }
82
83 void active_list_add(struct active_list *head, struct active_list *node) {
84     list_del_init(&node->node);
85     list_add_tail(&node->node, &head->node);
86     node->depended  = head;
87 }