]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/array.c
The KILL command killed much more than desired (including server links!)
[ngircd-alex.git] / src / ngircd / array.c
index 8b83c2c4ba767b52034f35bfb6be4d83ba8f15f4..bbff5a145c937fa125fd507a563890495662c43d 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "array.h"
 
-static char UNUSED id[] = "$Id: array.c,v 1.1 2005/07/07 18:38:14 fw Exp $";
+static char UNUSED id[] = "$Id: array.c,v 1.5 2005/07/28 16:12:50 fw Exp $";
 
 #include <assert.h>
 
@@ -28,26 +28,36 @@ static char UNUSED id[] = "$Id: array.c,v 1.1 2005/07/07 18:38:14 fw Exp $";
 #define ALIGN_4096U(x)         ((x | 0xfffU) +1)
 
 
-static int
+static bool
 safemult_uint(unsigned int a, unsigned int b, unsigned int *res)
 {
        unsigned int tmp;
 
        if (!a || !b) {
                *res = 0;
-               return 1;
+               return true;
        }
 
        tmp = a * b;
 
        if (tmp / b != a)
-               return 0;
+               return false;
 
        *res = tmp;
-       return 1;
+       return true;
 }
 
 
+void
+array_init(array *a)
+{
+       assert(a);      
+       a->mem = NULL;
+       a->allocated = 0;
+       a->used = 0;
+}
+       
+
 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
 void *
 array_alloc(array * a, unsigned int size, unsigned int pos)
@@ -56,7 +66,7 @@ array_alloc(array * a, unsigned int size, unsigned int pos)
        unsigned int aligned = 0;
        char *tmp;
 
-       assert(size);
+       assert(size > 0);
 
        if (pos_plus1 < pos)
                return NULL;
@@ -75,7 +85,7 @@ array_alloc(array * a, unsigned int size, unsigned int pos)
                        }
                }
 #ifdef DEBUG
-               Log(LOG_DEBUG, "Rounded %u to %u byte.", alloc, aligned);
+               Log(LOG_DEBUG, "array_alloc(): rounded %u to %u bytes.", alloc, aligned);
 #endif
 
                assert(aligned >= alloc);
@@ -85,7 +95,7 @@ array_alloc(array * a, unsigned int size, unsigned int pos)
 
                alloc = aligned;
 #ifdef DEBUG
-               Log(LOG_DEBUG, "array_alloc: changing size from %u to %u byte",
+               Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
                    a->allocated, aligned);
 #endif
 
@@ -110,8 +120,8 @@ array_alloc(array * a, unsigned int size, unsigned int pos)
 unsigned int
 array_length(const array * const a, unsigned int membersize)
 {
-       assert(a);
-       assert(membersize);
+       assert(a != NULL);
+       assert(membersize > 0);
 
        if (array_UNUSABLE(a))
                return 0;
@@ -135,8 +145,8 @@ array_copy(array * dest, const array * const src)
 bool
 array_copyb(array * dest, const char *src, unsigned int len)
 {
-       assert(dest);
-       assert(src);
+       assert(dest != NULL);
+       assert(src != NULL );
 
        if (!len || !src)
                return true;
@@ -148,7 +158,7 @@ array_copyb(array * dest, const char *src, unsigned int len)
        memcpy(dest->mem, src, len);
 #ifdef DEBUG
        Log(LOG_DEBUG,
-           "array_copyb: copied %u bytes to array (%u total bytes allocated)",
+           "array_copyb(): copied %u bytes to array (%u bytes allocated).",
            len, dest->allocated);
 #endif
        return true;
@@ -172,8 +182,8 @@ array_catb(array * dest, const char *src, unsigned int len)
        unsigned int used;
        char *ptr;
 
-       assert(dest);
-       assert(src);
+       assert(dest != NULL);
+       assert(src != NULL);
 
        if (!len)
                return true;
@@ -192,11 +202,11 @@ array_catb(array * dest, const char *src, unsigned int len)
 
        ptr = dest->mem;
 
-       assert(ptr);
+       assert(ptr != NULL);
 
 #ifdef DEBUG
        Log(LOG_DEBUG,
-           "array_catb: appending %u bytes to array (now %u total bytes in array)",
+           "array_catb(): appending %u bytes to array (now %u bytes in array).",
            len, tmp);
 #endif
        memcpy(ptr + used, src, len);
@@ -221,6 +231,18 @@ array_cat0(array * a)
 }
 
 
+/* append trailing NUL byte to array, but do not count it. */
+bool
+array_cat0_temporary(array * a)
+{
+       unsigned int len = array_bytes(a);
+       if (!array_catb(a, "", 1))
+               return false;
+
+       array_truncate(a, 1, len);
+       return true;
+}
+
 /* add contents of array src to array dest. */
 bool
 array_cat(array * dest, const array * const src)
@@ -240,8 +262,8 @@ array_get(array * a, unsigned int membersize, unsigned int pos)
 {
        unsigned int totalsize;
 
-       assert(membersize);
-       assert(a);
+       assert(membersize > 0);
+       assert(a != NULL);
 
        if (array_UNUSABLE(a))
                return NULL;
@@ -259,10 +281,10 @@ array_get(array * a, unsigned int membersize, unsigned int pos)
 void
 array_free(array * a)
 {
-       assert(a);
+       assert(a != NULL);
 #ifdef DEBUG
        Log(LOG_DEBUG,
-           "array_free: %u bytes free'd (%u bytes still used at time of free())",
+           "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
            a->allocated, a->used);
 #endif
        free(a->mem);
@@ -285,7 +307,7 @@ array_free_wipe(array * a)
 void *
 array_start(const array * const a)
 {
-       assert(a);
+       assert(a != NULL);
        return a->mem;
 }
 
@@ -293,7 +315,7 @@ array_start(const array * const a)
 void
 array_trunc(array * a)
 {
-       assert(a);
+       assert(a != NULL);
        a->used = 0;
 }
 
@@ -302,7 +324,7 @@ void
 array_truncate(array * a, unsigned int membersize, unsigned int len)
 {
        unsigned int newlen;
-       assert(a);
+       assert(a != NULL);
        if (!safemult_uint(membersize, len, &newlen))
                return;
 
@@ -317,8 +339,8 @@ array_moveleft(array * a, unsigned int membersize, unsigned int pos)
 {
        unsigned int bytepos;
 
-       assert(a);
-       assert(membersize);
+       assert(a != NULL);
+       assert(membersize > 0);
 
        if (!pos)
                return;
@@ -333,7 +355,7 @@ array_moveleft(array * a, unsigned int membersize, unsigned int pos)
 
 #ifdef DEBUG
        Log(LOG_DEBUG,
-           "array_moveleft: %u used bytes in array, move to beginning, starting at pos %u",
+           "array_moveleft(): %u bytes used in array, starting at position %u.",
            a->used, bytepos);
 #endif
        if (a->used <= bytepos) {