Jspice3
inpeval.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: 1985 Thomas L. Quarles
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 #include "spice.h"
9 #include <stdio.h>
10 #include <math.h>
11 #include <ctype.h>
12 #include "inpdefs.h"
13 
14 
15 double
16 INPevaluate(line,error,gobble)
17 
18 char ** line;
19 int *error;
20 int gobble; /* non-zero to gobble rest of token, zero to leave it alone */
21 {
22  double mantis;
23  char *token;
24  char *here;
25  char *tmpline;
26  int expo1;
27  int expo2;
28  int sign;
29  int expsgn;
30 
31  /* setup */
32  tmpline = *line;
33  if (gobble) {
34  *error = INPgetTok(line,&token,1);
35  if (*error)
36  return ((double)0.0);
37  }
38  else {
39  token = *line;
40  *error = 0;
41  }
42  mantis = 0;
43  expo1 = 0;
44  expo2 = 0;
45  sign = 1;
46  expsgn = 1;
47 
48  /* loop through all of the input token */
49  here = token;
50  if (*here == '+')
51  /* plus, so do nothing except skip it */
52  here++;
53  if (*here == '-') {
54  /* minus, so skip it, and change sign */
55  here++;
56  sign = -1;
57  }
58  if (*here == '\0' || ((!(isdigit(*here))) && (*here != '.'))) {
59  /* number looks like just a sign! */
60  *error = 1;
61  /* back out the 'gettok' operation */
62  *line = tmpline;
63  if (gobble) {
64  txfree(token);
65  }
66  else {
67  *line = here;
68  }
69  return (0);
70  }
71  while (isdigit(*here)) {
72  /* digit, so accumulate it. */
73  mantis = 10*mantis + (*here - '0');
74  here++;
75  }
76  if (*here == '\0') {
77  /* reached the end of token - done. */
78  if (gobble) {
79  txfree(token);
80  }
81  else {
82  *line = here;
83  }
84  return ((double)mantis*sign);
85  }
86  if (*here == ':') {
87  /* hack for subcircuit node numbering */
88  *error = 1;
89  return (0.0);
90  }
91  /* after decimal point! */
92  if (*here == '.') {
93  /* found a decimal point! */
94  here++; /* skip to next character */
95  if (*here == '\0') {
96  /* number ends in the decimal point */
97  if (gobble) {
98  txfree(token);
99  }
100  else {
101  *line = here;
102  }
103  return ((double)mantis*sign);
104  }
105  while (isdigit(*here)) {
106  /* digit, so accumulate it. */
107  mantis = 10*mantis + (*here - '0');
108  expo1--;
109  if (*here == '\0') {
110  /* reached the end of token - done. */
111  if (gobble) {
112  txfree(token);
113  }
114  else {
115  *line = here;
116  }
117  return (mantis*sign*pow(10.0,(double)expo1));
118  }
119  here++;
120  }
121  }
122  /* now look for "E","e",etc to indicate an exponent */
123  if ((*here == 'E') || (*here == 'e') ||
124  (*here == 'D') || (*here == 'd')) {
125  /* have an exponent, so skip the e */
126  here++;
127  /* now look for exponent sign */
128  if (*here == '+')
129  /* just skip + */
130  here++;
131  if (*here == '-') {
132  /* skip over minus sign */
133  here++;
134  /* and make a negative exponent */
135  expsgn = -1;
136  }
137  /* now look for the digits of the exponent */
138  while (isdigit(*here)) {
139  expo2 = 10*expo2 + (*here - '0');
140  here++;
141  }
142  }
143  /* now we have all of the numeric part of the number, time to
144  * look for the scale factor (alphabetic)
145  */
146  switch(*here) {
147  case 't':
148  case 'T':
149  expo1 += 12;
150  here++;
151  break;
152  case 'g':
153  case 'G':
154  expo1 += 9;
155  here++;
156  break;
157  case 'k':
158  case 'K':
159  expo1 += 3;
160  here++;
161  break;
162  case 'u':
163  case 'U':
164  expo1 -= 6;
165  here++;
166  break;
167  case 'n':
168  case 'N':
169  expo1 -= 9;
170  here++;
171  break;
172  case 'p':
173  case 'P':
174  expo1 -= 12;
175  here++;
176  break;
177  case 'f':
178  case 'F':
179  expo1 -= 15;
180  here++;
181  break;
182  case 'm':
183  case 'M':
184  /* special case for m - may be m or mil or meg */
185  if (*(here+1) != '\0' && *(here+2) != '\0') {
186  /* at least 2 characters, so check them. */
187  if ((*(here+1) == 'E') || (*(here+1) == 'e')) {
188  if ((*(here+2) == 'G') || (*(here+2) == 'g')) {
189  expo1 += 6;
190  here += 3;
191  if (gobble) {
192  txfree(token);
193  }
194  else {
195  *line = here;
196  }
197  return (sign*mantis*
198  pow(10.0,(double)(expo1 + expsgn*expo2)));
199  }
200  }
201  else if ((*(here+1) == 'I') || (*(here+1) == 'i')) {
202  if ( (*(here+2) == 'L') || (*(here+2) == 'l')) {
203  expo1 -= 6;
204  here += 3;
205  mantis = mantis*25.4;
206  if (gobble) {
207  txfree(token);
208  }
209  else {
210  *line = here;
211  }
212  return (sign*mantis*
213  pow(10.0,(double)(expo1 + expsgn*expo2)));
214  }
215  }
216  }
217  /* not either special case, so just m => 1e-3 */
218  expo1 -= 3;
219  here++;
220  break;
221  default:
222  break;
223  }
224  if (gobble) {
225  txfree(token);
226  }
227  else {
228  *line = here;
229  }
230  return (sign*mantis*pow(10.0,(double)(expo1 + expsgn*expo2)));
231 }
double INPevaluate(char **line, int *error, int gobble)
Definition: inpeval.c:16
Definition: fteinp.h:14
void txfree()
int INPgetTok()