Jspice3
inpsymt.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1987 Thomas L. Quarles
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * Stuff for the terminal and node symbol tables.
10  * Defined: INPtabInit, INPinsert, INPtermInsert, INPtabEnd
11  */
12 
13 #include "spice.h"
14 #include "inpdefs.h"
15 #include "iferrmsg.h"
16 #include "misc.h"
17 
18 #ifdef __STDC__
19 static int hash(char *, int);
20 #else /* stdc */
21 static int hash();
22 #endif /* stdc */
23 
24 
25 /* Initialize the symbol tables. */
26 
27 INPtables *
28 INPtabInit(numlines)
29 
30 int numlines;
31 {
32  INPtables *tab;
33  char *tmalloc();
34 
35  tab = (INPtables *)tmalloc(sizeof(INPtables));
36  tab->INPsymtab = (struct INPtab **) tmalloc((numlines / 4 + 1) *
37  sizeof (struct INPtab *));
38  tab->INPtermsymtab = (struct INPnTab **) tmalloc(numlines *
39  sizeof (struct INPnTab *));
40  tab->INPsize = numlines / 4 + 1;
41  tab->INPtermsize = numlines;
42  return (tab);
43 }
44 
45 
46 /* insert 'token' into the terminal symbol table */
47 /* create a NEW NODE and return a pointer to it in *node */
48 
49 int
50 INPtermInsert(ckt,token,tab,node)
51 
52 GENERIC *ckt;
53 char **token;
54 INPtables *tab;
55 GENERIC**node;
56 {
57  int key;
58  int error;
59  struct INPnTab *t;
60  char *tmalloc();
61 
62  key = hash(*token, tab->INPtermsize);
63  for (t = tab->INPtermsymtab[key]; t; t = t->t_next) {
64  if (!strcmp(*token, t->t_ent)) {
65  txfree(*token);
66  *token = t->t_ent;
67  if (node)
68  *node = t->t_node;
69  return (E_EXISTS);
70  }
71  }
72  t = (struct INPnTab *) tmalloc(sizeof (struct INPnTab));
73  if (t == (struct INPnTab*)NULL)
74  return (E_NOMEM);
75  error = (*(ft_sim->newNode))(ckt,&t->t_node,*token);
76  if (error)
77  return (error);
78  if (node)
79  *node = t->t_node;
80  t->t_ent = *token;
81  t->t_next = tab->INPtermsymtab[key];
82  tab->INPtermsymtab[key] = t;
83  return (OK);
84 }
85 
86 
87 /* insert 'token' into the terminal symbol table */
88 /* USE node as the node pointer */
89 
90 /* ARGSUSED */
91 int
92 INPmkTerm(ckt,token,tab,node)
93 
94 GENERIC *ckt;
95 char **token;
96 INPtables *tab;
97 GENERIC**node;
98 {
99  int key;
100  struct INPnTab *t;
101  char *tmalloc();
102 
103  key = hash(*token, tab->INPtermsize);
104  for (t = tab->INPtermsymtab[key]; t; t = t->t_next) {
105  if (!strcmp(*token, t->t_ent)) {
106  txfree(*token);
107  *token = t->t_ent;
108  if (node)
109  *node = t->t_node;
110  return (E_EXISTS);
111  }
112  }
113  t = (struct INPnTab *) tmalloc(sizeof (struct INPnTab));
114  if (t == (struct INPnTab*)NULL)
115  return (E_NOMEM);
116  t->t_node = *node ;
117  t->t_ent = *token;
118  t->t_next = tab->INPtermsymtab[key];
119  tab->INPtermsymtab[key] = t;
120  return (OK);
121 }
122 
123 
124 /* insert 'token' into the terminal symbol table as a name for ground*/
125 
126 int
127 INPgndInsert(ckt,token,tab,node)
128 
129 GENERIC *ckt;
130 char **token;
131 INPtables *tab;
132 GENERIC**node;
133 {
134  int key;
135  int error;
136  struct INPnTab *t;
137  char *tmalloc();
138 
139  key = hash(*token, tab->INPtermsize);
140  for (t = tab->INPtermsymtab[key]; t; t = t->t_next) {
141  if (!strcmp(*token, t->t_ent)) {
142  txfree(*token);
143  *token = t->t_ent;
144  if (node)
145  *node = t->t_node;
146  return (E_EXISTS);
147  }
148  }
149  t = (struct INPnTab *) tmalloc(sizeof (struct INPnTab));
150  if (t == (struct INPnTab*)NULL)
151  return(E_NOMEM);
152  error = (*(ft_sim->groundNode))(ckt,&t->t_node,*token);
153  if (error)
154  return(error);
155  if (node)
156  *node = t->t_node;
157  t->t_ent = *token;
158  t->t_next = tab->INPtermsymtab[key];
159  tab->INPtermsymtab[key] = t;
160  return (OK);
161 }
162 
163 
164 /* insert 'token' into the symbol table */
165 
166 int
168 
169 char **token;
170 INPtables *tab;
171 {
172  struct INPtab *t;
173  int key;
174  char *tmalloc();
175 
176  key = hash(*token, tab->INPsize);
177  for (t = tab->INPsymtab[key]; t; t = t->t_next)
178  if (!strcmp(*token, t->t_ent)) {
179  txfree(*token);
180  *token = t->t_ent;
181  return (E_EXISTS);
182  }
183  t = (struct INPtab *) tmalloc(sizeof (struct INPtab));
184  if (t == (struct INPtab*)NULL)
185  return (E_NOMEM);
186  t->t_ent = *token;
187  t->t_next = tab->INPsymtab[key];
188  tab->INPsymtab[key] = t;
189  return (OK);
190 }
191 
192 
193 /* Free the space used by the symbol tables. */
194 
195 void
197 
198 INPtables *tab;
199 {
200  struct INPtab *t, *lt;
201  struct INPnTab *n, *ln;
202  int i;
203  /* SRW - used to not free t_ent, for whatever reason */
204 
205  for (i = 0; i < tab->INPsize; i++)
206  for (t = tab->INPsymtab[i]; t; t = lt) {
207  lt = t->t_next;
208  txfree(t->t_ent);
209  txfree((char*)t);
210  }
211  txfree((char*)tab->INPsymtab);
212  for (i = 0; i < tab->INPtermsize; i++)
213  for (n = tab->INPtermsymtab[i]; n; n = ln) {
214  ln = n->t_next;
215  txfree(n->t_ent);
216  txfree((char*)n);
217  }
218  txfree((char*)tab->INPtermsymtab);
219  txfree((char*)tab);
220  return;
221 }
222 
223 
224 static int
225 hash(name, tsize)
226 
227 char *name;
228 int tsize;
229 {
230  char *s;
231  int i = 0;
232 
233  for (s = name; *s; s++)
234  i += *s;
235  return (i % tsize);
236 }
237 
char * t_ent
Definition: inpdefs.h:25
IFsimulator * ft_sim
Definition: main.c:111
INPtables * INPtabInit(int numlines)
Definition: inpsymt.c:28
Definition: cddefs.h:119
char * t_ent
Definition: inpdefs.h:20
int INPtermsize
Definition: inpdefs.h:34
Definition: subckt.c:51
#define tab(num)
Definition: front.c:1365
Definition: inpdefs.h:19
#define E_EXISTS
Definition: iferrmsg.h:20
void INPtabEnd(INPtables *tab)
Definition: inpsymt.c:196
#define OK
Definition: iferrmsg.h:17
char * tmalloc()
static int hash()
void txfree()
#define NULL
Definition: spdefs.h:121
#define E_NOMEM
Definition: iferrmsg.h:27
int INPinsert(char **token, INPtables *tab)
Definition: inpsymt.c:167
struct INPnTab ** INPtermsymtab
Definition: inpdefs.h:32
int INPgndInsert(GENERIC *ckt, char **token, INPtables *tab, GENERIC **node)
Definition: inpsymt.c:127
int INPsize
Definition: inpdefs.h:33
int INPtermInsert(GENERIC *ckt, char **token, INPtables *tab, GENERIC **node)
Definition: inpsymt.c:50
struct INPtab * t_next
Definition: inpdefs.h:21
int INPmkTerm(GENERIC *ckt, char **token, INPtables *tab, GENERIC **node)
Definition: inpsymt.c:92
Definition: cddefs.h:192
struct INPtab ** INPsymtab
Definition: inpdefs.h:31
struct INPnTab * t_next
Definition: inpdefs.h:27
GENERIC * t_node
Definition: inpdefs.h:26
char GENERIC
Definition: ifsim.h:27