2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ** Strings.c: various routines for dealing with strings
24 #include "safemalloc.h"
28 #define CHUNK_SIZE 256
29 char *CatString3(const char *a
, const char *b
, const char *c
)
31 static char* buffer
= NULL
;
32 static int buffer_len
= 0;
48 /* Expand buffer to fit string, to a multiple of CHUNK_SIZE */
51 buffer_len
= CHUNK_SIZE
* (1 + ((len
- 1) / CHUNK_SIZE
));
52 buffer
= saferealloc(buffer
, buffer_len
);
73 void CopyString(char **dest
, const char *source
)
84 /* set 'start' to the first character of the string,
85 skipping over spaces, but not newlines
86 (newline terminates the string) */
88 while ( isspace((unsigned char)*source
) && (*source
!= '\n') )
94 /* set 'len' to the length of the string, ignoring
98 while ( (*source
!= '\n') && (*source
!= 0) )
105 while( len
> 0 && isspace((unsigned char)*source
) )
111 *dest
= safemalloc(len
+1);
112 strncpy(*dest
,start
,len
);
117 void CopyStringWithQuotes(char **dest
, const char *src
)
119 while (src
&& src
[0] == ' ')
123 if (src
&& src
[0] == '"')
128 CopyString(dest
, src
);
130 if (len
> 0 && (*dest
)[len
- 1] == '"')
132 (*dest
)[len
- 1] = '\0';
137 CopyString(dest
, src
);
144 * Copies a string into a new, malloc'ed string
145 * Strips leading spaces and trailing spaces and new lines
148 char *stripcpy( const char *source
)
159 while(isspace((unsigned char)*source
))
163 len
= strlen(source
);
164 tmp
= source
+ len
-1;
166 while( (tmp
>= source
) && ((isspace((unsigned char)*tmp
)) ||
172 ptr
= safemalloc(len
+1);
175 strncpy(ptr
,source
,len
);
183 int StrEquals( const char *s1
, const char *s2
)
185 if (s1
== NULL
&& s2
== NULL
)
189 if (s1
== NULL
|| s2
== NULL
)
194 return strcasecmp(s1
,s2
) == 0;
198 int StrHasPrefix( const char* string
, const char* prefix
)
200 if ( prefix
== NULL
)
204 if ( string
== NULL
)
209 return strncasecmp( string
, prefix
, strlen(prefix
) ) == 0;
215 * Adds single quotes arround the string and escapes single quotes with
216 * backslashes. The result is placed in the given dest, not allocated.
217 * The end of destination, i.e. pointer to '\0' is returned.
218 * You should allocate dest yourself, at least strlen(source) * 2 + 3.
221 char *QuoteString(char *dest
, const char *source
)
225 for(i
= 0; source
[i
]; i
++)
227 if (source
[i
] == '\'')
240 * Adds delim around the source and escapes all characters in escape with
241 * the corresponding escaper. The dest string must be preallocated.
242 * delim should be included in escape with a proper escaper.
243 * Returns a pointer to the end of dest.
246 char *QuoteEscapeString(char *dest
, const char *source
, char delim
,
247 const char *escape
, const char *escaper
)
253 esc
= strchr(escape
, *source
);
256 *dest
++ = escaper
[(int)(esc
-escape
)];
267 * Calculates the lenght needed by a escaped by QuoteEscapeString
268 * the corresponding escaper.
271 unsigned int QuoteEscapeStringLength(const char *source
, const char *escape
)
273 unsigned int len
= 2;
277 if (strchr(escape
, *source
) != NULL
)