Debian Codegraph @

libsrc/tw/str1.c

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