1 /*
2  * gdsreader - simple Calma parser/printer tool
3  * Copyright (C) 1999 Serban-Mihai Popescu, serbanp@ix.netcom.com
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 
24 #include <GDSstructs.h>
25 #include <GDSconsts.h>
26 #include <GDSglobals.h>
27 
28 
29 /* Read in the layers.config file.  If there is no datatype
30   specified in a record, then we should use that layer for all
31   unspecified datatypes?  I added a dummy entry at position
32   zero which is hidden.
33 */
34 
35 int
GDSinitPSStyles(FILE * cfgfile)36 GDSinitPSStyles(FILE *cfgfile)
37 {
38   char seps[] = {" \t\n"};
39   char line[1024], buf[1024], *token;
40   int counter = 0, lineno = 0, firsttime;
41   color edgecolor;
42 
43             fprintf(stdout, "dummy hidden layer body\n");
44             edgecolor.r = edgecolor.g = edgecolor.b = 0.0;
45             psStyles[counter].edgecolor = edgecolor;
46             psStyles[counter].gdsno = -1;
47             psStyles[counter].datatype = -3;
48             psStyles[counter].dataspec = 0;
49             psStyles[counter].layername = strdup("NULL");
50             psStyles[counter].hidden = 1;
51             psStyles[counter].fill = 0;
52             psStyles[counter].hatch = 0;
53             psStyles[counter].angle = 0.0;  /* arbitrary numbers since the */
54             psStyles[counter].step = 100.0; /* hatch is 0 */
55             psStyles[counter].depth = 0.0;
56             psStyles[counter].thickness = 0.0;
57   counter++;
58 
59   while(!feof(cfgfile))
60   {
61     if(fgets(line, 1024, cfgfile) == NULL)
62       break;
63     lineno++;
64     firsttime = 1;
65     if(line[0] == '#')
66     /* comment line */
67       continue;
68     do
69     {
70       if(firsttime)
71       {
72         firsttime = 0;
73         token = strtok(line, seps);
74       }
75       else
76         token = strtok(NULL, seps);
77       if(token == NULL)
78         continue;
79       switch(token[0])
80       {
81 	case 'd':
82           if(!strcmp(token, "depth"))
83           {
84             token = strtok(NULL, seps);
85             if(token != NULL)
86             {
87               psStyles[counter].depth = atof(token);
88               if(errno == EINVAL)
89               {
90                 fprintf(stderr,
91                         "Syntax error at line %d: bad number syntax %s\n",
92                         lineno, token);
93                 exit(1);
94               }
95             }
96             else
97             {
98               fprintf(stderr, "Syntax error at line %d: missing number\n",
99                       lineno);
100               exit(1);
101             }
102           }
103           else {
104 
105            if(!strcmp(token, "datatype"))
106         	  {
107         	    token = strtok(NULL, seps);
108         	    if(token != NULL)
109         	    {
110         	      psStyles[counter].datatype = atoi(token);
111         	      psStyles[counter].dataspec = 1;
112         	      if(errno == EINVAL)
113         	      {
114                 	fprintf(stderr,
115                         	"Syntax error at line %d: bad number syntax %s\n",
116                         	lineno, token);
117                 	exit(1);
118         	      }
119         	    }
120         	    else
121         	    {
122         	      fprintf(stderr, "Syntax error at line %d: missing number\n",
123                 	      lineno);
124         	      exit(1);
125         	    }
126         	  }
127 
128         	  else
129         	  {
130         	    fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
131                 	    lineno, token);
132         	    exit(1);
133         	  }
134           }
135           break;
136        case 't':
137           if(!strcmp(token, "thickness"))
138           {
139             token = strtok(NULL, seps);
140             if(token != NULL)
141             {
142               psStyles[counter].thickness = atof(token);
143               if(errno == EINVAL)
144               {
145                 fprintf(stderr,
146                         "Syntax error at line %d: bad number syntax %s\n",
147                         lineno, token);
148                 exit(1);
149               }
150             }
151             else
152             {
153               fprintf(stderr, "Syntax error at line %d: missing number\n",
154                       lineno);
155               exit(1);
156             }
157           }
158           else
159           {
160             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
161                     lineno, token);
162             exit(1);
163           }
164           break;
165        case 'e':
166           if(!strcmp(token, "endlayer"))
167           {
168             psStyles[counter].edgecolor = edgecolor;
169             if(psStyles[counter].layername == NULL)
170             {
171               sprintf(buf, "Layer%d", psStyles[counter].gdsno);
172               psStyles[counter].layername = strdup(buf);
173             }
174             fprintf(stdout, "layername = %s, gdsno = %d, type = %d, hidden = %d, red = %.2f, green = %.2f, blue = %.2f,\n"
175                             " fill = %d hatch = %d, angle = %.2f, step = %.2f, depth = %.2f, thickness = %.2f\n",
176                     psStyles[counter].layername,
177                     psStyles[counter].gdsno,
178                     (psStyles[counter].dataspec?psStyles[counter].datatype:-1),
179                     psStyles[counter].hidden,
180                     psStyles[counter].edgecolor.r,
181                     psStyles[counter].edgecolor.g,
182                     psStyles[counter].edgecolor.b,
183                     psStyles[counter].fill, psStyles[counter].hatch,
184                     psStyles[counter].angle, psStyles[counter].step,
185                     psStyles[counter].depth,psStyles[counter].thickness);
186             counter++;
187           }
188           else
189           {
190             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
191                     lineno, token);
192             exit(1);
193           }
194           break;
195         case 'n':
196           if(!strcmp(token, "newlayer"))
197           {
198             fprintf(stdout, "new layer body\n");
199             edgecolor.r = edgecolor.g = edgecolor.b = 0.0;
200             psStyles[counter].edgecolor = edgecolor;
201             psStyles[counter].gdsno = 0;
202             psStyles[counter].datatype = 0;
203             psStyles[counter].dataspec = 0;
204             psStyles[counter].layername = NULL;
205             psStyles[counter].hidden = 1;
206             psStyles[counter].fill = 0;
207             psStyles[counter].hatch = 0;
208             psStyles[counter].angle = 0.0;  /* arbitrary numbers since the */
209             psStyles[counter].step = 100.0; /* hatch is 0 */
210             psStyles[counter].depth = 0.0;
211             psStyles[counter].thickness = 0.0;
212           }
213           else if(!strcmp(token, "name"))
214           {
215             token = strtok(NULL, seps);
216             if(token != NULL)
217               psStyles[counter].layername = strdup(token);
218             else
219             {
220               fprintf(stderr, "Syntax error at line %d: missing layer name\n",
221                       lineno);
222               exit(1);
223             }
224           }
225           else
226           {
227             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
228                     lineno, token);
229             exit(1);
230           }
231           break;
232         case 'h':
233           if(!strcmp(token, "hatch"))
234           {
235             token = strtok(NULL, seps);
236             if(token != NULL)
237             {
238               if(!strcmp(token, "no") || !strcmp(token, "false"))
239                 psStyles[counter].hatch = 0;
240               else if(!strcmp(token, "single"))
241                 psStyles[counter].hatch = 1;
242               else if(!strcmp(token, "cross"))
243                 psStyles[counter].hatch = 2;
244               else
245               {
246                 fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
247                         lineno, token);
248                 exit(1);
249               }
250             }
251             else
252             {
253               fprintf(stderr, "Syntax error at line %d: missing token\n",
254                       lineno);
255               exit(1);
256             }
257           }
258           else if(!strcmp(token, "hidden"))
259           {
260             token = strtok(NULL, seps);
261             if(token != NULL)
262             {
263               if(!strcmp(token, "no") || !strcmp(token, "false"))
264                 psStyles[counter].hidden = 0;
265               else if(!strcmp(token, "yes") || !strcmp(token, "true"))
266                 psStyles[counter].hidden = 1;
267               else
268               {
269                 fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
270                         lineno, token);
271                 exit(1);
272               }
273             }
274             else
275             {
276               fprintf(stderr, "Syntax error at line %d: missing token\n",
277                       lineno);
278               exit(1);
279             }
280           }
281           else
282           {
283             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
284                     lineno, token);
285             exit(1);
286           }
287           break;
288         case 'r':
289           if(!strcmp(token, "red"))
290           {
291             token = strtok(NULL, seps);
292             if(token != NULL)
293             {
294               edgecolor.r = atof(token);
295               if(errno == EINVAL)
296               {
297                 fprintf(stderr,
298                         "Syntax error at line %d: bad number syntax %s\n",
299                         lineno, token);
300                 exit(1);
301               }
302             }
303             else
304             {
305               fprintf(stderr, "Syntax error at line %d: missing number\n",
306                       lineno);
307               exit(1);
308             }
309           }
310           else
311           {
312             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
313                     lineno, token);
314             exit(1);
315           }
316           break;
317         case 'g':
318           if(!strcmp(token, "gdsno"))
319           {
320             token = strtok(NULL, seps);
321             if(token != NULL)
322             {
323               psStyles[counter].gdsno = atoi(token);
324               if(errno == EINVAL)
325               {
326                 fprintf(stderr,
327                         "Syntax error at line %d: bad number syntax %s\n",
328                         lineno, token);
329                 exit(1);
330               }
331             }
332             else
333             {
334               fprintf(stderr, "Syntax error at line %d: missing number\n",
335                       lineno);
336               exit(1);
337             }
338           }
339           else if(!strcmp(token, "green"))
340           {
341             token = strtok(NULL, seps);
342             if(token != NULL)
343             {
344               edgecolor.g = atof(token);
345               if(errno == EINVAL)
346               {
347                 fprintf(stderr,
348                         "Syntax error at line %d: bad number syntax %s\n",
349                         lineno, token);
350                 exit(1);
351               }
352             }
353             else
354             {
355               fprintf(stderr, "Syntax error at line %d: missing number\n",
356                       lineno);
357               exit(1);
358             }
359           }
360           else
361           {
362             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
363                     lineno, token);
364             exit(1);
365           }
366           break;
367         case 'b':
368           if(!strcmp(token, "blue"))
369           {
370             token = strtok(NULL, seps);
371             if(token != NULL)
372             {
373               edgecolor.b = atof(token);
374               if(errno == EINVAL)
375               {
376                 fprintf(stderr,
377                         "Syntax error at line %d: bad number syntax %s\n",
378                         lineno, token);
379                 exit(1);
380               }
381             }
382             else
383             {
384               fprintf(stderr, "Syntax error at line %d: missing number\n",
385                       lineno);
386               exit(1);
387             }
388           }
389           else
390           {
391             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
392                     lineno, token);
393             exit(1);
394           }
395           break;
396         case 'f':
397           if(!strcmp(token, "fill"))
398           {
399             token = strtok(NULL, seps);
400             if(token != NULL)
401             {
402               if(!strcmp(token, "no") || !strcmp(token, "false"))
403                 psStyles[counter].fill = 0;
404               else if(!strcmp(token, "yes") || !strcmp(token, "true"))
405                 psStyles[counter].fill = 1;
406               else
407               {
408                 fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
409                       lineno, token);
410                 exit(1);
411               }
412             }
413             else
414             {
415               fprintf(stderr, "Syntax error at line %d: missing token\n",
416                       lineno);
417               exit(1);
418             }
419           }
420           else
421           {
422             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
423                     lineno, token);
424             exit(1);
425           }
426           break;
427         case 'a':
428           if(!strcmp(token, "angle"))
429           {
430             token = strtok(NULL, seps);
431             if(token != NULL)
432             {
433               psStyles[counter].angle = atof(token);
434               if(errno == EINVAL)
435               {
436                 fprintf(stderr,
437                         "Syntax error at line %d: bad number syntax %s\n",
438                         lineno, token);
439                 exit(1);
440               }
441               while(psStyles[counter].angle > 180.0)
442                 psStyles[counter].angle -= 180.0;
443               while(psStyles[counter].angle < 0.0)
444                 psStyles[counter].angle += 180.0;
445             }
446             else
447             {
448               fprintf(stderr, "Syntax error at line %d: missing number\n",
449                       lineno);
450               exit(1);
451             }
452           }
453           else
454           {
455             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
456                     lineno, token);
457             exit(1);
458           }
459           break;
460         case 's':
461           if(!strcmp(token, "step"))
462           {
463             token = strtok(NULL, seps);
464             if(token != NULL)
465             {
466               psStyles[counter].step = atof(token);
467               if(errno == EINVAL)
468               {
469                 fprintf(stderr,
470                         "Syntax error at line %d: bad number syntax %s\n",
471                         lineno, token);
472                 exit(1);
473               }
474             }
475             else
476             {
477               fprintf(stderr, "Syntax error at line %d: missing number\n",
478                       lineno);
479               exit(1);
480             }
481           }
482           else
483           {
484             fprintf(stderr, "Syntax error at line %d: unknown token %s\n",
485                     lineno, token);
486             exit(1);
487           }
488           break;
489         default:
490           fprintf(stderr, "token = %s\n", token);
491           break;
492       }
493     }
494     while(token != NULL);
495   }
496   psStyles[counter].gdsno = 0; /* Last entry.  Not really used as a flag. */
497 
498   return counter;
499 }
500