2 * Copyright Timo Hirvonen
12 struct searchable_ops ops
;
15 static void search_lock(void)
20 static void search_unlock(void)
25 /* returns next matching track (can be current!) or NULL if not found */
26 static int do_search(struct searchable
*s
, struct iter
*iter
, const char *text
, int direction
)
29 if (s
->ops
.matches(s
->data
, iter
, text
))
31 if (direction
== SEARCH_FORWARD
) {
32 if (!s
->ops
.get_next(iter
))
35 if (!s
->ops
.get_prev(iter
))
41 struct searchable
*searchable_new(void *data
, const struct iter
*head
, const struct searchable_ops
*ops
)
45 s
= xnew(struct searchable
, 1);
52 void searchable_free(struct searchable
*s
)
57 int search(struct searchable
*s
, const char *text
, enum search_direction dir
, int beginning
)
64 /* first or last item */
66 if (dir
== SEARCH_FORWARD
){
67 ret
= s
->ops
.get_next(&iter
);
69 ret
= s
->ops
.get_prev(&iter
);
73 ret
= s
->ops
.get_current(s
->data
, &iter
);
76 ret
= do_search(s
, &iter
, text
, dir
);
81 int search_next(struct searchable
*s
, const char *text
, enum search_direction dir
)
87 if (!s
->ops
.get_current(s
->data
, &iter
)) {
91 if (dir
== SEARCH_FORWARD
) {
92 ret
= s
->ops
.get_next(&iter
);
94 ret
= s
->ops
.get_prev(&iter
);
97 ret
= do_search(s
, &iter
, text
, dir
);