… | |
… | |
282 | /* INTPROTO */ |
282 | /* INTPROTO */ |
283 | char * |
283 | char * |
284 | rxvt_Str_trim (char *str) |
284 | rxvt_Str_trim (char *str) |
285 | { |
285 | { |
286 | char *r, *s; |
286 | char *r, *s; |
287 | int n; |
|
|
288 | |
287 | |
289 | if (!str || !*str) /* shortcut */ |
288 | if (!str || !*str) /* shortcut */ |
290 | return str; |
289 | return str; |
291 | |
290 | |
292 | /* skip leading spaces */ |
291 | /* skip leading spaces */ |
293 | for (s = str; *s && isspace (*s); s++) ; |
292 | for (s = str; *s && isspace (*s); s++) ; |
|
|
293 | |
294 | /* goto end of string */ |
294 | /* goto end of string */ |
295 | for (n = 0, r = s; *r++; n++) ; |
295 | r = s + strlen (s) - 1; |
296 | r -= 2; |
296 | |
297 | /* dump return */ |
297 | /* dump return and other trailing whitespace */ |
298 | if (n > 0 && *r == '\n') |
298 | while (r > s && isspace (*r)) |
299 | n--, r--; |
299 | r--; |
300 | /* backtrack along trailing spaces */ |
300 | |
301 | for (; n > 0 && isspace (*r); r--, n--) ; |
301 | #if 0 |
302 | /* skip matching leading/trailing quotes */ |
302 | /* skip matching leading/trailing quotes */ |
303 | if (*s == '"' && *r == '"' && n > 1) |
303 | if (*s == '"' && *r == '"' && n > 1) |
304 | { |
304 | { |
305 | s++; |
305 | s++; |
306 | n -= 2; |
306 | n -= 2; |
307 | } |
307 | } |
|
|
308 | #endif |
308 | |
309 | |
309 | /* copy back over: forwards copy */ |
310 | memmove (str, s, r + 1 - s); |
310 | for (r = str; n; n--) |
311 | str[r + 1 - s] = 0; |
311 | *r++ = *s++; |
|
|
312 | *r = '\0'; |
|
|
313 | |
312 | |
314 | return str; |
313 | return str; |
315 | } |
314 | } |
316 | |
315 | |
317 | /* |
316 | /* |
… | |
… | |
400 | return (d - str); |
399 | return (d - str); |
401 | } |
400 | } |
402 | |
401 | |
403 | /* |
402 | /* |
404 | * Split a comma-separated string into an array, stripping leading and |
403 | * Split a comma-separated string into an array, stripping leading and |
405 | * trailing spaces (and paired quotes) from each entry. Empty strings |
404 | * trailing spaces from each entry. Empty strings are properly returned |
406 | * are properly returned |
|
|
407 | * Caller should free each entry and array when done |
405 | * Caller should free each entry and array when done |
408 | */ |
406 | */ |
409 | /* INTPROTO */ |
407 | /* INTPROTO */ |
410 | char ** |
408 | char ** |
411 | rxvt_splitcommastring (const char *cs) |
409 | rxvt_splitcommastring (const char *cs) |
… | |
… | |
418 | s = ""; |
416 | s = ""; |
419 | |
417 | |
420 | for (n = 1, t = s; *t; t++) |
418 | for (n = 1, t = s; *t; t++) |
421 | if (*t == ',') |
419 | if (*t == ',') |
422 | n++; |
420 | n++; |
|
|
421 | |
423 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
422 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
424 | ret[n] = NULL; |
423 | ret[n] = NULL; |
425 | |
424 | |
426 | for (l = 0, t = s; l < n; l++) |
425 | for (l = 0, t = s; l < n; l++) |
427 | { |
426 | { |
… | |
… | |
431 | strncpy (ret[l], s, p); |
430 | strncpy (ret[l], s, p); |
432 | ret[l][p] = '\0'; |
431 | ret[l][p] = '\0'; |
433 | rxvt_Str_trim (ret[l]); |
432 | rxvt_Str_trim (ret[l]); |
434 | s = ++t; |
433 | s = ++t; |
435 | } |
434 | } |
|
|
435 | |
436 | return ret; |
436 | return ret; |
437 | } |
437 | } |
438 | |
438 | |
439 | void |
439 | void |
440 | rxvt_freecommastring (char **cs) |
440 | rxvt_freecommastring (char **cs) |