… | |
… | |
263 | |
263 | |
264 | return str; |
264 | return str; |
265 | } |
265 | } |
266 | |
266 | |
267 | /* |
267 | /* |
268 | * Split a comma-separated string into an array, stripping leading and |
268 | * Split a string into an array based on the given delimiter, stripping leading and |
269 | * trailing spaces from each entry. Empty strings are properly returned |
269 | * trailing spaces from each entry. Empty strings are properly returned |
270 | */ |
270 | */ |
271 | char ** |
271 | char ** |
272 | rxvt_splitcommastring (const char *cs) NOTHROW |
272 | rxvt_strsplit (char delim, const char *str) NOTHROW |
273 | { |
273 | { |
274 | int l, n, p; |
274 | int l, n; |
275 | const char *s, *t; |
275 | char *s, *t; |
276 | char **ret; |
276 | char **ret; |
277 | |
277 | |
278 | if ((s = cs) == NULL) |
278 | s = strdup (str ? str : ""); |
279 | s = ""; |
|
|
280 | |
279 | |
281 | for (n = 1, t = s; *t; t++) |
280 | for (n = 1, t = s; *t; t++) |
282 | if (*t == ',') |
281 | if (*t == delim) |
283 | n++; |
282 | n++; |
284 | |
283 | |
285 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
284 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
286 | ret[n] = NULL; |
285 | ret[n] = NULL; |
287 | |
286 | |
288 | for (l = 0, t = s; l < n; l++) |
287 | for (l = 0, t = s; l < n; l++) |
289 | { |
288 | { |
290 | for ( ; *t && *t != ','; t++) ; |
289 | for (; *t && *t != delim; t++) |
291 | p = t - s; |
290 | ; |
292 | ret[l] = (char *)malloc (p + 1); |
291 | *t = '\0'; |
293 | memcpy (ret[l], s, p); |
|
|
294 | ret[l][p] = '\0'; |
292 | ret[l] = s; |
295 | rxvt_strtrim (ret[l]); |
293 | rxvt_strtrim (ret[l]); |
296 | s = ++t; |
294 | s = ++t; |
297 | } |
295 | } |
298 | |
296 | |
299 | return ret; |
297 | return ret; |
300 | } |
298 | } |
301 | |
299 | |
302 | void |
|
|
303 | rxvt_freecommastring (char **cs) NOTHROW |
|
|
304 | { |
|
|
305 | for (int i = 0; cs[i]; ++i) |
|
|
306 | free (cs[i]); |
|
|
307 | |
|
|
308 | free (cs); |
|
|
309 | } |
|
|
310 | |
|
|
311 | void * |
300 | void * |
312 | rxvt_malloc (size_t size) |
301 | rxvt_malloc (size_t size) |
313 | { |
302 | { |
314 | void *p = malloc (size); |
303 | void *p = malloc (size); |
315 | |
304 | |