Filter results by

Older Versions

Older API versions are available as a download. To view, extract the file and open the index.html file in a web browser.

artik_list.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  */
18 
19 #ifndef __ARTIK_LIST_H__
20 #define __ARTIK_LIST_H__
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <stdlib.h>
27 #include <string.h>
28 #include "artik_error.h"
29 
44  typedef void *ARTIK_LIST_HANDLE;
51  typedef void (*ARTIK_LIST_FUNCA) (void *);
57  typedef void *(*ARTIK_LIST_FUNCB) (void *, void *);
58 
65  typedef struct artik_list {
66  struct artik_list *next;
67  ARTIK_LIST_FUNCA clear;
68  ARTIK_LIST_HANDLE handle;
69  unsigned int size_data;
70  void *data;
71  } artik_list;
72 
78  #define ARTIK_LIST_INVALID_HANDLE NULL
79 
92  static inline artik_list *artik_list_add(artik_list **list,
93  ARTIK_LIST_HANDLE handle, int size_of_node)
94  {
95  artik_list *elem = *list;
96 
97  while ((elem != NULL) && (elem->next != NULL))
98  elem = elem->next;
99 
100  if (elem) {
101  elem->next = (artik_list *) malloc(size_of_node);
102  if (!elem->next)
103  return NULL;
104  memset(elem->next, 0, size_of_node);
105  elem = elem->next;
106  } else {
107  elem = (artik_list *) malloc(size_of_node);
108  if (!elem)
109  return NULL;
110  memset(elem, 0, size_of_node);
111  *list = elem;
112  }
113 
114  if (handle != 0)
115  elem->handle = handle;
116  else
117  elem->handle = (ARTIK_LIST_HANDLE)elem;
118 
119  elem->size_data = size_of_node - sizeof(*elem);
120 
121  return elem;
122  }
123 
133  static inline artik_error artik_list_cpy(artik_list *list,
134  artik_list **dest)
135  {
136  artik_list *elem = list;
137  artik_list **res = dest;
138 
139  if (!list)
140  return E_BAD_ARGS;
141 
142  while (elem) {
143  if (*res == NULL)
144  *res = (artik_list *)malloc(sizeof(artik_list));
145 
146  memcpy(*res, elem, sizeof(artik_list));
147  res = &(*res)->next;
148  elem = elem->next;
149  }
150 
151  return S_OK;
152  }
153 
161  static inline unsigned int artik_list_size(artik_list *list)
162  {
163  artik_list *elem = list;
164  unsigned int res = 0;
165 
166  while (elem) {
167  ++res;
168  elem = elem->next;
169  }
170  return res;
171  }
172 
181  static inline artik_error artik_list_delete_node(artik_list **list,
182  artik_list *node)
183  {
184  artik_list *elem = NULL;
185  artik_list *prev = NULL;
186 
187  if (!list || !*list || !node)
188  return E_BAD_ARGS;
189 
190  elem = *list;
191 
192  while (elem && (elem != node)) {
193  prev = elem;
194  elem = elem->next;
195  }
196 
197  if (!elem)
198  return E_BAD_ARGS;
199 
200  if (prev != NULL)
201  prev->next = elem->next;
202  if (elem == *list)
203  *list = elem->next;
204  if (elem->clear)
205  (*elem->clear) (elem);
206  if (elem->data)
207  free(elem->data);
208  free(elem);
209 
210  return S_OK;
211  }
212 
221  static inline artik_error artik_list_delete_handle(artik_list **list,
222  ARTIK_LIST_HANDLE
223  handle)
224  {
225  artik_list *elem = NULL;
226  artik_list *prev = NULL;
227 
228  if (!list || !*list || (handle == ARTIK_LIST_INVALID_HANDLE))
229  return E_BAD_ARGS;
230 
231  elem = *list;
232 
233  while (elem && (elem->handle != handle)) {
234  prev = elem;
235  elem = elem->next;
236  }
237 
238  if (!elem)
239  return E_BAD_ARGS;
240  if (prev != NULL)
241  prev->next = elem->next;
242  if (elem == *list)
243  *list = elem->next;
244  if (elem->clear)
245  (*elem->clear) (elem);
246  if (elem->data)
247  free(elem->data);
248  free(elem);
249 
250  return S_OK;
251  }
252 
261  static inline artik_error artik_list_delete_pos(artik_list **list,
262  unsigned int pos)
263  {
264  artik_list *elem = NULL;
265  artik_list *prev = NULL;
266 
267  if (!list || !*list || (pos >= artik_list_size(*list)))
268  return E_BAD_ARGS;
269 
270  elem = *list;
271 
272  while (elem && pos > 0) {
273  prev = elem;
274  elem = elem->next;
275  pos--;
276  }
277 
278  if (!elem)
279  return E_BAD_ARGS;
280  if (prev != NULL)
281  prev->next = elem->next;
282  if (elem == *list)
283  *list = elem->next;
284  if (elem->clear)
285  (*elem->clear) (elem);
286  if (elem->data)
287  free(elem->data);
288  free(elem);
289 
290  return S_OK;
291  }
304  static inline artik_error artik_list_delete_check(artik_list **list,
306  check_func,
307  void *param_of_check)
308  {
309  artik_list *elem = NULL;
310  artik_list *prev = NULL;
311 
312  if (!list || !check_func)
313  return E_BAD_ARGS;
314 
315  elem = *list;
316 
317  while (elem && ((*check_func) (elem, param_of_check) == 0)) {
318  prev = elem;
319  elem = elem->next;
320  }
321 
322  if (!elem)
323  return E_BAD_ARGS;
324  if (prev != NULL)
325  prev->next = elem->next;
326  if (elem == *list)
327  *list = elem->next;
328  if (elem->clear)
329  (*elem->clear) (elem);
330  if (elem->data)
331  free(elem->data);
332  free(elem);
333 
334  return S_OK;
335  }
336 
344  static inline artik_error artik_list_delete_all(artik_list **list)
345  {
346  artik_list *elem = NULL;
347 
348  if (!list || !*list)
349  return E_BAD_ARGS;
350 
351  while (*list) {
352  elem = *list;
353  *list = (*list)->next;
354  if (elem->clear)
355  (*elem->clear) (elem);
356  if (elem->data)
357  free(elem->data);
358  free(elem);
359  }
360 
361  *list = NULL;
362 
363  return S_OK;
364  }
365 
375  static inline artik_list *artik_list_get_by_handle(artik_list *list,
376  ARTIK_LIST_HANDLE
377  handle)
378  {
379  artik_list *elem = list;
380 
381  if (!elem || (handle == ARTIK_LIST_INVALID_HANDLE))
382  return NULL;
383 
384  while (elem && (elem->handle != handle))
385  elem = elem->next;
386 
387  return elem;
388  }
389 
399  static inline artik_list *artik_list_get_by_pos(artik_list *list,
400  unsigned int pos)
401  {
402  artik_list *elem = list;
403  unsigned int i = 0;
404 
405  if (!list || (pos >= artik_list_size(list)))
406  return NULL;
407 
408  while (elem && i++ < pos)
409  elem = elem->next;
410 
411  return elem;
412  }
413 
426  static inline artik_list *artik_list_get_by_check(artik_list *list,
428  check_func,
429  void *param_of_check)
430  {
431  artik_list *elem = list;
432 
433  if (!list || !check_func)
434  return NULL;
435 
436  while (elem && ((*check_func) (elem, param_of_check) == 0))
437  elem = elem->next;
438 
439  return elem;
440  }
441 
450  static inline artik_list *artik_list_end(artik_list *list)
451  {
452  artik_list *elem = list;
453 
454  while (elem && elem->next)
455  elem = elem->next;
456 
457  return elem;
458  }
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #endif /*__ARTIK_LIST_H__ */
void * ARTIK_LIST_HANDLE
ARTIK_LIST_ID type.
Definition: artik_list.h:44
void(* ARTIK_LIST_FUNCA)(void *)
ARTIK_LIST_FUNCA type.
Definition: artik_list.h:51
#define S_OK
No error occurred while processing the function.
Definition: artik_error.h:40
int artik_error
Error type.
Definition: artik_error.h:35
#define E_BAD_ARGS
Wrong arguments passed to the function.
Definition: artik_error.h:45
struct artik_list artik_list
Generic linked list structure.
void *(* ARTIK_LIST_FUNCB)(void *, void *)
ARTIK_LIST_FUNCB type.
Definition: artik_list.h:57
#define ARTIK_LIST_INVALID_HANDLE
ARTIK_LIST_INVALID_HANDLE.
Definition: artik_list.h:78
Error codes.
Generic linked list structure.
Definition: artik_list.h:65
Last updated on: