Fix DSO linker error (ld) with missing "-lfontconfig" for fontconfig
[fvwm.git] / libs / Strings.c
blob5851bb826b70fb47d20bb68e6691a3e1c0d96f61
1 /* -*-c-*- */
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
20 #include "config.h"
22 #include <ctype.h>
24 #include "safemalloc.h"
25 #include "Strings.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;
33 int len = 1;
35 if (a != NULL)
37 len += strlen(a);
39 if (b != NULL)
41 len += strlen(b);
43 if (c != NULL)
45 len += strlen(c);
48 /* Expand buffer to fit string, to a multiple of CHUNK_SIZE */
49 if (len > buffer_len)
51 buffer_len = CHUNK_SIZE * (1 + ((len - 1) / CHUNK_SIZE));
52 buffer = saferealloc(buffer, buffer_len);
54 buffer[0] = 0;
55 if (a != NULL)
57 strcat(buffer, a);
59 if (b != NULL)
61 strcat(buffer, b);
63 if (c != NULL)
65 strcat(buffer, c);
68 return buffer;
70 #undef CHUNK_SIZE
73 void CopyString(char **dest, const char *source)
75 int len;
76 const char *start;
78 if (source == NULL)
80 *dest = NULL;
81 return;
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') )
90 source++;
92 start = source;
94 /* set 'len' to the length of the string, ignoring
95 trailing spaces */
97 len = 0;
98 while ( (*source != '\n') && (*source != 0) )
100 len++;
101 source++;
103 source--;
105 while( len > 0 && isspace((unsigned char)*source) )
107 len--;
108 source--;
111 *dest = safemalloc(len+1);
112 strncpy(*dest,start,len);
113 (*dest)[len]=0;
117 void CopyStringWithQuotes(char **dest, const char *src)
119 while (src && src[0] == ' ')
121 src++;
123 if (src && src[0] == '"')
125 int len;
127 src++;
128 CopyString(dest, src);
129 len = strlen(*dest);
130 if (len > 0 && (*dest)[len - 1] == '"')
132 (*dest)[len - 1] = '\0';
135 else
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 )
150 const char* tmp;
151 char* ptr;
152 int len;
154 if(source == NULL)
156 return NULL;
159 while(isspace((unsigned char)*source))
161 source++;
163 len = strlen(source);
164 tmp = source + len -1;
166 while( (tmp >= source) && ((isspace((unsigned char)*tmp)) ||
167 (*tmp == '\n')) )
169 tmp--;
170 len--;
172 ptr = safemalloc(len+1);
173 if (len)
175 strncpy(ptr,source,len);
177 ptr[len]=0;
179 return ptr;
183 int StrEquals( const char *s1, const char *s2 )
185 if (s1 == NULL && s2 == NULL)
187 return 1;
189 if (s1 == NULL || s2 == NULL)
191 return 0;
194 return strcasecmp(s1,s2) == 0;
198 int StrHasPrefix( const char* string, const char* prefix )
200 if ( prefix == NULL )
202 return 1;
204 if ( string == NULL )
206 return 0;
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)
223 int i = 0;
224 *dest++ = '\'';
225 for(i = 0; source[i]; i++)
227 if (source[i] == '\'')
229 *dest++ = '\\';
231 *dest++ = source[i];
233 *dest++ = '\'';
234 *dest = '\0';
236 return dest;
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)
249 *dest++ = delim;
250 while (*source)
252 char *esc;
253 esc = strchr(escape, *source);
254 if (esc != NULL)
256 *dest++ = escaper[(int)(esc-escape)];
258 *dest++ = *source++;
260 *dest++ = delim;
261 *dest = '\0';
263 return dest;
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;
275 while (*source)
277 if (strchr(escape, *source) != NULL)
279 len++;
281 len++;
282 source++;
285 return len;