diff options
author | Simo Sorce <idra@samba.org> | 2002-07-09 23:59:46 +0000 |
---|---|---|
committer | Simo Sorce <idra@samba.org> | 2002-07-09 23:59:46 +0000 |
commit | fe23e902c643a4f12ac73cfc1e95b32dbac83248 (patch) | |
tree | 166071ce875bd62d0377301207cb9c85c9df2d63 | |
parent | 51eeaa494cf88b3f2438406cd8f716091069134f (diff) | |
download | samba-fe23e902c643a4f12ac73cfc1e95b32dbac83248.tar.gz samba-fe23e902c643a4f12ac73cfc1e95b32dbac83248.tar.bz2 samba-fe23e902c643a4f12ac73cfc1e95b32dbac83248.zip |
some support functions to ease the next commit.
(This used to be commit 4be1c882be56ae0fe18bb134a1513e0e6f6965b0)
-rw-r--r-- | source3/lib/util_str.c | 102 |
1 files changed, 96 insertions, 6 deletions
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 9b4282c6e0..4c8d5d8bf7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -654,24 +654,21 @@ if len==0 then the string cannot be extended. This is different from the old use of len==0 which was for no length checks to be done. ****************************************************************************/ -void string_sub(char *s,const char *pattern,const char *insert, size_t len) +void string_sub(char *s,const char *pattern, const char *insert, size_t len) { char *p; ssize_t ls,lp,li, i; - if (!insert || !pattern || !s) + if (!insert || !pattern || !*pattern || !s) return; ls = (ssize_t)strlen(s); lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); - if (!*pattern) - return; - if (len == 0) len = ls; - + while (lp <= ls && (p = strstr(s,pattern))) { if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", @@ -713,6 +710,71 @@ void pstring_sub(char *s,const char *pattern,const char *insert) string_sub(s, pattern, insert, sizeof(pstring)); } +/* similar to string_sub, but it will accept only allocated strings + * and may realloc them so pay attention at what you pass on no + * pointers inside strings, no pstrings or const must be passed + * as string. + */ + +char *realloc_string_sub(char *string, const char *pattern, const char *insert) +{ + char *p, *in; + char *s; + ssize_t ls,lp,li,ld, i; + + if (!insert || !pattern || !*pattern || !string || !*string) + return NULL; + + s = string; + + in = strdup(insert); + if (!in) { + DEBUG(0, ("realloc_string_sub: out of memory!\n")); + return NULL; + } + ls = (ssize_t)strlen(s); + lp = (ssize_t)strlen(pattern); + li = (ssize_t)strlen(insert); + ld = li - lp; + for (i=0;i<li;i++) { + switch (in[i]) { + case '`': + case '"': + case '\'': + case ';': + case '$': + case '%': + case '\r': + case '\n': + in[i] = '_'; + default: + /* ok */ + break; + } + } + + while ((p = strstr(s,pattern))) { + if (ld > 0) { + char *t = Realloc(string, ls + ld + 1); + if (!t) { + DEBUG(0, ("realloc_string_sub: out of memory!\n")); + SAFE_FREE(in); + return NULL; + } + string = t; + s = t + (p - s); + } + if (li != lp) { + memmove(p+li,p+lp,strlen(p+lp)+1); + } + memcpy(p, in, li); + s = p + li; + ls += ld; + } + SAFE_FREE(in); + return string; +} + /**************************************************************************** similar to string_sub() but allows for any character to be substituted. Use with caution! @@ -928,6 +990,20 @@ void strlower_m(char *s) } /******************************************************************* + duplicate convert a string to lower case +********************************************************************/ +char *strdup_lower(char *s) +{ + char *t = strdup(s); + if (t == NULL) { + DEBUG(0, ("strdup_lower: Out of memory!\n")); + return NULL; + } + strlower_m(t); + return t; +} + +/******************************************************************* convert a string to upper case ********************************************************************/ void strupper_m(char *s) @@ -947,6 +1023,20 @@ void strupper_m(char *s) unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } +/******************************************************************* + convert a string to upper case +********************************************************************/ +char *strdup_upper(char *s) +{ + char *t = strdup(s); + if (t == NULL) { + DEBUG(0, ("strdup_upper: Out of memory!\n")); + return NULL; + } + strupper_m(t); + return t; +} + /* return a RFC2254 binary string representation of a buffer used in LDAP filters |