1    | /***************************************
2    |   $Revision: 1.3 $
3    | 
4    |   string utilities (ut). 
5    | 
6    |   Status: NOT REVUED, NOT TESTED
7    | 
8    |   ******************/ /******************
9    |   Filename            : stringutil.c
10   |   Author              : marek@ripe.net
11   |   ******************/ /******************
12   |   Copyright (c) 1999                              RIPE NCC
13   |  
14   |   All Rights Reserved
15   |   
16   |   Permission to use, copy, modify, and distribute this software and its
17   |   documentation for any purpose and without fee is hereby granted,
18   |   provided that the above copyright notice appear in all copies and that
19   |   both that copyright notice and this permission notice appear in
20   |   supporting documentation, and that the name of the author not be
21   |   used in advertising or publicity pertaining to distribution of the
22   |   software without specific, written prior permission.
23   |   
24   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
26   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
27   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30   |   ***************************************/
31   | 
32   | #include <string.h>
33   | #include <stdlib.h>
34   | #include <ctype.h>
35   | #include "ut_string.h"
36   | 
37   | #define SPACE 0
38   | #define WORD  1
39   |  
40   | /*+
41   |   compress by removing leading/trailing spaces and compressing multiple
42   |   whitespaces to one.
43   | 
44   |   allocates a string and returns it - it must be freed
45   | +*/
46   | 
47   | 
48   | /*++++++++++++++++++++++++++++++++++++++
49   | char *
50   | ut_string_compress    removes leading/trailing whitespaces and compresses 
51   |                       multiple whitespaces to one. The result is an allocated
52   | 		      string and must be freed.
53   | 
54   | char *input           source string
55   |   ++++++++++++++++++++++++++++++++++++++*/
56   | char *
57   | ut_string_compress(char *input)
58   | {
59   |   char *inchr;
60   |   char *copy = malloc(strlen(input)+1);
61   |   char *outchr = copy;
62   |   char *lastchr;
63   |   int nowexpected = WORD; /* skip initial spaces */
64   | 
65   |   /* find trailing space */
66   |   lastchr = strchr(input, '\0'); 
67   |   while( lastchr != input ) {
68   |     lastchr--;
69   |     if( !isspace((int)*lastchr) ) {
70   |       break;
71   |     }
72   |   }
73   |    
74   |   /* copy the string word by word, inserting spaces */
75   |   inchr=input;
76   |   do { 
77   |     unsigned char ch = *inchr;
78   |     
79   |     if( isspace(ch) ) {
80   |       if( nowexpected == WORD ) { 
81   | 	continue; /* skip this whitespace */
82   |       }
83   |       else { 
84   | 	ch = ' '; /* convert to a plain space */
85   | 	nowexpected = WORD; /* ignore any more spaces */
86   |       }
87   |     }
88   |     else {
89   |       nowexpected = SPACE; 
90   |     }
91   |     
92   |     *outchr = ch;
93   |     outchr++;
94   |   } while(inchr++ != lastchr);
95   |   
96   |   *outchr = '\0';
97   |   
98   |   return copy;
99   | }
100  | 
101  | 
102  | /*++++++++++++++++++++++++++++++++++++++
103  | 
104  | void
105  | ut_string_chop       removes trailing whitespaces from the string, replacing
106  |                      them with '\0' characters. 
107  | 		     So it modifies the source string.
108  | 
109  | char *input          source string
110  |   ++++++++++++++++++++++++++++++++++++++*/
111  | void
112  | ut_string_chop(char *input)
113  | {
114  |   unsigned char *co = (unsigned char *)strchr(input, '\0');
115  | 
116  |   while( co != (unsigned char *)input && (isspace(*co) || *co == '\0') ) {
117  |     *co = '\0';
118  |     co--;
119  |   }
120  | }
121  |