Fix compilation error with IRIX 6.5 cc.
[gnulib.git] / lib / trim.c
1 /* Removes leading and/or trailing whitespaces
2    Copyright (C) 2006-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (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 License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Davide Angelocola <davide.angelocola@gmail.com> */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "trim.h"
23
24 #include <ctype.h>
25 #include <string.h>
26
27 #if HAVE_MBRTOWC 
28 # include <stddef.h>
29 # include <stdlib.h>
30 # include "mbchar.h"
31 # include "mbiter.h"
32 #endif
33
34 #include "xalloc.h"
35
36 char *
37 trim2(const char *s, int how)
38 {
39   char *d;
40     
41   d = strdup(s);
42
43   if (!d)
44     xalloc_die();
45   
46 #if HAVE_MBRTOWC
47   if (MB_CUR_MAX > 1)
48     {
49       mbi_iterator_t i;
50       
51       /* Trim leading whitespaces. */
52       if (how != TRIM_TRAILING) 
53         {
54           mbi_init (i, d, strlen (d));
55       
56           for (; mbi_avail (i) && mb_isspace (mbi_cur (i)); mbi_advance (i))
57             ;
58           
59           memmove (d, mbi_cur_ptr (i), strlen (mbi_cur_ptr (i)) + 1);
60         }
61   
62       /* Trim trailing whitespaces. */
63       if (how != TRIM_LEADING) 
64         {
65           int state = 0;
66           char *r; /* used only while state = 2 */
67           
68           mbi_init (i, d, strlen (d));
69
70           for (; mbi_avail (i); mbi_advance (i)) 
71             {
72               if (state == 0 && mb_isspace (mbi_cur (i))) 
73                 {
74                   state = 0;
75                   continue;
76                 }
77               
78               if (state == 0 && !mb_isspace (mbi_cur (i)))
79                 {
80                   state = 1;
81                   continue;
82                 }
83               
84               if (state == 1 && !mb_isspace (mbi_cur (i)))
85                 {
86                   state = 1;
87                   continue;
88                 }
89               
90               if (state == 1 && mb_isspace (mbi_cur (i))) 
91                 {
92                   state = 2;
93                   r = (char *) mbi_cur_ptr (i);
94                 }
95               else if (state == 2 && mb_isspace (mbi_cur (i))) 
96                 {
97                   state = 2;
98                 } 
99               else 
100                 {
101                   state = 1;
102                 }
103             }
104           
105           if (state == 2) 
106             *r = '\0';
107         }
108     }
109   else
110 #endif /* HAVE_MBRTOWC */
111     {
112       char *p;
113       
114       /* Trim leading whitespaces. */
115       if (how != TRIM_TRAILING) {
116         for (p = d; *p && isspace (*p); p++)
117           ;                     
118
119         memmove (d, p, strlen (p) + 1);
120       }
121
122       /* Trim trailing whitespaces. */
123       if (how != TRIM_LEADING) {
124         for (p = d + strlen (d) - 1; p >= d && isspace (*p); p--)
125           *p = '\0';
126       }
127     }
128   
129   return d;
130 }
131