Gtest code for mtp-responder
[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"); //      LCOV_EXCL_LINE
45                 return FALSE;   //      LCOV_EXCL_LINE
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 /* LCOV_EXCL_START */
62 slist_node_t* _util_delete_node(slist_t *l_ptr, void *data)
63 {
64         retv_if(data == NULL, NULL);
65         retv_if(l_ptr == NULL, NULL);
66         retv_if(l_ptr->start == NULL, NULL);
67
68         slist_node_t *nptr = l_ptr->start;
69         slist_node_t *temp = NULL;
70
71         if (nptr->value == data)
72                 return __util_del_first_node(l_ptr);
73
74         while (nptr->link) {
75                 if (nptr->link->value == data)
76                         break;
77                 nptr = nptr->link;
78         }
79
80         if (nptr->link == NULL) {
81                 ERR("Node not found in the list");
82                 return NULL;
83         }
84
85         temp = nptr->link;
86         nptr->link = nptr->link->link;
87         l_ptr->nnodes -= 1;
88
89         if (temp == l_ptr->end)
90                 l_ptr->end = nptr;
91
92         return temp;
93 }
94
95 static slist_node_t *__util_del_first_node(slist_t *l_ptr)
96 {
97         slist_node_t *temp = NULL;
98
99         temp = l_ptr->start;
100         l_ptr->nnodes -= 1;
101         l_ptr->start = temp->link;
102         if (temp == l_ptr->end)
103                 l_ptr->end = NULL;
104
105         return temp;
106 }
107 /* LCOV_EXCL_STOP */
108
109 /* This API will send NULL if list does not have elements */
110 slist_iterator* _util_init_list_iterator(slist_t *l_ptr)
111 {
112         slist_iterator *temp = NULL;
113
114         retv_if(l_ptr == NULL, NULL);
115         retv_if(l_ptr->start == NULL, NULL);
116
117         temp = (slist_iterator *)g_malloc(sizeof(slist_iterator));
118         if (temp == NULL) {
119                 ERR("g_malloc() Fail"); //      LCOV_EXCL_LINE
120                 return NULL;    //      LCOV_EXCL_LINE
121         }
122
123         temp->node_ptr = l_ptr->start;
124         return temp;
125 }
126
127 /* Befor calling this please make sure
128    next element exists using _util_check_list_next */
129 void* _util_get_list_next(slist_iterator *iter)
130 {
131         slist_node_t *temp = NULL;
132
133         retv_if(iter == NULL, NULL);
134
135         temp = iter->node_ptr;
136         iter->node_ptr = iter->node_ptr->link;
137
138         return temp->value;
139 }
140
141 /* LCOV_EXCL_START */
142 slist_node_t* _util_get_first_node(slist_t *l_ptr)
143 {
144         retv_if(l_ptr == NULL, NULL);
145
146         return l_ptr->start;
147 }
148 /* LCOV_EXCL_STOP */
149
150 void _util_deinit_list_iterator(slist_iterator *iter)
151 {
152         ret_if(iter == NULL);
153
154         g_free(iter);
155 }