2003-01-09 12:36:05 +00:00
|
|
|
/* stringhelp.c - standard string helper functions
|
2006-04-28 14:32:13 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005,
|
|
|
|
* 2006 Free Software Foundation, Inc.
|
2003-01-09 12:36:05 +00:00
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
* This file is part of JNLIB.
|
2003-01-09 12:36:05 +00:00
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
* JNLIB is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
2003-01-09 12:36:05 +00:00
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
* JNLIB is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2003-01-09 12:36:05 +00:00
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2003-01-09 12:36:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <ctype.h>
|
2004-12-13 15:49:56 +00:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
|
|
#include "libjnlib-config.h"
|
2003-06-18 19:56:13 +00:00
|
|
|
#include "utf8conv.h"
|
2003-01-09 12:36:05 +00:00
|
|
|
#include "stringhelp.h"
|
|
|
|
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
/*
|
|
|
|
* Look for the substring SUB in buffer and return a pointer to that
|
2005-06-16 08:12:03 +00:00
|
|
|
* substring in BUFFER or NULL if not found.
|
2003-01-09 12:36:05 +00:00
|
|
|
* Comparison is case-insensitive.
|
|
|
|
*/
|
|
|
|
const char *
|
2005-06-16 08:12:03 +00:00
|
|
|
memistr (const void *buffer, size_t buflen, const char *sub)
|
2003-01-09 12:36:05 +00:00
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
const unsigned char *buf = buffer;
|
|
|
|
const unsigned char *t = (const unsigned char *)buffer;
|
|
|
|
const unsigned char *s = (const unsigned char *)sub;
|
|
|
|
size_t n = buflen;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
2005-06-16 08:12:03 +00:00
|
|
|
for ( ; n ; t++, n-- )
|
|
|
|
{
|
|
|
|
if ( toupper (*t) == toupper (*s) )
|
|
|
|
{
|
|
|
|
for ( buf=t++, buflen = n--, s++;
|
|
|
|
n && toupper (*t) == toupper (*s); t++, s++, n-- )
|
|
|
|
;
|
|
|
|
if (!*s)
|
|
|
|
return (const char*)buf;
|
|
|
|
t = buf;
|
|
|
|
s = (const unsigned char *)sub ;
|
|
|
|
n = buflen;
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
2005-06-16 08:12:03 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
const char *
|
2005-06-16 08:12:03 +00:00
|
|
|
ascii_memistr ( const void *buffer, size_t buflen, const char *sub )
|
2003-06-18 19:56:13 +00:00
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
const unsigned char *buf = buffer;
|
|
|
|
const unsigned char *t = (const unsigned char *)buf;
|
|
|
|
const unsigned char *s = (const unsigned char *)sub;
|
|
|
|
size_t n = buflen;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
2005-06-16 08:12:03 +00:00
|
|
|
for ( ; n ; t++, n-- )
|
|
|
|
{
|
|
|
|
if (ascii_toupper (*t) == ascii_toupper (*s) )
|
|
|
|
{
|
|
|
|
for ( buf=t++, buflen = n--, s++;
|
|
|
|
n && ascii_toupper (*t) == ascii_toupper (*s); t++, s++, n-- )
|
|
|
|
;
|
|
|
|
if (!*s)
|
|
|
|
return (const char*)buf;
|
|
|
|
t = (const unsigned char *)buf;
|
|
|
|
s = (const unsigned char *)sub ;
|
|
|
|
n = buflen;
|
2003-06-18 19:56:13 +00:00
|
|
|
}
|
2005-06-16 08:12:03 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2003-06-18 19:56:13 +00:00
|
|
|
}
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
/* This function is similar to strncpy(). However it won't copy more
|
|
|
|
than N - 1 characters and makes sure that a '\0' is appended. With
|
|
|
|
N given as 0, nothing will happen. With DEST given as NULL, memory
|
|
|
|
will be allocated using jnlib_xmalloc (i.e. if it runs out of core
|
|
|
|
the function terminates). Returns DES or a pointer to the
|
|
|
|
allocated memory.
|
2003-01-09 12:36:05 +00:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
mem2str( char *dest , const void *src , size_t n )
|
|
|
|
{
|
|
|
|
char *d;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
if( n ) {
|
|
|
|
if( !dest )
|
|
|
|
dest = jnlib_xmalloc( n ) ;
|
|
|
|
d = dest;
|
|
|
|
s = src ;
|
|
|
|
for(n--; n && *s; n-- )
|
|
|
|
*d++ = *s++;
|
|
|
|
*d = '\0' ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* remove leading and trailing white spaces
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
trim_spaces( char *str )
|
|
|
|
{
|
|
|
|
char *string, *p, *mark;
|
|
|
|
|
|
|
|
string = str;
|
|
|
|
/* find first non space character */
|
|
|
|
for( p=string; *p && isspace( *(byte*)p ) ; p++ )
|
|
|
|
;
|
|
|
|
/* move characters */
|
|
|
|
for( (mark = NULL); (*string = *p); string++, p++ )
|
|
|
|
if( isspace( *(byte*)p ) ) {
|
|
|
|
if( !mark )
|
|
|
|
mark = string ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mark = NULL ;
|
|
|
|
if( mark )
|
|
|
|
*mark = '\0' ; /* remove trailing spaces */
|
|
|
|
|
|
|
|
return str ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* remove trailing white spaces
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
trim_trailing_spaces( char *string )
|
|
|
|
{
|
|
|
|
char *p, *mark;
|
|
|
|
|
|
|
|
for( mark = NULL, p = string; *p; p++ ) {
|
|
|
|
if( isspace( *(byte*)p ) ) {
|
|
|
|
if( !mark )
|
|
|
|
mark = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mark = NULL;
|
|
|
|
}
|
|
|
|
if( mark )
|
|
|
|
*mark = '\0' ;
|
|
|
|
|
|
|
|
return string ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
|
|
|
|
{
|
|
|
|
byte *p, *mark;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
|
|
|
|
if( strchr(trimchars, *p ) ) {
|
|
|
|
if( !mark )
|
|
|
|
mark = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mark = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mark ) {
|
|
|
|
*mark = 0;
|
|
|
|
return mark - line;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* remove trailing white spaces and return the length of the buffer
|
|
|
|
*/
|
|
|
|
unsigned
|
|
|
|
trim_trailing_ws( byte *line, unsigned len )
|
|
|
|
{
|
|
|
|
return trim_trailing_chars( line, len, " \t\r\n" );
|
|
|
|
}
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
size_t
|
|
|
|
length_sans_trailing_chars (const unsigned char *line, size_t len,
|
|
|
|
const char *trimchars )
|
|
|
|
{
|
|
|
|
const unsigned char *p, *mark;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
for( mark=NULL, p=line, n=0; n < len; n++, p++ )
|
|
|
|
{
|
|
|
|
if (strchr (trimchars, *p ))
|
|
|
|
{
|
|
|
|
if( !mark )
|
|
|
|
mark = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mark = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mark)
|
|
|
|
return mark - line;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
/*
|
|
|
|
* Return the length of line ignoring trailing white-space.
|
2003-06-18 19:56:13 +00:00
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
length_sans_trailing_ws (const unsigned char *line, size_t len)
|
|
|
|
{
|
|
|
|
return length_sans_trailing_chars (line, len, " \t\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
|
|
/***************
|
|
|
|
* Extract from a given path the filename component.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
char *
|
2006-04-21 12:56:40 +00:00
|
|
|
make_basename(const char *filepath, const char *inputpath)
|
2003-01-09 12:36:05 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
2006-04-21 12:56:40 +00:00
|
|
|
#ifdef __riscos__
|
|
|
|
return riscos_make_basename(filepath, inputpath);
|
|
|
|
#endif
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
if ( !(p=strrchr(filepath, '/')) )
|
2006-04-21 12:56:40 +00:00
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
2003-01-09 12:36:05 +00:00
|
|
|
if ( !(p=strrchr(filepath, '\\')) )
|
|
|
|
if ( !(p=strrchr(filepath, ':')) )
|
2006-04-21 12:56:40 +00:00
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
{
|
|
|
|
return jnlib_xstrdup(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return jnlib_xstrdup(p+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************
|
|
|
|
* Extract from a given filename the path prepended to it.
|
|
|
|
* If their isn't a path prepended to the filename, a dot
|
|
|
|
* is returned ('.').
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
make_dirname(const char *filepath)
|
|
|
|
{
|
|
|
|
char *dirname;
|
|
|
|
int dirname_length;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ( !(p=strrchr(filepath, '/')) )
|
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
|
|
|
if ( !(p=strrchr(filepath, '\\')) )
|
|
|
|
if ( !(p=strrchr(filepath, ':')) )
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return jnlib_xstrdup(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
dirname_length = p-filepath;
|
|
|
|
dirname = jnlib_xmalloc(dirname_length+1);
|
|
|
|
strncpy(dirname, filepath, dirname_length);
|
|
|
|
dirname[dirname_length] = 0;
|
|
|
|
|
|
|
|
return dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Construct a filename from the NULL terminated list of parts.
|
|
|
|
* Tilde expansion is done here.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
make_filename( const char *first_part, ... )
|
|
|
|
{
|
|
|
|
va_list arg_ptr ;
|
|
|
|
size_t n;
|
|
|
|
const char *s;
|
|
|
|
char *name, *home, *p;
|
|
|
|
|
|
|
|
va_start( arg_ptr, first_part ) ;
|
|
|
|
n = strlen(first_part)+1;
|
|
|
|
while( (s=va_arg(arg_ptr, const char *)) )
|
|
|
|
n += strlen(s) + 1;
|
|
|
|
va_end(arg_ptr);
|
|
|
|
|
|
|
|
home = NULL;
|
|
|
|
if( *first_part == '~' && first_part[1] == '/'
|
|
|
|
&& (home = getenv("HOME")) && *home )
|
|
|
|
n += strlen(home);
|
|
|
|
|
|
|
|
name = jnlib_xmalloc(n);
|
|
|
|
p = home ? stpcpy(stpcpy(name,home), first_part+1)
|
|
|
|
: stpcpy(name, first_part);
|
|
|
|
va_start( arg_ptr, first_part ) ;
|
|
|
|
while( (s=va_arg(arg_ptr, const char *)) )
|
|
|
|
p = stpcpy(stpcpy(p,"/"), s);
|
|
|
|
va_end(arg_ptr);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
compare_filenames( const char *a, const char *b )
|
|
|
|
{
|
|
|
|
/* ? check whether this is an absolute filename and
|
|
|
|
* resolve symlinks?
|
|
|
|
*/
|
2003-06-18 19:56:13 +00:00
|
|
|
#ifdef HAVE_DRIVE_LETTERS
|
2003-01-09 12:36:05 +00:00
|
|
|
return stricmp(a,b);
|
2003-06-18 19:56:13 +00:00
|
|
|
#else
|
2003-01-09 12:36:05 +00:00
|
|
|
return strcmp(a,b);
|
2003-06-18 19:56:13 +00:00
|
|
|
#endif
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
|
|
/* Convert 2 hex characters at S to a byte value. Return this value
|
|
|
|
or -1 if there is an error. */
|
|
|
|
int
|
|
|
|
hextobyte (const char *s)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if ( *s >= '0' && *s <= '9' )
|
|
|
|
c = 16 * (*s - '0');
|
|
|
|
else if ( *s >= 'A' && *s <= 'F' )
|
|
|
|
c = 16 * (10 + *s - 'A');
|
|
|
|
else if ( *s >= 'a' && *s <= 'f' )
|
|
|
|
c = 16 * (10 + *s - 'a');
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
s++;
|
|
|
|
if ( *s >= '0' && *s <= '9' )
|
|
|
|
c += *s - '0';
|
|
|
|
else if ( *s >= 'A' && *s <= 'F' )
|
|
|
|
c += 10 + *s - 'A';
|
|
|
|
else if ( *s >= 'a' && *s <= 'f' )
|
|
|
|
c += 10 + *s - 'a';
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
/* Print a BUFFER to stream FP while replacing all control characters
|
2006-05-23 16:19:43 +00:00
|
|
|
and the characters DELIM and DELIM2 with standard C escape
|
|
|
|
sequences. Returns the number of characters printed. */
|
2003-01-09 12:36:05 +00:00
|
|
|
size_t
|
2006-05-23 16:19:43 +00:00
|
|
|
print_sanitized_buffer2 (FILE *fp, const void *buffer, size_t length,
|
|
|
|
int delim, int delim2)
|
2003-01-09 12:36:05 +00:00
|
|
|
{
|
|
|
|
const unsigned char *p = buffer;
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
for (; length; length--, p++, count++)
|
|
|
|
{
|
2006-05-23 16:19:43 +00:00
|
|
|
/* Fixme: Check whether *p < 0xa0 is correct for utf8 encoding. */
|
|
|
|
if (*p < 0x20
|
|
|
|
|| (*p >= 0x7f && *p < 0xa0)
|
|
|
|
|| *p == delim
|
|
|
|
|| *p == delim2
|
|
|
|
|| ((delim || delim2) && *p=='\\'))
|
2003-01-09 12:36:05 +00:00
|
|
|
{
|
|
|
|
putc ('\\', fp);
|
|
|
|
count++;
|
|
|
|
if (*p == '\n')
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc ('n', fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
else if (*p == '\r')
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc ('r', fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
else if (*p == '\f')
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc ('f', fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
else if (*p == '\v')
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc ('v', fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
else if (*p == '\b')
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc ('b', fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
else if (!*p)
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc('0', fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (fp, "x%02x", *p);
|
2006-04-28 14:32:13 +00:00
|
|
|
count += 3;
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2006-05-23 16:19:43 +00:00
|
|
|
{
|
|
|
|
putc (*p, fp);
|
|
|
|
count++;
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
/* Same as print_sanitized_buffer2 but with just one delimiter. */
|
|
|
|
size_t
|
|
|
|
print_sanitized_buffer (FILE *fp, const void *buffer, size_t length,
|
|
|
|
int delim)
|
|
|
|
{
|
|
|
|
return print_sanitized_buffer2 (fp, buffer, length, delim, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
size_t
|
|
|
|
print_sanitized_utf8_buffer (FILE *fp, const void *buffer,
|
|
|
|
size_t length, int delim)
|
|
|
|
{
|
2003-06-18 19:56:13 +00:00
|
|
|
const char *p = buffer;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* We can handle plain ascii simpler, so check for it first. */
|
|
|
|
for (i=0; i < length; i++ )
|
|
|
|
{
|
|
|
|
if ( (p[i] & 0x80) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i < length)
|
|
|
|
{
|
|
|
|
char *buf = utf8_to_native (p, length, delim);
|
|
|
|
/*(utf8 conversion already does the control character quoting)*/
|
|
|
|
i = strlen (buf);
|
|
|
|
fputs (buf, fp);
|
|
|
|
jnlib_free (buf);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return print_sanitized_buffer (fp, p, length, delim);
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
size_t
|
|
|
|
print_sanitized_string2 (FILE *fp, const char *string, int delim, int delim2)
|
|
|
|
{
|
|
|
|
return string? print_sanitized_buffer2 (fp, string, strlen (string),
|
|
|
|
delim, delim2):0;
|
|
|
|
}
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
size_t
|
|
|
|
print_sanitized_string (FILE *fp, const char *string, int delim)
|
|
|
|
{
|
|
|
|
return string? print_sanitized_buffer (fp, string, strlen (string), delim):0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
print_sanitized_utf8_string (FILE *fp, const char *string, int delim)
|
|
|
|
{
|
2004-08-18 13:21:56 +00:00
|
|
|
return string? print_sanitized_utf8_buffer (fp,
|
|
|
|
string, strlen (string),
|
|
|
|
delim) : 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
2005-06-16 08:12:03 +00:00
|
|
|
/* Create a string from the buffer P_ARG of length N which is suitable for
|
2003-06-18 19:56:13 +00:00
|
|
|
printing. Caller must release the created string using xfree. */
|
|
|
|
char *
|
2005-06-16 08:12:03 +00:00
|
|
|
sanitize_buffer (const void *p_arg, size_t n, int delim)
|
2003-06-18 19:56:13 +00:00
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
const unsigned char *p = p_arg;
|
2003-06-18 19:56:13 +00:00
|
|
|
size_t save_n, buflen;
|
2005-06-16 08:12:03 +00:00
|
|
|
const unsigned char *save_p;
|
2003-06-18 19:56:13 +00:00
|
|
|
char *buffer, *d;
|
|
|
|
|
2006-04-28 14:32:13 +00:00
|
|
|
/* First count length. */
|
2003-06-18 19:56:13 +00:00
|
|
|
for (save_n = n, save_p = p, buflen=1 ; n; n--, p++ )
|
|
|
|
{
|
2004-02-18 16:59:19 +00:00
|
|
|
if ( *p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\'))
|
2003-06-18 19:56:13 +00:00
|
|
|
{
|
|
|
|
if ( *p=='\n' || *p=='\r' || *p=='\f'
|
|
|
|
|| *p=='\v' || *p=='\b' || !*p )
|
|
|
|
buflen += 2;
|
|
|
|
else
|
2006-04-28 14:32:13 +00:00
|
|
|
buflen += 5;
|
2003-06-18 19:56:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
buflen++;
|
|
|
|
}
|
|
|
|
p = save_p;
|
|
|
|
n = save_n;
|
2006-04-28 14:32:13 +00:00
|
|
|
/* And now make the string */
|
2003-06-18 19:56:13 +00:00
|
|
|
d = buffer = jnlib_xmalloc( buflen );
|
|
|
|
for ( ; n; n--, p++ )
|
|
|
|
{
|
2004-02-18 16:59:19 +00:00
|
|
|
if (*p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) {
|
2003-06-18 19:56:13 +00:00
|
|
|
*d++ = '\\';
|
|
|
|
if( *p == '\n' )
|
|
|
|
*d++ = 'n';
|
|
|
|
else if( *p == '\r' )
|
|
|
|
*d++ = 'r';
|
|
|
|
else if( *p == '\f' )
|
|
|
|
*d++ = 'f';
|
|
|
|
else if( *p == '\v' )
|
|
|
|
*d++ = 'v';
|
|
|
|
else if( *p == '\b' )
|
|
|
|
*d++ = 'b';
|
|
|
|
else if( !*p )
|
|
|
|
*d++ = '0';
|
|
|
|
else {
|
|
|
|
sprintf(d, "x%02x", *p );
|
2006-04-28 14:32:13 +00:00
|
|
|
d += 3;
|
2003-06-18 19:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*d++ = *p;
|
|
|
|
}
|
|
|
|
*d = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2004-12-13 15:49:56 +00:00
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
/****************************************************
|
2004-12-13 15:49:56 +00:00
|
|
|
********** W32 specific functions ****************
|
|
|
|
****************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
const char *
|
|
|
|
w32_strerror (int ec)
|
|
|
|
{
|
|
|
|
static char strerr[256];
|
|
|
|
|
|
|
|
if (ec == -1)
|
|
|
|
ec = (int)GetLastError ();
|
|
|
|
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec,
|
|
|
|
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
strerr, DIM (strerr)-1, NULL);
|
|
|
|
return strerr;
|
|
|
|
}
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
******** Locale insensitive ctype functions ********
|
2003-01-09 12:36:05 +00:00
|
|
|
****************************************************/
|
|
|
|
/* FIXME: replace them by a table lookup and macros */
|
|
|
|
int
|
|
|
|
ascii_isupper (int c)
|
|
|
|
{
|
|
|
|
return c >= 'A' && c <= 'Z';
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ascii_islower (int c)
|
|
|
|
{
|
|
|
|
return c >= 'a' && c <= 'z';
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ascii_toupper (int c)
|
|
|
|
{
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
c &= ~0x20;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ascii_tolower (int c)
|
|
|
|
{
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
c |= 0x20;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
ascii_strcasecmp( const char *a, const char *b )
|
|
|
|
{
|
|
|
|
if (a == b)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (; *a && *b; a++, b++) {
|
|
|
|
if (*a != *b && ascii_toupper(*a) != ascii_toupper(*b))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
|
|
|
|
}
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
int
|
|
|
|
ascii_strncasecmp (const char *a, const char *b, size_t n)
|
|
|
|
{
|
|
|
|
const unsigned char *p1 = (const unsigned char *)a;
|
|
|
|
const unsigned char *p2 = (const unsigned char *)b;
|
|
|
|
unsigned char c1, c2;
|
|
|
|
|
|
|
|
if (p1 == p2 || !n )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c1 = ascii_tolower (*p1);
|
|
|
|
c2 = ascii_tolower (*p2);
|
|
|
|
|
|
|
|
if ( !--n || c1 == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
++p1;
|
|
|
|
++p2;
|
|
|
|
}
|
|
|
|
while (c1 == c2);
|
|
|
|
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
int
|
2005-06-16 08:12:03 +00:00
|
|
|
ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n )
|
2003-01-09 12:36:05 +00:00
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
const char *a = a_arg;
|
|
|
|
const char *b = b_arg;
|
|
|
|
|
|
|
|
if (a == b)
|
2003-01-09 12:36:05 +00:00
|
|
|
return 0;
|
2005-06-16 08:12:03 +00:00
|
|
|
for ( ; n; n--, a++, b++ )
|
|
|
|
{
|
|
|
|
if( *a != *b && ascii_toupper (*a) != ascii_toupper (*b) )
|
|
|
|
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
|
|
|
|
}
|
|
|
|
return 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ascii_strcmp( const char *a, const char *b )
|
|
|
|
{
|
|
|
|
if (a == b)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (; *a && *b; a++, b++) {
|
|
|
|
if (*a != *b )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *a == *b? 0 : (*(signed char *)a - *(signed char *)b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
ascii_memcasemem (const void *haystack, size_t nhaystack,
|
|
|
|
const void *needle, size_t nneedle)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!nneedle)
|
|
|
|
return (void*)haystack; /* finding an empty needle is really easy */
|
|
|
|
if (nneedle <= nhaystack)
|
|
|
|
{
|
2005-06-16 08:12:03 +00:00
|
|
|
const char *a = haystack;
|
|
|
|
const char *b = a + nhaystack - nneedle;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
|
|
for (; a <= b; a++)
|
|
|
|
{
|
|
|
|
if ( !ascii_memcasecmp (a, needle, nneedle) )
|
|
|
|
return (void *)a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************
|
|
|
|
********** missing string functions *********
|
|
|
|
*********************************************/
|
|
|
|
|
|
|
|
#ifndef HAVE_STPCPY
|
|
|
|
char *
|
|
|
|
stpcpy(char *a,const char *b)
|
|
|
|
{
|
|
|
|
while( *b )
|
|
|
|
*a++ = *b++;
|
|
|
|
*a = 0;
|
|
|
|
|
|
|
|
return (char*)a;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-02 11:54:35 +00:00
|
|
|
#ifndef HAVE_STRSEP
|
|
|
|
/* Code taken from glibc-2.2.1/sysdeps/generic/strsep.c. */
|
|
|
|
char *
|
|
|
|
strsep (char **stringp, const char *delim)
|
|
|
|
{
|
|
|
|
char *begin, *end;
|
|
|
|
|
|
|
|
begin = *stringp;
|
|
|
|
if (begin == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* A frequent case is when the delimiter string contains only one
|
|
|
|
character. Here we don't need to call the expensive `strpbrk'
|
|
|
|
function and instead work using `strchr'. */
|
|
|
|
if (delim[0] == '\0' || delim[1] == '\0')
|
|
|
|
{
|
|
|
|
char ch = delim[0];
|
|
|
|
|
|
|
|
if (ch == '\0')
|
|
|
|
end = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*begin == ch)
|
|
|
|
end = begin;
|
|
|
|
else if (*begin == '\0')
|
|
|
|
end = NULL;
|
|
|
|
else
|
|
|
|
end = strchr (begin + 1, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Find the end of the token. */
|
|
|
|
end = strpbrk (begin, delim);
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
{
|
|
|
|
/* Terminate the token and set *STRINGP past NUL character. */
|
|
|
|
*end++ = '\0';
|
|
|
|
*stringp = end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* No more delimiters; this is the last token. */
|
|
|
|
*stringp = NULL;
|
|
|
|
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
#endif /*HAVE_STRSEP*/
|
|
|
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
#ifndef HAVE_STRLWR
|
|
|
|
char *
|
|
|
|
strlwr(char *s)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
for(p=s; *p; p++ )
|
|
|
|
*p = tolower(*p);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_STRCASECMP
|
|
|
|
int
|
|
|
|
strcasecmp( const char *a, const char *b )
|
|
|
|
{
|
|
|
|
for( ; *a && *b; a++, b++ ) {
|
|
|
|
if( *a != *b && toupper(*a) != toupper(*b) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *(const byte*)a - *(const byte*)b;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* mingw32/cpd has a memicmp()
|
|
|
|
*/
|
|
|
|
#ifndef HAVE_MEMICMP
|
|
|
|
int
|
|
|
|
memicmp( const char *a, const char *b, size_t n )
|
|
|
|
{
|
|
|
|
for( ; n; n--, a++, b++ )
|
|
|
|
if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
|
|
|
|
return *(const byte *)a - *(const byte*)b;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2004-12-13 15:49:56 +00:00
|
|
|
|
|
|
|
|
2006-10-19 14:22:06 +00:00
|
|
|
#ifndef HAVE_MEMRCHR
|
|
|
|
void *
|
|
|
|
memrchr (const void *buffer, int c, size_t n)
|
|
|
|
{
|
|
|
|
const unsigned char *p = buffer;
|
|
|
|
|
|
|
|
for (p += n; n ; n--)
|
|
|
|
if (*--p == c)
|
|
|
|
return p;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /*HAVE_MEMRCHR*/
|