Initialize Tizen 2.3
[apps/livebox/livebox-cpp.git] / src / dlist.cpp
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.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
38         item->next = NULL;
39         item->data = data;
40
41         if (!list) {
42                 item->prev = item;
43
44                 list = item;
45         } else {
46                 item->prev = list->prev;
47                 item->prev->next = item;
48
49                 list->prev = item;
50         }
51
52         assert(!list->prev->next && "item NEXT");
53
54         return list;
55 }
56
57 struct dlist *dlist_prepend(struct dlist *list, void *data)
58 {
59         struct dlist *item;
60
61         item = (struct dlist *)malloc(sizeof(*item));
62         if (!item) {
63                 return NULL;
64         }
65
66         if (!list) {
67                 item->prev = item;
68                 item->next = NULL;
69         } else {
70                 item->prev = list->prev;
71                 list->prev = item;
72                 item->next = list;
73         }
74
75         return item;
76 }
77
78 struct dlist *dlist_remove(struct dlist *list, struct dlist *l)
79 {
80         if (!list || !l) {
81                 return NULL;
82         }
83
84         if (l == list) {
85                 l->prev = list->prev;
86                 list = l->next;
87         } else {
88                 l->prev->next = l->next;
89         }
90
91         if (l->next) {
92                 l->next->prev = l->prev;
93         }
94
95         free(l);
96         return list;
97 }
98
99 struct dlist *dlist_find_data(struct dlist *list, void *data)
100 {
101         struct dlist *l;
102         void *_data;
103
104         dlist_foreach(list, l, _data) {
105                 if (data == _data) {
106                         return l;
107                 }
108         }
109
110         return NULL;
111 }
112
113 void *dlist_data(struct dlist *l)
114 {
115         return l ? l->data : NULL;
116 }
117
118 struct dlist *dlist_next(struct dlist *l)
119 {
120         return l ? l->next : NULL;
121 }
122
123 struct dlist *dlist_prev(struct dlist *l)
124 {
125         return l ? l->prev : NULL;
126 }
127
128 int dlist_count(struct dlist *l)
129 {
130         register int i;
131         struct dlist *n;
132         void *data;
133
134         i = 0;
135         dlist_foreach(l, n, data) {
136                 i++;
137         }
138
139         return i;
140 }
141
142 struct dlist *dlist_nth(struct dlist *l, int nth)
143 {
144         register int i;
145         struct dlist *n;
146         void *data;
147
148         i = 0;
149         dlist_foreach(l, n, data) {
150                 if (i == nth) {
151                         return l;
152                 }
153
154                 i++;
155         }
156
157         return NULL;
158 }
159
160 /* End of a file */