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 <stdlib.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <math.h>
28 
29 #include <GDSstructs.h>
30 #include <GDSconsts.h>
31 #include <GDSaux.h>
32 #include <GDSstruct.h>
33 
34 void
GDSreadHeader(int gdsfile)35 GDSreadHeader(int gdsfile)
36 {
37   unsigned char *record;
38   int nbytes;
39 
40   if(GDSreadRecord(gdsfile, &record, &nbytes) != HEADER)
41   {
42     fprintf(stderr, "HEADER is missing. Aborting\n");
43     exit(1);
44   }
45   FREE(record);
46 }
47 
48 void
GDSreadUpToFirstStruct(int gdsfildes,GDSlibrary * libptr)49 GDSreadUpToFirstStruct(int gdsfildes, GDSlibrary *libptr)
50 {
51   unsigned char *record;
52   int nbytes;
53 
54   while(1)
55   {
56     switch(GDSreadRecord(gdsfildes, &record, &nbytes))
57     {
58       case BGNLIB:
59         GDSaddDates(libptr, record);
60         fprintf(stdout, "lastmod = %s, lastacc = %s\n",
61                 libptr->lastmod, libptr->lastacc);
62         break;
63       case LIBDIRSIZE:
64         fprintf(stderr, "Not yet handling LIBDIRSIZE!\n");
65         break;
66 
67       case SRFNAME:
68         fprintf(stderr, "Not yet handling SRFNAME!\n");
69         break;
70       case LIBSECUR:
71         fprintf(stderr, "Not yet handling LIBSECUR!\n");
72         break;
73       case REFLIBS:
74         fprintf(stderr, "Not yet handling REFLIBS!\n");
75         break;
76       case FONTS:
77         fprintf(stderr, "Not yet handling FONTS!\n");
78         break;
79       case LIBNAME:
80         if((libptr->name = GDSreadString(record + 2, nbytes - 4)) == NULL)
81         {
82           fprintf(stderr, "Bad LIBNAME record. Aborting\n");
83           exit(1);
84         }
85         fprintf(stdout, "libname = %s\n", libptr->name);
86         break;
87       case UNITS:
88         libptr->userunit = GDSreadReal8(record + 2);
89         libptr->meterunit = GDSreadReal8(record + 10);
90         fprintf(stdout, "userunit = %e, meterunit = %e\n",
91                 libptr->userunit, libptr->meterunit);
92         break;
93       case BGNSTR:
94         /* should read the last modified/read dates */
95         GDSunreadRecord(gdsfildes, nbytes);
96         return;
97       default:
98         break;
99     }
100     FREE(record);
101   }
102 }
103 
104 GDSlibrary *
GDSreadLib(int gdsfildes)105 GDSreadLib(int gdsfildes)
106 {
107   FILE *outfile;
108   GDSlibrary *libptr;
109   GDSstruct *structptr, *structptr1;
110   GDScell *cellptr;
111   layer *layerptr;
112 
113   if((outfile = fopen("result.txt", "w")) == NULL)
114   {
115     fprintf(stderr, "Couldn't open result.txt for writing\n");
116     exit(1);
117   }
118   libptr = (GDSlibrary *)MALLOC(sizeof(GDSlibrary));
119   libptr->structs = NULL;
120 
121   GDSreadHeader(gdsfildes);
122   GDSreadUpToFirstStruct(gdsfildes, libptr);
123   while((structptr = GDSreadStruct(gdsfildes, libptr)) != NULL)
124   {
125     fprintf(outfile, "Read a struct named %s which contains the layers:\n",
126             structptr->name);
127     for(layerptr = structptr->layers;
128         layerptr != NULL; layerptr = layerptr->next)
129       fprintf(outfile, "no. %d, type %d,", layerptr->layerno,layerptr->datatype);
130     fprintf(outfile, "\nand the cells:\n:");
131     for(cellptr = structptr->cells; cellptr != NULL; cellptr = cellptr->next)
132     {
133       switch(cellptr->type)
134       {
135         case BOUNDARY:
136           fprintf(outfile, "BOUNDARY, ");
137           break;
138         case PATH:
139           fprintf(outfile, "PATH, ");
140           break;
141         case SREF:
142           fprintf(outfile, "SREF(%s), ", cellptr->detail.sref->refname);
143           break;
144         case AREF:
145           fprintf(outfile, "AREF(%s), ", cellptr->detail.aref->refname);
146           break;
147         case TEXT:
148           fprintf(outfile, "TEXT, ");
149           break;
150         case NODE:
151           fprintf(outfile, "NODE, ");
152           break;
153         case BOX:
154           fprintf(outfile, "BOX, ");
155           break;
156       }
157     }
158 
159     fprintf(outfile, "\n\n");
160 
161 
162     structptr->next = libptr->structs;
163     libptr->structs = structptr;
164   }
165 
166   fclose(outfile);
167   /* update the strptr pointers for SREF and AREF elements */
168   for(structptr = libptr->structs; structptr != NULL;
169       structptr = structptr->next)
170     for(cellptr = structptr->cells; cellptr != NULL;
171         cellptr = cellptr->next)
172     {
173       if(cellptr->type == SREF)
174       {
175         for(structptr1 = libptr->structs; structptr1 != NULL;
176             structptr1 = structptr1->next)
177           if(!strcmp(structptr1->name, cellptr->detail.sref->refname))
178           {
179             cellptr->detail.sref->strptr = structptr1;
180             fprintf(stdout, "Fixed reference for SREF \"%s\"\n",
181                     cellptr->detail.sref->refname);
182             break;
183           }
184       }
185       else if(cellptr->type == AREF)
186       {
187         for(structptr1 = libptr->structs; structptr1 != NULL;
188             structptr1 = structptr1->next)
189           if(!strcmp(structptr1->name, cellptr->detail.aref->refname))
190           {
191             cellptr->detail.aref->strptr = structptr1;
192             fprintf(stdout, "Fixed reference for AREF \"%s\"\n",
193                     cellptr->detail.aref->refname);
194             break;
195           }
196       }
197     }
198   return libptr;
199 }
200 
201 GDSstruct *
GDSgetStructByName(GDSlibrary * library,char * structname)202 GDSgetStructByName(GDSlibrary *library, char *structname)
203 {
204   GDSstruct *structptr;
205 
206   for(structptr = library->structs;
207       structptr != NULL; structptr = structptr->next)
208   {
209     fprintf(stdout, "\"%s\"\n", structptr->name);
210     if(!strcmp(structptr->name, structname))
211       break;
212   }
213 
214   return structptr;
215 }
216