aboutsummaryrefslogtreecommitdiffstats
path: root/src/util-list.c
blob: 37bc48d82b7fedbf64d3057ef72b5e257d42d83c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 *  Matchbox Keyboard - A lightweight software keyboard.
 *
 *  Authored By Matthew Allum <mallum@o-hand.com>
 *
 *  Copyright (c) 2005-2012 Intel Corp
 *
 *  SPDX-License-Identifier: LGPL-2.1
 *
 */

#include "matchbox-keyboard.h"

List *
util_list_alloc_item(void)
{
  return util_malloc0(sizeof(List));
}

int
util_list_length(List *list)
{
  int result = 1;

  list = util_list_get_first(list);

  while ((list = util_list_next(list)) != NULL)
    result++;

  return result;
}

List*
util_list_get_last(List *list)
{
  if (list == NULL)
    return NULL;

  while (list->next)
    list = util_list_next(list);
  return list;
}

List*
util_list_get_first(List *list)
{
  if (list == NULL)
    return NULL;

  while (list->prev)
    list = util_list_previous(list);
  return list;
}

void*
util_list_get_nth_data(List *list, int n)
{
  if (list == NULL)
    return NULL;

  list = util_list_get_first(list);

  while (list->next && n)
    {
      list = util_list_next(list);
      n--;
    }

  if (n) return NULL;

  return (void *)list->data;
}



List*
util_list_append(List *list, void *data)
{
  if (list == NULL)
    {
      list = util_list_alloc_item();
      list->data = data;
    }
  else
    {
      List *last = util_list_get_last(list);

      last->next = util_list_alloc_item();
      last->next->prev = list;
      last->next->data = data;
    }

  return list;
}

void
util_list_foreach(List *list, ListForEachCB func, void *userdata)
{
  while (list)
    {
      func(list->data, userdata);
      list = util_list_next(list);
    }
}