Coverage Report

Created: 2026-01-03 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libplist/libcnary/node_list.c
Line
Count
Source
1
/*
2
 * node_list.c
3
 *
4
 *  Created on: Mar 8, 2011
5
 *      Author: posixninja
6
 *
7
 * Copyright (c) 2011 Joshua Hill. All Rights Reserved.
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
 */
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include "node.h"
29
#include "node_list.h"
30
31
void node_list_destroy(node_list_t list)
32
543k
{
33
543k
  free(list);
34
543k
}
35
36
node_list_t node_list_create()
37
29.3k
{
38
29.3k
  node_list_t list = (node_list_t)calloc(1, sizeof(struct node_list));
39
29.3k
  if (list == NULL) {
40
0
    return NULL;
41
0
  }
42
43
  // Initialize structure
44
29.3k
  list->begin = NULL;
45
29.3k
  list->end = NULL;
46
29.3k
  list->count = 0;
47
29.3k
  return list;
48
29.3k
}
49
50
int node_list_add(node_list_t list, node_t node)
51
409k
{
52
409k
  if (!list || !node) return -1;
53
54
  // Find the last element in the list
55
409k
  node_t last = list->end;
56
57
  // Setup our new node as the new last element
58
409k
  node->next = NULL;
59
409k
  node->prev = last;
60
61
  // Set the next element of our old "last" element
62
409k
  if (last) {
63
    // but only if the node list is not empty
64
380k
    last->next = node;
65
380k
  } else {
66
    // otherwise this is the start of the list
67
29.3k
    list->begin = node;
68
29.3k
  }
69
70
  // Set the lists prev to the new last element
71
409k
  list->end = node;
72
73
  // Increment our node count for this list
74
409k
  list->count++;
75
409k
  return 0;
76
409k
}
77
78
int node_list_insert(node_list_t list, unsigned int node_index, node_t node)
79
7.08k
{
80
7.08k
  if (!list || !node) return -1;
81
7.08k
  if (node_index >= list->count) {
82
4.61k
    return node_list_add(list, node);
83
4.61k
  }
84
85
  // Get the first element in the list
86
2.46k
  node_t cur = list->begin;
87
88
2.46k
  unsigned int pos = 0;
89
2.46k
  node_t prev = NULL;
90
91
2.46k
  if (node_index > 0) {
92
322k
    while (pos < node_index) {
93
319k
      prev = cur;
94
319k
      cur = cur->next;
95
319k
      pos++;
96
319k
    }
97
2.46k
  }
98
99
2.46k
  if (prev) {
100
    // Set previous node
101
2.46k
    node->prev = prev;
102
    // Set next node of our new node to next node of the previous node
103
2.46k
    node->next = prev->next;
104
    // Set next node of previous node to our new node
105
2.46k
    prev->next = node;
106
2.46k
  } else {
107
0
    node->prev = NULL;
108
    // get old first element in list
109
0
    node->next = list->begin;
110
    // set new node as first element in list
111
0
    list->begin = node;
112
0
  }
113
114
2.46k
  if (node->next == NULL) {
115
    // Set the lists prev to the new last element
116
0
    list->end = node;
117
2.46k
  } else {
118
    // set prev of the new next element to our node
119
2.46k
    node->next->prev = node;
120
2.46k
  }
121
122
  // Increment our node count for this list
123
2.46k
  list->count++;
124
2.46k
  return 0;
125
7.08k
}
126
127
int node_list_remove(node_list_t list, node_t node)
128
412k
{
129
412k
  if (!list || !node) return -1;
130
412k
  if (list->count == 0) return -1;
131
132
412k
  int node_index = 0;
133
412k
  node_t n;
134
746k
  for (n = list->begin; n; n = n->next) {
135
746k
    if (node == n) {
136
412k
      node_t newnode = node->next;
137
412k
      if (node->prev) {
138
7.08k
        node->prev->next = newnode;
139
7.08k
        if (newnode) {
140
2.46k
          newnode->prev = node->prev;
141
4.61k
        } else {
142
          // last element in the list
143
4.61k
          list->end = node->prev;
144
4.61k
        }
145
405k
      } else {
146
        // we just removed the first element
147
405k
        if (newnode) {
148
375k
          newnode->prev = NULL;
149
375k
        } else {
150
29.3k
          list->end = NULL;
151
29.3k
        }
152
405k
        list->begin = newnode;
153
405k
      }
154
412k
      list->count--;
155
412k
      return node_index;
156
412k
    }
157
334k
    node_index++;
158
334k
  }
159
0
  return -1;
160
412k
}