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
81
82
83
|
#include <stdio.h>
int
cpos(char *s, char c)
{
int i = 0;
while(s[i])
if(s[i++] == c)
return i - 1;
return -1;
}
unsigned char
chex(char c)
{
if(c >= 'a' && c <= 'f')
return 10 + c - 'a';
if(c >= 'A' && c <= 'F')
return 10 + c - 'A';
return (c - '0') & 0xF;
}
int
shex(char *s, int len)
{
int i, n = 0;
for(i = 0; i < len; ++i)
n |= (chex(s[i]) << ((len - i - 1) * 4));
return n;
}
char *
sstr(char *src, char *dst, int from, int to)
{
int i;
char *a = (char *)src + from, *b = (char *)dst;
for(i = 0; i < to; i++)
b[i] = a[i];
dst[to] = '\0';
return dst;
}
int
error(char *name)
{
printf("Error: %s\n", name);
return 0;
}
int
parse(FILE *f)
{
int i, id = 0;
long theme[9];
char line[256], hexs[9][7];
while(fgets(line, 256, f)) {
int split = cpos(line, '#');
if(split >= 0) {
sstr(line + split + 1, hexs[id], 0, 6);
theme[id++] = shex(line + split + 1, 6);
}
if(id >= 9)
break;
}
if(id != 9)
return error("Invalid theme");
for(i = 0; i < 9; ++i)
printf("#%s = %ld\n", hexs[i], theme[i]);
return 1;
}
int
main(int argc, char *argv[])
{
FILE *input;
if(argc == 2) {
input = fopen(argv[1], "rb");
if(input == NULL)
return error("Invalid input.\n");
} else
input = stdin;
return parse(input);
}
|