Debian Codegraph @

prim/tc3/libsrc/str1.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++
29 .MODULE str1.c
30 .AUTHOR Francois Ochsenbein [ESO]
31 .LANGUAGE C
32 .CATEGORY Basic string manipulation
33
34 .COMMENTS
35 \begin{TeX}
36 Function names start with {\bf str} for case-sensitive functions,
37 with {\bf stu} for case-insensitive functions.
38 Declarations and corresponding macro definitions are in the header
39 STR.H.
40 The general features of all these functions are:
41 \begin{itemize}
42 \item The first argument is always a pointer to the string to scan or modify;
43 \item All functions in this module return an integer, which is
44 \begin{itemize}
45 \item either the length
46 \item or an index, with values 0 to (length-1) for success,
47 length for failure (mismatch).
48 It is then easy to test the found byte, as in the
49 following example:
50 \begin{verbatim}
51 char s[80];
52 if (s[strloc(s,'.')])
53 printf("s contains a dot!");
54 \end{verbatim}
55 \item or the result of a comparison
56 \end{itemize}
57 \item The function name is followed by an underscore `\_' when a table is
58 required as the last parameter. The usage of 256-byte tables for
59 span / scan / translate operations allow higher speed.
60 \end{itemize}
61 \end{TeX}
62
63 .VERSION 1.0 25-Nov-1985: Creation
64
65 .VERSION 2.0 26-Jan-1988: Prepared for Midas, on top of osc routines.
66 .VERSION 2.1 28-Mar-1989: Removed bug in stritem (if 2. string has length 0)
67 .VERSION 2.2 12-May-1989: Added strncopy.
68 .VERSION 2.3 16-Jun-1989: Check for null addresses
69 .VERSION 2.4 31-Jan-1990: Removed strid (now in str2). Cosmetic modifications
70 Added strloc1, strscan1
71 .VERSION 2.5 30-Mar-1990: Added strred (replace macro),
72 .VERSION 2.6 11-Sep-1990: Added strins (Insert)
73
74 0900831 last modif
75 ----------------------------------------------------------------------------*/
76
77 #include <macrogen.h>
78 #include <atype.h>
79 #include <stdlib.h>
80 #include <string.h>
81
82 #include <midas_def.h>
83
84 #define BITS_PER_CHAR 8
85 #define SIZE (1<<BITS_PER_CHAR)
86
87 int strlower(), stuscans(), stubscans(), stuspans(), stubspans();
88
89
90
91 static unsigned char ttable[SIZE];
92 static char argstr[2] = "x"; /* for stuloc / stuskip */
93 static int (*f)();
94
95 static unsigned char *cpt(table, tabl0)
96 /*+++++++
97 .PURPOSE Copy table, inserting tabl0
98 .RETURNS Address of Copy
99 .REMARKS Copy because main_ascii is read-only ... can't just modify first byte
100 ---------*/
101 unsigned char *table; /* IN: Table to copy */
102 unsigned char tabl0; /* IN: Value of tabl[0] */
103 {
104 oscopy((char *)ttable, (char *)table, SIZE);
105 ttable[0] = tabl0;
106
107 return(ttable);
108 }
109
110 /*===========================================================================
111 * Copy strings
112 *===========================================================================*/
113 int strcopy(dest, source)
114 /*+++++++
115 .PURPOSE Copy strings, taking care of possible overlays.
116 .RETURNS Length of destination.
117 ---------*/
118 char *dest; /* OUT: destination string */
119 char *source; /* IN: source string */
120 {
121 register int len;
122
123 if (source)
124 len = strlen(source), oscopy(dest, source, len+1);
125 else *dest = '\0', len = 0;
126
127 return(len);
128 }
129
130 int strncopy(dest, maxsize, source)
131 /*+++++++
132 .PURPOSE Copy strings, taking care of possible overlays, but with max size.
133 (forbid any overfill)
134 .RETURNS Length of destination.
135 ---------*/
136 char *dest; /* OUT: destination string */
137 int maxsize; /* IN: Size of destination buffer (1+max.length) */
138 char *source; /* IN: source string */
139 {
140 register char *p;
141 register int len;
142
143 p = dest;
144
145 if (maxsize)
146 {
147 if (source)
148 {
149 len = strlen(source) + 1;
150 if (len > maxsize) len = maxsize;
151 len--;
152 p += oscopy(p, source, len);
153 }
154 *p = '\0';
155 }
156
157 return(p-dest);
158 }
159
160 #if 0
161 int strcat(dest, source)
162 /*+++++++
163 .PURPOSE Append source to dest
164 .RETURNS Length of destination (strlen(dest))
165 ---------*/
166 char *dest; /* OUT: destination string */
167 char *source; /* IN: source string */
168 {
169 register int len;
170 register char *pd;
171
172 pd = dest + strlen(dest);
173
174 if (source)
175 len = strlen(source), oscopy(dest, source, len+1);
176 return(pd - dest);
177 }
178
179 int strncat(dest, maxsize, source)
180 /*+++++++
181 .PURPOSE Append source to dest
182 .RETURNS Length of destination (strlen(dest))
183 ---------*/
184 char *dest; /* OUT: destination string */
185 int maxsize; /* IN: Size of destination buffer (1+max.length) */
186 char *source; /* IN: source string */
187 {
188 register int len;
189 register char *pd;
190
191 len = strlen(dest);
192 len += strncopy (dest+len, maxsize-len, source);
193 return(len);
194 }
195 #endif
196
197 int strfill(dest, len, filler)
198 /*+++++++
199 .PURPOSE Fill a string with specified char, and terminates with EOS.
200 .RETURNS Length of destination.
201 ---------*/
202 char *dest; /* OUT: destination string */
203 int len; /* IN: Size of destination buffer */
204 int filler; /* IN: Character to be used for filling */
205 {
206 *(dest+oscfill(dest,len,filler)) = '\0';
207 return(len);
208 }
209
210 /*===========================================================================
211 * Locate / Skip a char
212 *===========================================================================*/
213 int strloc(str, c)
214 /*+++++++
215 .PURPOSE Locate the first occurence of character `c'
216 .RETURNS Index of `c' in str; length of str if `c' not found.
217 ---------*/
218 char *str; /* IN: string to scan */
219 char c; /* IN: char to locate */
220 {
221 register char *p;
222
223 for (p=str; *p; p++)
224 if (*p == c) break;
225 return(p-str);
226 }
227
228 int strloc1(str, c)
229 /*+++++++
230 .PURPOSE Locate the first occurence of character `c' --- but only if `c'
231 is not escaped by a backslash
232 .RETURNS Index of `c' in str; length of str if `c' not found.
233 ---------*/
234 char *str; /* IN: string to scan */
235 char c; /* IN: char to locate */
236 {
237 register char *p;
238
239 for (p=str; *p; p++)
240 {
241 if (*p == '\\') { p++; continue; }
242 if (*p == c) break;
243 }
244 return(p-str);
245 }
246
247 int strbloc(str, c)
248 /*+++++++
249 .PURPOSE Locate the last occurence of character `c'
250 .RETURNS Index of `c' in str; -1 if `c' not found.
251 ---------*/
252 char *str; /* IN: string to scan */
253 char c; /* IN: char to locate */
254 {
255 return(oscbloc(str,strlen(str),c));
256 }
257
258 /*===========================================================================*/
259 int stuloc(str, c)
260 /*+++++++
261 .PURPOSE Locate the first occurence of character `c', case insensitive.
262 .RETURNS Index of `c' or `C' in str; length of str if `c' not found.
263 ---------*/
264 char *str; /* IN: string to scan */
265 char c; /* IN: char to locate */
266 {
267 argstr[0] = c;
268 return(stuscans(str, argstr));
269 }
270
271
272 int stubloc(str, c)
273 /*+++++++
274 .PURPOSE Locate the last occurence of character `c', case insensitive.
275 .RETURNS Index of `c' or `C' in str; -1 if `c' not found.
276 ---------*/
277 char *str; /* IN: string to scan */
278 char c; /* IN: char to locate */
279 {
280 argstr[0] = c;
281 return(stubscans(str, argstr));
282 }
283
284 /*===========================================================================*/
285 int strskip(str, c)
286 /*+++++++
287 .PURPOSE Locate the first character that differs from `c'
288 .RETURNS Index of `c' in str; length of str if str is made only of `c's
289 ---------*/
290 char *str; /* IN: string to scan */
291 char c; /* IN: char to locate */
292 {
293 register char *p;
294
295 for (p=str; *p; p++)
296 if (*p != c) break;
297 return(p-str);
298 }
299
300 int strbskip(str, c)
301 /*+++++++
302 .PURPOSE Locate the last character that differs from `c'
303 .RETURNS Index of `c' in str; -1 if str is made only of `c's
304 ---------*/
305 char *str; /* IN: string to scan */
306 char c; /* IN: char to locate */
307 {
308 return(oscbskip(str, strlen(str), c));
309 }
310
311 /*===========================================================================*/
312 int stuskip(str, c)
313 /*+++++++
314 .PURPOSE Locate the first character that differs from `c', case insensitive.
315 .RETURNS Index of `c' in str; length of str if str is made only of `c's
316 ---------*/
317 char *str; /* IN: string to scan */
318 char c; /* IN: char to locate */
319 {
320 argstr[0] = c;
321 return(stuspans(str, argstr));
322 }
323
324 int stubskip(str, c)
325 /*+++++++
326 .PURPOSE Locate the last character that differs from `c', case insensitive.
327 .RETURNS Index of `c' in str; -1 if str is made only of `c's
328 ---------*/
329 char *str; /* IN: string to scan */
330 char c; /* IN: char to locate */
331 {
332 argstr[0] = c;
333 return(stubspans(str, argstr));
334 }
335
336 /*===========================================================================
337 * Scan / Span strings
338 *===========================================================================*/
339
340 int strset(table, list)
341 /*+++++++
342 .PURPOSE Prepare table for scan / span operations: value of 1 for characters
343 in list, value 0 for other characters.
344 .RETURNS Length of list
345 ---------*/
346 unsigned char *table; /* OUT: Table [256] with flagged characters */
347 char *list; /* IN: list of characters to flag */
348 {
349 register char *p;
350
351 oscfill((char *)table, SIZE, 0);
352 for (p=list; *p; p++) *(table + *(unsigned char *)p) = 1;
353
354 return(p-list);
355 }
356
357 /*===========================================================================*/
358 int stuset(table, list)
359 /*+++++++
360 .PURPOSE Prepare table for scan / span operations: value of 1 for characters
361 in list, value 0 for other characters. Both upper and lower case
362 are inserted in table.
363 .RETURNS Length of list
364 ---------*/
365 unsigned char *table; /* OUT: Table with flagged characters */
366 char *list; /* IN: list of characters to flag */
367 {
368 register char *p;
369
370 oscfill((char *)table, SIZE, 0);
371 for (p=list; *p; p++)
372 { *(table + *(unsigned char *)p) = 1;
373 *(table + (unsigned)tocase(*p)) = 1;
374 }
375
376 return(p-list);
377 }
378
379 /*===========================================================================*/
380 int strspan_ (str, mask, table)
381 /*+++++++
382 .PURPOSE Match as many as possible of flagged (non-zero in table) chars.
383 .RETURNS Index of first character which is not flagged;
384 length of str if all characters are flagged.
385 .REMARKS Table can be created with strset.
386 If table[0] is not null, the result may be wrong.
387 ---------*/
388 char *str; /* IN: address of string to span */
389 unsigned char mask; /* IN: attribute mask */
390 unsigned char *table; /* IN: attribute table */
391 {
392 if (table[0] & mask) table = cpt(table, 0); /* EOS MUST end */
393 return(oscspan((unsigned char *)str, strlen(str), mask, table));
394 }
395
396 int strbspan_ (str, mask, table)
397 /*+++++++
398 .PURPOSE Match as many as possible of flagged chars,
399 starting from end (backwards matching)
400 .RETURNS Index of first character which is not flagged (RIHT to LEFT); -1
401 if all characters are flagged.
402 .REMARKS Table can be created with strset
403 ---------*/
404 char *str; /* IN: address of string to span */
405 unsigned char mask; /* IN: attribute mask */
406 unsigned char *table; /* IN: attribute table */
407 {
408 return(oscbspan((unsigned char *)str, strlen(str), mask, table));
409 }
410
411 /*===========================================================================*/
412 int strspans (str, list)
413 /*+++++++
414 .PURPOSE Match as many as possible chars specified in list.
415 .RETURNS Index of first character which is not in list;
416 length of str if all characters are in list.
417 ---------*/
418 char *str; /* IN: address of string to span */
419 char *list; /* IN: list of matching characters */
420 {
421 strset(ttable, list); /* ttable[0] is zero */
422 return(oscspan((unsigned char *)str, strlen(str), 1, ttable));
423 }
424
425 /*===========================================================================*/
426 int stuspans (str, list)
427 /*+++++++
428 .PURPOSE Match as many as possible chars specified in list, case insensitive.
429 .RETURNS Index of first character which is not in list;
430 length of str if all characters are in list.
431 .REMARK To span e.g. alphabetic chars, use macro strspan(str, _ALPHA_)
432 ---------*/
433 char *str; /* IN: address of string to span */
434 char *list; /* IN: list of matching characters */
435 {
436 stuset(ttable, list);
437 return(oscspan((unsigned char *)str, strlen(str), 1, ttable));
438 }
439
440 /*===========================================================================*/
441 int strbspans (str, list)
442 /*+++++++
443 .PURPOSE Match as many as possible chars specified in list,
444 looking from the end of the string.
445 .RETURNS Index of last character which is not in list; -1 if
446 all characters are in list.
447 ---------*/
448 char *str; /* IN: address of string to span */
449 char *list; /* IN: list of matching characters */
450 {
451 strset(ttable, list);
452 return(oscbspan((unsigned char *)str, strlen(str), 1, ttable));
453 }
454
455 /*===========================================================================*/
456 int stubspans (str, list)
457 /*+++++++
458 .PURPOSE Match as many as possible chars specified in list, case insensitive,
459 looking from the end of the string.
460 .RETURNS Index of last character which is not in list; -1 if
461 all characters are in list.
462 ---------*/
463 char *str; /* IN: address of string to span */
464 char *list; /* IN: list of matching characters */
465 {
466 stuset(ttable, list);
467 return(oscbspan((unsigned char *)str, strlen(str), 1, ttable));
468 }
469
470
471 /*===========================================================================*/
472 int strscan_ (str, mask, table)
473 /*+++++++
474 .PURPOSE Look for first flagged (non-zero in table) char.
475 .RETURNS Index of first flagged character; length of str if none was found.
476 .REMARKS Table can be created with strset
477 ---------*/
478 char *str; /* IN: address of string to span */
479 unsigned char mask; /* IN: attribute mask */
480 unsigned char *table; /* IN: attribute table */
481 {
482 if_not(table[0] & mask)
483 table = cpt(table, mask); /* EOS MUST end */
484
485 return(oscscan((unsigned char *)str, strlen(str), mask, table));
486 }
487
488 int strbscan_ (str, mask, table)
489 /*+++++++
490 .PURPOSE Look for last flagged (non-zero in table) char.
491 .RETURNS Index of last flagged character, -1 if none was found.
492 .REMARKS Table can be created with strset
493 ---------*/
494 char *str; /* IN: address of string to span */
495 unsigned char mask; /* IN: attribute mask */
496 unsigned char *table; /* IN: attribute table */
497 {
498 return(oscbscan((unsigned char *)str, strlen(str), mask, table));
499 }
500
501 /*===========================================================================*/
502 int strscans (str, list)
503 /*+++++++
504 .PURPOSE Look for first char specified in list.
505 .RETURNS Index of first character in str which is in list;
506 length of str if none was found.
507 .REMARK To scan e.g. alphabetic chars, use macro strscan(str, _ALPHA_)
508 ---------*/
509 char *str; /* IN: address of string to span */
510 char *list; /* IN: list of matching characters */
511 {
512 strset(ttable, list); ttable[0] = 1;
513 return(oscscan((unsigned char *)str, strlen(str), 1, ttable));
514 }
515
516 int strscan1 (str, list)
517 /*+++++++
518 .PURPOSE Look for first char specified in list, only if not escaped
519 by a backslash
520 .RETURNS Index of first character in str which is in list;
521 length of str if none was found.
522 ---------*/
523 char *str; /* IN: address of string to span */
524 char *list; /* IN: list of matching characters */
525 {
526 char *p;
527
528 strset(ttable, list); ttable[0] = 1; ttable['\\'] = 1;
529
530 for (p = str; *p; )
531 { p += oscscan((unsigned char *)p, strlen(p), 1, ttable);
532 if (*p != '\\') break;
533 p += 2;
534 }
535 return(p - str);
536 }
537
538 /*===========================================================================*/
539 int stuscans (str, list)
540 /*+++++++
541 .PURPOSE Look for first flagged (non-zero in table) char, case insensitive
542 .RETURNS Index of first character in str which is in list;
543 length of str if none was found.
544 ---------*/
545 char *str; /* IN: address of string to scan */
546 char *list; /* IN: list of matching characters */
547 {
548 stuset(ttable, list); ttable[0] = 1;
549 return(oscscan((unsigned char *)str, strlen(str), 1, ttable));
550 }
551
552 /*===========================================================================*/
553 int strbscans (str, list)
554 /*+++++++
555 .PURPOSE Retrieve last char specified in list.
556 .RETURNS Index of last char in str which is in list, -1 if none was found.
557 ---------*/
558 char *str; /* IN: address of string to span */
559 char *list; /* IN: list of matching characters */
560 {
561 strset(ttable, list);
562 return(oscbscan((unsigned char *)str, strlen(str), 1, ttable));
563 }
564
565 /*===========================================================================*/
566 int stubscans (str, list)
567 /*+++++++
568 .PURPOSE Retrieve last char specified in list, case insensitive.
569 .RETURNS Index of last char in str which is in list, -1 if none was found.
570 ---------*/
571 char *str; /* IN: address of string to scan */
572 char *list; /* IN: list of matching characters */
573 {
574 stuset(ttable, list);
575 return(oscbscan((unsigned char *)str, strlen(str), 1, ttable));
576 }
577
578 /*===========================================================================
579 * String Comparisons
580 *===========================================================================*/
581
582 int strcomp(s1,s2)
583 /*++++++++++++++++++++++++
584 .PURPOSE Compare (or compute difference of) two strings.
585 .RETURNS A positive value if s1 > s2, zero if strings are identical,
586 a negative value if s1 < s2.
587 -----------*/
588 char *s1; /* IN: address of first string */
589 char *s2; /* IN: address of 2nd string */
590 {
591 register char *p, *q;
592
593 for (p=s1, q=s2; *p == *q; p++, q++)
594 if (!(*p)) break;
595
596 return(*p - *q);
597 }
598
599 /*===========================================================================*/
600 int stucomp(s1,s2)
601 /*++++++++++++++++++++++++
602 .PURPOSE Compare (or compute difference of) two strings, case insensitive.
603 .RETURNS A positive value if s1 > s2, zero if strings are identical,
604 a negative value if s1 < s2.
605 -----------*/
606 char *s1; /* IN: address of first string */
607 char *s2; /* IN: address of 2nd string */
608 {
609 char *p, *q;
610 char cp, cq;
611
612 for (p=s1, q=s2; ; p++, q++)
613 { cp = toupper(*p), cq = toupper(*q);
614 if (cp != cq) break;
615 if (!cp) break;
616 }
617
618 return(cp - cq);
619 }
620
621 /*===========================================================================*/
622 int strmatch(s1,s2)
623 /*++++++++++++++++++++++++
624 .PURPOSE Look for longest matching string.
625 .RETURNS The number of characters common to s1 and s2. If strings are identical,
626 the common length of both strings.
627 -----------*/
628 char *s1; /* IN: address of first string */
629 char *s2; /* IN: address of 2nd string */
630 {
631 register char *p, *q;
632
633 for (p=s1, q=s2; *p == *q; p++, q++)
634 if (!(*p)) break;
635
636 return(p - s1);
637 }
638
639 /*===========================================================================*/
640 int stumatch(s1,s2)
641 /*++++++++++++++++++++++++
642 .PURPOSE Look for longest matching string, case insensitive.
643 .RETURNS The number of characters common to s1 and s2. If strings are identical,
644 the common length of both strings.
645 -----------*/
646 char *s1; /* IN: address of first string */
647 char *s2; /* IN: address of 2nd string */
648 {
649 char *p, *q;
650 char cp, cq;
651
652 for (p=s1, q=s2; ; p++, q++)
653 { cp = toupper(*p), cq = toupper(*q);
654 if (cp != cq) break;
655 if (!cp) break;
656 }
657
658 return(p - s1);
659 }
660
661 /*===========================================================================*/
662 int strindex( s1, s2)
663 /*++++++++++++++++++++++++
664 .PURPOSE Locates a substring within a string
665 .RETURNS Index within first string of second string;
666 length of the first string for mismatch.
667 .METHOD Use a scan on first char, then compare.
668 -----------*/
669 char *s1; /* IN: address of first string (source) */
670 char *s2; /* IN: address of 2nd string (object to find) */
671 {
672 register char *s;
673
674 s = strstr(s1,s2);
675 if (s == NULL) return(strlen(s1));
676 else return(s-s1);
677
678 }
679
680 /*===========================================================================*/
681 int stuindex( s1, s2)
682 /*++++++++++++++++++++++++
683 .PURPOSE Locates a substring within a string, case insensitive
684 .RETURNS Index within first string of second string;
685 length of the first string for mismatch.
686 .METHOD Use a scan on first char, then compare.
687 -----------*/
688 char *s1; /* IN: address of first string (source) */
689 char *s2; /* IN: address of 2nd string (object to find) */
690 {
691 register char *p1, *p2, *s;
692 register int result;
693
694 p1 = strcpy(malloc((size_t)(strlen(s1)+1)),s1);
695 p2 = strcpy(malloc((size_t)(strlen(s2)+1)),s2);
696
697 strlower(p1);
698 strlower(p2);
699
700 s = strstr(p1,p2);
701
702 if (s == NULL) result = strlen(s1);
703 else result = s-p1;
704
705 free(p1);
706 free(p2);
707
708 return(result);
709
710 }
711
712 /*===========================================================================*/
713 static int _item( s1, s2, sep)
714 /*++++++++++++++++++++++++
715 .PURPOSE Locates a substring within a string, which must be followed / preceded
716 by separator(s) characters
717 .RETURNS Index within first string of second string. The index
718 is the length of the first string for mismatch.
719 .METHOD Use strindex, then check preceding / following char.
720 -----------*/
721 char *s1; /* IN: address of first string (source) */
722 char *s2; /* IN: address of 2nd string (object to find) */
723 char *sep; /* IN: list of separators */
724 {
725 register char *p, *p1;
726 register int i;
727
728 i = strlen(s2);
729 if (i == 0) return(0);
730
731 for (p = p1 = s1; ; p1 += i)
732 { p1 += (*f)(p1, s2); /* Index of s2 in p1 */
733 if_not(*p1) break; /* Not Found... */
734 if (p1 > s1) /* Check preceding char */
735 { p = p1-1;
736 if_not(sep[strloc(sep, *p)])
737 continue;
738 }
739 p = p1 + i; /* Check following char */
740 if_not(*p) break; /* Located at end : OK */
741 if (sep[strloc(sep, *p)]) /* Separator found */
742 break;
743 }
744
745 return(p1-s1);
746 }
747
748 /*===========================================================================*/
749 int stritem( s1, s2, sep)
750 /*++++++++++++++++++++++++
751 .PURPOSE Locates a substring within a string, which must be followed / preceded
752 by separator(s) characters
753 .RETURNS Index within first string of second string;
754 length of the first string for mismatch.
755 .METHOD Use strindex, then check preceding / following char.
756 -----------*/
757 char *s1; /* IN: address of first string (source) */
758 char *s2; /* IN: address of 2nd string (object to find) */
759 char *sep; /* IN: list of separators */
760 {
761 int strindex();
762
763 f = strindex;
764
765 return(_item(s1,s2,sep));
766 }
767
768 /*===========================================================================*/
769 int stuitem( s1, s2, sep)
770 /*++++++++++++++++++++++++
771 .PURPOSE Locates a substring within a string, which must be followed / preceded
772 by separator(s) characters, case insensitive.
773 .RETURNS Index within first string of second string;
774 length of the first string for mismatch.
775 .METHOD Use stuindex, then check preceding / following char.
776 -----------*/
777 char *s1; /* IN: address of first string (source) */
778 char *s2; /* IN: address of 2nd string (object to find) */
779 char *sep; /* IN: list of separators */
780 {
781 int stuindex();
782
783 f = stuindex;
784
785 return(_item(s1,s2,sep));
786 }
787
788 /*===========================================================================
789 * String Conversions
790 *===========================================================================*/
791
792 int strsetr (table, s1, s2)
793 /*+++++++
794 .PURPOSE Create a table for character translations.
795 .RETURNS Length of s1.
796 .REMARKS Characters in s1 without counterpart in s2 (i.e. s1 is longer
797 than s2) are replaced by a tilde (~).
798 ---------*/
799 unsigned char *table; /* OUT: translation table[256] */
800 char *s1; /* IN: list characters to translate */
801 char *s2; /* IN: list of translated characters */
802 {
803 register char *p, *q;
804 register int i;
805
806 for (i=0; i<SIZE; i++) *(table + i) = i; /* Fill table */
807 for (p=s1, q=s2; *p; )
808 *(table + *(p++)) = (*q ? *(q++) : '~');
809
810 return(p-s1);
811 }
812
813 /*===========================================================================*/
814 int strtr_ (dest, source, table)
815 /*+++++++
816 .PURPOSE Translate source into destination, according to translation table.
817 .RETURNS Length of source / destination
818 .REMARKS The translation table may be created by strsetr.
819 ---------*/
820 char *dest; /* OUT: the translated string */
821 char *source; /* IN: the string to translate */
822 unsigned char *table; /* IN: translation table */
823 {
824 return(osctr((unsigned char *)dest ,(unsigned char *)source, strlen(source)+1, table));
825 }
826
827 /*===========================================================================*/
828 int strtrs (dest, source, s1, s2)
829 /*+++++++
830 .PURPOSE Translate source into destination, transforming characters in s1
831 into the corresponding character in s2.
832 .RETURNS Length of source / dest.
833 .REMARKS Characters in s1 without counterpart in s2 (i.e. s1 is longer
834 than s2) are replaced by a tilde (~).
835 The strings may overlap.
836 ---------*/
837 char *dest; /* OUT: the translated string */
838 char *source; /* IN: the string to translate */
839 char *s1; /* IN: list characters to translate */
840 char *s2; /* IN: list of translated characters */
841 {
842 register int len;
843
844 strsetr(ttable, s1, s2);
845 len = strlen(source);
846 osctr((unsigned char *)dest, (unsigned char *)source, len+1, ttable);
847 return(len);
848 }
849
850 /*===========================================================================
851 * Change Cases
852 *===========================================================================*/
853 int strlower ( str )
854 /*+++++++++
855 .PURPOSE Converts (in place) a string to lower case
856 .RETURNS Length of string
857 --------*/
858 char *str; /* MOD: starting address */
859 {
860 register char *p;
861
862 for (p = str; *p; p++)
863 *p = tolower(*p);
864
865 return(p-str);
866 }
867
868 /*===========================================================================*/
869 int strupper ( str )
870 /*+++++++++
871 .PURPOSE Converts (in place) a string to upper case
872 .RETURNS Length of string
873 --------*/
874 char *str; /* MOD: starting address */
875 {
876 register char *p;
877
878 for (p = str; *p; p++)
879 *p = toupper(*p);
880
881 return(p-str);
882 }
883
884 /*===========================================================================*/
885 int strcase ( str )
886 /*+++++++++
887 .PURPOSE Changes (in place) the case of a string, i.e. uppercase to lowercase
888 and vice-versa.
889 .RETURNS Length of string
890 --------*/
891 char *str; /* MOD: starting address */
892 {
893 register char *p;
894
895 for (p = str; *p; p++)
896 *p = tocase(*p);
897
898 return(p-str);
899 }
900
901 /*===========================================================================
902 * Reduce a string
903 *===========================================================================*/
904
905 int strdel_ (str, mask, table)
906 /*+++++++
907 .PURPOSE Delete all chars flagged in table
908 .RETURNS New Length of string
909 .REMARKS The string is EOS-terminated.
910 ---------*/
911 char *str; /* MOD: string to modify */
912 unsigned char mask; /* IN: Mask to use (ANDed with table) */
913 unsigned char *table; /* IN: table of flags */
914 {
915 register char *p, *q;
916
917 for ( p=str, q=p; *p; p++)
918 if (!(ischar_(*p, mask, table))) *(q++) = *p;
919
920 *q = EOS;
921
922 return(q-str);
923 }
924
925 /*===========================================================================*/
926 int strred_ (str, mask, table)
927 /*+++++++++++
928 .PURPOSE Reduces (in place) a string with suppression of redundant flagged
929 characters (e.g. spaces).
930 Leading and trailing spaces are also removed.
931 .RETURNS Length of modified (shorter) string.
932 -----------*/
933 char *str; /* MOD: string to modify */
934 unsigned char mask; /* IN: Mask to use (ANDed with table) */
935 unsigned char *table; /* IN: table of flags */
936 {
937 register char *p, *q;
938 char this_char, prec_char;
939
940 this_char = prec_char = mask; /* for a suppression of leading spaces */
941
942 for (p=str, q=p; *p; prec_char = this_char, p++)
943 { this_char = ischar_(*p, mask, table);
944 if (prec_char && this_char) continue;
945 *(q++) = *p;
946 }
947 /* suppress last char if = fill */
948 if(prec_char && (q != str)) q--;
949 *q = EOS;
950
951 return(q-str);
952 }
953
954 /*===========================================================================*/
955 int strred (str)
956 /*+++++++++++
957 .PURPOSE Reduces (in place) a string with suppression of redundant spaces.
958 Leading and trailing spaces are also removed.
959 .RETURNS Length of modified (shorter) string.
960 -----------*/
961 char *str; /* MOD: string to modify */
962 {
963 register char *p, *q;
964 char this_char, prec_char;
965
966 this_char = prec_char = ' '; /* for a suppression of leading spaces */
967
968 for (p=str, q=p; *p; prec_char = this_char, p++)
969 { this_char = (isspace(*p) ? ' ' : *p);
970 if ((prec_char == ' ') && (this_char == ' ')) continue;
971 *(q++) = *p;
972 }
973 /* suppress last char if = fill */
974 if((prec_char == ' ') && (q != str)) q--;
975 *q = EOS;
976
977 return(q-str);
978 }
979
980 /*===========================================================================*/
981 int strred1 ( str , escape )
982 /*+++++++++
983 .PURPOSE Reduces (in place) a string with suppression of `escape' char's,
984 e.g. if escape is \ replace \\ by \, \% by %, etc.
985 .RETURNS Length of reduced string.
986 .REMARKS First and last `escape' are also removed. This function can be
987 used e.g. to extract a string within quotes.
988 ----------*/
989 char *str; /* MOD: String to modify */
990 char escape; /* IN: escape char to be removed */
991 {
992 register char *p, *q;
993
994 for (p = str, q=p; *p; )
995 { if (*p == escape) if (*(++p) == EOS) break;
996 *(q++) = *(p++);
997 }
998 *q = EOS;
999
1000 return(q-str);
1001 }
1002
1003
1004 /*===========================================================================
1005 * Other string utilities
1006 *===========================================================================*/
1007 int strline_ ( str, lmax, mask, table)
1008 /*+++++++++++++++
1009 .PURPOSE Look for the longest match of a line in a string without
1010 cutting words; a word is made of chars surrounded by sep_char(s).
1011 .RETURNS Returns the length of the best match.
1012 .REMARKS The rightestmost space is ignored.
1013 ----------------*/
1014 char *str; /* MOD: string to scan */
1015 int lmax; /* IN: max length of a line */
1016 unsigned char mask; /* IN: Mask to use (ANDed with table) */
1017 unsigned char *table; /* IN: table of flags */
1018 {
1019 register int len;
1020 register char *p;
1021
1022 table = cpt(table, mask); /* EOS MUST end */
1023
1024 for (p = str; *p; )
1025 { len = oscscan((unsigned char *)p, lmax, mask, table);
1026 if (((p-str)+len) > lmax) break;
1027 p += len;
1028 table[0] = 0;
1029 p += oscspan((unsigned char *)p, lmax, mask, table); /* Don't span EOS! */
1030 table[0] = mask;
1031 }
1032 /* If no space was found, set to lmax */
1033 if ((*p) && (p == str)) p = str + lmax;
1034
1035 return(p-str);
1036 }
1037
1038 /*===========================================================================*/
1039 int strnins (source, size, text)
1040 /*+++++++++++
1041 .PURPOSE Insert text in front of source
1042 .RETURNS Length of modified (longer) source string.
1043 -----------*/
1044 char *source; /* MOD: string to modify */
1045 int size; /* IN: max size of source (INCLUDING the '\0) */
1046 char *text; /* IN: Text to insert */
1047 {
1048 int lt, ls, lm;
1049
1050 /* How many bytes can I insert */
1051 ls = strlen(source);
1052 lm = size - 1 - ls;
1053 lt = strlen(text);
1054 if (lt > lm) lt = lm;
1055 if (lt > 0) {
1056 oscopy (source + lt, source, ls+1);
1057 oscopy (source, text, lt);
1058 ls += lt;
1059 }
1060 return(lt);
1061 }
1062
1063 /*===========================================================================*/
1064 int strins (source, text)
1065 /*+++++++++++
1066 .PURPOSE Insert text in front of source
1067 .RETURNS Length of modified (longer) source string.
1068 -----------*/
1069 char *source; /* MOD: string to modify */
1070 char *text; /* IN: Text to insert */
1071 {
1072 int lt, ls;
1073
1074 ls = strlen(source);
1075 lt = strlen(text);
1076 oscopy (source + lt, source, ls+1);
1077 oscopy (source, text, lt);
1078
1079 return(lt);
1080 }
1081