Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / mbkspice / spi_int.c
1 /*
2 * This file is part of the Alliance CAD System
3 * Copyright (C) Laboratoire LIP6 - Département ASIM
4 * Universite Pierre et Marie Curie
5 *
6 * Home page : http://www-asim.lip6.fr/alliance/
7 * E-mail support : mailto:alliance-support@asim.lip6.fr
8 *
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Library General Public License as published
11 * by the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * Alliance VLSI CAD System is distributed in the hope that it will be
15 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 * Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with the GNU C Library; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25 /*******************************************************************************
26 * *
27 * Tool : Spice parser / driver v 7.00 *
28 * Author(s) : Gregoire AVOT *
29 * Updates : March, 18th 1998 *
30 * *
31 *******************************************************************************/
32
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include MUT_H
38 #include "spi_int.h"
39
40 tableint* creattableint()
41 {
42 tableint *new;
43 int i;
44
45 new = (tableint*) mbkalloc( sizeof( tableint ) * SPI_TABLEINTMAX );
46
47 for( i = 0 ; i < SPI_TABLEINTMAX ; i++ )
48 {
49 new[ i ].index = 0;
50 new[ i ].data = NULL;
51 new[ i ].down = NULL;
52 }
53
54 return( new );
55 }
56
57 int settableint( table, value, data )
58 tableint *table;
59 int value;
60 void *data;
61 {
62 int p;
63 int niveau;
64
65 niveau = 3;
66
67 while( 1 )
68 {
69 p = ( value >> ( niveau * 8 ) ) & ( SPI_TABLEINTMASK );
70
71 if( table[ p ].index == value )
72 {
73 /* L'element existe déjà : on le remplace */
74 table[ p ].data = data;
75 return( 0 );
76 }
77
78 if( table[ p ].index == 0 )
79 {
80 /* Nouvel élément */
81 table[ p ].index = value;
82 table[ p ].data = data;
83 return( 1 );
84 }
85
86 if( table[ p ].down == NULL )
87 table[ p ].down = creattableint();
88
89 niveau--;
90
91 table = table[p].down;
92 }
93 }
94
95 void removetableint( table, value )
96 tableint *table;
97 int value;
98 {
99 int p;
100 int niveau;
101
102 niveau = 3;
103
104 while( 1 )
105 {
106 p = ( value >> ( niveau * 8 ) ) & ( SPI_TABLEINTMASK );
107
108 if( table[ p ].index == value )
109 {
110 /* L'element existe déjà : on le vire */
111 table[ p ].index = 0;
112 table[ p ].data = NULL;
113 return;
114 }
115
116 if( table[ p ].down != NULL )
117 table = table[ p ].down ;
118 else
119 return;
120
121 niveau--;
122 }
123 }
124 void* tsttableint( table, value )
125 tableint *table;
126 int value;
127 {
128 int p;
129 int niveau;
130
131 niveau = 3;
132
133 while( 1 )
134 {
135 p = ( value >> ( niveau * 8 ) ) & (SPI_TABLEINTMASK);
136
137 if( table[ p ].index == value )
138 return( table[ p ].data );
139
140 if( table[ p ].down == NULL )
141 return( NULL );
142
143 niveau--;
144
145 table = table[ p ].down;
146 }
147 }
148
149 void freetableint( table )
150 tableint *table;
151 {
152 int i;
153
154 for( i = 0 ; i < SPI_TABLEINTMAX ; i++ )
155 {
156 if( table[ i ].down )
157 freetableint( table[ i ].down );
158 }
159 mbkfree( table );
160 }
161
162 int scanint( table, n )
163 tableint *table;
164 int n;
165 {
166 int i=0, l=0 ;
167 int n3, n2, n1, n0 ;
168 tableint *t=NULL, *t3, *t2, *t1, *t0 ;
169
170 if( n == 0 )
171 {
172 for( i = 0 ; i < SPI_TABLEINTMAX ; i++ )
173 {
174 if( table[i].index )
175 return( table[i].index );
176 }
177 return( 0 );
178 }
179
180 n0 = n & SPI_TABLEINTMASK;
181 n1 = ( n >> 8 ) & SPI_TABLEINTMASK;
182 n2 = ( n >> 16 ) & SPI_TABLEINTMASK;
183 n3 = ( n >> 24 ) & SPI_TABLEINTMASK;
184
185 /* recherche de l'element n */
186
187 t3 = NULL;
188 t2 = NULL;
189 t1 = NULL;
190 t0 = NULL;
191
192 if( table[ n3 ].index == n )
193 {
194 t = table;
195 l = n3;
196 t3 = t;
197 }
198 else
199 if( table[ n3 ].down[ n2 ].index == n )
200 {
201 t = table[ n3 ].down ;
202 l = n2;
203 t3 = table;
204 t2 = t;
205 }
206 else
207 if( table[ n3 ].down[ n2 ].down[ n1 ].index == n)
208 {
209 t = table[ n3 ].down[ n2 ].down;
210 l = n1;
211 t3 = table;
212 t2 = table[ n3 ].down;
213 t1 = t;
214 }
215 else
216 if( table[ n3 ].down[ n2 ].down[ n1 ].down[ n0 ].index == n )
217 {
218 t = table[ n3 ].down[ n2 ].down[ n1 ].down;
219 l = n0;
220 t3 = table;
221 t2 = table[ n3 ].down;
222 t1 = table[ n3 ].down[ n2 ].down;
223 t0 = t;
224 }
225
226 /* Descent d'un niveau */
227
228 if( t[ l ].down )
229 {
230 t = t[ l ].down;
231
232 for( i = 0 ; i < SPI_TABLEINTMAX ; i++ )
233 {
234 if( t[ i ].index )
235 return( t[ i ].index );
236 }
237 /* jamais executé */
238 }
239
240 while( 1 )
241 {
242 /* Parcour un niveau horizontalement */
243
244 for( i = l + 1 ; i < SPI_TABLEINTMAX ; i++ )
245 {
246 if( t[ i ].index )
247 return( t[ i ].index );
248 }
249
250 /* Remonte les niveaux */
251
252 if( t == t0 )
253 {
254 t = t1;
255 l = n1;
256 }
257 else
258 if( t == t1 )
259 {
260 t = t2;
261 l = n2;
262 }
263 else
264 if( t == t2 )
265 {
266 t = t3;
267 l = n3;
268 }
269 else
270 if( t == t3 )
271 return( 0 );
272 }
273 /* Cette boucle se finie en interne par un return */
274 }