forked from embeddedTS/libtsctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookupRef.c
40 lines (33 loc) · 891 Bytes
/
LookupRef.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
#include "ts/Array.h"
#include "LookupRef.h"
int LookupRefCompare(const void *a,const void *b) {
const LookupRef *r1 = a, *r2 = b;
return ArrayCompare(r1->name,r2->name);
}
LookupRef* LookupRefNew() {
LookupRef* ret = ArrayAlloc(0,sizeof(LookupRef));
ArraySort(ret,LookupRefCompare);
return ret;
}
void LookupRefOld(LookupRef* lu) {
int i;
for (i=0;i<ArrayLength(lu);i++) {
ArrayFree(lu[i].offset);
ArrayFree(lu[i].name);
}
ArrayFree(lu);
}
LookupRef *LookupRefName(LookupRef* *lu,char* name) {
LookupRef ref;
ref.name = name;
if (ArrayLength(*lu) > 0) {
unsigned index = ArrayFind(*lu,&ref);
if (index < ArrayLength(*lu) && !LookupRefCompare(*lu+index,&ref)) {
return *lu + index;
}
}
ref.name = ArrayDup(ref.name);
ref.offset = ArrayAlloc(0,sizeof(int));
*lu = ArrayInsert(*lu,&ref);
return &lu[0][ArrayFind(*lu,&ref)];
}