Jspice3
library.c
Go to the documentation of this file.
1 /***************************************************************************
2 SCED - Schematic Capture Editor
3 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
4 Copyright 1990 Regents of the University of California. All rights reserved.
5 Authors: 1981 Giles C. Billingsley (parts of KIC layout editor)
6  1992 Stephen R. Whiteley
7 ****************************************************************************/
8 
9 /*
10  * SCED library access routines.
11  */
12 
13 #include "spice.h"
14 #include "sced.h"
15 #include <string.h>
16 
17 /* linked list to keep track of models actually referenced */
18 struct wl {
19  char *wl_name;
20  struct wl *wl_next;
21 };
22 
23 /* list of library elements */
24 struct lib {
25  char *lib_name;
26  long lib_offset;
27  struct wl *lib_info;
28  struct lib *lib_next;
29 };
30 
31 
32 
33 /***********************************************************************
34  *
35  * Routines to access the device library.
36  *
37  ***********************************************************************/
38 
39 static struct lib *Lib;
40 static char *LibName;
41 
42 #ifdef __STDC__
43 static struct lib *allocate_lib(char*,long,struct wl*);
44 #else
45 static struct lib *allocate_lib();
46 #endif
47 
48 
49 void
51 
52 /* Create a list of library devices with file offsets. */
53 char *name;
54 {
55  FILE *fp;
56  char buf[512],*c,*d,*fgets();
57  struct lib *l;
58  int InSymbol = False;
59  long ftold, ftell();
60  char *nam;
61 
62  if (Lib != NULL) return;
63  fp = POpen(name,"r",&nam);
64  if (fp == NULL) return;
65 
66  (void) fgets(buf,512,fp);
67  if (buf[0] != 'S' || buf[1] != 'C' || buf[2] != 'E' || buf[3] != 'D') {
68  fclose(fp);
69  return;
70  }
71  LibName = nam;
72 
73  ftold = ftell(fp);
74  while (fgets(buf,512,fp) != NULL) {
75  if (*buf == '9' && isspace(*(buf+1)) && !InSymbol) {
76  c = buf+2;
77  while (isspace(*c)) c++;
78  for (d = c+1; d; d++) {
79  if (isspace(*d) || *d == ';') {
80  *d = '\0';
81  break;
82  }
83  }
84  if (Lib == NULL)
85  l = Lib = allocate_lib(c,ftold,(struct wl *)NULL);
86  else {
87  l->lib_next = allocate_lib(c,ftold,(struct wl *)NULL);
88  l = l->lib_next;
89  }
90  InSymbol = True;
91  }
92  if (*buf == 'E' && (isspace(*(buf+1)) || *(buf+1) == '\0'))
93  InSymbol = False;
94  ftold = ftell(fp);
95  }
96  fclose(fp);
97 }
98 
99 
100 FILE *
102 
103 /* Return a FILE pointer to the library at the
104  * correct position for the device.
105  */
106 char *name;
107 {
108  struct lib *l;
109  FILE *fp;
110 
111  for (l = Lib; l; l = l->lib_next) {
112  if (!strcmp(name,l->lib_name)) {
113  fp = fopen(LibName,"r");
114  if (fp == NULL) return (NULL);
115  fseek(fp,l->lib_offset,0);
116  return (fp);
117  }
118  }
119  return (NULL);
120 }
121 
122 
123 int
125 
126 /* Return True is name is found in the device library. */
127 char *name;
128 {
129  struct lib *l;
130  FILE *fp;
131 
132  for (l = Lib; l; l = l->lib_next)
133  if (!strcmp(name,l->lib_name))
134  return (True);
135  return (False);
136 }
137 
138 
139 void
141 
142 /* Create a menu of the devices in the device library */
143 {
144  struct lib *w;
145  int i;
146 
147  for (i = 0,w = Lib; w; w = w->lib_next) {
148  DeviceMenu[i].mEntry = w->lib_name;
149  i++;
150  if (i == 79) break;
151  }
152  DeviceMenu[i].mEntry = NULL;
153  /*
154  FixMenuPrefix(DeviceMenu);
155  */
156 }
157 
158 
159 static struct lib *
160 allocate_lib(Name,Offset,Info)
161 
162 char *Name;
163 long Offset;
164 struct wl *Info;
165 {
166  struct lib *l;
167 
168  l = alloc(lib);
169  if (l == NULL) MallocFailed();
170  l->lib_next = NULL;
171  l->lib_name = CopyString(Name);
172  l->lib_offset = Offset;
173  l->lib_info = Info;
174  return (l);
175 }
176 
177 
178 
179 /***********************************************************************
180  *
181  * Routines to access the spice model library.
182  *
183  ***********************************************************************/
184 
185 static struct lib *Mod;
186 static char *ModLibName;
187 
188 struct ml {
189  char *ml_name;
190  struct ml *ml_next;
191  int ml_level;
192 };
193 
194 /* referenced model list head */
195 static struct ml *Models;
196 
197 static int Mlevel;
198 
199 #ifdef __STDC__
200 static struct wl *allocate_wl(char*);
201 static struct ml *allocate_ml(char*,int);
202 #else
203 static struct wl *allocate_wl();
204 static struct ml *allocate_ml();
205 #endif
206 
207 
208 void
210 
211 /* Create a list of model library entries and file offsets. */
212 char *name;
213 {
214  FILE *fp;
215  char buf[512],*c,*d,*fgets();
216  struct lib *l;
217  long ftold, ftell();
218  char *nam;
219 
220  if (Mod != NULL) return;
221  fp = POpen(name,"r",&nam);
222  if (fp == NULL) return;
223  (void) fgets(buf,512,fp);
224  if (buf[0] != 'S' || buf[1] != 'C' || buf[2] != 'E' || buf[3] != 'D') {
225  fclose(fp);
226  return;
227  }
228  ModLibName = nam;
229 
230  ftold = ftell(fp);
231  while (fgets(buf,512,fp) != NULL) {
232  if (ciprefix(".model",buf)) {
233  c = buf+7;
234  while (isspace(*c)) c++;
235  for (d = c+1; d; d++) {
236  if (isspace(*d) || ispunct(*d)) {
237  *d = '\0';
238  break;
239  }
240  }
241  if (Mod == NULL)
242  l = Mod = allocate_lib(c,ftold,(struct wl *)NULL);
243  else {
244  l->lib_next = allocate_lib(c,ftold,(struct wl *)NULL);
245  l = l->lib_next;
246  }
247  }
248  ftold = ftell(fp);
249  }
250 }
251 
252 
253 void
255 
256 /* Put model in referenced list if it is not there already.
257  * If name is NULL, increment the "stack" pointer.
258  */
259 char *name;
260 {
261  struct ml *m;
262 
263  if (name) {
264  while (isspace(*name)) name++;
265  if (!*name)
266  return;
267  for (m = Models; m; m = m->ml_next)
268  if (cieq(name,m->ml_name)) return;
269  m = allocate_ml(name,Mlevel);
270  m->ml_next = Models;
271  Models = m;
272  }
273  else
274  Mlevel++;
275 }
276 
277 
278 struct line *
280 
281 /* Return the spice lines for the models referenced in the current
282  * context.
283  */
284 {
285  struct ml *m;
286  struct line *d,*d1,*d0;
287 
288  d0 = NULL;
289  while (Models && Models->ml_level == Mlevel) {
290  d1 = GetModelText(Models->ml_name);
291  if (d1 != NULL) {
292  if (d0 == NULL)
293  d = d0 = d1;
294  else
295  d->li_next = d1;
296  for (; d->li_next; d = d->li_next) ;
297  }
298  m = Models;
299  Models = Models->ml_next;
300  txfree(m->ml_name);
301  tfree(m);
302  }
303  Mlevel--;
304  return (d0);
305 }
306 
307 
308 struct line *
310 
311 /* Return the text for the model. */
312 char *name;
313 {
314  FILE *ip;
315  struct lib *l;
316  struct line *d,*d0 = NULL;
317  struct wl *w,*w0;
318  char buf[512],*c;
319 
320  for (l = Mod; l; l = l->lib_next) {
321  if (cieq(name,l->lib_name)) {
322  if (l->lib_info == NULL) {
323  ip = fopen(ModLibName,"r");
324  if (ip == NULL) return (NULL);
325  fseek(ip,l->lib_offset,0);
326  if (fgets(buf,512,ip) == NULL) return (NULL);
327  if ((c = strchr(buf,'\n')) != NULL) *c = '\0';
328  w = w0 = allocate_wl(buf);
329  while (fgets(buf,512,ip) != NULL) {
330  if (ciprefix(".model",buf)) break;
331  if ((c = strchr(buf,'\n')) != NULL) *c = '\0';
332  w->wl_next = allocate_wl(buf);
333  w = w->wl_next;
334  }
335  fclose(ip);
336  l->lib_info = w0;
337  }
338  d = d0 = AllocateLine(l->lib_info->wl_name);
339  for (w = l->lib_info->wl_next; w; w = w->wl_next) {
340  d->li_next = AllocateLine(w->wl_name);
341  d = d->li_next;
342  }
343  return (d0);
344  }
345  }
346  return (NULL);
347 }
348 
349 
350 static struct wl *
351 allocate_wl(string)
352 
353 char *string;
354 {
355  struct wl *w;
356 
357  w = alloc(wl);
358  if (w == NULL) MallocFailed();
359  w->wl_next = NULL;
360  w->wl_name = CopyString(string);
361  return (w);
362 }
363 
364 
365 static struct ml *
366 allocate_ml(string,level)
367 
368 char *string;
369 int level;
370 {
371  struct ml *m;
372 
373  m = alloc(ml);
374  if (m == NULL) MallocFailed();
375  m->ml_next = NULL;
376  m->ml_name = CopyString(string);
377  m->ml_level = level;
378  return (m);
379 }
380 
381 
382 
383 /***********************************************************************
384  *
385  * Routine to delete libraries.
386  *
387  ***********************************************************************/
388 
389 
390 void
392 
393 {
394  struct lib *l;
395  struct wl *w;
396  struct ml *m;
397 
398  for (l = Lib; l; l = Lib) {
399  Lib = l->lib_next;
400  txfree(l->lib_name);
401  tfree(l);
402  }
403  Lib = NULL;
404  tfree(LibName);
405 
406  for (l = Mod; l; l = Mod) {
407  Mod = l->lib_next;
408  txfree(l->lib_name);
409  for (w = l->lib_info; w; w = l->lib_info) {
410  l->lib_info = w->wl_next;
411  txfree(w->wl_name);
412  tfree(w);
413  }
414  tfree(l);
415  }
416  Mod = NULL;
417  tfree(ModLibName);
418 
419  for (m = Models; m; m = Models) {
420  Models = m->ml_next;
421  txfree(m->ml_name);
422  tfree(m);
423  }
424  Models = NULL;
425  Mlevel = 0;
426 }
427 
428 
429 void
431 
432 {
433  struct lib *l;
434 
435  for (l = Lib; l; l = Lib) {
436  Lib = l->lib_next;
437  txfree(l->lib_name);
438  tfree(l);
439  }
440  Lib = NULL;
441  tfree(LibName);
442 }
443 
444 
445 void
447 
448 {
449  struct wl *w;
450  struct ml *m;
451  struct lib *l;
452 
453  for (l = Mod; l; l = Mod) {
454  Mod = l->lib_next;
455  txfree(l->lib_name);
456  for (w = l->lib_info; w; w = l->lib_info) {
457  l->lib_info = w->wl_next;
458  txfree(w->wl_name);
459  tfree(w);
460  }
461  tfree(l);
462  }
463  Mod = NULL;
464  tfree(ModLibName);
465 
466  for (m = Models; m; m = Models) {
467  Models = m->ml_next;
468  txfree(m->ml_name);
469  tfree(m);
470  }
471  Models = NULL;
472  Mlevel = 0;
473 }
struct wl * lib_info
Definition: library.c:27
static struct lib * allocate_lib()
static char buf[MAXPROMPT]
Definition: arg.c:18
void ModelLibraryOpen(char *name)
Definition: library.c:209
int ciprefix()
void QueueModel(char *name)
Definition: library.c:254
static struct lib * Lib
Definition: library.c:39
void ModelLibraryClose()
Definition: library.c:446
static struct ml * allocate_ml()
int cieq()
static int Mlevel
Definition: library.c:197
char * CopyString()
static char * ModLibName
Definition: library.c:186
struct line * AllocateLine()
static struct wl * allocate_wl()
char * ml_name
Definition: library.c:189
char * mEntry
Definition: sced.h:364
Definition: cddefs.h:169
Definition: library.c:18
Definition: cddefs.h:312
struct line * PrintModels()
Definition: library.c:279
#define alloc(type)
Definition: cdmacs.h:21
FILE * m
Definition: proc2mod.c:47
Definition: library.c:188
static struct lib * Mod
Definition: library.c:185
void MallocFailed()
Definition: scedintr.c:857
Definition: fteinp.h:14
void DestroySCEDlibs()
Definition: library.c:391
Definition: cddefs.h:237
#define tfree(x)
Definition: cdmacs.h:22
void txfree()
#define NULL
Definition: spdefs.h:121
static struct ml * Models
Definition: library.c:195
MENU * DeviceMenu
Definition: scedintr.c:50
#define True
Definition: scedstub.c:16
static double c
Definition: vectors.c:16
struct line * li_next
Definition: fteinp.h:18
static char * LibName
Definition: library.c:40
struct wl * wl_next
Definition: library.c:20
int ml_level
Definition: library.c:191
void LibraryClose()
Definition: library.c:430
FILE * OpenDevice(char *name)
Definition: library.c:101
char * lib_name
Definition: library.c:25
Definition: library.c:24
#define False
Definition: scedstub.c:15
struct line * GetModelText(char *name)
Definition: library.c:309
long lib_offset
Definition: library.c:26
void LibraryOpen(char *name)
Definition: library.c:50
char * wl_name
Definition: library.c:19
FILE * POpen()
struct ml * ml_next
Definition: library.c:190
struct lib * lib_next
Definition: library.c:28
int IsCellInLib(char *name)
Definition: library.c:124
void SetLibraryChoices()
Definition: library.c:140