-
Notifications
You must be signed in to change notification settings - Fork 0
/
td_path_previous.c
63 lines (59 loc) · 1.29 KB
/
td_path_previous.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
/*
** Copyright 2015 K.J. Hermans (kees.jan.hermans@gmail.com)
** This code is part of simpledbm, an API to a dbm on a finite resource.
** License: BSD
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "td_private.h"
/**
* \ingroup btree_private
*
* Moves a path structure (used in normal searches and cursors)
* to the previous node.
*
* \param td Non-NULL pointer to initialized td_t structure.
* \param path Non-NULL pointer to initialized struct path.
*
* \returns Zero on success, or non-zero on error.
*/
int td_path_previous
(td_t* td, struct path* path)
{
while (path->size) {
unsigned previous;
switch (path->head->cmp) {
case 1:
path->head->cmp = 0;
return 0;
case 0:
if ((previous = path->head->keyhead.previous) != 0) {
if (path->size < TD_MAXPATH) {
path->head->cmp = -1;
++(path->size);
++(path->head);
path->head->ptr = previous;
CHECK(td_iterate_to_max(td, path));
return 0;
} else {
RETURNERR(TDERR_INVAL);
}
}
case -1:
break;
default:
RETURNERR(TDERR_STRUCT);
}
--(path->size);
if (path->size) {
--(path->head);
} else {
path->head = 0;
}
}
return TDERR_NOTFOUND;
}
#ifdef __cplusplus
}
#endif