diff options
author | Dan McGee <dan@archlinux.org> | 2007-06-04 12:01:53 -0400 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2007-06-04 23:10:49 -0400 |
commit | 65662315b6d180f7066f6578c55a9feec7250114 (patch) | |
tree | 01ebd7b5355f59abd1c128a23f0c76133b19df76 /src/pacman/util.c | |
parent | e0afe6e94a71a6aeb964e9624c2df4f4a2821704 (diff) |
Add a parseconfig to the pacman frontend that compiles
Warning: this compiles but may not work as intended quite yet. :)
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'src/pacman/util.c')
-rw-r--r-- | src/pacman/util.c | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c index a5876122..406145e3 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -209,6 +209,12 @@ char *strtoupper(char *str) char *strtrim(char *str) { char *pch = str; + + if(*str == '\0') { + /* string is empty, so we're done. */ + return(str); + } + while(isspace(*pch)) { pch++; } @@ -216,13 +222,62 @@ char *strtrim(char *str) memmove(str, pch, (strlen(pch) + 1)); } + /* check if there wasn't anything but whitespace in the string. */ + if(*str == '\0') { + return(str); + } + pch = (str + (strlen(str) - 1)); while(isspace(*pch)) { pch--; } *++pch = '\0'; - return str; + return(str); +} + +/* Helper function for strreplace */ +static void _strnadd(char **str, const char *append, unsigned int count) +{ + if(*str) { + *str = realloc(*str, strlen(*str) + count + 1); + } else { + *str = calloc(sizeof(char), count + 1); + } + + strncat(*str, append, count); +} + +/* Replace all occurances of 'needle' with 'replace' in 'str', returning + * a new string (must be free'd) */ +char *strreplace(const char *str, const char *needle, const char *replace) +{ + const char *p, *q; + p = q = str; + + char *newstr = NULL; + unsigned int needlesz = strlen(needle), + replacesz = strlen(replace); + + while (1) { + q = strstr(p, needle); + if(!q) { /* not found */ + if(*p) { + /* add the rest of 'p' */ + _strnadd(&newstr, p, strlen(p)); + } + break; + } else { /* found match */ + if(q > p){ + /* add chars between this occurance and last occurance, if any */ + _strnadd(&newstr, p, q - p); + } + _strnadd(&newstr, replace, replacesz); + p = q + needlesz; + } + } + + return newstr; } void list_display(const char *title, alpm_list_t *list) |