Based on a routine by GJC@VILLAGE.COM.
Extensively modified by Andrew.Tridgell@anu.edu.au
****************************************************************************/
-BOOL next_token(char **ptr,char *buff,char *sep, int bufsize)
+BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize)
{
char *s;
BOOL quoted;
- int len=1;
+ size_t len=1;
if (!ptr) ptr = &last_ptr;
if (!ptr) return(False);
/*******************************************************************
case insensitive string compararison
********************************************************************/
-int StrCaseCmp(char *s, char *t)
+int StrCaseCmp(const char *s, const char *t)
{
/* compare until we run out of string, either t or s, or find a difference */
/* We *must* use toupper rather than tolower here due to the
/*******************************************************************
case insensitive string compararison, length limited
********************************************************************/
-int StrnCaseCmp(char *s, char *t, int n)
+int StrnCaseCmp(const char *s, const char *t, size_t n)
{
/* compare until we run out of string, either t or s, or chars */
/* We *must* use toupper rather than tolower here due to the
/*******************************************************************
compare 2 strings
********************************************************************/
-BOOL strequal(char *s1, char *s2)
+BOOL strequal(const char *s1, const char *s2)
{
if (s1 == s2) return(True);
if (!s1 || !s2) return(False);
/*******************************************************************
compare 2 strings up to and including the nth char.
******************************************************************/
-BOOL strnequal(char *s1,char *s2,int n)
+BOOL strnequal(const char *s1,const char *s2,size_t n)
{
if (s1 == s2) return(True);
if (!s1 || !s2 || !n) return(False);
/*******************************************************************
compare 2 strings (case sensitive)
********************************************************************/
-BOOL strcsequal(char *s1,char *s2)
+BOOL strcsequal(const char *s1,const char *s2)
{
if (s1 == s2) return(True);
if (!s1 || !s2) return(False);
else
#endif /* KANJI_WIN95_COMPATIBILITY */
{
- int skip = skip_multibyte_char( *s );
+ size_t skip = skip_multibyte_char( *s );
if( skip != 0 )
s += skip;
else
else
#endif /* KANJI_WIN95_COMPATIBILITY */
{
- int skip = skip_multibyte_char( *s );
+ size_t skip = skip_multibyte_char( *s );
if( skip != 0 )
s += skip;
else
****************************************************************************/
void string_replace(char *s,char oldc,char newc)
{
- int skip;
+ size_t skip;
while (*s)
{
skip = skip_multibyte_char( *s );
/*******************************************************************
skip past some strings in a buffer
********************************************************************/
-char *skip_string(char *buf,int n)
+char *skip_string(const char *buf,size_t n)
{
while (n--)
buf += strlen(buf) + 1;
- return(buf);
+ return((char *)buf);
}
/*******************************************************************
16.oct.98, jdblair@cobaltnet.com.
********************************************************************/
-size_t str_charnum(char *s)
+size_t str_charnum(const char *s)
{
size_t len = 0;
trim the specified elements off the front and back of a string
********************************************************************/
-BOOL trim_string(char *s,char *front,char *back)
+BOOL trim_string(char *s,const char *front,const char *back)
{
BOOL ret = False;
size_t front_len = (front && *front) ? strlen(front) : 0;
/****************************************************************************
does a string have any uppercase chars in it?
****************************************************************************/
-BOOL strhasupper(char *s)
+BOOL strhasupper(const char *s)
{
while (*s)
{
else
#endif /* KANJI_WIN95_COMPATIBILITY */
{
- int skip = skip_multibyte_char( *s );
+ size_t skip = skip_multibyte_char( *s );
if( skip != 0 )
s += skip;
else {
/****************************************************************************
does a string have any lowercase chars in it?
****************************************************************************/
-BOOL strhaslower(char *s)
+BOOL strhaslower(const char *s)
{
while (*s)
{
else
#endif /* KANJI_WIN95_COMPATIBILITY */
{
- int skip = skip_multibyte_char( *s );
+ size_t skip = skip_multibyte_char( *s );
if( skip != 0 )
s += skip;
else {
/****************************************************************************
find the number of chars in a string
****************************************************************************/
-int count_chars(char *s,char c)
+size_t count_chars(const char *s,char c)
{
- int count=0;
+ size_t count=0;
#if !defined(KANJI_WIN95_COMPATIBILITY)
/*
{
while (*s)
{
- int skip = skip_multibyte_char( *s );
+ size_t skip = skip_multibyte_char( *s );
if( skip != 0 )
s += skip;
else {
safe string copy into a known length string. maxlength does not
include the terminating zero.
********************************************************************/
-char *safe_strcpy(char *dest,const char *src, int maxlength)
+char *safe_strcpy(char *dest,const char *src, size_t maxlength)
{
- int len;
+ size_t len;
if (!dest) {
DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
safe string cat into a string. maxlength does not
include the terminating zero.
********************************************************************/
-char *safe_strcat(char *dest, char *src, int maxlength)
+char *safe_strcat(char *dest, const char *src, size_t maxlength)
{
- int src_len, dest_len;
+ size_t src_len, dest_len;
if (!dest) {
DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
/****************************************************************************
this is a safer strcpy(), meant to prevent core dumps when nasty things happen
****************************************************************************/
-char *StrCpy(char *dest,char *src)
+char *StrCpy(char *dest,const char *src)
{
char *d = dest;
/****************************************************************************
like strncpy but always null terminates. Make sure there is room!
****************************************************************************/
-char *StrnCpy(char *dest,char *src,int n)
+char *StrnCpy(char *dest,const char *src,size_t n)
{
char *d = dest;
if (!dest) return(NULL);
like strncpy but copies up to the character marker. always null terminates.
returns a pointer to the character marker in the source string (src).
****************************************************************************/
-char *strncpyn(char *dest, char *src,int n, char c)
+char *strncpyn(char *dest, const char *src,size_t n, char c)
{
char *p;
- int str_len;
+ size_t str_len;
p = strchr(src, c);
if (p == NULL)
valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
**************************************************************/
-int strhex_to_str(char *p, int len, const char *strhex)
+size_t strhex_to_str(char *p, size_t len, const char *strhex)
{
- int i;
- int num_chars = 0;
+ size_t i;
+ size_t num_chars = 0;
unsigned char lonybble, hinybble;
char *hexchars = "0123456789ABCDEF";
char *p1 = NULL, *p2 = NULL;
/****************************************************************************
set a string value, allocing the space for the string
****************************************************************************/
-BOOL string_init(char **dest,char *src)
+BOOL string_init(char **dest,const char *src)
{
- int l;
+ size_t l;
if (!src)
src = "";
set a string value, allocing the space for the string, and deallocating any
existing space
****************************************************************************/
-BOOL string_set(char **dest,char *src)
+BOOL string_set(char **dest,const char *src)
{
string_free(dest);
return(string_init(dest,src));
}
+
/****************************************************************************
substitute a string for a pattern in another string. Make sure there is
enough room!
This routine looks for pattern in s and replaces it with
insert. It may do multiple replacements.
-return True if a substitution was done.
+any of " ; ' or ` in the insert string are replaced with _
****************************************************************************/
-BOOL string_sub(char *s,char *pattern,char *insert)
+void string_sub(char *s,const char *pattern,const char *insert)
{
- BOOL ret = False;
- char *p;
- int ls,lp,li;
+ char *p;
+ size_t ls,lp,li, i;
+
+ if (!insert || !pattern || !s) return;
+
+ ls = strlen(s);
+ lp = strlen(pattern);
+ li = strlen(insert);
+
+ if (!*pattern) return;
+
+ while (lp <= ls && (p = strstr(s,pattern))) {
+ memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
+ for (i=0;i<li;i++) {
+ switch (insert[i]) {
+ case '`':
+ case '"':
+ case '\'':
+ case ';':
+ p[i] = '_';
+ break;
+ default:
+ p[i] = insert[i];
+ }
+ }
+ s = p + li;
+ ls += (li-lp);
+ }
+}
- if (!insert || !pattern || !s) return(False);
- ls = strlen(s);
- lp = strlen(pattern);
- li = strlen(insert);
+/****************************************************************************
+similar to string_sub() but allows for any character to be substituted.
+Use with caution!
+****************************************************************************/
+void all_string_sub(char *s,const char *pattern,const char *insert)
+{
+ char *p;
+ size_t ls,lp,li, i;
+
+ if (!insert || !pattern || !s) return;
- if (!*pattern) return(False);
+ ls = strlen(s);
+ lp = strlen(pattern);
+ li = strlen(insert);
- while (lp <= ls && (p = strstr(s,pattern)))
- {
- ret = True;
- memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
- memcpy(p,insert,li);
- s = p + li;
- ls = strlen(s);
- }
- return(ret);
+ if (!*pattern) return;
+
+ while (lp <= ls && (p = strstr(s,pattern))) {
+ memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
+ memcpy(p, insert, li);
+ s = p + li;
+ ls += (li-lp);
+ }
}
+/****************************************************************************
+ splits out the front and back at a separator.
+****************************************************************************/
+void split_at_last_component(char *path, char *front, char sep, char *back)
+{
+ char *p = strrchr(path, sep);
+
+ if (p != NULL)
+ {
+ *p = 0;
+ }
+ if (front != NULL)
+ {
+ pstrcpy(front, path);
+ }
+ if (p != NULL)
+ {
+ if (back != NULL)
+ {
+ pstrcpy(back, p+1);
+ }
+ *p = '\\';
+ }
+ else
+ {
+ if (back != NULL)
+ {
+ back[0] = 0;
+ }
+ }
+}