Thu, 11 Jul 2013 14:21:23 +0200
fixed pathcheck behavior
71 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include "list.h" |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | UcxList *ret = NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | while (l != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | if (fnc != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | ret = ucx_list_append(ret, fnc(l->data, data)); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | } else { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | ret = ucx_list_append(ret, l->data); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | l = l->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | return ret; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | |
71 | 44 | int ucx_list_equals(const UcxList *l1, const UcxList *l2, |
45 | cmp_func fnc, void* data) { | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | if (l1 == l2) return 1; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
16
diff
changeset
|
47 | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | while (l1 != NULL && l2 != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | if (fnc == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | if (l1->data != l2->data) return 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | } else { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | if (fnc(l1->data, l2->data, data) != 0) return 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | l1 = l1->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | l2 = l2->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
57 | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | return (l1 == NULL && l2 == NULL); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | void ucx_list_free(UcxList *l) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | UcxList *e = l, *f; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | while (e != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | f = e; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | e = e->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | free(f); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | UcxList *ucx_list_append(UcxList *l, void *data) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | UcxList *nl = (UcxList*) malloc(sizeof(UcxList)); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | if (nl == NULL) return NULL; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
73 | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | nl->data = data; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | nl->next = NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | if (l == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | return nl; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | } else { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | UcxList *t = ucx_list_last(l); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | t->next = nl; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | return l; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | UcxList *ucx_list_prepend(UcxList *l, void *data) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | UcxList *nl = ucx_list_append(NULL, data); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | if (nl == NULL) return NULL; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
88 | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | if (l != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | nl->next = l; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | return nl; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | if (l1 == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | return l2; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | } else { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | UcxList *last = ucx_list_last(l1); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | last->next = l2; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | return l1; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | |
71 | 105 | UcxList *ucx_list_last(const UcxList *l) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | if (l == NULL) return NULL; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
107 | |
71 | 108 | const UcxList *e = l; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | while (e->next != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | e = e->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | } |
71 | 112 | return (UcxList*)e; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | |
71 | 115 | UcxList *ucx_list_get(const UcxList *l, int index) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | if (l == NULL) return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | |
71 | 118 | const UcxList *e = l; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | while (e->next != NULL && index > 0) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | e = e->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | index--; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
123 | |
71 | 124 | return (UcxList*)(index == 0 ? e : NULL); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | |
71 | 127 | int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { |
128 | UCX_FOREACH(UcxList*, l, e) { | |
129 | if (!fnc(elem, e->data, cmpdata)) { | |
130 | return 1; | |
131 | } | |
132 | } | |
133 | return 0; | |
134 | } | |
135 | ||
136 | size_t ucx_list_size(const UcxList *l) { | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | if (l == NULL) return 0; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
138 | |
71 | 139 | const UcxList *e = l; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | size_t s = 1; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | while (e->next != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | e = e->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | s++; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | return s; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
149 | UcxList *ucx_list_sort_merge(int length, |
71 | 150 | UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
151 | cmp_func fnc, void* data) { |
71 | 152 | |
153 | UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
154 | UcxList *rc, *lc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
155 | |
71 | 156 | lc = ls; rc = le; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
157 | int n = 0; |
71 | 158 | while (lc && lc != le && rc != re) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
159 | if (fnc(lc->data, rc->data, data) <= 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
160 | sorted[n] = lc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
161 | lc = lc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
162 | } else { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
163 | sorted[n] = rc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
164 | rc = rc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
165 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
166 | n++; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
167 | } |
71 | 168 | while (lc && lc != le) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
169 | sorted[n] = lc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
170 | lc = lc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
171 | n++; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
172 | } |
71 | 173 | while (rc && rc != re) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
174 | sorted[n] = rc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
175 | rc = rc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
176 | n++; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
177 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
178 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
179 | // Update pointer |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
180 | for (int i = 0 ; i < length-1 ; i++) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
181 | sorted[i]->next = sorted[i+1]; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
182 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
183 | sorted[length-1]->next = NULL; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
184 | |
71 | 185 | UcxList *ret = sorted[0]; |
186 | free(sorted); | |
187 | return ret; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
188 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
189 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
190 | UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
191 | if (l == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
192 | return NULL; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
193 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
194 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
195 | UcxList *lc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
196 | int ln = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
197 | |
71 | 198 | UcxList *restrict ls = l, *restrict le, *restrict re; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
199 | lc = ls; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
200 | while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
201 | lc = lc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
202 | ln++; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
203 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
204 | le = lc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
205 | |
71 | 206 | if (le == NULL) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
207 | return l; // this list is already sorted :) |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
208 | } else { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
209 | UcxList *rc; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
210 | int rn = 1; |
71 | 211 | rc = le; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
212 | while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
213 | rc = rc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
214 | rn++; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
215 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
216 | re = rc->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
217 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
218 | // Something left? Sort it! |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
219 | UcxList *remainder = re; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
220 | size_t remainder_length = ucx_list_size(remainder); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
221 | if (remainder != NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
222 | remainder = ucx_list_sort(remainder, fnc, data); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
223 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
224 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
225 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
226 | UcxList *sorted = ucx_list_sort_merge(ln+rn, |
71 | 227 | ls, le, re, |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
228 | fnc, data); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
229 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
230 | // merge sorted list with (also sorted) remainder |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
231 | l = ucx_list_sort_merge(ln+rn+remainder_length, |
71 | 232 | sorted, remainder, NULL, fnc, data); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
233 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
234 | return l; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | } |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
237 | |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
238 | /* list specific functions */ |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
239 | UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
240 | if (e == l) { |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
241 | l = e->next; |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
242 | free(e); |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
243 | } else { |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
244 | UcxList *f = l; |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
245 | while (f->next != NULL && f->next != e) { |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
246 | f = f->next; |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
247 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
248 | /* perform remove if this element is found in this list */ |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
249 | if (f->next == e) { |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
250 | f->next = e->next; |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
251 | free(e); |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
252 | } |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
253 | } |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
254 | return l; |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
255 | } |