diff options
author | 2008-05-29 08:00:23 +0000 | |
---|---|---|
committer | 2008-05-29 08:00:23 +0000 | |
commit | 65934e7aa5f3ae7c7644162c7cf0752e2fbc12dc (patch) | |
tree | 50f041b12cefcb60b231a4c4a44e1cd57215390c /src/lib | |
parent | eeb821032e7556896ad4c4d26c79bddd9882a022 (diff) |
Added new option -snapname which lets you provide a template for how snapshot
names are generated.
Added new astring functions astring_del, astring_replace, and astring_replacec
to help perform simple search/replace substitution.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/util/astring.c | 59 | ||||
-rw-r--r-- | src/lib/util/astring.h | 9 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/lib/util/astring.c b/src/lib/util/astring.c index d6956121ef9..9f47108804f 100644 --- a/src/lib/util/astring.c +++ b/src/lib/util/astring.c @@ -291,6 +291,30 @@ astring *astring_substr(astring *str, int start, int count) /*------------------------------------------------- + astring_del - delete a substring of + ourself, keeping everything else +-------------------------------------------------*/ + +astring *astring_del(astring *str, int start, int count) +{ + int strlength = strlen(str->text); + + /* ignore attempts to do this on the dummy */ + if (str == &dummy_astring) + return str; + + /* normalize parameters */ + normalize_substr(&start, &count, strlength); + + /* move the data and NULL-terminate */ + if (count > 0) + memmove(str->text + start, str->text + start + count, strlength - (start + count)); + str->text[strlength - count] = 0; + return str; +} + + +/*------------------------------------------------- astring_printf - printf text into an astring -------------------------------------------------*/ @@ -498,6 +522,41 @@ int astring_findc(const astring *str, int start, const char *search) } +/*------------------------------------------------- + astring_replace - search in an astring for + another astring, replacing all instances with + a third and returning the number of matches +-------------------------------------------------*/ + +int astring_replace(astring *str, int start, const astring *search, const astring *replace) +{ + return astring_replacec(str, start, search->text, replace->text); +} + + +/*------------------------------------------------- + astring_replacec - search in an astring for a + C string, replacing all instances with another + C string and returning the number of matches +-------------------------------------------------*/ + +int astring_replacec(astring *str, int start, const char *search, const char *replace) +{ + int searchlen = strlen(search); + int replacelen = strlen(replace); + int matches = 0; + int curindex; + + for (curindex = astring_findc(str, start, search); curindex != -1; curindex = astring_findc(str, curindex + replacelen, search)) + { + matches++; + astring_del(str, curindex, searchlen); + astring_insc(str, curindex, replace); + } + return matches; +} + + /*************************************************************************** ASTRING UTILITIES diff --git a/src/lib/util/astring.h b/src/lib/util/astring.h index 7be29ab8f27..854ea876248 100644 --- a/src/lib/util/astring.h +++ b/src/lib/util/astring.h @@ -69,6 +69,9 @@ astring *astring_inssubstr(astring *dst, int insbefore, const astring *src, int /* extract a substring of ourself, removing everything else */ astring *astring_substr(astring *str, int start, int count); +/* delete a substring from ourself, keeping everything else */ +astring *astring_del(astring *str, int start, int count); + /* formatted printf to an astring */ int astring_printf(astring *dst, const char *format, ...) ATTR_PRINTF(2,3); @@ -118,6 +121,12 @@ int astring_find(const astring *str, int start, const astring *search); /* search in an astring for a C string, returning offset or -1 if not found */ int astring_findc(const astring *str, int start, const char *search); +/* search in an astring for another astring, replacing all instances with a third and returning the number of matches */ +int astring_replace(astring *str, int start, const astring *search, const astring *replace); + +/* search in an astring for a C string, replacing all instances with another C string and returning the number of matches */ +int astring_replacec(astring *str, int start, const char *search, const char *replace); + /* ----- astring utilties ----- */ |