xref: /plan9-contrib/sys/src/libndb/ndbgetval.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "ndb.h"
5 
6 /*
7  *  search for a tuple that has the given 'attr=val' and also 'rattr=x'.
8  *  copy 'x' into 'buf' and return the whole tuple.
9  *
10  *  return 0 if not found.
11  */
12 Ndbtuple*
13 ndbgetval(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, char *buf)
14 {
15 	Ndbtuple *t, *nt;
16 
17 	t = ndbsearch(db, s, attr, val);
18 	while(t){
19 		/* first look on same line (closer binding) */
20 		nt = s->t;
21 		for(;;){
22 			if(strcmp(rattr, nt->attr) == 0){
23 				strncpy(buf, nt->val, Ndbvlen);
24 				return t;
25 			}
26 			nt = nt->line;
27 			if(nt == s->t)
28 				break;
29 		}
30 		/* search whole tuple */
31 		for(nt = t; nt; nt = nt->entry)
32 			if(strcmp(rattr, nt->attr) == 0){
33 				strncpy(buf, nt->val, Ndbvlen);
34 				return t;
35 			}
36 		ndbfree(t);
37 		t = ndbsnext(s, attr, val);
38 	}
39 	return 0;
40 }
41