forked from SteveMaddison/cabrio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lookup.c
80 lines (68 loc) · 1.36 KB
/
lookup.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include "category.h"
#include "lookup.h"
#include "config.h"
int lookup_match( const char *pattern, const char *value ) {
if( !(pattern && value) )
return 0;
if( strcmp( pattern, value ) == 0 ) {
return 0;
}
else {
char *ppos = (char*)pattern;
char *vpos = (char*)value;
while( *ppos && *vpos ) {
if( *ppos == '*' ) {
if( *vpos == *(ppos+1) || strncasecmp( ppos+1, vpos, 1 ) == 0 )
ppos++;
else
vpos++;
}
else if( *vpos == *ppos || strncasecmp( ppos, vpos, 1 ) == 0 ) {
ppos++;
vpos++;
}
else {
return -1;
}
}
if( ppos == pattern + strlen(pattern) - 1 ) {
return 0;
}
}
return -1;
}
const char *lookup_category( struct config_category *category, const char *value ) {
char *display_name = NULL;
struct config_lookup *lookup = category->lookups;
while( lookup ) {
if( lookup_match( lookup->match, value ) == 0 ) {
display_name = lookup->value;
break;
}
lookup = lookup->next;
}
if( display_name )
return display_name;
else
return value;
}
/*
while( *ppos && *vpos ) {
printf("p:%c <=> v:%c\n", *ppos, *vpos );
if( *vpos == *ppos ) {
vpos++;
ppos++;
}
else {
if( *ppos == '*' ) {
vpos++;
printf(" (v:%c p:%c)\n", *vpos, *(ppos+1) );
if( *vpos == *(ppos+1) )
ppos++;
}
else
return -1;
}
}
*/