-
Notifications
You must be signed in to change notification settings - Fork 5
/
toolbox-tree.c
389 lines (333 loc) · 8.77 KB
/
toolbox-tree.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// ~~~~~~~~~~~~~~~ C Toolbox ~~~~~~~~~~~~~~~~
// portable data manipulation functions
// portable socket server functions
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// toolbox-tree.c
//
// Copyright (c) 2015 Francois Oligny-Lemieux
// All rights reserved
//
// Created: 12.Apr.2015
//
// Description:
// To store filesystem folders/files
//
// License: Yipp Dual Personal Open Source License and Business Monetary License
// http://yipp.ca/licenses/dual-personal-open-source-business-monetary-license/
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "toolbox.h"
#if C_TOOLBOX_GENERIC_TREE == 1
# include "toolbox-tree.h"
# include "toolbox-errors.h"
# include "toolbox-basic-types.h"
# include <string.h>
# include <stdlib.h>
int genericTree_Constructor(genericTree_T * genericTree)
{
if ( genericTree == NULL )
{
return -1;
}
genericTree->should_always_be_0xAFAF7878 = 0xAFAF7878;
genericTree->current_iterator = NULL;
genericTree->accept_duplicate_names = 1;
genericTree->accept_duplicate_unique_ids = 0;
genericTree->allow_zero_unique_ids = 0;
genericTree->next_unique_id = 1;
genericTree->max_name_length = 65000; // 0 for unlimited
genericTree->itemAmount = 0;
memset(&genericTree->top, 0, sizeof(genericTree->top));
genericTree->top.tree = genericTree;
return 1;
}
// will insert as a child of level, alphabetically sorted between childs
int genericTree_Insert(genericTree_T * genericTree,
treeItem_T * level, // level to insert on
const char * name,
uint64_t unique_id,
void * client,
treeItem_T ** new_element)
{
treeItem_T * newItem;
treeItem_T * loopItem;
treeItem_T * prvLoopItem;
// treeItem_T * iterator = NULL;
unsigned int strLen;
int i = 0;
int inserted = 0;
if ( genericTree == NULL )
{
return -1;
}
if ( genericTree->should_always_be_0xAFAF7878 != 0xAFAF7878 )
{
return TOOLBOX_ERROR_NOT_INITIALIZED_OR_GOT_CORRUPTED;
}
if ( name == NULL )
{
return -2;
}
newItem = (treeItem_T*)malloc(sizeof(treeItem_T));
if ( newItem == NULL )
{
return TOOLBOX_ERROR_MALLOC;
}
memset(newItem, 0, sizeof(treeItem_T));
strLen = (unsigned int)strlen(name);
if ( genericTree->max_name_length > 0 && strLen >= genericTree->max_name_length )
{
free(newItem);
return TOOLBOX_ERROR_YOU_PASSED_A_STRING_TOO_BIG;
}
flexstrcpy(&newItem->name, name);
if ( unique_id == 0 )
{
if ( genericTree->allow_zero_unique_ids == 0 )
{
// generate one
unique_id = genericTree->next_unique_id;
genericTree->next_unique_id++;
}
else
{
// keep the zero
}
}
else if ( genericTree->next_unique_id <= unique_id )
{ genericTree->next_unique_id = unique_id + 1;
}
newItem->unique_id = unique_id;
newItem->client = client;
loopItem = level->childs;
if (loopItem == NULL)
{
level->childs = newItem;
newItem->parent = level;
goto end_of_func_success;
}
while ( loopItem )
{
i = flexstrcasecmp(&loopItem->name, name);
if ( i < 0 )
{ // candidate > iterator
// continue looping
/*
// insert after
if (loopItem->next)
{
newItem->next = loopItem->next;
loopItem->next->prev = newItem;
}
loopItem->next = newItem;
newItem->prev = loopItem;
newItem->parent = loopItem->parent;
inserted = 1;
break;*/
}
else if ( i > 0 )
{ // candidate < iterator
// insert before
if (loopItem->prev)
{
loopItem->prev->next = newItem;
newItem->prev = loopItem->prev;
}
else
{ // loopItem->prev == NULL
if (loopItem->parent == NULL)
{ return -20; // corruption
}
loopItem->parent->childs = newItem; // replace parent's first child
}
newItem->next = loopItem;
loopItem->prev = newItem;
newItem->parent = loopItem->parent;
inserted = 1;
break;
}
else
{
i = flexstrcmp(&loopItem->name, name);
if ( i < 0 )
{ // candidate > iterator
/*
// insert after
if (loopItem->next)
{
newItem->next = loopItem->next;
loopItem->next->prev = newItem;
}
loopItem->next = newItem;
newItem->prev = loopItem;
newItem->parent = loopItem->parent;
inserted = 1;
break;*/
}
else if ( i > 0 )
{ // candidate < iterator
// continue looping
if (loopItem->prev)
{
loopItem->prev->next = newItem;
newItem->prev = loopItem->prev;
}
else
{ // loopItem->prev == NULL
if (loopItem->parent == NULL)
{
free(newItem);
return -21; // corruption error
}
loopItem->parent->childs = newItem;
}
newItem->next = loopItem;
loopItem->prev = newItem;
newItem->parent = loopItem->parent;
inserted = 1;
break;
}
else
{
// already exists!!!
if ( genericTree->accept_duplicate_names )
{
// insert after
if (loopItem->next)
{
newItem->next = loopItem->next;
loopItem->next->prev = newItem;
}
loopItem->next = newItem;
newItem->prev = loopItem;
newItem->parent = loopItem->parent;
inserted = 1;
break;
}
else
{
free(newItem);
return TOOLBOX_ERROR_ALREADY_EXISTS;
}
}
}
prvLoopItem = loopItem;
loopItem = loopItem->next;
}
if ( inserted == 0 )
{
// append at end
loopItem = prvLoopItem;
if (loopItem->next)
{
return -22; // internal implementation error
}
if (loopItem->parent == NULL)
{
return -23; // corruption error
}
loopItem->next = newItem;
newItem->prev = loopItem;
newItem->parent = loopItem->parent;
inserted = 1;
}
end_of_func_success:
if ( new_element ) *new_element = newItem;
genericTree->itemAmount++;
newItem->tree = genericTree;
return 1;
}
// written 29.Jan.2008
int genericTree_Remove(genericTree_T * genericTree, treeItem_T * remItem)
{
int iret;
if ( genericTree == NULL )
{
return -1;
}
if ( remItem == NULL )
{
return -2;
}
iret = 1;
while ( remItem->childs != NULL && iret > 0 )
{
iret = genericTree_Delete(genericTree, remItem->childs);
}
if ( remItem->parent != NULL && remItem->parent->childs == remItem )
{
remItem->parent->childs = remItem->next;
}
if (remItem->prev)
{
remItem->prev->next = remItem->next;
}
if (remItem->next)
{
remItem->next->prev = remItem->prev;
}
if (remItem == genericTree->top.childs)
{
genericTree->top.childs = remItem->next;
}
free(remItem);
genericTree->itemAmount--;
return 1;
}
// deletes and clean clients
int genericTree_Delete(genericTree_T * genericTree, treeItem_T * remItem)
{
int iret;
if ( genericTree == NULL )
{
return -1;
}
if ( remItem == NULL )
{
return -2;
}
iret = 1;
while ( remItem->childs != NULL && iret > 0 )
{
iret = genericTree_Delete(genericTree, remItem->childs);
}
if ( remItem->parent != NULL && remItem->parent->childs == remItem )
{
remItem->parent->childs = remItem->next;
}
if (remItem->prev)
{
remItem->prev->next = remItem->next;
}
if (remItem->next)
{
remItem->next->prev = remItem->prev;
}
if (remItem == genericTree->top.childs)
{
genericTree->top.childs = remItem->next;
}
if (remItem->client) free(remItem->client);
remItem->client = NULL;
free(remItem);
genericTree->itemAmount--;
return 1;
}
int genericTree_Destructor(genericTree_T * genericTree)
{
treeItem_T * remItem;
if ( genericTree == NULL )
{
return -1;
}
remItem = genericTree->top.childs;
while (remItem != NULL)
{
genericTree_Delete(genericTree, remItem);
remItem = genericTree->top.childs;
}
genericTree->should_always_be_0xAFAF7878 = 0;
return 1;
}
#endif //C_TOOLBOX_BINARY_TREE