Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

miscutil.c File Reference

zalloc, hash_string, safe_strerror, strcmpic, strncmpic, chomp, and MinGW32 strdup functions. More...


Functions

void * zalloc (size_t size)
unsigned int hash_string (const char *s)
char * safe_strerror (int err)
int strcmpic (const char *s1, const char *s2)
int strncmpic (const char *s1, const char *s2, size_t n)
char * chomp (char *string)
char * strsav (char *old, const char *text_to_append)
jb_err string_append (char **target_string, const char *text_to_append)
jb_err string_join (char **target_string, char *text_to_append)
char * string_toupper (const char *string)
int simplematch (char *pattern, char *text)
char * bindup (const char *string, size_t len)
char * make_path (const char *dir, const char *file)

Variables

const char miscutil_rcs [] = "$Id: miscutil.c,v 2.1 2002/06/04 17:22:37 jongfoster Exp $"
const char miscutil_h_rcs [] = MISCUTIL_H_VERSION


Detailed Description

zalloc, hash_string, safe_strerror, strcmpic, strncmpic, chomp, and MinGW32 strdup functions.

These are each too small to deserve their own file but don't really fit in any other file.

Log:
miscutil.c,v
Revision 2.1 2002/06/04 17:22:37 jongfoster Adding comments

Revision 2.0 2002/06/04 14:34:21 jongfoster Moving source files to src/

Revision 1.37 2002/04/26 18:29:43 jongfoster Fixing this Visual C++ warning: miscutil.c(710) : warning C4090: '=' : different 'const' qualifiers

Revision 1.36 2002/04/26 12:55:38 oes New function string_toupper

Revision 1.35 2002/03/26 22:29:55 swa we have a new homepage!

Revision 1.34 2002/03/24 13:25:43 swa name change related issues

Revision 1.33 2002/03/07 03:46:53 oes Fixed compiler warnings etc

Revision 1.32 2002/03/06 23:02:57 jongfoster Removing tabs

Revision 1.31 2002/03/05 04:52:42 oes Deleted non-errlog debugging code

Revision 1.30 2002/03/04 18:27:42 oes

Revision 1.29 2002/03/04 02:08:02 david__schmidt Enable web editing of actions file on OS/2 (it had been broken all this time!)

Revision 1.28 2002/03/03 09:18:03 joergs Made jumbjuster work on AmigaOS again.

Revision 1.27 2002/01/21 00:52:32 jongfoster Adding string_join()

Revision 1.26 2001/12/30 14:07:32 steudten

Revision 1.25 2001/11/13 00:16:38 jongfoster Replacing references to malloc.h with the standard stdlib.h (See ANSI or K&R 2nd Ed)

Revision 1.24 2001/11/05 21:41:43 steudten Add changes to be a real daemon just for unix os. (change cwd to /, detach from controlling tty, set process group and session leader to the own process. Add DBG() Macro. Add some fatal-error log message for failed malloc(). Add '-d' if compiled with 'configure --with-debug' to enable debug output.

Revision 1.23 2001/10/29 03:48:10 david__schmidt OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted by and __OS2__ ifdef.

Revision 1.22 2001/10/26 17:39:38 oes Moved ijb_isspace and ijb_tolower to project.h

Revision 1.21 2001/10/23 21:27:50 jongfoster Standardising error codes in string_append make_path() no longer adds '\' if the dir already ends in '\' (this is just copying a UNIX-specific fix to the Windows-specific part)

Revision 1.20 2001/10/22 15:33:56 david__schmidt Special-cased OS/2 out of the Netscape-abort-on-404-in-js problem in filters.c. Added a FIXME in front of the offending code. I'll gladly put in a better/more robust fix for all parties if one is presented... It seems that just returning 200 instead of 404 would pretty much fix it for everyone, but I don't know all the history of the problem.

Revision 1.19 2001/10/14 22:02:57 jongfoster New function string_append() which is like strsav(), but running out of memory isn't automatically FATAL.

Revision 1.18 2001/09/20 13:33:43 steudten

change long to int as return value in hash_string(). Remember the wraparound for int = long = sizeof(4) - thats maybe not what we want.

Revision 1.17 2001/09/13 20:51:29 jongfoster Fixing potential problems with characters >=128 in simplematch() This was also a compiler warning.

Revision 1.16 2001/09/10 10:56:59 oes Silenced compiler warnings

Revision 1.15 2001/07/13 14:02:24 oes Removed vim-settings

Revision 1.14 2001/06/29 21:45:41 oes Indentation, CRLF->LF, Tab-> Space

Revision 1.13 2001/06/29 13:32:14 oes Removed logentry from cancelled commit

Revision 1.12 2001/06/09 10:55:28 jongfoster Changing BUFSIZ ==> BUFFER_SIZE

Revision 1.11 2001/06/07 23:09:19 jongfoster Cosmetic indentation changes.

Revision 1.10 2001/06/07 14:51:38 joergs make_path() no longer adds '/' if the dir already ends in '/'.

Revision 1.9 2001/06/07 14:43:17 swa slight mistake in make_path, unix path style is /.

Revision 1.8 2001/06/05 22:32:01 jongfoster New function make_path() to splice directory and file names together.

Revision 1.7 2001/06/03 19:12:30 oes introduced bindup()

Revision 1.6 2001/06/01 18:14:49 jongfoster Changing the calls to strerr() to check HAVE_STRERR (which is defined in config.h if appropriate) rather than the NO_STRERR macro.

Revision 1.5 2001/06/01 10:31:51 oes Added character class matching to trivimatch; renamed to simplematch

Revision 1.4 2001/05/31 17:32:31 oes

Revision 1.3 2001/05/29 23:10:09 oes

Revision 1.2 2001/05/29 09:50:24 jongfoster Unified blocklist/imagelist/permissionslist. File format is still under discussion, but the internal changes are (mostly) done.

Also modified interceptor behaviour:

Revision 1.1.1.1 2001/05/15 13:59:00 oes Initial import of version 2.9.3 source tree


Function Documentation

char* bindup const char *    string,
size_t    len
 

Duplicate the first n characters of a string that may contain '\0' characters.

Parameters:
string  string to be duplicated
len  number of bytes to duplicate
Returns:
pointer to copy, or NULL if failiure

char* chomp char *    string
 

In-situ-eliminate all leading and trailing whitespace from a string.

Parameters:
string  string to be chomped.
Returns:
chomped string

unsigned int hash_string const char *    s
 

Take a string and compute a (hopefuly) unique numeric integer value.

This has several uses, but being able to "switch" a string the one of my favorites.

Parameters:
s  string to be hashed.
Returns:
an unsigned long variable with the hashed value.

char* safe_strerror int    err
 

Variant of the library routine strerror() which will work on systems without the library routine, and which should never return NULL.

Parameters:
err  the `errno' of the last operation.
Returns:
An "English" string of the last `errno'. Allocated with strdup(), so caller frees. May be NULL if the system is out of memory.

int simplematch char *    pattern,
char *    text
 

String matching, with a (greedy) '*' wildcard that stands for zero or more arbitrary characters and character classes in [], which take both enumerations and ranges.

Parameters:
pattern  pattern for matching
text  text to be matched
Returns:
0 if match, else nonzero

int strcmpic const char *    s1,
const char *    s2
 

Case insensitive string comparison.

Parameters:
s1  string 1 to compare
s2  string 2 to compare
Returns:
0 if s1==s2, Negative if s1<s2, Positive if s1>s2

jb_err string_append char **    target_string,
const char *    text_to_append
 

Reallocate target_string and append text to it.

This makes it easier to append to malloc'd strings. This is similar to the (removed) strsav(), but running out of memory isn't catastrophic. Programming style: The following style provides sufficient error checking for this routine, with minimal clutter in the source code. It is recommended if you have many calls to this function: char * s = strdup(...); // don't check for error string_append(&s, ...); // don't check for error string_append(&s, ...); // don't check for error string_append(&s, ...); // don't check for error if (NULL == s) { ... handle error ... } OR, equivalently: char * s = strdup(...); // don't check for error string_append(&s, ...); // don't check for error string_append(&s, ...); // don't check for error if (string_append(&s, ...)) {... handle error ...}

Parameters:
target_string  Pointer to old text that is to be extended. *target_string will be free()d by this routine. target_string must be non-NULL. If *target_string is NULL, this routine will do nothing and return with an error - this allows you to make many calls to this routine and only check for errors after the last one.
text_to_append  Text to be appended to old. Must not be NULL.
Returns:
JB_ERR_OK on success, and sets *target_string to newly malloc'ed appended string. Caller must free(*target_string). JB_ERR_MEMORY on out-of-memory. (And free()s *target_string and sets it to NULL). JB_ERR_MEMORY if *target_string is NULL.

jb_err string_join char **    target_string,
char *    text_to_append
 

Join two strings together.

Frees BOTH the original strings. If either or both input strings are NULL, fails as if it had run out of memory. For comparison, string_append requires that the second string is non-NULL, and doesn't free it.

Rationale Too often, we want to do string_append(s, html_encode(s2)). That assert()s if s2 is NULL or if html_encode() runs out of memory. It also leaks memory. Proper checking is cumbersome. The solution: string_join(s, html_encode(s2)) is safe, and will free the memory allocated by html_encode().

Parameters:
target_string  Pointer to old text that is to be extended. *target_string will be free()d by this routine. target_string must be non-NULL.
text_to_append  Text to be appended to old.
Returns:
JB_ERR_OK on success, and sets *target_string to newly malloc'ed appended string. Caller must free(*target_string). JB_ERR_MEMORY on out-of-memory, or if *target_string or text_to_append is NULL. (In this case, frees *target_string and text_to_append, sets *target_string to NULL).

char* string_toupper const char *    string
 

Produce a copy of string with all convertible characters converted to uppercase.

Parameters:
string  string to convert
Returns:
Uppercase copy of string if possible, NULL on out-of-memory or if string was NULL.

int strncmpic const char *    s1,
const char *    s2,
size_t    n
 

Case insensitive string comparison (upto n characters).

Parameters:
s1  string 1 to compare
s2  string 2 to compare
n  maximum characters to compare
Returns:
0 if s1==s2, Negative if s1<s2, Positive if s1>s2

char* strsav char *    old,
const char *    text_to_append
 

Reallocate "old" and append text to it.

This makes it easier to append to malloc'd strings. Running out of memory is a FATAL error.

Parameters:
old  Old text that is to be extended. Will be free()d by this routine. May be NULL.
text_to_append  Text to be appended to old. May be NULL.
Returns:
Pointer to newly malloc'ed appended string. If there is no text to append, return old. Caller must free().

void* zalloc size_t    size
 

Malloc some memory and set it to '\0'.

The way calloc() ought to be -acjc

Parameters:
size  Size of memory chunk to return.
Returns:
Pointer to newly malloc'd memory chunk.


Variable Documentation

const char miscutil_h_rcs[] = MISCUTIL_H_VERSION
 

Version information about miscutil.h.

const char miscutil_rcs[] = "$Id: miscutil.c,v 2.1 2002/06/04 17:22:37 jongfoster Exp $"
 

Version information about miscutil.c.


Generated on Tue Jun 4 18:54:49 2002 for Privoxy 3.1.1 by doxygen1.2.15