4e99e2237b8ca5abe026f426fb3cd2c59ef7acc4
[platform/framework/web/livebox-cpp.git] / src / dlist.cpp
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <assert.h>
20
21 #include "dlist.h"
22
23 struct dlist {
24         struct dlist *next;
25         struct dlist *prev;
26         void *data;
27 };
28
29 struct dlist *dlist_append(struct dlist *list, void *data)
30 {
31         struct dlist *item;
32
33         item = (struct dlist *)malloc(sizeof(*item));
34         if (!item)
35                 return NULL;
36
37         item->next = NULL;
38         item->data = data;
39
40         if (!list) {
41                 item->prev = item;
42
43                 list = item;
44         } else {
45                 item->prev = list->prev;
46                 item->prev->next = item;
47
48                 list->prev = item;
49         }
50
51         assert(!list->prev->next && "item NEXT");
52
53         return list;
54 }
55
56 struct dlist *dlist_prepend(struct dlist *list, void *data)
57 {
58         struct dlist *item;
59
60         item = (struct dlist *)malloc(sizeof(*item));
61         if (!item)
62                 return NULL;
63
64         if (!list) {
65                 item->prev = item;
66                 item->next = NULL;
67         } else {
68                 item->prev = list->prev;
69                 list->prev = item;
70                 item->next = list;
71         }
72
73         return item;
74 }
75
76 struct dlist *dlist_remove(struct dlist *list, struct dlist *l)
77 {
78         if (!list || !l)
79                 return NULL;
80
81         if (l == list) {
82                 l->prev = list->prev;
83                 list = l->next;
84         } else {
85                 l->prev->next = l->next;
86         }
87
88         if (l->next)
89                 l->next->prev = l->prev;
90
91         free(l);
92         return list;
93 }
94
95 struct dlist *dlist_find_data(struct dlist *list, void *data)
96 {
97         struct dlist *l;
98         void *_data;
99
100         dlist_foreach(list, l, _data) {
101                 if (data == _data)
102                         return l;
103         }
104
105         return NULL;
106 }
107
108 void *dlist_data(struct dlist *l)
109 {
110         return l ? l->data : NULL;
111 }
112
113 struct dlist *dlist_next(struct dlist *l)
114 {
115         return l ? l->next : NULL;
116 }
117
118 struct dlist *dlist_prev(struct dlist *l)
119 {
120         return l ? l->prev : NULL;
121 }
122
123 int dlist_count(struct dlist *l)
124 {
125         register int i;
126         struct dlist *n;
127         void *data;
128
129         i = 0;
130         dlist_foreach(l, n, data) {
131                 i++;
132         }
133
134         return i;
135 }
136
137 struct dlist *dlist_nth(struct dlist *l, int nth)
138 {
139         register int i;
140         struct dlist *n;
141         void *data;
142
143         i = 0;
144         dlist_foreach(l, n, data) {
145                 if (i == nth)
146                         return l;
147
148                 i++;
149         }
150
151         return NULL;
152 }
153
154 /* End of a file */