Jspice3
string.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: UCB CAD Group
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * String functions
10  */
11 
12 #include "spice.h"
13 #include "misc.h"
14 
15 
16 /***************************************************************************/
17 /* true if p is a leading substring of s.
18  */
19 
20 int
22 
23 char *p, *s;
24 {
25  if (p == NULL || s == NULL)
26  return (false);
27  while (*p && (*p == *s))
28  p++, s++;
29  if (!*p)
30  return (true);
31  else
32  return (false);
33 }
34 
35 
36 /***************************************************************************/
37 /* Create a copy of a string.
38  */
39 
40 char *
41 copy(str)
42 
43 char *str;
44 {
45  char *p;
46 
47  if (str == NULL)
48  return (NULL);
49  p = tmalloc(strlen(str) + 1);
50  (void) strcpy(p, str);
51  return(p);
52 }
53 
54 
55 /***************************************************************************/
56 /* Determine whether sub is a substring of str.
57  */
58 
59 int
60 substring(sub, str)
61 
62 char *str, *sub;
63 {
64  char *s;
65 
66  if (str == NULL || sub == NULL)
67  return (false);
68  while (*str) {
69  if (*str == *sub) {
70  for (s = sub; *s; s++) {
71  if (!*str || (*s != *str++))
72  break;
73  }
74  if (*s == '\0')
75  return (true);
76  }
77  str++;
78  }
79  return (false);
80 }
81 
82 
83 /***************************************************************************/
84 /* Append one character to a string. Don't check for overflow.
85  */
86 
87 void
89 
90 char *s, c;
91 {
92  if (s) {
93  while (*s)
94  s++;
95  *s++ = c;
96  *s = '\0';
97  }
98  return;
99 }
100 
101 
102 /***************************************************************************/
103 /* Try to identify an integer that begins a string. Stop when a non-
104  * numeric character is reached.
105  */
106 
107 int
109 
110 char *str;
111 {
112  int i = 0;
113 
114  if (str)
115  while (isdigit(*str))
116  i = i * 10 + *(str++) - '0';
117  return(i);
118 }
119 
120 
121 /***************************************************************************/
122 /* Case insensitive str eq.
123  */
124 
125 int
127 
128 char *p, *s;
129 {
130  if (p == NULL || s == NULL)
131  return (false);
132  while (*p) {
133  if ((isupper(*p) ? tolower(*p) : *p) !=
134  (isupper(*s) ? tolower(*s) : *s))
135  return (false);
136  p++;
137  s++;
138  }
139  return (*s ? false : true);
140 }
141 
142 
143 /***************************************************************************/
144 /* Case insensitive prefix.
145  */
146 
147 int
149 
150 char *p, *s;
151 {
152  if (p == NULL || s == NULL)
153  return (false);
154  while (*p) {
155  if ((isupper(*p) ? tolower(*p) : *p) !=
156  (isupper(*s) ? tolower(*s) : *s))
157  return (false);
158  p++;
159  s++;
160  }
161  return (true);
162 }
163 
164 
165 /***************************************************************************/
166 /* Convert str to lower case.
167  */
168 
169 void
171 
172 char *str;
173 {
174  if (str)
175  while (*str) {
176  *str = tolower(*str);
177  str++;
178  }
179 }
180 
181 
182 /***************************************************************************/
183 /* Return a malloc'ed token, advance the pointer to next token.
184  */
185 
186 char *
188 
189 char **s;
190 {
191  char buf[BSIZE_SP];
192  int i = 0;
193 
194  if (s == NULL || *s == NULL)
195  return (NULL);
196  while (isspace(**s))
197  (*s)++;
198  if (!**s)
199  return (NULL);
200  while (**s && !isspace(**s))
201  buf[i++] = *(*s)++;
202  buf[i] = '\0';
203  while (isspace(**s))
204  (*s)++;
205  return (copy(buf));
206 }
207 
208 
209 /***************************************************************************/
210 /* Copy token to caller's buffer dst, advance the pointer to next token.
211  * Returns true if token is valid.
212  */
213 
214 int
215 copytok(dst,s)
216 
217 char *dst,**s;
218 {
219  if (s == NULL || *s == NULL)
220  return (0);
221  while (isspace(**s))
222  (*s)++;
223  if (!**s)
224  return (0);
225  while (**s && !isspace(**s))
226  *dst++ = *(*s)++;
227  *dst = '\0';
228  while (isspace(**s))
229  (*s)++;
230  return (1);
231 }
232 
233 
234 /***************************************************************************/
235 /* Copy token to caller's buffer dst, advance the pointer to next token.
236  * Leave a space after the copied token.
237  * Returns true if token is valid.
238  */
239 
240 int
242 
243 char *dst,**s;
244 {
245  if (s == NULL || *s == NULL)
246  return (0);
247  while (isspace(**s))
248  (*s)++;
249  if (!**s)
250  return (0);
251  while (**s && !isspace(**s))
252  *dst++ = *(*s)++;
253  *dst++ = ' ';
254  *dst++ = '\0';
255  while (isspace(**s))
256  (*s)++;
257  return (1);
258 }
259 
260 
261 /***************************************************************************/
262 /* Advance the pointer to next token.
263  */
264 
265 void
267 
268 char **s;
269 {
270  int i = 0;
271 
272  if (s == NULL || *s == NULL)
273  return;
274  while (isspace(**s))
275  (*s)++;
276  if (!**s)
277  return;
278  while (**s && !isspace(**s))
279  (*s)++;
280  while (isspace(**s))
281  (*s)++;
282  return;
283 }
284 
285 
286 /***************************************************************************/
287 /* Compatibility routines. */
288 /***************************************************************************/
289 #ifndef HAVE_INDEX
290 
291 #ifndef index
292 
293 char *
295 
296 const char *s;
297 char c;
298 {
299  while ((*s != c) && (*s != '\0'))
300  s++;
301  if (*s == '\0')
302  return ((char *) 0);
303  else
304  return ((char*)s);
305 }
306 
307 #endif
308 
309 
310 #ifndef rindex
311 
312 char *
314 
315 const char *s;
316 char c;
317 {
318  char *t;
319 
320  for (t = (char*)s; *t != '\0'; t++);
321  while ((*t != c) && (t != (char*)s))
322  t--;
323  if (t == (char*)s)
324  return ((char *) 0);
325  else
326  return (t);
327 }
328 
329 #endif
330 #endif
331 
332 
333 /***************************************************************************/
334 #ifndef HAVE_BCOPY
335 
336 #ifndef bcopy
337 
338 void
339 bcopy(from, to, num)
340 
341 char *from, *to;
342 int num;
343 {
344  while (num-- > 0)
345  *to++ = *from++;
346  return;
347 }
348 
349 #endif
350 
351 
352 #ifndef bzero
353 
354 /* can't declare void here, because we've already used it in this file */
355 /* and haven't declared it void before the use */
356 int
357 bzero(ptr, num)
358 
359 char *ptr;
360 int num;
361 {
362  while (num-- > 0)
363  *ptr++ = '\0';
364  return (0);
365 }
366 
367 #endif
368 #endif
369 /***************************************************************************/
370 
371 /* This might not be around. If not then forget about sorting. */
372 
373 #ifndef HAVE_QSORT
374 #ifndef qsort
375 qsort() {}
376 #endif
377 #endif
378 
379 /***************************************************************************/
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
int copytok1(char *dst, char **s)
Definition: string.c:241
int substring(char *sub, char *str)
Definition: string.c:60
char * strcpy()
Definition: cddefs.h:119
FILE * p
Definition: proc2mod.c:48
Definition: cddefs.h:215
int bzero(char *ptr, int num)
Definition: string.c:357
char * copy(char *str)
Definition: string.c:41
char * tmalloc()
char * gettok(char **s)
Definition: string.c:187
int copytok(char *dst, char **s)
Definition: string.c:215
#define NULL
Definition: spdefs.h:121
int prefix(char *p, char *s)
Definition: string.c:21
void advtok(char **s)
Definition: string.c:266
void strtolower(char *str)
Definition: string.c:170
static double c
Definition: vectors.c:16
int cieq(char *p, char *s)
Definition: string.c:126
char * ptr
Definition: output.c:153
qsort()
Definition: string.c:375
Definition: cddefs.h:177
void appendc(char *s, char c)
Definition: string.c:88
int ciprefix(char *p, char *s)
Definition: string.c:148
char * rindex(char *s, char c) const
Definition: string.c:313
char * index(char *s, char c) const
Definition: string.c:294
void bcopy(char *from, char *to, int num)
Definition: string.c:339
Definition: cddefs.h:192
int scannum(char *str)
Definition: string.c:108