1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.9 |
static const char RCSid[] = "$Id: lamps.c,v 2.8 2003/06/07 12:50:20 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Load lamp data. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
2.7 |
#include "copyright.h" |
9 |
greg |
2.6 |
|
10 |
greg |
1.1 |
#include <stdio.h> |
11 |
greg |
2.6 |
#include <stdlib.h> |
12 |
greg |
1.1 |
#include <ctype.h> |
13 |
schorsch |
2.8 |
|
14 |
|
|
#include "standard.h" |
15 |
greg |
2.5 |
#include "color.h" |
16 |
greg |
1.1 |
|
17 |
greg |
2.6 |
extern char *eindex(), *expsave(); |
18 |
greg |
2.4 |
extern FILE *frlibopen(); |
19 |
greg |
1.1 |
|
20 |
|
|
typedef struct lamp { |
21 |
|
|
char *pattern; /* search pattern */ |
22 |
|
|
float *color; /* pointer to lamp value */ |
23 |
|
|
struct lamp *next; /* next lamp in list */ |
24 |
|
|
} LAMP; /* a lamp entry */ |
25 |
|
|
|
26 |
|
|
static LAMP *lamps = NULL; /* lamp list */ |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
float * |
30 |
schorsch |
2.9 |
matchlamp( /* see if string matches any lamp */ |
31 |
|
|
char *s |
32 |
|
|
) |
33 |
greg |
1.1 |
{ |
34 |
|
|
register LAMP *lp; |
35 |
|
|
|
36 |
|
|
for (lp = lamps; lp != NULL; lp = lp->next) { |
37 |
|
|
expset(lp->pattern); |
38 |
|
|
if (eindex(s) != NULL) |
39 |
|
|
return(lp->color); |
40 |
|
|
} |
41 |
|
|
return(NULL); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
|
45 |
schorsch |
2.9 |
int |
46 |
|
|
loadlamps( /* load lamp type file */ |
47 |
|
|
char *file |
48 |
|
|
) |
49 |
greg |
1.1 |
{ |
50 |
|
|
LAMP *lastp; |
51 |
|
|
register LAMP *lp; |
52 |
|
|
FILE *fp; |
53 |
|
|
float xyz[3]; |
54 |
|
|
char buf[128], str[128]; |
55 |
|
|
register char *cp1, *cp2; |
56 |
|
|
|
57 |
greg |
2.4 |
if ((fp = frlibopen(file)) == NULL) |
58 |
greg |
1.1 |
return(0); |
59 |
|
|
lastp = NULL; |
60 |
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) { |
61 |
|
|
/* work on a copy of buffer */ |
62 |
|
|
strcpy(str, buf); |
63 |
|
|
/* get pattern for this entry */ |
64 |
|
|
for (cp1 = str; isspace(*cp1); cp1++) |
65 |
|
|
; |
66 |
|
|
if (!*cp1 || *cp1 == '#') |
67 |
|
|
continue; |
68 |
|
|
for (cp2 = cp1+1; *cp2; cp2++) |
69 |
|
|
if (*cp2 == *cp1 && cp2[-1] != '\\') |
70 |
|
|
break; |
71 |
|
|
if (!*cp2) { |
72 |
|
|
cp1 = "unclosed pattern"; |
73 |
|
|
goto fmterr; |
74 |
|
|
} |
75 |
|
|
cp1++; *cp2++ = '\0'; |
76 |
|
|
if (ecompile(cp1, 1, 0) < 0) { |
77 |
|
|
cp1 = "bad regular expression"; |
78 |
|
|
goto fmterr; |
79 |
|
|
} |
80 |
|
|
if ((lp = (LAMP *)malloc(sizeof(LAMP))) == NULL) |
81 |
|
|
goto memerr; |
82 |
|
|
if ((lp->pattern = expsave()) == NULL) |
83 |
|
|
goto memerr; |
84 |
|
|
/* get specification */ |
85 |
|
|
for (cp1 = cp2; isspace(*cp1); cp1++) |
86 |
|
|
; |
87 |
|
|
if (!isdigit(*cp1) && *cp1 != '.' && *cp1 != '(') { |
88 |
|
|
cp1 = "missing lamp specification"; |
89 |
|
|
goto fmterr; |
90 |
|
|
} |
91 |
|
|
if (*cp1 == '(') { /* find alias */ |
92 |
|
|
for (cp2 = ++cp1; *cp2; cp2++) |
93 |
|
|
if (*cp2 == ')' && cp2[-1] != '\\') |
94 |
|
|
break; |
95 |
|
|
*cp2 = '\0'; |
96 |
|
|
if ((lp->color = matchlamp(cp1)) == NULL) { |
97 |
|
|
cp1 = "unmatched alias"; |
98 |
|
|
goto fmterr; |
99 |
|
|
} |
100 |
|
|
} else { /* or read specificaion */ |
101 |
greg |
2.3 |
if ((lp->color=(float *)malloc(6*sizeof(float)))==NULL) |
102 |
greg |
1.1 |
goto memerr; |
103 |
greg |
2.3 |
if (sscanf(cp1, "%f %f %f", &lp->color[3], & |
104 |
|
|
lp->color[4], &lp->color[5]) != 3) { |
105 |
greg |
1.1 |
cp1 = "bad lamp data"; |
106 |
|
|
goto fmterr; |
107 |
|
|
} |
108 |
|
|
/* convert xyY to XYZ */ |
109 |
greg |
2.3 |
xyz[1] = lp->color[5]; |
110 |
|
|
xyz[0] = lp->color[3]/lp->color[4] * xyz[1]; |
111 |
|
|
xyz[2] = xyz[1]*(1./lp->color[4] - 1.) - xyz[0]; |
112 |
greg |
1.1 |
/* XYZ to RGB */ |
113 |
|
|
cie_rgb(lp->color, xyz); |
114 |
|
|
} |
115 |
|
|
if (lastp == NULL) |
116 |
|
|
lamps = lp; |
117 |
|
|
else |
118 |
|
|
lastp->next = lp; |
119 |
|
|
lp->next = NULL; |
120 |
|
|
lastp = lp; |
121 |
|
|
} |
122 |
|
|
fclose(fp); |
123 |
greg |
1.3 |
return(lastp != NULL); |
124 |
greg |
1.1 |
memerr: |
125 |
|
|
fputs("Out of memory in loadlamps\n", stderr); |
126 |
|
|
return(-1); |
127 |
|
|
fmterr: |
128 |
|
|
fputs(buf, stderr); |
129 |
|
|
fprintf(stderr, "%s: %s\n", file, cp1); |
130 |
|
|
return(-1); |
131 |
|
|
} |
132 |
greg |
1.2 |
|
133 |
|
|
|
134 |
schorsch |
2.9 |
void |
135 |
|
|
freelamps(void) /* free our lamps list */ |
136 |
greg |
1.2 |
{ |
137 |
|
|
register LAMP *lp1, *lp2; |
138 |
|
|
|
139 |
greg |
2.2 |
for (lp1 = lamps; lp1 != NULL; lp1 = lp2) { |
140 |
greg |
1.2 |
free(lp1->pattern); |
141 |
|
|
if (lp1->color != NULL) { |
142 |
|
|
for (lp2 = lp1->next; lp2 != NULL; lp2 = lp2->next) |
143 |
|
|
if (lp2->color == lp1->color) |
144 |
|
|
lp2->color = NULL; |
145 |
greg |
2.6 |
free((void *)lp1->color); |
146 |
greg |
1.2 |
} |
147 |
greg |
2.2 |
lp2 = lp1->next; |
148 |
greg |
2.6 |
free((void *)lp1); |
149 |
greg |
1.2 |
} |
150 |
|
|
lamps = NULL; |
151 |
|
|
} |