]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libventi/parsescore.c
merge
[plan9front.git] / sys / src / libventi / parsescore.c
1 #include <u.h>
2 #include <libc.h>
3 #include <venti.h>
4
5 int
6 vtparsescore(char *s, char **prefix, uchar score[VtScoreSize])
7 {
8         int i, c;
9         char *buf, *colon;
10
11         if((colon = strchr(s, ':')) != nil)
12                 buf = colon+1;
13         else
14                 buf = s;
15
16         if(strlen(buf) != 2*VtScoreSize)
17                 return -1;
18
19         memset(score, 0, VtScoreSize);
20         for(i=0; i<2*VtScoreSize; i++){
21                 if(buf[i] >= '0' && buf[i] <= '9')
22                         c = buf[i] - '0';
23                 else if(buf[i] >= 'a' && buf[i] <= 'z')
24                         c = buf[i] - 'a' + 10;
25                 else if(buf[i] >= 'A' && buf[i] <= 'Z')
26                         c = buf[i] - 'A' + 10;
27                 else
28                         return -1;
29
30                 if((i & 1) == 0)
31                         c <<= 4;
32                 score[i>>1] |= c;
33         }
34         if(colon){
35                 *colon = 0;
36                 if(prefix)
37                         *prefix = s;
38         }else{
39                 if(prefix)
40                         *prefix = nil;
41         }
42         return 0;
43 }