xref: /plan9/sys/src/libndb/ndblookval.c (revision 57837e0bd5c9fc29e2017ad8430ad11f4e09d373)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ip.h>
5 #include <ndb.h>
6 
7 /*
8  *  Look for a pair with the given attribute.  look first on the same line,
9  *  then in the whole entry.
10  */
11 Ndbtuple*
ndbfindattr(Ndbtuple * entry,Ndbtuple * line,char * attr)12 ndbfindattr(Ndbtuple *entry, Ndbtuple *line, char *attr)
13 {
14 	Ndbtuple *nt;
15 
16 	/* first look on same line (closer binding) */
17 	for(nt = line; nt;){
18 		if(strcmp(attr, nt->attr) == 0)
19 			return nt;
20 		nt = nt->line;
21 		if(nt == line)
22 			break;
23 	}
24 
25 	/* search whole tuple */
26 	for(nt = entry; nt; nt = nt->entry)
27 		if(strcmp(attr, nt->attr) == 0)
28 			return nt;
29 
30 	return nil;
31 }
32 
33 Ndbtuple*
ndblookval(Ndbtuple * entry,Ndbtuple * line,char * attr,char * to)34 ndblookval(Ndbtuple *entry, Ndbtuple *line, char *attr, char *to)
35 {
36 	Ndbtuple *t;
37 
38 	t = ndbfindattr(entry, line, attr);
39 	if(t != nil){
40 		strncpy(to, t->val, Ndbvlen-1);
41 		to[Ndbvlen-1] = 0;
42 	}
43 	return t;
44 }
45