]> git.lizzy.rs Git - plan9front.git/blob - sys/src/games/music/jukefs/search.c
games/snes: added open bus emulation
[plan9front.git] / sys / src / games / music / jukefs / search.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <thread.h>
5 #include "object.h"
6 #include "parse.h"
7 #include "search.h"
8
9 Object *
10 search(Object *rt, Object *parent, Reprog *preg) {
11         /* Create a `search object', a subtree of rt containing
12          * only objects with s in their value of key fields plus
13          * their parentage.
14          *
15          * Algorithm: depth-first traversal of rt.  On the way down,
16          * copy rt to nr (new root), on the way back up, delete
17          * subtrees without match.
18          *
19          * returns null when there are no matches in rt's subtree
20          */
21         Object *o, *nr;
22         char *s;
23         int i;
24         int yes = 0;
25
26         nr = newobject(rt->type, parent);
27         nr->orig = rt->orig?rt->orig:rt;
28         nr->value = rt->value;
29         strncpy(nr->key, rt->key, KEYLEN);
30
31         if((((s = nr->value)) && regexec(preg, s, nil, 0) == 1)
32         || (((s = nr->value)) && regexec(preg, s, nil, 0) == 1))
33                 yes = 1;
34         for(i = 0; i < rt->nchildren; i++)
35                 if((o = search((Object*)rt->children[i], nr, preg))){
36                         yes = 1;
37                         addchild(nr, o, "search");
38                 }
39         if(yes == 0){
40                 freeobject(nr, "s");
41                 return nil;
42         }
43         return nr;
44 }