"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "libsn/sn-util.c" between
xfe-1.42.tar.gz and xfe-1.43.tar.gz

About: Xfe (X File Explorer) is a file manager and browser (using the FOX toolkit).

sn-util.c  (xfe-1.42):sn-util.c  (xfe-1.43)
skipping to change at line 33 skipping to change at line 33
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#ifndef REALLOC_0_WORKS #ifndef REALLOC_0_WORKS
static void* static void*
standard_realloc (void* mem, standard_realloc (void* mem,
sn_size_t n_bytes) sn_size_t n_bytes)
{ {
if (!mem) if (!mem)
return malloc (n_bytes); return malloc (n_bytes);
else else
return realloc (mem, n_bytes); return realloc (mem, n_bytes);
} }
#endif /* !REALLOC_0_WORKS */ #endif /* !REALLOC_0_WORKS */
#ifdef SANE_MALLOC_PROTOS #ifdef SANE_MALLOC_PROTOS
# define standard_malloc malloc # define standard_malloc malloc
# ifdef REALLOC_0_WORKS # ifdef REALLOC_0_WORKS
# define standard_realloc realloc # define standard_realloc realloc
# endif /* REALLOC_0_WORKS */ # endif /* REALLOC_0_WORKS */
# define standard_free free # define standard_free free
# define standard_calloc calloc # define standard_calloc calloc
# define standard_try_malloc malloc # define standard_try_malloc malloc
# define standard_try_realloc realloc # define standard_try_realloc realloc
#else /* !SANE_MALLOC_PROTOS */ #else /* !SANE_MALLOC_PROTOS */
static void* static void*
standard_malloc (sn_size_t n_bytes) standard_malloc (sn_size_t n_bytes)
{ {
return malloc (n_bytes); return malloc (n_bytes);
} }
# ifdef REALLOC_0_WORKS # ifdef REALLOC_0_WORKS
static void* static void*
standard_realloc (void* mem, standard_realloc (void* mem,
sn_size_t n_bytes) sn_size_t n_bytes)
{ {
return realloc (mem, n_bytes); return realloc (mem, n_bytes);
} }
# endif /* REALLOC_0_WORKS */ # endif /* REALLOC_0_WORKS */
static void static void
standard_free (void* mem) standard_free (void* mem)
{ {
free (mem); free (mem);
} }
static void* static void*
standard_calloc (sn_size_t n_blocks, standard_calloc (sn_size_t n_blocks,
sn_size_t n_bytes) sn_size_t n_bytes)
{ {
return calloc (n_blocks, n_bytes); return calloc (n_blocks, n_bytes);
} }
#define standard_try_malloc standard_malloc #define standard_try_malloc standard_malloc
#define standard_try_realloc standard_realloc #define standard_try_realloc standard_realloc
#endif /* !SANE_MALLOC_PROTOS */ #endif /* !SANE_MALLOC_PROTOS */
/* --- variables --- */ /* --- variables --- */
static SnMemVTable sn_mem_vtable = static SnMemVTable sn_mem_vtable = {
{ standard_malloc,
standard_malloc, standard_realloc,
standard_realloc, standard_free,
standard_free, standard_calloc,
standard_calloc, standard_try_malloc,
standard_try_malloc, standard_try_realloc,
standard_try_realloc, NULL,
NULL, NULL
NULL
}; };
/* --- functions --- */ /* --- functions --- */
void* void*
sn_malloc (sn_size_t n_bytes) sn_malloc (sn_size_t n_bytes)
{ {
if (n_bytes) if (n_bytes)
{ {
void* mem; void* mem;
mem = sn_mem_vtable.malloc (n_bytes); mem = sn_mem_vtable.malloc (n_bytes);
if (mem) if (mem)
return mem; return mem;
fprintf (stderr, fprintf (stderr,
"libsn: failed to allocate %lu bytes", "libsn: failed to allocate %lu bytes",
(unsigned long) n_bytes); (unsigned long) n_bytes);
} }
return NULL; return NULL;
} }
void* void*
sn_malloc0 (sn_size_t n_bytes) sn_malloc0 (sn_size_t n_bytes)
{ {
if (n_bytes) if (n_bytes)
{ {
void* mem; void* mem;
mem = sn_mem_vtable.calloc (1, n_bytes); mem = sn_mem_vtable.calloc (1, n_bytes);
if (mem) if (mem)
return mem; return mem;
fprintf (stderr, fprintf (stderr,
"libsn: failed to allocate %lu bytes", "libsn: failed to allocate %lu bytes",
(unsigned long) n_bytes); (unsigned long) n_bytes);
} }
return NULL; return NULL;
} }
void* void*
sn_realloc (void *mem, sn_realloc (void *mem,
sn_size_t n_bytes) sn_size_t n_bytes)
{ {
if (n_bytes) if (n_bytes)
{ {
mem = sn_mem_vtable.realloc (mem, n_bytes); mem = sn_mem_vtable.realloc (mem, n_bytes);
if (mem) if (mem)
return mem; return mem;
fprintf (stderr, fprintf (stderr,
"libsn: failed to allocate %lu bytes", "libsn: failed to allocate %lu bytes",
(unsigned long) n_bytes); (unsigned long) n_bytes);
} }
if (mem) if (mem)
sn_mem_vtable.free (mem); sn_mem_vtable.free (mem);
return NULL; return NULL;
} }
void void
sn_free (void* mem) sn_free (void* mem)
{ {
if (mem) if (mem)
sn_mem_vtable.free (mem); sn_mem_vtable.free (mem);
} }
void* void*
sn_try_malloc (sn_size_t n_bytes) sn_try_malloc (sn_size_t n_bytes)
{ {
if (n_bytes) if (n_bytes)
return sn_mem_vtable.try_malloc (n_bytes); return sn_mem_vtable.try_malloc (n_bytes);
else else
return NULL; return NULL;
} }
void* void*
sn_try_realloc (void *mem, sn_try_realloc (void *mem,
sn_size_t n_bytes) sn_size_t n_bytes)
{ {
if (n_bytes) if (n_bytes)
return sn_mem_vtable.try_realloc (mem, n_bytes); return sn_mem_vtable.try_realloc (mem, n_bytes);
if (mem) if (mem)
sn_mem_vtable.free (mem); sn_mem_vtable.free (mem);
return NULL; return NULL;
} }
static void* static void*
fallback_calloc (sn_size_t n_blocks, fallback_calloc (sn_size_t n_blocks,
sn_size_t n_block_bytes) sn_size_t n_block_bytes)
{ {
sn_size_t l = n_blocks * n_block_bytes; sn_size_t l = n_blocks * n_block_bytes;
void* mem = sn_mem_vtable.malloc (l); void* mem = sn_mem_vtable.malloc (l);
if (mem) if (mem)
memset (mem, 0, l); memset (mem, 0, l);
return mem; return mem;
} }
static sn_bool_t vtable_set = FALSE; static sn_bool_t vtable_set = FALSE;
sn_bool_t sn_bool_t
sn_mem_is_system_malloc (void) sn_mem_is_system_malloc (void)
{ {
return !vtable_set; return !vtable_set;
} }
void void
sn_mem_set_vtable (SnMemVTable *vtable) sn_mem_set_vtable (SnMemVTable *vtable)
{ {
if (!vtable_set) if (!vtable_set)
{ {
vtable_set = TRUE; vtable_set = TRUE;
if (vtable->malloc && vtable->realloc && vtable->free) if (vtable->malloc && vtable->realloc && vtable->free)
{ {
sn_mem_vtable.malloc = vtable->malloc; sn_mem_vtable.malloc = vtable->malloc;
sn_mem_vtable.realloc = vtable->realloc; sn_mem_vtable.realloc = vtable->realloc;
sn_mem_vtable.free = vtable->free; sn_mem_vtable.free = vtable->free;
sn_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_ca sn_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_callo
lloc; c;
sn_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : sn_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : sn
sn_mem_vtable.malloc; _mem_vtable.malloc;
sn_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_reallo sn_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc :
c : sn_mem_vtable.realloc; sn_mem_vtable.realloc;
} }
else else
{ {
fprintf (stderr, fprintf (stderr,
"libsn: memory allocation vtable lacks one of malloc(), rea "libsn: memory allocation vtable lacks one of malloc(), reall
lloc() or free()"); oc() or free()");
} }
} }
else else
{ {
fprintf (stderr, fprintf (stderr,
"libsn: memory allocation vtable can only be set once at startu "libsn: memory allocation vtable can only be set once at startup"
p"); );
} }
} }
static SnUtf8ValidateFunc utf8_validator = NULL; static SnUtf8ValidateFunc utf8_validator = NULL;
void void
sn_set_utf8_validator (SnUtf8ValidateFunc validate_func) sn_set_utf8_validator (SnUtf8ValidateFunc validate_func)
{ {
utf8_validator = validate_func; utf8_validator = validate_func;
} }
sn_bool_t sn_bool_t
sn_internal_utf8_validate (const char *str, sn_internal_utf8_validate (const char *str,
int max_len) int max_len)
{ {
if (utf8_validator) if (utf8_validator)
{ {
if (max_len < 0) if (max_len < 0)
max_len = strlen (str); max_len = strlen (str);
return (* utf8_validator) (str, max_len); return (* utf8_validator) (str, max_len);
} }
else else
return TRUE; return TRUE;
} }
char* char*
sn_internal_strdup (const char *str) sn_internal_strdup (const char *str)
{ {
char *s; char *s;
s = sn_malloc (strlen (str) + 1); s = sn_malloc (strlen (str) + 1);
strcpy (s, str); strcpy (s, str);
return s; return s;
} }
char* char*
sn_internal_strndup (const char *str, sn_internal_strndup (const char *str,
int n) int n)
{ {
char *new_str; char *new_str;
if (str) if (str)
{ {
new_str = sn_new (char, n + 1); new_str = sn_new (char, n + 1);
strncpy (new_str, str, n); strncpy (new_str, str, n);
new_str[n] = '\0'; new_str[n] = '\0';
} }
else else
new_str = NULL; new_str = NULL;
return new_str; return new_str;
} }
void void
sn_internal_strfreev (char **strings) sn_internal_strfreev (char **strings)
{ {
int i; int i;
if (strings == NULL) if (strings == NULL)
return; return;
i = 0; i = 0;
while (strings[i]) while (strings[i])
{ {
sn_free (strings[i]); sn_free (strings[i]);
++i; ++i;
} }
sn_free (strings); sn_free (strings);
} }
unsigned long unsigned long
sn_internal_string_to_ulong (const char* str) sn_internal_string_to_ulong (const char* str)
{ {
unsigned long retval; unsigned long retval;
char *end; char *end;
errno = 0; errno = 0;
retval = strtoul (str, &end, 0); retval = strtoul (str, &end, 0);
if (end == str || errno != 0) if (end == str || errno != 0)
retval = 0; retval = 0;
return retval; return retval;
} }
/** /**
* sn_internal_find_last_occurrence: * sn_internal_find_last_occurrence:
* @haystack: a nul-terminated string. * @haystack: a nul-terminated string.
* @needle: the nul-terminated string to search for. * @needle: the nul-terminated string to search for.
* *
* Searches the string @haystack for the last occurrence * Searches the string @haystack for the last occurrence
* of the string @needle. * of the string @needle.
* *
* Return value: a pointer to the found occurrence, or * Return value: a pointer to the found occurrence, or
* %NULL if not found. * %NULL if not found.
**/ **/
char* char*
sn_internal_find_last_occurrence (const char* haystack, sn_internal_find_last_occurrence (const char* haystack,
const char* needle) const char* needle)
{ {
int i; int i;
int needle_len; int needle_len;
int haystack_len; int haystack_len;
const char *p; const char *p;
if (haystack == NULL) if (haystack == NULL)
return NULL; return NULL;
if (needle == NULL) if (needle == NULL)
return NULL; return NULL;
needle_len = strlen (needle); needle_len = strlen (needle);
haystack_len = strlen (haystack); haystack_len = strlen (haystack);
if (needle_len == 0) if (needle_len == 0)
return (char *)haystack; return (char *)haystack;
if (haystack_len < needle_len) if (haystack_len < needle_len)
return NULL; return NULL;
p = haystack + haystack_len - needle_len; p = haystack + haystack_len - needle_len;
while (p >= haystack) while (p >= haystack)
{ {
for (i = 0; i < needle_len; i++) for (i = 0; i < needle_len; i++)
if (p[i] != needle[i]) if (p[i] != needle[i])
goto next; goto next;
return (char *)p; return (char *)p;
next: next:
p--; p--;
} }
return NULL; return NULL;
} }
void void
sn_internal_append_to_string (char **append_to, sn_internal_append_to_string (char **append_to,
int *current_len, int *current_len,
const char *append) const char *append)
{ {
int len; int len;
char *end; char *end;
assert (append != NULL); assert (append != NULL);
len = strlen (append); len = strlen (append);
*append_to = sn_realloc (*append_to, *current_len + len + 1); *append_to = sn_realloc (*append_to, *current_len + len + 1);
end = *append_to + *current_len; end = *append_to + *current_len;
strcpy (end, append); strcpy (end, append);
*current_len = *current_len + len; *current_len = *current_len + len;
} }
 End of changes. 71 change blocks. 
157 lines changed or deleted 156 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)