4bf2e15f14133411e4e53b6a0349984ae4b66e53
[platform/core/connectivity/mtp-responder.git] / src / util / mtp_list.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <glib.h>
18 #include "mtp_list.h"
19
20 /*
21  * FUNCTIONS
22  */
23 static slist_node_t *__util_del_first_node(slist_t *l_ptr);
24
25 void _util_init_list(slist_t *l_ptr)
26 {
27         ret_if(l_ptr == NULL);
28
29         l_ptr->start = NULL;
30         l_ptr->end = NULL;
31         l_ptr->nnodes = 0;
32
33         return;
34 }
35
36 mtp_bool _util_add_node(slist_t *l_ptr, void *data)
37 {
38         retv_if(l_ptr == NULL, FALSE);
39
40         slist_node_t *node = NULL;
41
42         node = (slist_node_t *)g_malloc(sizeof(slist_node_t));
43         if (node == NULL) {
44                 ERR("g_malloc() Fail");
45                 return FALSE;
46         }
47
48         node->value = data;
49         node->link = NULL;
50
51         if (l_ptr->start == NULL)
52                 l_ptr->start = node;
53         else
54                 l_ptr->end->link = node;
55
56         l_ptr->end = node;
57         l_ptr->nnodes += 1;
58         return TRUE;
59 }
60
61 slist_node_t* _util_delete_node(slist_t *l_ptr, void *data)
62 {
63         retv_if(data == NULL, NULL);
64         retv_if(l_ptr == NULL, NULL);
65         retv_if(l_ptr->start == NULL, NULL);
66
67         slist_node_t *nptr = l_ptr->start;
68         slist_node_t *temp = NULL;
69
70         if (nptr->value == data)
71                 return __util_del_first_node(l_ptr);
72
73         while (nptr->link) {
74                 if (nptr->link->value == data)
75                         break;
76                 nptr = nptr->link;
77         }
78
79         if (nptr->link == NULL) {
80                 ERR("Node not found in the list");
81                 return NULL;
82         }
83
84         temp = nptr->link;
85         nptr->link = nptr->link->link;
86         l_ptr->nnodes -= 1;
87
88         if (temp == l_ptr->end)
89                 l_ptr->end = nptr;
90
91         return temp;
92 }
93
94 static slist_node_t *__util_del_first_node(slist_t *l_ptr)
95 {
96         slist_node_t *temp = NULL;
97
98         temp = l_ptr->start;
99         l_ptr->nnodes -= 1;
100         l_ptr->start = temp->link;
101         if (temp == l_ptr->end)
102                 l_ptr->end = NULL;
103
104         return temp;
105 }
106
107 /* This API will send NULL if list does not have elements */
108 slist_iterator* _util_init_list_iterator(slist_t *l_ptr)
109 {
110         slist_iterator *temp = NULL;
111
112         retv_if(l_ptr == NULL, NULL);
113         retv_if(l_ptr->start == NULL, NULL);
114
115         temp = (slist_iterator *)g_malloc(sizeof(slist_iterator));
116         if (temp == NULL) {
117                 ERR("g_malloc() Fail");
118                 return NULL;
119         }
120
121         temp->node_ptr = l_ptr->start;
122         return temp;
123 }
124
125 /* Befor calling this please make sure
126    next element exists using _util_check_list_next */
127 void* _util_get_list_next(slist_iterator *iter)
128 {
129         slist_node_t *temp = NULL;
130
131         retv_if(iter == NULL, NULL);
132
133         temp = iter->node_ptr;
134         iter->node_ptr = iter->node_ptr->link;
135
136         return temp->value;
137 }
138
139 slist_node_t* _util_get_first_node(slist_t *l_ptr)
140 {
141         retv_if(l_ptr == NULL, NULL);
142
143         return l_ptr->start;
144 }
145
146 void _util_deinit_list_iterator(slist_iterator *iter)
147 {
148         ret_if(iter == NULL);
149
150         g_free(iter);
151 }