ks_array.c

Go to the documentation of this file.
00001 /*
00002  * libkarmaclient - A C Library to the Karmasphere Reputation Server
00003  * Copyright (C) 2006 Karmasphere <http://www.karmasphere.com/>
00004  *  - Shevek <shevek@karmasphere.com>
00005  *  - Dave Stewart <dave.stewart@karmasphere.com>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  */
00021 
00027 #include "ks_config.h"
00028 #include "ks_array.h"
00029 #include "ks_string.h"
00030 
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 
00035 #ifdef HAVE_STDIO_H
00036 #include <stdio.h>
00037 #endif
00038 
00046 inline int 
00047 ks_array_length(ks_array_t *a)
00048 {
00049         return a->length;
00050 }
00051 
00061 static void
00062 ks_array_bappend(ks_string_t *r, ks_base_t *b)
00063 {
00064         ks_array_t      *a = KS_CAST(ks_array, b);
00065         int                      i;
00066 
00067         ks_string_append(r, "l", 1);
00068         for (i = 0; i < ks_array_length(a); i++)
00069                 ks_bappend(r, ks_array_get(a, i));
00070         ks_string_append(r, "e", 1);
00071 }
00072 
00076 static ks_type_t ks_type_array = {
00077         "ks_array",                    
00078         (ks_destroy_t)ks_array_free,   
00079         (ks_bappend_t)ks_array_bappend 
00080 };
00081 
00087 ks_type_t *
00088 ks_array_type()
00089 {
00090         return &ks_type_array;
00091 }
00092 
00099 ks_array_t *
00100 ks_array_new(void)
00101 {
00102         ks_array_t      *a = (ks_array_t *)ks_malloc(sizeof(ks_array_t));
00103         ks_array_init(a);
00104         return a;
00105 }
00106 
00116 void
00117 ks_array_init(ks_array_t *a)
00118 {
00119         a->base.type = &ks_type_array;
00120         a->size = 20;
00121         a->length = 0;
00122         a->data = (ks_base_t **)ks_malloc(a->size * sizeof(ks_base_t *));
00123         memset(a->data, 0, a->size * sizeof(ks_base_t *));
00124 }
00125 
00133 void
00134 ks_array_free(ks_array_t *a)
00135 {
00136         ks_array_fini(a);
00137         ks_free(a);
00138 }
00139 
00150 void
00151 ks_array_fini(ks_array_t *a)
00152 {
00153         ks_base_t       *b;
00154     int                  i;
00155         for (i = 0; i < a->length; i++) {
00156                 b = ks_array_get(a, i);
00157                 if (b != NULL)
00158                         ks_destroy(b);
00159         }
00160         ks_free(a->data);
00161 }
00162 
00175 static void
00176 ks_array_check_idx(ks_array_t *a, int idx)
00177 {
00178         if (idx >= a->length)
00179                 ks_error("Warning: index %d off end of array length %d",
00180                         idx, a->length);
00181 }
00182 
00192 void
00193 ks_array_extend(ks_array_t *a, int size)
00194 {
00195         if (size > a->size) {
00196                 a->data = (ks_base_t **)ks_realloc(a->data,
00197                                                 size * sizeof(ks_base_t *));
00198                 a->size = size;
00199         }
00200 }
00201 
00215 void
00216 ks_array_set(ks_array_t *a, int idx, ks_base_t *ptr)
00217 {
00218         ks_array_check_idx(a, idx);
00219         ks_destroy(a->data[idx]);
00220         a->data[idx] = ptr;
00221 }
00222 
00233 ks_base_t *
00234 ks_array_get(ks_array_t *a, int idx)
00235 {
00236         ks_array_check_idx(a, idx);
00237         return a->data[idx];
00238 }
00239 
00251 void
00252 ks_array_add(ks_array_t *a, ks_base_t *ptr)
00253 {
00254         if (a->length == a->size)
00255                 ks_array_extend(a, 2 * a->length);
00256         a->data[a->length] = ptr;
00257         a->length++;
00258 }
00259 
00269 void
00270 ks_array_remove(ks_array_t *a, int idx)
00271 {
00272         ks_array_check_idx(a, idx);
00273         if (idx < a->length) {
00274                 memmove(
00275                                 &(a->data[idx]),
00276                                 &(a->data[idx + 1]),
00277                                 (a->length - idx - 1) * sizeof(ks_base_t *));
00278                 a->length--;
00279         }
00280 }
00281 
00289 void
00290 ks_array_sort(ks_array_t *a, ks_array_compar func)
00291 {
00292         qsort(a->data, ks_array_length(a), sizeof(ks_base_t *),
00293                                         (int(*)(const void *, const void *))func);
00294 }

Generated on Wed May 9 01:01:56 2007 for libkarmaclient by  doxygen 1.5.1